1 | /* Copyright JS Foundation and other contributors, http://js.foundation |
2 | * |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
4 | * you may not use this file except in compliance with the License. |
5 | * You may obtain a copy of the License at |
6 | * |
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
8 | * |
9 | * Unless required by applicable law or agreed to in writing, software |
10 | * distributed under the License is distributed on an "AS IS" BASIS |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | * See the License for the specific language governing permissions and |
13 | * limitations under the License. |
14 | */ |
15 | |
16 | #ifndef ECMA_MODULE_H |
17 | #define ECMA_MODULE_H |
18 | |
19 | #include "common.h" |
20 | #include "ecma-globals.h" |
21 | |
22 | #if ENABLED (JERRY_MODULE_SYSTEM) |
23 | |
24 | #define ECMA_MODULE_MAX_PATH 255u |
25 | |
26 | /** |
27 | * Imported or exported names, such as "a as b" |
28 | * Note: See https://www.ecma-international.org/ecma-262/6.0/#table-39 |
29 | * and https://www.ecma-international.org/ecma-262/6.0/#table-41 |
30 | */ |
31 | typedef struct ecma_module_names |
32 | { |
33 | struct ecma_module_names *next_p; /**< next linked list node */ |
34 | ecma_string_t *imex_name_p; /**< Import/export name of the item */ |
35 | ecma_string_t *local_name_p; /**< Local name of the item */ |
36 | } ecma_module_names_t; |
37 | |
38 | /** |
39 | * An enum identifing the current state of the module |
40 | */ |
41 | typedef enum |
42 | { |
43 | ECMA_MODULE_STATE_INIT = 0, /**< module is initialized */ |
44 | ECMA_MODULE_STATE_PARSING = 1, /**< module is currently being parsed */ |
45 | ECMA_MODULE_STATE_PARSED = 2, /**< module has been parsed */ |
46 | ECMA_MODULE_STATE_EVALUATING = 3, /**< module is currently being evaluated */ |
47 | ECMA_MODULE_STATE_EVALUATED = 4, /**< module has been evaluated */ |
48 | ECMA_MODULE_STATE_NATIVE = 5, /**< module is native */ |
49 | ECMA_MODULE_STATE_ROOT = 6, /**< module is a root module */ |
50 | } ecma_module_state_t; |
51 | |
52 | /** |
53 | * Module structure storing an instance of a module |
54 | */ |
55 | typedef struct ecma_module |
56 | { |
57 | /* TODO(dbatyai): These could be compressed pointers */ |
58 | struct ecma_module *next_p; /**< next module in the list */ |
59 | struct ecma_module_node *imports_p; /**< import requests of the module */ |
60 | struct ecma_module_node *local_exports_p; /**< local exports of the module */ |
61 | struct ecma_module_node *indirect_exports_p; /**< indirect exports of the module */ |
62 | struct ecma_module_node *star_exports_p; /**< star exports of the module*/ |
63 | ecma_string_t *path_p; /**< path of the module */ |
64 | ecma_compiled_code_t *compiled_code_p; /**< compiled code for the module */ |
65 | ecma_object_t *scope_p; /**< lexical lenvironment of the module */ |
66 | ecma_object_t *namespace_object_p; /**< namespace object of the module */ |
67 | ecma_module_state_t state; /**< evaluation state of the module */ |
68 | } ecma_module_t; |
69 | |
70 | /** |
71 | * Module node to store imports / exports. |
72 | */ |
73 | typedef struct ecma_module_node |
74 | { |
75 | struct ecma_module_node *next_p; /**< next linked list node */ |
76 | ecma_module_names_t *module_names_p; /**< names of the requested import/export node */ |
77 | ecma_module_t *module_request_p; /**< module structure of the requested module */ |
78 | } ecma_module_node_t; |
79 | |
80 | /** |
81 | * A record that can be used to store {module, identifier} pairs |
82 | */ |
83 | typedef struct ecma_module_record |
84 | { |
85 | ecma_module_t *module_p; /**< module */ |
86 | ecma_string_t *name_p; /**< identifier name */ |
87 | } ecma_module_record_t; |
88 | |
89 | /** |
90 | * A list of module records that can be used to identify circular imports during resolution |
91 | */ |
92 | typedef struct ecma_module_resolve_set |
93 | { |
94 | struct ecma_module_resolve_set *next_p; /**< next in linked list */ |
95 | ecma_module_record_t record; /**< module record */ |
96 | } ecma_module_resolve_set_t; |
97 | |
98 | /** |
99 | * A list that is used like a stack to drive the resolution process, instead of recursion. |
100 | */ |
101 | typedef struct ecma_module_resolve_stack |
102 | { |
103 | struct ecma_module_resolve_stack *next_p; /**< next in linked list */ |
104 | ecma_module_t *module_p; /**< module request */ |
105 | ecma_string_t *export_name_p; /**< export identifier name */ |
106 | bool resolving; /**< flag storing wether the current frame started resolving */ |
107 | } ecma_module_resolve_stack_t; |
108 | |
109 | bool ecma_module_resolve_set_insert (ecma_module_resolve_set_t **set_p, |
110 | ecma_module_t *const module_p, |
111 | ecma_string_t *const export_name_p); |
112 | void ecma_module_resolve_set_cleanup (ecma_module_resolve_set_t *set_p); |
113 | |
114 | void ecma_module_resolve_stack_push (ecma_module_resolve_stack_t **stack_p, |
115 | ecma_module_t *const module_p, |
116 | ecma_string_t *const export_name_p); |
117 | void ecma_module_resolve_stack_pop (ecma_module_resolve_stack_t **stack_p); |
118 | |
119 | ecma_string_t *ecma_module_create_normalized_path (const lit_utf8_byte_t *char_p, |
120 | lit_utf8_size_t size, |
121 | ecma_string_t *const base_path_p); |
122 | |
123 | ecma_module_t *ecma_module_find_module (ecma_string_t *const path_p); |
124 | ecma_module_t *ecma_module_find_native_module (ecma_string_t *const path_p); |
125 | |
126 | ecma_value_t ecma_module_parse_referenced_modules (void); |
127 | ecma_value_t ecma_module_initialize (ecma_module_t *module_p); |
128 | |
129 | void ecma_module_initialize_context (ecma_string_t *root_path_p); |
130 | void ecma_module_cleanup_context (void); |
131 | |
132 | void ecma_module_release_module_nodes (ecma_module_node_t *module_node_p); |
133 | void ecma_module_cleanup (ecma_module_t *head_p); |
134 | #endif /* ENABLED (JERRY_MODULE_SYSTEM) */ |
135 | |
136 | #endif /* !ECMA_MODULE_H */ |
137 | |