• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if ENABLED (JERRY_ES2015_MODULE_SYSTEM)
20 
21 #include "common.h"
22 #include "ecma-globals.h"
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 typedef struct ecma_module ecma_module_t;
39 
40 /**
41  * Module node to store imports / exports.
42  */
43 typedef struct ecma_module_node
44 {
45   struct ecma_module_node *next_p;     /**< next linked list node */
46   ecma_module_names_t *module_names_p; /**< names of the requested import/export node */
47   ecma_module_t *module_request_p;     /**< module structure of the requested module */
48 } ecma_module_node_t;
49 
50 /**
51  * Module context containing all import and export nodes.
52  */
53 typedef struct ecma_module_context
54 {
55   struct ecma_module_context *parent_p;   /**< parent context */
56   ecma_module_node_t *imports_p;          /**< import item of the current context */
57   ecma_module_node_t *local_exports_p;    /**< export item of the current context */
58   ecma_module_node_t *indirect_exports_p; /**< export item of the current context */
59   ecma_module_node_t *star_exports_p;     /**< export item of the current context */
60   ecma_module_t *module_p;                /**< module request */
61 } ecma_module_context_t;
62 
63 /**
64  * An enum identifing the current state of the module
65  */
66 typedef enum
67 {
68   ECMA_MODULE_STATE_INIT = 0,       /**< module is initialized */
69   ECMA_MODULE_STATE_PARSING = 1,    /**< module is currently being parsed */
70   ECMA_MODULE_STATE_PARSED = 2,     /**< module has been parsed */
71   ECMA_MODULE_STATE_EVALUATING = 3, /**< module is currently being evaluated */
72   ECMA_MODULE_STATE_EVALUATED = 4,  /**< module has been evaluated */
73   ECMA_MODULE_STATE_NATIVE = 5,     /**< module is native */
74 } ecma_module_state_t;
75 
76 /**
77  * Module structure storing an instance of a module
78  */
79 struct ecma_module
80 {
81   struct ecma_module *next_p;            /**< next linked list node */
82   ecma_module_state_t state;             /**< state of the mode */
83   ecma_string_t *path_p;                 /**< path of the module */
84   ecma_module_context_t *context_p;      /**< module context of the module */
85   ecma_compiled_code_t *compiled_code_p; /**< compiled code of the module */
86   ecma_object_t *scope_p;                /**< lexica lenvironment of the module */
87   ecma_object_t *namespace_object_p;     /**< namespace import object of the module */
88 };
89 
90 /**
91  *  A record that can be used to store {module, identifier} pairs
92  */
93 typedef struct ecma_module_record
94 {
95   ecma_module_t *module_p;  /**< module */
96   ecma_string_t *name_p;    /**< identifier name */
97 } ecma_module_record_t;
98 
99 /**
100  *  A list of module records that can be used to identify circular imports during resolution
101  */
102 typedef struct ecma_module_resolve_set
103 {
104   struct ecma_module_resolve_set *next_p; /**< next in linked list */
105   ecma_module_record_t record;            /**< module record */
106 } ecma_module_resolve_set_t;
107 
108 /**
109  * A list that is used like a stack to drive the resolution process, instead of recursion.
110  */
111 typedef struct ecma_module_resolve_stack
112 {
113   struct ecma_module_resolve_stack *next_p; /**< next in linked list */
114   ecma_module_t *module_p;                  /**< module request */
115   ecma_string_t *export_name_p;             /**< export identifier name */
116   bool resolving;                           /**< flag storing wether the current frame started resolving */
117 } ecma_module_resolve_stack_t;
118 
119 bool ecma_module_resolve_set_insert (ecma_module_resolve_set_t **set_p,
120                                      ecma_module_t *const module_p,
121                                      ecma_string_t *const export_name_p);
122 void ecma_module_resolve_set_cleanup (ecma_module_resolve_set_t *set_p);
123 
124 void ecma_module_resolve_stack_push (ecma_module_resolve_stack_t **stack_p,
125                                      ecma_module_t *const module_p,
126                                      ecma_string_t *const export_name_p);
127 void ecma_module_resolve_stack_pop (ecma_module_resolve_stack_t **stack_p);
128 
129 ecma_string_t *ecma_module_create_normalized_path (const uint8_t *char_p,
130                                                    prop_length_t size);
131 ecma_module_t *ecma_module_find_module (ecma_string_t *const path_p);
132 ecma_module_t *ecma_module_create_native_module (ecma_string_t *const path_p,
133                                                  ecma_object_t *const namespace_p);
134 ecma_module_t *ecma_module_find_or_create_module (ecma_string_t *const path_p);
135 
136 ecma_value_t ecma_module_initialize_current (void);
137 ecma_value_t ecma_module_parse_modules (void);
138 ecma_value_t ecma_module_check_indirect_exports (void);
139 
140 void ecma_module_release_module_nodes (ecma_module_node_t *module_node_p);
141 void ecma_module_cleanup (void);
142 #endif /* ENABLED (JERRY_ES2015_MODULE_SYSTEM) */
143 
144 #endif /* !ECMA_MODULE_H */
145