1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci *
3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci * You may obtain a copy of the License at
6425bb815Sopenharmony_ci *
7425bb815Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci *
9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci * limitations under the License.
14425bb815Sopenharmony_ci */
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci#ifndef ECMA_MODULE_H
17425bb815Sopenharmony_ci#define ECMA_MODULE_H
18425bb815Sopenharmony_ci
19425bb815Sopenharmony_ci#if ENABLED (JERRY_ES2015_MODULE_SYSTEM)
20425bb815Sopenharmony_ci
21425bb815Sopenharmony_ci#include "common.h"
22425bb815Sopenharmony_ci#include "ecma-globals.h"
23425bb815Sopenharmony_ci
24425bb815Sopenharmony_ci#define ECMA_MODULE_MAX_PATH 255u
25425bb815Sopenharmony_ci
26425bb815Sopenharmony_ci/**
27425bb815Sopenharmony_ci * Imported or exported names, such as "a as b"
28425bb815Sopenharmony_ci * Note: See https://www.ecma-international.org/ecma-262/6.0/#table-39
29425bb815Sopenharmony_ci *       and https://www.ecma-international.org/ecma-262/6.0/#table-41
30425bb815Sopenharmony_ci */
31425bb815Sopenharmony_citypedef struct ecma_module_names
32425bb815Sopenharmony_ci{
33425bb815Sopenharmony_ci  struct ecma_module_names *next_p; /**< next linked list node */
34425bb815Sopenharmony_ci  ecma_string_t *imex_name_p;       /**< Import/export name of the item */
35425bb815Sopenharmony_ci  ecma_string_t *local_name_p;      /**< Local name of the item */
36425bb815Sopenharmony_ci} ecma_module_names_t;
37425bb815Sopenharmony_ci
38425bb815Sopenharmony_citypedef struct ecma_module ecma_module_t;
39425bb815Sopenharmony_ci
40425bb815Sopenharmony_ci/**
41425bb815Sopenharmony_ci * Module node to store imports / exports.
42425bb815Sopenharmony_ci */
43425bb815Sopenharmony_citypedef struct ecma_module_node
44425bb815Sopenharmony_ci{
45425bb815Sopenharmony_ci  struct ecma_module_node *next_p;     /**< next linked list node */
46425bb815Sopenharmony_ci  ecma_module_names_t *module_names_p; /**< names of the requested import/export node */
47425bb815Sopenharmony_ci  ecma_module_t *module_request_p;     /**< module structure of the requested module */
48425bb815Sopenharmony_ci} ecma_module_node_t;
49425bb815Sopenharmony_ci
50425bb815Sopenharmony_ci/**
51425bb815Sopenharmony_ci * Module context containing all import and export nodes.
52425bb815Sopenharmony_ci */
53425bb815Sopenharmony_citypedef struct ecma_module_context
54425bb815Sopenharmony_ci{
55425bb815Sopenharmony_ci  struct ecma_module_context *parent_p;   /**< parent context */
56425bb815Sopenharmony_ci  ecma_module_node_t *imports_p;          /**< import item of the current context */
57425bb815Sopenharmony_ci  ecma_module_node_t *local_exports_p;    /**< export item of the current context */
58425bb815Sopenharmony_ci  ecma_module_node_t *indirect_exports_p; /**< export item of the current context */
59425bb815Sopenharmony_ci  ecma_module_node_t *star_exports_p;     /**< export item of the current context */
60425bb815Sopenharmony_ci  ecma_module_t *module_p;                /**< module request */
61425bb815Sopenharmony_ci} ecma_module_context_t;
62425bb815Sopenharmony_ci
63425bb815Sopenharmony_ci/**
64425bb815Sopenharmony_ci * An enum identifing the current state of the module
65425bb815Sopenharmony_ci */
66425bb815Sopenharmony_citypedef enum
67425bb815Sopenharmony_ci{
68425bb815Sopenharmony_ci  ECMA_MODULE_STATE_INIT = 0,       /**< module is initialized */
69425bb815Sopenharmony_ci  ECMA_MODULE_STATE_PARSING = 1,    /**< module is currently being parsed */
70425bb815Sopenharmony_ci  ECMA_MODULE_STATE_PARSED = 2,     /**< module has been parsed */
71425bb815Sopenharmony_ci  ECMA_MODULE_STATE_EVALUATING = 3, /**< module is currently being evaluated */
72425bb815Sopenharmony_ci  ECMA_MODULE_STATE_EVALUATED = 4,  /**< module has been evaluated */
73425bb815Sopenharmony_ci  ECMA_MODULE_STATE_NATIVE = 5,     /**< module is native */
74425bb815Sopenharmony_ci} ecma_module_state_t;
75425bb815Sopenharmony_ci
76425bb815Sopenharmony_ci/**
77425bb815Sopenharmony_ci * Module structure storing an instance of a module
78425bb815Sopenharmony_ci */
79425bb815Sopenharmony_cistruct ecma_module
80425bb815Sopenharmony_ci{
81425bb815Sopenharmony_ci  struct ecma_module *next_p;            /**< next linked list node */
82425bb815Sopenharmony_ci  ecma_module_state_t state;             /**< state of the mode */
83425bb815Sopenharmony_ci  ecma_string_t *path_p;                 /**< path of the module */
84425bb815Sopenharmony_ci  ecma_module_context_t *context_p;      /**< module context of the module */
85425bb815Sopenharmony_ci  ecma_compiled_code_t *compiled_code_p; /**< compiled code of the module */
86425bb815Sopenharmony_ci  ecma_object_t *scope_p;                /**< lexica lenvironment of the module */
87425bb815Sopenharmony_ci  ecma_object_t *namespace_object_p;     /**< namespace import object of the module */
88425bb815Sopenharmony_ci};
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci/**
91425bb815Sopenharmony_ci *  A record that can be used to store {module, identifier} pairs
92425bb815Sopenharmony_ci */
93425bb815Sopenharmony_citypedef struct ecma_module_record
94425bb815Sopenharmony_ci{
95425bb815Sopenharmony_ci  ecma_module_t *module_p;  /**< module */
96425bb815Sopenharmony_ci  ecma_string_t *name_p;    /**< identifier name */
97425bb815Sopenharmony_ci} ecma_module_record_t;
98425bb815Sopenharmony_ci
99425bb815Sopenharmony_ci/**
100425bb815Sopenharmony_ci *  A list of module records that can be used to identify circular imports during resolution
101425bb815Sopenharmony_ci */
102425bb815Sopenharmony_citypedef struct ecma_module_resolve_set
103425bb815Sopenharmony_ci{
104425bb815Sopenharmony_ci  struct ecma_module_resolve_set *next_p; /**< next in linked list */
105425bb815Sopenharmony_ci  ecma_module_record_t record;            /**< module record */
106425bb815Sopenharmony_ci} ecma_module_resolve_set_t;
107425bb815Sopenharmony_ci
108425bb815Sopenharmony_ci/**
109425bb815Sopenharmony_ci * A list that is used like a stack to drive the resolution process, instead of recursion.
110425bb815Sopenharmony_ci */
111425bb815Sopenharmony_citypedef struct ecma_module_resolve_stack
112425bb815Sopenharmony_ci{
113425bb815Sopenharmony_ci  struct ecma_module_resolve_stack *next_p; /**< next in linked list */
114425bb815Sopenharmony_ci  ecma_module_t *module_p;                  /**< module request */
115425bb815Sopenharmony_ci  ecma_string_t *export_name_p;             /**< export identifier name */
116425bb815Sopenharmony_ci  bool resolving;                           /**< flag storing wether the current frame started resolving */
117425bb815Sopenharmony_ci} ecma_module_resolve_stack_t;
118425bb815Sopenharmony_ci
119425bb815Sopenharmony_cibool ecma_module_resolve_set_insert (ecma_module_resolve_set_t **set_p,
120425bb815Sopenharmony_ci                                     ecma_module_t *const module_p,
121425bb815Sopenharmony_ci                                     ecma_string_t *const export_name_p);
122425bb815Sopenharmony_civoid ecma_module_resolve_set_cleanup (ecma_module_resolve_set_t *set_p);
123425bb815Sopenharmony_ci
124425bb815Sopenharmony_civoid ecma_module_resolve_stack_push (ecma_module_resolve_stack_t **stack_p,
125425bb815Sopenharmony_ci                                     ecma_module_t *const module_p,
126425bb815Sopenharmony_ci                                     ecma_string_t *const export_name_p);
127425bb815Sopenharmony_civoid ecma_module_resolve_stack_pop (ecma_module_resolve_stack_t **stack_p);
128425bb815Sopenharmony_ci
129425bb815Sopenharmony_ciecma_string_t *ecma_module_create_normalized_path (const uint8_t *char_p,
130425bb815Sopenharmony_ci                                                   prop_length_t size);
131425bb815Sopenharmony_ciecma_module_t *ecma_module_find_module (ecma_string_t *const path_p);
132425bb815Sopenharmony_ciecma_module_t *ecma_module_create_native_module (ecma_string_t *const path_p,
133425bb815Sopenharmony_ci                                                 ecma_object_t *const namespace_p);
134425bb815Sopenharmony_ciecma_module_t *ecma_module_find_or_create_module (ecma_string_t *const path_p);
135425bb815Sopenharmony_ci
136425bb815Sopenharmony_ciecma_value_t ecma_module_initialize_current (void);
137425bb815Sopenharmony_ciecma_value_t ecma_module_parse_modules (void);
138425bb815Sopenharmony_ciecma_value_t ecma_module_check_indirect_exports (void);
139425bb815Sopenharmony_ci
140425bb815Sopenharmony_civoid ecma_module_release_module_nodes (ecma_module_node_t *module_node_p);
141425bb815Sopenharmony_civoid ecma_module_cleanup (void);
142425bb815Sopenharmony_ci#endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
143425bb815Sopenharmony_ci
144425bb815Sopenharmony_ci#endif /* !ECMA_MODULE_H */
145