bare-module 6.3.2 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +237 -270
- package/binding.c +654 -331
- package/index.d.ts +156 -63
- package/index.js +88 -975
- package/lib/errors.js +27 -10
- package/lib/loader.d.ts +56 -0
- package/lib/loader.js +1192 -0
- package/lib/module/addon.js +7 -0
- package/lib/module/binary.js +7 -0
- package/lib/module/builtin.js +19 -0
- package/lib/module/bundle.js +47 -0
- package/lib/module/json.js +19 -0
- package/lib/module/script.js +23 -0
- package/lib/module/source-text.js +13 -0
- package/lib/module/synthetic.js +20 -0
- package/lib/module/text.js +7 -0
- package/lib/module/value.js +27 -0
- package/lib/module.js +392 -0
- package/lib/protocol.d.ts +56 -8
- package/lib/protocol.js +93 -26
- package/lib/source.js +18 -0
- package/lib/url.js +52 -0
- package/package.json +19 -8
- package/prebuilds/android-arm/bare-module.bare +0 -0
- package/prebuilds/android-arm64/bare-module.bare +0 -0
- package/prebuilds/android-ia32/bare-module.bare +0 -0
- package/prebuilds/android-x64/bare-module.bare +0 -0
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-arm64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
- package/lib/constants.d.ts +0 -19
- package/lib/constants.js +0 -17
package/binding.c
CHANGED
|
@@ -8,16 +8,360 @@
|
|
|
8
8
|
#include <utf.h>
|
|
9
9
|
#include <uv.h>
|
|
10
10
|
|
|
11
|
+
#define BARE_MODULE_MAX_EXPORT_NAMES 0x100000
|
|
12
|
+
|
|
11
13
|
typedef struct {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
uint32_t refs;
|
|
15
|
+
|
|
14
16
|
js_ref_t *on_import;
|
|
17
|
+
js_ref_t *on_dynamic_import;
|
|
15
18
|
js_ref_t *on_evaluate;
|
|
16
19
|
js_ref_t *on_meta;
|
|
17
20
|
} bare_module_context_t;
|
|
18
21
|
|
|
22
|
+
typedef struct {
|
|
23
|
+
js_module_t *module;
|
|
24
|
+
bare_module_context_t *context;
|
|
25
|
+
} bare_module_record_t;
|
|
26
|
+
|
|
27
|
+
static const js_type_tag_t bare_module__context_tag = {
|
|
28
|
+
.lower = 0x4d2a8f13c6b74e05,
|
|
29
|
+
.upper = 0x8e17b309a5df42c1,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
static const js_type_tag_t bare_module__module_tag = {
|
|
33
|
+
.lower = 0xb6c41e7a2f083d59,
|
|
34
|
+
.upper = 0x21fa9d5c8b3e6704,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
static const js_type_tag_t bare_module__synthetic_module_tag = {
|
|
38
|
+
.lower = 0x7c53e0a91d6b48f2,
|
|
39
|
+
.upper = 0x0d94fc27e8a15b36,
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
static int
|
|
43
|
+
bare_module__has_tag(js_env_t *env, js_value_t *value, const js_type_tag_t *tag) {
|
|
44
|
+
int err;
|
|
45
|
+
|
|
46
|
+
bool is_object;
|
|
47
|
+
err = js_is_object(env, value, &is_object);
|
|
48
|
+
assert(err == 0);
|
|
49
|
+
|
|
50
|
+
if (!is_object) return 0;
|
|
51
|
+
|
|
52
|
+
bool tagged;
|
|
53
|
+
err = js_check_type_tag(env, value, tag, &tagged);
|
|
54
|
+
if (err < 0) return -1;
|
|
55
|
+
|
|
56
|
+
return tagged ? 1 : 0;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
static int
|
|
60
|
+
bare_module__is_module(js_env_t *env, js_value_t *value) {
|
|
61
|
+
int tagged = bare_module__has_tag(env, value, &bare_module__module_tag);
|
|
62
|
+
|
|
63
|
+
if (tagged != 0) return tagged;
|
|
64
|
+
|
|
65
|
+
return bare_module__has_tag(env, value, &bare_module__synthetic_module_tag);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static bool
|
|
69
|
+
bare_module__check_context(js_env_t *env, js_value_t *value) {
|
|
70
|
+
int err;
|
|
71
|
+
|
|
72
|
+
int tagged = bare_module__has_tag(env, value, &bare_module__context_tag);
|
|
73
|
+
|
|
74
|
+
if (tagged < 0) return false;
|
|
75
|
+
if (tagged) return true;
|
|
76
|
+
|
|
77
|
+
err = js_throw_type_error(env, NULL, "Context must be a module context");
|
|
78
|
+
assert(err == 0);
|
|
79
|
+
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static bool
|
|
84
|
+
bare_module__check_module(js_env_t *env, js_value_t *value) {
|
|
85
|
+
int err;
|
|
86
|
+
|
|
87
|
+
int tagged = bare_module__is_module(env, value);
|
|
88
|
+
|
|
89
|
+
if (tagged < 0) return false;
|
|
90
|
+
if (tagged) return true;
|
|
91
|
+
|
|
92
|
+
err = js_throw_type_error(env, NULL, "Receiver must be a module");
|
|
93
|
+
assert(err == 0);
|
|
94
|
+
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
static bool
|
|
99
|
+
bare_module__check_synthetic_module(js_env_t *env, js_value_t *value) {
|
|
100
|
+
int err;
|
|
101
|
+
|
|
102
|
+
int tagged = bare_module__has_tag(env, value, &bare_module__synthetic_module_tag);
|
|
103
|
+
|
|
104
|
+
if (tagged < 0) return false;
|
|
105
|
+
if (tagged) return true;
|
|
106
|
+
|
|
107
|
+
err = js_throw_type_error(env, NULL, "Receiver must be a synthetic module");
|
|
108
|
+
assert(err == 0);
|
|
109
|
+
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
static bool
|
|
114
|
+
bare_module__check_unclaimed(js_env_t *env, js_value_t *value) {
|
|
115
|
+
int err;
|
|
116
|
+
|
|
117
|
+
bool is_object;
|
|
118
|
+
err = js_is_object(env, value, &is_object);
|
|
119
|
+
assert(err == 0);
|
|
120
|
+
|
|
121
|
+
if (is_object) {
|
|
122
|
+
int claimed = bare_module__is_module(env, value);
|
|
123
|
+
|
|
124
|
+
if (claimed == 0) claimed = bare_module__has_tag(env, value, &bare_module__context_tag);
|
|
125
|
+
|
|
126
|
+
if (claimed < 0) return false;
|
|
127
|
+
|
|
128
|
+
if (claimed == 0) return true;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
err = js_throw_type_error(env, NULL, "Receiver must be an unclaimed object");
|
|
132
|
+
assert(err == 0);
|
|
133
|
+
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
static bool
|
|
138
|
+
bare_module__release_wrap(js_env_t *env, js_value_t *value) {
|
|
139
|
+
int err;
|
|
140
|
+
|
|
141
|
+
js_value_t *exception;
|
|
142
|
+
err = js_get_and_clear_last_exception(env, &exception);
|
|
143
|
+
assert(err == 0);
|
|
144
|
+
|
|
145
|
+
int removed = js_remove_wrap(env, value, NULL);
|
|
146
|
+
|
|
147
|
+
err = js_throw(env, exception);
|
|
148
|
+
assert(err == 0);
|
|
149
|
+
|
|
150
|
+
return removed == 0;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static bool
|
|
154
|
+
bare_module__check_function(js_env_t *env, js_value_t *value, const char *message) {
|
|
155
|
+
int err;
|
|
156
|
+
|
|
157
|
+
bool is_function;
|
|
158
|
+
err = js_is_function(env, value, &is_function);
|
|
159
|
+
assert(err == 0);
|
|
160
|
+
|
|
161
|
+
if (!is_function) {
|
|
162
|
+
err = js_throw_type_error(env, NULL, message);
|
|
163
|
+
assert(err == 0);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return is_function;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static bool
|
|
170
|
+
bare_module__check_string(js_env_t *env, js_value_t *value, const char *message) {
|
|
171
|
+
int err;
|
|
172
|
+
|
|
173
|
+
bool is_string;
|
|
174
|
+
err = js_is_string(env, value, &is_string);
|
|
175
|
+
assert(err == 0);
|
|
176
|
+
|
|
177
|
+
if (!is_string) {
|
|
178
|
+
err = js_throw_type_error(env, NULL, message);
|
|
179
|
+
assert(err == 0);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return is_string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
static bool
|
|
186
|
+
bare_module__get_int32(js_env_t *env, js_value_t *value, int32_t *result, const char *message) {
|
|
187
|
+
int err;
|
|
188
|
+
|
|
189
|
+
bool is_int32;
|
|
190
|
+
err = js_is_int32(env, value, &is_int32);
|
|
191
|
+
assert(err == 0);
|
|
192
|
+
|
|
193
|
+
if (!is_int32) {
|
|
194
|
+
err = js_throw_type_error(env, NULL, message);
|
|
195
|
+
assert(err == 0);
|
|
196
|
+
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
err = js_get_value_int32(env, value, result);
|
|
201
|
+
assert(err == 0);
|
|
202
|
+
|
|
203
|
+
return true;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
static bool
|
|
207
|
+
bare_module__get_offset(js_env_t *env, js_value_t *value, int32_t *result) {
|
|
208
|
+
int err;
|
|
209
|
+
|
|
210
|
+
if (!bare_module__get_int32(env, value, result, "Offset must be a 32-bit integer")) return false;
|
|
211
|
+
|
|
212
|
+
if (*result < 0) {
|
|
213
|
+
err = js_throw_range_error(env, NULL, "Offset must not be negative");
|
|
214
|
+
assert(err == 0);
|
|
215
|
+
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return true;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
static bool
|
|
223
|
+
bare_module__get_string(js_env_t *env, js_value_t *value, utf8_t *result, size_t len, size_t *result_len) {
|
|
224
|
+
int err;
|
|
225
|
+
|
|
226
|
+
if (!bare_module__check_string(env, value, "Value must be a string")) return false;
|
|
227
|
+
|
|
228
|
+
size_t str_len;
|
|
229
|
+
err = js_get_value_string_utf8(env, value, NULL, 0, &str_len);
|
|
230
|
+
assert(err == 0);
|
|
231
|
+
|
|
232
|
+
if (str_len + 1 /* NULL */ > len) {
|
|
233
|
+
err = js_throw_error(env, uv_err_name(UV_ENAMETOOLONG), uv_strerror(UV_ENAMETOOLONG));
|
|
234
|
+
assert(err == 0);
|
|
235
|
+
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
err = js_get_value_string_utf8(env, value, result, len, result_len);
|
|
240
|
+
assert(err == 0);
|
|
241
|
+
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
static bool
|
|
246
|
+
bare_module__get_array_length(js_env_t *env, js_value_t *array, uint32_t *result, const char *message) {
|
|
247
|
+
int err;
|
|
248
|
+
|
|
249
|
+
bool is_array;
|
|
250
|
+
err = js_is_array(env, array, &is_array);
|
|
251
|
+
assert(err == 0);
|
|
252
|
+
|
|
253
|
+
if (!is_array) {
|
|
254
|
+
err = js_throw_type_error(env, NULL, message);
|
|
255
|
+
assert(err == 0);
|
|
256
|
+
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
err = js_get_array_length(env, array, result);
|
|
261
|
+
|
|
262
|
+
return err >= 0;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
static bool
|
|
266
|
+
bare_module__check_strings(js_env_t *env, js_value_t **elements, uint32_t len, const char *message) {
|
|
267
|
+
for (uint32_t i = 0; i < len; i++) {
|
|
268
|
+
if (!bare_module__check_string(env, elements[i], message)) return false;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return true;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
static bool
|
|
275
|
+
bare_module__get_strings(js_env_t *env, js_value_t *array, js_value_t **result, uint32_t len, uint32_t *result_len, const char *message) {
|
|
276
|
+
int err;
|
|
277
|
+
|
|
278
|
+
uint32_t array_len;
|
|
279
|
+
if (!bare_module__get_array_length(env, array, &array_len, message)) return false;
|
|
280
|
+
|
|
281
|
+
if (array_len > len) {
|
|
282
|
+
err = js_throw_error(env, uv_err_name(UV_E2BIG), uv_strerror(UV_E2BIG));
|
|
283
|
+
assert(err == 0);
|
|
284
|
+
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
uint32_t written;
|
|
289
|
+
err = js_get_array_elements(env, array, result, array_len, 0, &written);
|
|
290
|
+
if (err < 0) return false;
|
|
291
|
+
|
|
292
|
+
if (!bare_module__check_strings(env, result, written, message)) return false;
|
|
293
|
+
|
|
294
|
+
*result_len = written;
|
|
295
|
+
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
static bool
|
|
300
|
+
bare_module__alloc_strings(js_env_t *env, js_value_t *array, js_value_t ***result, uint32_t len, uint32_t *result_len, const char *message) {
|
|
301
|
+
int err;
|
|
302
|
+
|
|
303
|
+
uint32_t array_len;
|
|
304
|
+
if (!bare_module__get_array_length(env, array, &array_len, message)) return false;
|
|
305
|
+
|
|
306
|
+
if (array_len > len) {
|
|
307
|
+
err = js_throw_error(env, uv_err_name(UV_E2BIG), uv_strerror(UV_E2BIG));
|
|
308
|
+
assert(err == 0);
|
|
309
|
+
|
|
310
|
+
return false;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
js_value_t **elements = calloc(array_len, sizeof(js_value_t *));
|
|
314
|
+
|
|
315
|
+
if (elements == NULL && array_len > 0) {
|
|
316
|
+
err = js_throw_error(env, uv_err_name(UV_ENOMEM), uv_strerror(UV_ENOMEM));
|
|
317
|
+
assert(err == 0);
|
|
318
|
+
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
uint32_t written;
|
|
323
|
+
err = js_get_array_elements(env, array, elements, array_len, 0, &written);
|
|
324
|
+
|
|
325
|
+
if (err < 0) {
|
|
326
|
+
free(elements);
|
|
327
|
+
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (!bare_module__check_strings(env, elements, written, message)) {
|
|
332
|
+
free(elements);
|
|
333
|
+
|
|
334
|
+
return false;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
*result = elements;
|
|
338
|
+
*result_len = written;
|
|
339
|
+
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static bool
|
|
344
|
+
bare_module__get_module(js_env_t *env, js_value_t *value, bare_module_context_t *context, js_module_t **result) {
|
|
345
|
+
int err;
|
|
346
|
+
|
|
347
|
+
bare_module_record_t *handle;
|
|
348
|
+
err = js_unwrap(env, value, (void **) &handle);
|
|
349
|
+
if (err < 0) return false;
|
|
350
|
+
|
|
351
|
+
if (handle->context != context) {
|
|
352
|
+
err = js_throw_error(env, NULL, "Module belongs to another module context");
|
|
353
|
+
assert(err == 0);
|
|
354
|
+
|
|
355
|
+
return false;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
*result = handle->module;
|
|
359
|
+
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
|
|
19
363
|
static js_module_t *
|
|
20
|
-
|
|
364
|
+
bare_module__on_import(js_env_t *env, js_value_t *specifier, js_value_t *assertions, js_module_t *referrer, void *data) {
|
|
21
365
|
bare_module_context_t *context = (bare_module_context_t *) data;
|
|
22
366
|
|
|
23
367
|
int err;
|
|
@@ -27,32 +371,28 @@ bare_module__on_static_import(js_env_t *env, js_value_t *specifier, js_value_t *
|
|
|
27
371
|
assert(err == 0);
|
|
28
372
|
|
|
29
373
|
js_value_t *ctx;
|
|
30
|
-
err =
|
|
374
|
+
err = js_get_undefined(env, &ctx);
|
|
31
375
|
assert(err == 0);
|
|
32
376
|
|
|
33
377
|
js_value_t *on_import;
|
|
34
378
|
err = js_get_reference_value(env, context->on_import, &on_import);
|
|
35
379
|
assert(err == 0);
|
|
36
380
|
|
|
37
|
-
|
|
38
|
-
err =
|
|
381
|
+
js_value_t *id;
|
|
382
|
+
err = js_get_module_id(env, referrer, &id);
|
|
39
383
|
assert(err == 0);
|
|
40
384
|
|
|
41
|
-
js_value_t *args[
|
|
385
|
+
js_value_t *args[3] = {specifier, assertions, id};
|
|
42
386
|
|
|
43
|
-
|
|
44
|
-
if (err < 0) goto err;
|
|
45
|
-
|
|
46
|
-
err = js_get_boolean(env, false, &args[3]);
|
|
47
|
-
assert(err == 0);
|
|
387
|
+
js_module_t *module = NULL;
|
|
48
388
|
|
|
49
389
|
js_value_t *result;
|
|
50
|
-
err = js_call_function(env, ctx, on_import,
|
|
390
|
+
err = js_call_function(env, ctx, on_import, 3, args, &result);
|
|
51
391
|
if (err < 0) goto err;
|
|
52
392
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (
|
|
393
|
+
if (!bare_module__check_module(env, result)) goto err;
|
|
394
|
+
|
|
395
|
+
if (!bare_module__get_module(env, result, context, &module)) goto err;
|
|
56
396
|
|
|
57
397
|
err = js_close_handle_scope(env, scope);
|
|
58
398
|
assert(err == 0);
|
|
@@ -77,20 +417,17 @@ bare_module__on_dynamic_import(js_env_t *env, js_value_t *specifier, js_value_t
|
|
|
77
417
|
assert(err == 0);
|
|
78
418
|
|
|
79
419
|
js_value_t *ctx;
|
|
80
|
-
err =
|
|
420
|
+
err = js_get_undefined(env, &ctx);
|
|
81
421
|
assert(err == 0);
|
|
82
422
|
|
|
83
|
-
js_value_t *
|
|
84
|
-
err = js_get_reference_value(env, context->
|
|
423
|
+
js_value_t *on_dynamic_import;
|
|
424
|
+
err = js_get_reference_value(env, context->on_dynamic_import, &on_dynamic_import);
|
|
85
425
|
assert(err == 0);
|
|
86
426
|
|
|
87
|
-
js_value_t *args[4] = {specifier, assertions, referrer};
|
|
88
|
-
|
|
89
|
-
err = js_get_boolean(env, true, &args[3]);
|
|
90
|
-
assert(err == 0);
|
|
427
|
+
js_value_t *args[4] = {specifier, assertions, referrer, id};
|
|
91
428
|
|
|
92
429
|
js_value_t *result;
|
|
93
|
-
err = js_call_function(env, ctx,
|
|
430
|
+
err = js_call_function(env, ctx, on_dynamic_import, 4, args, &result);
|
|
94
431
|
if (err < 0) goto err;
|
|
95
432
|
|
|
96
433
|
err = js_escape_handle(env, scope, result, &result);
|
|
@@ -119,21 +456,18 @@ bare_module__on_evaluate(js_env_t *env, js_module_t *module, void *data) {
|
|
|
119
456
|
assert(err == 0);
|
|
120
457
|
|
|
121
458
|
js_value_t *ctx;
|
|
122
|
-
err =
|
|
459
|
+
err = js_get_undefined(env, &ctx);
|
|
123
460
|
assert(err == 0);
|
|
124
461
|
|
|
125
462
|
js_value_t *on_evaluate;
|
|
126
463
|
err = js_get_reference_value(env, context->on_evaluate, &on_evaluate);
|
|
127
464
|
assert(err == 0);
|
|
128
465
|
|
|
129
|
-
|
|
130
|
-
err =
|
|
466
|
+
js_value_t *id;
|
|
467
|
+
err = js_get_module_id(env, module, &id);
|
|
131
468
|
assert(err == 0);
|
|
132
469
|
|
|
133
|
-
js_value_t *args[1];
|
|
134
|
-
|
|
135
|
-
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
|
|
136
|
-
if (err < 0) goto err;
|
|
470
|
+
js_value_t *args[1] = {id};
|
|
137
471
|
|
|
138
472
|
js_value_t *result;
|
|
139
473
|
err = js_call_function(env, ctx, on_evaluate, 1, args, &result);
|
|
@@ -160,23 +494,18 @@ bare_module__on_meta(js_env_t *env, js_module_t *module, js_value_t *meta, void
|
|
|
160
494
|
assert(err == 0);
|
|
161
495
|
|
|
162
496
|
js_value_t *ctx;
|
|
163
|
-
err =
|
|
497
|
+
err = js_get_undefined(env, &ctx);
|
|
164
498
|
assert(err == 0);
|
|
165
499
|
|
|
166
500
|
js_value_t *on_meta;
|
|
167
501
|
err = js_get_reference_value(env, context->on_meta, &on_meta);
|
|
168
502
|
assert(err == 0);
|
|
169
503
|
|
|
170
|
-
|
|
171
|
-
err =
|
|
504
|
+
js_value_t *id;
|
|
505
|
+
err = js_get_module_id(env, module, &id);
|
|
172
506
|
assert(err == 0);
|
|
173
507
|
|
|
174
|
-
js_value_t *args[2];
|
|
175
|
-
|
|
176
|
-
err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
|
|
177
|
-
if (err < 0) goto err;
|
|
178
|
-
|
|
179
|
-
args[1] = meta;
|
|
508
|
+
js_value_t *args[2] = {id, meta};
|
|
180
509
|
|
|
181
510
|
js_value_t *result;
|
|
182
511
|
err = js_call_function(env, ctx, on_meta, 2, args, &result);
|
|
@@ -193,156 +522,272 @@ err:
|
|
|
193
522
|
}
|
|
194
523
|
|
|
195
524
|
static void
|
|
196
|
-
|
|
525
|
+
bare_module__destroy_context(js_env_t *env, bare_module_context_t *context) {
|
|
197
526
|
int err;
|
|
198
527
|
|
|
199
|
-
bare_module_context_t *context = data;
|
|
200
|
-
|
|
201
|
-
js_env_t *env = context->env;
|
|
202
|
-
|
|
203
528
|
err = js_delete_reference(env, context->on_import);
|
|
204
529
|
assert(err == 0);
|
|
205
530
|
|
|
531
|
+
err = js_delete_reference(env, context->on_dynamic_import);
|
|
532
|
+
assert(err == 0);
|
|
533
|
+
|
|
206
534
|
err = js_delete_reference(env, context->on_evaluate);
|
|
207
535
|
assert(err == 0);
|
|
208
536
|
|
|
209
537
|
err = js_delete_reference(env, context->on_meta);
|
|
210
538
|
assert(err == 0);
|
|
211
539
|
|
|
212
|
-
|
|
213
|
-
|
|
540
|
+
free(context);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
static void
|
|
544
|
+
bare_module__retain_context(bare_module_context_t *context) {
|
|
545
|
+
context->refs++;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
static void
|
|
549
|
+
bare_module__release_context(js_env_t *env, bare_module_context_t *context) {
|
|
550
|
+
if (--context->refs == 0) bare_module__destroy_context(env, context);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
static void
|
|
554
|
+
bare_module__on_finalize_context(js_env_t *env, void *data, void *finalize_hint) {
|
|
555
|
+
bare_module__release_context(env, (bare_module_context_t *) data);
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
static void
|
|
559
|
+
bare_module__on_finalize_unit(js_env_t *env, void *data, void *finalize_hint) {
|
|
560
|
+
bare_module__release_context(env, (bare_module_context_t *) data);
|
|
214
561
|
}
|
|
215
562
|
|
|
216
563
|
static js_value_t *
|
|
217
|
-
|
|
564
|
+
bare_module_create_context(js_env_t *env, js_callback_info_t *info) {
|
|
218
565
|
int err;
|
|
219
566
|
|
|
220
|
-
size_t argc =
|
|
221
|
-
js_value_t *argv[
|
|
567
|
+
size_t argc = 5;
|
|
568
|
+
js_value_t *argv[5];
|
|
222
569
|
|
|
223
570
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
224
571
|
assert(err == 0);
|
|
225
572
|
|
|
226
|
-
|
|
573
|
+
if (!bare_module__check_unclaimed(env, argv[0])) return NULL;
|
|
574
|
+
if (!bare_module__check_function(env, argv[1], "Import handler must be a function")) return NULL;
|
|
575
|
+
if (!bare_module__check_function(env, argv[2], "Dynamic import handler must be a function")) return NULL;
|
|
576
|
+
if (!bare_module__check_function(env, argv[3], "Evaluate handler must be a function")) return NULL;
|
|
577
|
+
if (!bare_module__check_function(env, argv[4], "Meta handler must be a function")) return NULL;
|
|
227
578
|
|
|
228
|
-
bare_module_context_t *context;
|
|
579
|
+
bare_module_context_t *context = malloc(sizeof(bare_module_context_t));
|
|
229
580
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
581
|
+
if (context == NULL) {
|
|
582
|
+
err = js_throw_error(env, uv_err_name(UV_ENOMEM), uv_strerror(UV_ENOMEM));
|
|
583
|
+
assert(err == 0);
|
|
233
584
|
|
|
234
|
-
|
|
585
|
+
return NULL;
|
|
586
|
+
}
|
|
235
587
|
|
|
236
|
-
|
|
237
|
-
assert(err == 0);
|
|
588
|
+
context->refs = 1; // Held by the wrap installed below.
|
|
238
589
|
|
|
239
590
|
err = js_create_reference(env, argv[1], 1, &context->on_import);
|
|
240
591
|
assert(err == 0);
|
|
241
592
|
|
|
242
|
-
err = js_create_reference(env, argv[2], 1, &context->
|
|
593
|
+
err = js_create_reference(env, argv[2], 1, &context->on_dynamic_import);
|
|
243
594
|
assert(err == 0);
|
|
244
595
|
|
|
245
|
-
err = js_create_reference(env, argv[3], 1, &context->
|
|
596
|
+
err = js_create_reference(env, argv[3], 1, &context->on_evaluate);
|
|
246
597
|
assert(err == 0);
|
|
247
598
|
|
|
248
|
-
err =
|
|
599
|
+
err = js_create_reference(env, argv[4], 1, &context->on_meta);
|
|
249
600
|
assert(err == 0);
|
|
250
601
|
|
|
251
|
-
err =
|
|
252
|
-
assert(err == 0);
|
|
602
|
+
err = js_wrap(env, argv[0], (void *) context, bare_module__on_finalize_context, NULL, NULL);
|
|
253
603
|
|
|
254
|
-
|
|
604
|
+
if (err < 0) {
|
|
605
|
+
bare_module__destroy_context(env, context);
|
|
606
|
+
|
|
607
|
+
return NULL;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
err = js_add_type_tag(env, argv[0], &bare_module__context_tag);
|
|
611
|
+
|
|
612
|
+
if (err < 0 && bare_module__release_wrap(env, argv[0])) {
|
|
613
|
+
bare_module__destroy_context(env, context);
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return NULL;
|
|
255
617
|
}
|
|
256
618
|
|
|
257
619
|
static js_value_t *
|
|
258
620
|
bare_module_create_function(js_env_t *env, js_callback_info_t *info) {
|
|
259
621
|
int err;
|
|
260
622
|
|
|
261
|
-
size_t argc =
|
|
262
|
-
js_value_t *argv[
|
|
623
|
+
size_t argc = 5;
|
|
624
|
+
js_value_t *argv[5];
|
|
263
625
|
|
|
264
626
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
265
627
|
assert(err == 0);
|
|
266
628
|
|
|
267
|
-
|
|
629
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
630
|
+
if (!bare_module__check_string(env, argv[3], "Source must be a string")) return NULL;
|
|
268
631
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
632
|
+
int32_t offset;
|
|
633
|
+
if (!bare_module__get_offset(env, argv[4], &offset)) return NULL;
|
|
634
|
+
|
|
635
|
+
bare_module_context_t *context;
|
|
636
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
272
637
|
if (err < 0) return NULL;
|
|
273
638
|
|
|
639
|
+
size_t file_len;
|
|
640
|
+
utf8_t file[4096];
|
|
641
|
+
if (!bare_module__get_string(env, argv[1], file, sizeof(file), &file_len)) return NULL;
|
|
642
|
+
|
|
274
643
|
uint32_t args_len;
|
|
275
|
-
|
|
276
|
-
if (
|
|
644
|
+
js_value_t *args[5];
|
|
645
|
+
if (!bare_module__get_strings(env, argv[2], args, sizeof(args) / sizeof(args[0]), &args_len, "Argument names must be strings")) return NULL;
|
|
277
646
|
|
|
278
|
-
js_value_t
|
|
647
|
+
js_value_t *source = argv[3];
|
|
279
648
|
|
|
280
|
-
|
|
281
|
-
|
|
649
|
+
js_value_t *result;
|
|
650
|
+
err = js_create_function_with_source(env, NULL, 0, (char *) file, file_len, args, args_len, offset, source, &result);
|
|
651
|
+
if (err < 0) return NULL;
|
|
282
652
|
|
|
283
|
-
|
|
653
|
+
err = js_on_function_dynamic_import(env, result, bare_module__on_dynamic_import, (void *) context);
|
|
654
|
+
if (err < 0) return NULL;
|
|
284
655
|
|
|
285
|
-
|
|
286
|
-
err = js_get_value_int32(env, argv[3], &offset);
|
|
287
|
-
if (err < 0) goto err;
|
|
656
|
+
bare_module__retain_context(context);
|
|
288
657
|
|
|
289
|
-
|
|
290
|
-
err = js_create_function_with_source(env, NULL, 0, (char *) file, file_len, args, args_len, 0, source, &result);
|
|
291
|
-
if (err < 0) goto err;
|
|
658
|
+
err = js_add_finalizer(env, result, (void *) context, bare_module__on_finalize_unit, NULL, NULL);
|
|
292
659
|
|
|
293
|
-
|
|
660
|
+
if (err < 0) {
|
|
661
|
+
bare_module__release_context(env, context);
|
|
662
|
+
|
|
663
|
+
return NULL;
|
|
664
|
+
}
|
|
294
665
|
|
|
295
666
|
return result;
|
|
667
|
+
}
|
|
296
668
|
|
|
297
|
-
|
|
298
|
-
|
|
669
|
+
static js_value_t *
|
|
670
|
+
bare_module_get_function_id(js_env_t *env, js_callback_info_t *info) {
|
|
671
|
+
int err;
|
|
299
672
|
|
|
300
|
-
|
|
673
|
+
size_t argc = 1;
|
|
674
|
+
js_value_t *argv[1];
|
|
675
|
+
|
|
676
|
+
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
677
|
+
assert(err == 0);
|
|
678
|
+
|
|
679
|
+
if (!bare_module__check_function(env, argv[0], "Receiver must be a function")) return NULL;
|
|
680
|
+
|
|
681
|
+
js_value_t *result;
|
|
682
|
+
err = js_get_function_id(env, argv[0], &result);
|
|
683
|
+
if (err < 0) return NULL;
|
|
684
|
+
|
|
685
|
+
return result;
|
|
301
686
|
}
|
|
302
687
|
|
|
303
688
|
static void
|
|
304
689
|
bare_module__on_finalize(js_env_t *env, void *data, void *finalize_hint) {
|
|
305
690
|
int err;
|
|
306
691
|
|
|
307
|
-
|
|
692
|
+
bare_module_record_t *handle = (bare_module_record_t *) data;
|
|
308
693
|
|
|
309
|
-
err = js_delete_module(env, module);
|
|
694
|
+
err = js_delete_module(env, handle->module);
|
|
310
695
|
assert(err == 0);
|
|
696
|
+
|
|
697
|
+
bare_module__release_context(env, handle->context);
|
|
698
|
+
|
|
699
|
+
free(handle);
|
|
311
700
|
}
|
|
312
701
|
|
|
313
702
|
static js_value_t *
|
|
314
703
|
bare_module_create_module(js_env_t *env, js_callback_info_t *info) {
|
|
315
704
|
int err;
|
|
316
705
|
|
|
317
|
-
size_t argc =
|
|
318
|
-
js_value_t *argv[
|
|
706
|
+
size_t argc = 5;
|
|
707
|
+
js_value_t *argv[5];
|
|
319
708
|
|
|
320
709
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
321
710
|
assert(err == 0);
|
|
322
711
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
utf8_t file[1024];
|
|
327
|
-
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
328
|
-
if (err < 0) return NULL;
|
|
329
|
-
|
|
330
|
-
js_value_t *source = argv[1];
|
|
712
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
713
|
+
if (!bare_module__check_unclaimed(env, argv[1])) return NULL;
|
|
714
|
+
if (!bare_module__check_string(env, argv[3], "Source must be a string")) return NULL;
|
|
331
715
|
|
|
332
716
|
int32_t offset;
|
|
333
|
-
|
|
334
|
-
if (err < 0) return NULL;
|
|
717
|
+
if (!bare_module__get_offset(env, argv[4], &offset)) return NULL;
|
|
335
718
|
|
|
336
719
|
bare_module_context_t *context;
|
|
337
|
-
err =
|
|
720
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
338
721
|
if (err < 0) return NULL;
|
|
339
722
|
|
|
723
|
+
size_t file_len;
|
|
724
|
+
utf8_t file[4096];
|
|
725
|
+
if (!bare_module__get_string(env, argv[2], file, sizeof(file), &file_len)) return NULL;
|
|
726
|
+
|
|
727
|
+
js_value_t *source = argv[3];
|
|
728
|
+
|
|
729
|
+
bare_module_record_t *handle = malloc(sizeof(bare_module_record_t));
|
|
730
|
+
|
|
731
|
+
if (handle == NULL) {
|
|
732
|
+
err = js_throw_error(env, uv_err_name(UV_ENOMEM), uv_strerror(UV_ENOMEM));
|
|
733
|
+
assert(err == 0);
|
|
734
|
+
|
|
735
|
+
return NULL;
|
|
736
|
+
}
|
|
737
|
+
|
|
340
738
|
js_module_t *module;
|
|
341
739
|
err = js_create_module(env, (char *) file, file_len, offset, source, bare_module__on_meta, (void *) context, &module);
|
|
342
|
-
|
|
740
|
+
|
|
741
|
+
if (err < 0) {
|
|
742
|
+
free(handle);
|
|
743
|
+
|
|
744
|
+
return NULL;
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
err = js_on_module_dynamic_import(env, module, bare_module__on_dynamic_import, (void *) context);
|
|
748
|
+
|
|
749
|
+
if (err < 0) {
|
|
750
|
+
err = js_delete_module(env, module);
|
|
751
|
+
assert(err == 0);
|
|
752
|
+
|
|
753
|
+
free(handle);
|
|
754
|
+
|
|
755
|
+
return NULL;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
handle->module = module;
|
|
759
|
+
handle->context = context;
|
|
760
|
+
|
|
761
|
+
err = js_wrap(env, argv[1], (void *) handle, bare_module__on_finalize, NULL, NULL);
|
|
762
|
+
|
|
763
|
+
if (err < 0) {
|
|
764
|
+
err = js_delete_module(env, module);
|
|
765
|
+
assert(err == 0);
|
|
766
|
+
|
|
767
|
+
free(handle);
|
|
768
|
+
|
|
769
|
+
return NULL;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
bare_module__retain_context(context);
|
|
773
|
+
|
|
774
|
+
err = js_add_type_tag(env, argv[1], &bare_module__module_tag);
|
|
775
|
+
|
|
776
|
+
if (err < 0) {
|
|
777
|
+
if (bare_module__release_wrap(env, argv[1])) {
|
|
778
|
+
err = js_delete_module(env, module);
|
|
779
|
+
assert(err == 0);
|
|
780
|
+
|
|
781
|
+
free(handle);
|
|
782
|
+
|
|
783
|
+
bare_module__release_context(env, context);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
return NULL;
|
|
787
|
+
}
|
|
343
788
|
|
|
344
789
|
js_value_t *result;
|
|
345
|
-
err =
|
|
790
|
+
err = js_get_module_id(env, module, &result);
|
|
346
791
|
if (err < 0) return NULL;
|
|
347
792
|
|
|
348
793
|
return result;
|
|
@@ -352,67 +797,115 @@ static js_value_t *
|
|
|
352
797
|
bare_module_create_synthetic_module(js_env_t *env, js_callback_info_t *info) {
|
|
353
798
|
int err;
|
|
354
799
|
|
|
355
|
-
size_t argc =
|
|
356
|
-
js_value_t *argv[
|
|
800
|
+
size_t argc = 4;
|
|
801
|
+
js_value_t *argv[4];
|
|
357
802
|
|
|
358
803
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
359
804
|
assert(err == 0);
|
|
360
805
|
|
|
361
|
-
|
|
806
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
362
807
|
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
808
|
+
bare_module_context_t *context;
|
|
809
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
366
810
|
if (err < 0) return NULL;
|
|
367
811
|
|
|
812
|
+
size_t file_len;
|
|
813
|
+
utf8_t file[4096];
|
|
814
|
+
if (!bare_module__get_string(env, argv[2], file, sizeof(file), &file_len)) return NULL;
|
|
815
|
+
|
|
816
|
+
js_value_t **export_names;
|
|
368
817
|
uint32_t names_len;
|
|
369
|
-
|
|
370
|
-
if (err < 0) return NULL;
|
|
818
|
+
if (!bare_module__alloc_strings(env, argv[3], &export_names, BARE_MODULE_MAX_EXPORT_NAMES, &names_len, "Export names must be strings")) return NULL;
|
|
371
819
|
|
|
372
|
-
|
|
820
|
+
if (!bare_module__check_unclaimed(env, argv[1])) {
|
|
821
|
+
free(export_names);
|
|
373
822
|
|
|
374
|
-
|
|
375
|
-
|
|
823
|
+
return NULL;
|
|
824
|
+
}
|
|
376
825
|
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if (
|
|
826
|
+
bare_module_record_t *handle = malloc(sizeof(bare_module_record_t));
|
|
827
|
+
|
|
828
|
+
if (handle == NULL) {
|
|
829
|
+
free(export_names);
|
|
830
|
+
|
|
831
|
+
err = js_throw_error(env, uv_err_name(UV_ENOMEM), uv_strerror(UV_ENOMEM));
|
|
832
|
+
assert(err == 0);
|
|
833
|
+
|
|
834
|
+
return NULL;
|
|
835
|
+
}
|
|
380
836
|
|
|
381
837
|
js_module_t *module;
|
|
382
838
|
err = js_create_synthetic_module(env, (char *) file, file_len, export_names, names_len, bare_module__on_evaluate, (void *) context, &module);
|
|
383
|
-
if (err < 0) goto err;
|
|
384
|
-
|
|
385
|
-
js_value_t *result;
|
|
386
|
-
err = js_create_external(env, (void *) module, bare_module__on_finalize, NULL, &result);
|
|
387
|
-
if (err < 0) goto err;
|
|
388
839
|
|
|
389
840
|
free(export_names);
|
|
390
841
|
|
|
391
|
-
|
|
842
|
+
if (err < 0) {
|
|
843
|
+
free(handle);
|
|
392
844
|
|
|
393
|
-
|
|
394
|
-
|
|
845
|
+
return NULL;
|
|
846
|
+
}
|
|
395
847
|
|
|
396
|
-
|
|
848
|
+
handle->module = module;
|
|
849
|
+
handle->context = context;
|
|
850
|
+
|
|
851
|
+
err = js_wrap(env, argv[1], (void *) handle, bare_module__on_finalize, NULL, NULL);
|
|
852
|
+
|
|
853
|
+
if (err < 0) {
|
|
854
|
+
err = js_delete_module(env, module);
|
|
855
|
+
assert(err == 0);
|
|
856
|
+
|
|
857
|
+
free(handle);
|
|
858
|
+
|
|
859
|
+
return NULL;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
bare_module__retain_context(context);
|
|
863
|
+
|
|
864
|
+
err = js_add_type_tag(env, argv[1], &bare_module__synthetic_module_tag);
|
|
865
|
+
|
|
866
|
+
if (err < 0) {
|
|
867
|
+
if (bare_module__release_wrap(env, argv[1])) {
|
|
868
|
+
err = js_delete_module(env, module);
|
|
869
|
+
assert(err == 0);
|
|
870
|
+
|
|
871
|
+
free(handle);
|
|
872
|
+
|
|
873
|
+
bare_module__release_context(env, context);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
return NULL;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
js_value_t *result;
|
|
880
|
+
err = js_get_module_id(env, module, &result);
|
|
881
|
+
if (err < 0) return NULL;
|
|
882
|
+
|
|
883
|
+
return result;
|
|
397
884
|
}
|
|
398
885
|
|
|
399
886
|
static js_value_t *
|
|
400
|
-
|
|
887
|
+
bare_module_set_module_export(js_env_t *env, js_callback_info_t *info) {
|
|
401
888
|
int err;
|
|
402
889
|
|
|
403
|
-
size_t argc =
|
|
404
|
-
js_value_t *argv[
|
|
890
|
+
size_t argc = 4;
|
|
891
|
+
js_value_t *argv[4];
|
|
405
892
|
|
|
406
893
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
407
894
|
assert(err == 0);
|
|
408
895
|
|
|
409
|
-
|
|
896
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
897
|
+
if (!bare_module__check_synthetic_module(env, argv[1])) return NULL;
|
|
898
|
+
if (!bare_module__check_string(env, argv[2], "Export name must be a string")) return NULL;
|
|
410
899
|
|
|
411
|
-
|
|
412
|
-
err =
|
|
900
|
+
bare_module_context_t *context;
|
|
901
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
413
902
|
if (err < 0) return NULL;
|
|
414
903
|
|
|
415
|
-
|
|
904
|
+
js_module_t *module;
|
|
905
|
+
if (!bare_module__get_module(env, argv[1], context, &module)) return NULL;
|
|
906
|
+
|
|
907
|
+
err = js_set_module_export(env, module, argv[2], argv[3]);
|
|
908
|
+
if (err < 0) return NULL;
|
|
416
909
|
|
|
417
910
|
return NULL;
|
|
418
911
|
}
|
|
@@ -427,17 +920,18 @@ bare_module_run_module(js_env_t *env, js_callback_info_t *info) {
|
|
|
427
920
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
428
921
|
assert(err == 0);
|
|
429
922
|
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
err = js_get_value_external(env, argv[0], (void **) &module);
|
|
434
|
-
if (err < 0) return NULL;
|
|
923
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
924
|
+
if (!bare_module__check_module(env, argv[1])) return NULL;
|
|
925
|
+
if (!bare_module__check_function(env, argv[2], "Run handler must be a function")) return NULL;
|
|
435
926
|
|
|
436
927
|
bare_module_context_t *context;
|
|
437
|
-
err =
|
|
928
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
438
929
|
if (err < 0) return NULL;
|
|
439
930
|
|
|
440
|
-
|
|
931
|
+
js_module_t *module;
|
|
932
|
+
if (!bare_module__get_module(env, argv[1], context, &module)) return NULL;
|
|
933
|
+
|
|
934
|
+
err = js_instantiate_module(env, module, bare_module__on_import, (void *) context);
|
|
441
935
|
if (err < 0) return NULL;
|
|
442
936
|
|
|
443
937
|
js_value_t *promise;
|
|
@@ -467,200 +961,44 @@ bare_module_run_module(js_env_t *env, js_callback_info_t *info) {
|
|
|
467
961
|
err = js_get_and_clear_last_exception(env, &exception);
|
|
468
962
|
assert(err == 0);
|
|
469
963
|
|
|
470
|
-
js_value_t *
|
|
471
|
-
err =
|
|
964
|
+
js_value_t *undefined;
|
|
965
|
+
err = js_get_undefined(env, &undefined);
|
|
472
966
|
assert(err == 0);
|
|
473
967
|
|
|
474
968
|
js_value_t *args[3] = {reason, promise, exception};
|
|
475
969
|
|
|
476
|
-
js_call_function(env,
|
|
970
|
+
err = js_call_function(env, undefined, argv[2], 3, args, NULL);
|
|
971
|
+
if (err < 0) return NULL;
|
|
477
972
|
}
|
|
478
973
|
|
|
479
974
|
return promise;
|
|
480
975
|
}
|
|
481
976
|
|
|
482
977
|
static js_value_t *
|
|
483
|
-
|
|
484
|
-
int err;
|
|
485
|
-
|
|
486
|
-
size_t argc = 1;
|
|
487
|
-
js_value_t *argv[1];
|
|
488
|
-
|
|
489
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
490
|
-
assert(err == 0);
|
|
491
|
-
|
|
492
|
-
js_module_t *module;
|
|
493
|
-
err = js_get_value_external(env, argv[0], (void **) &module);
|
|
494
|
-
if (err < 0) return NULL;
|
|
495
|
-
|
|
496
|
-
js_value_t *result;
|
|
497
|
-
err = js_get_module_namespace(env, module, &result);
|
|
498
|
-
if (err < 0) return NULL;
|
|
499
|
-
|
|
500
|
-
return result;
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
static js_value_t *
|
|
504
|
-
bare_module_exists(js_env_t *env, js_callback_info_t *info) {
|
|
978
|
+
bare_module_get_module_namespace(js_env_t *env, js_callback_info_t *info) {
|
|
505
979
|
int err;
|
|
506
980
|
|
|
507
|
-
uv_loop_t *loop;
|
|
508
|
-
err = js_get_env_loop(env, &loop);
|
|
509
|
-
assert(err == 0);
|
|
510
|
-
|
|
511
|
-
js_value_t *argv[2];
|
|
512
981
|
size_t argc = 2;
|
|
982
|
+
js_value_t *argv[2];
|
|
513
983
|
|
|
514
984
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
515
985
|
assert(err == 0);
|
|
516
986
|
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
utf8_t path[4096];
|
|
520
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
521
|
-
assert(err == 0);
|
|
522
|
-
|
|
523
|
-
uint32_t mode;
|
|
524
|
-
err = js_get_value_uint32(env, argv[1], &mode);
|
|
525
|
-
assert(err == 0);
|
|
526
|
-
|
|
527
|
-
uv_fs_t req;
|
|
528
|
-
uv_fs_stat(loop, &req, (char *) path, NULL);
|
|
529
|
-
|
|
530
|
-
uv_stat_t *st = req.result < 0 ? NULL : req.ptr;
|
|
531
|
-
|
|
532
|
-
js_value_t *result;
|
|
533
|
-
err = js_get_boolean(env, st && st->st_mode & mode, &result);
|
|
534
|
-
assert(err == 0);
|
|
535
|
-
|
|
536
|
-
uv_fs_req_cleanup(&req);
|
|
537
|
-
|
|
538
|
-
return result;
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
static js_value_t *
|
|
542
|
-
bare_module_realpath(js_env_t *env, js_callback_info_t *info) {
|
|
543
|
-
int err;
|
|
544
|
-
|
|
545
|
-
uv_loop_t *loop;
|
|
546
|
-
err = js_get_env_loop(env, &loop);
|
|
547
|
-
assert(err == 0);
|
|
548
|
-
|
|
549
|
-
js_value_t *argv[1];
|
|
550
|
-
size_t argc = 1;
|
|
551
|
-
|
|
552
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
553
|
-
assert(err == 0);
|
|
554
|
-
|
|
555
|
-
assert(argc == 1);
|
|
556
|
-
|
|
557
|
-
utf8_t path[4096];
|
|
558
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
559
|
-
assert(err == 0);
|
|
560
|
-
|
|
561
|
-
uv_fs_t req;
|
|
562
|
-
uv_fs_realpath(loop, &req, (char *) path, NULL);
|
|
563
|
-
|
|
564
|
-
int res = req.result;
|
|
565
|
-
|
|
566
|
-
if (res < 0) {
|
|
567
|
-
uv_fs_req_cleanup(&req);
|
|
568
|
-
err = res;
|
|
569
|
-
goto err;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
js_value_t *result;
|
|
573
|
-
err = js_create_string_utf8(env, (utf8_t *) req.ptr, -1, &result);
|
|
574
|
-
assert(err == 0);
|
|
575
|
-
|
|
576
|
-
uv_fs_req_cleanup(&req);
|
|
577
|
-
|
|
578
|
-
return result;
|
|
579
|
-
|
|
580
|
-
err:
|
|
581
|
-
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
|
|
582
|
-
assert(err == 0);
|
|
583
|
-
|
|
584
|
-
return NULL;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
static js_value_t *
|
|
588
|
-
bare_module_read(js_env_t *env, js_callback_info_t *info) {
|
|
589
|
-
int err;
|
|
590
|
-
|
|
591
|
-
uv_loop_t *loop;
|
|
592
|
-
err = js_get_env_loop(env, &loop);
|
|
593
|
-
assert(err == 0);
|
|
594
|
-
|
|
595
|
-
js_value_t *argv[1];
|
|
596
|
-
size_t argc = 1;
|
|
597
|
-
|
|
598
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
599
|
-
assert(err == 0);
|
|
600
|
-
|
|
601
|
-
assert(argc == 1);
|
|
602
|
-
|
|
603
|
-
utf8_t path[4096];
|
|
604
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
605
|
-
assert(err == 0);
|
|
987
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
988
|
+
if (!bare_module__check_module(env, argv[1])) return NULL;
|
|
606
989
|
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
int fd = req.result;
|
|
611
|
-
uv_fs_req_cleanup(&req);
|
|
612
|
-
|
|
613
|
-
if (fd < 0) {
|
|
614
|
-
err = fd;
|
|
615
|
-
goto err;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
uv_fs_fstat(loop, &req, fd, NULL);
|
|
619
|
-
uv_stat_t *st = req.ptr;
|
|
990
|
+
bare_module_context_t *context;
|
|
991
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
992
|
+
if (err < 0) return NULL;
|
|
620
993
|
|
|
621
|
-
|
|
622
|
-
|
|
994
|
+
js_module_t *module;
|
|
995
|
+
if (!bare_module__get_module(env, argv[1], context, &module)) return NULL;
|
|
623
996
|
|
|
624
997
|
js_value_t *result;
|
|
625
|
-
err =
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
uv_buf_t buffer = uv_buf_init(base, len);
|
|
629
|
-
|
|
630
|
-
uv_fs_req_cleanup(&req);
|
|
631
|
-
|
|
632
|
-
int64_t read = 0;
|
|
633
|
-
|
|
634
|
-
while (true) {
|
|
635
|
-
uv_fs_read(loop, &req, fd, &buffer, 1, read, NULL);
|
|
636
|
-
|
|
637
|
-
int res = req.result;
|
|
638
|
-
uv_fs_req_cleanup(&req);
|
|
639
|
-
|
|
640
|
-
if (res < 0) {
|
|
641
|
-
uv_fs_close(loop, &req, fd, NULL);
|
|
642
|
-
uv_fs_req_cleanup(&req);
|
|
643
|
-
err = res;
|
|
644
|
-
goto err;
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
buffer.base += res;
|
|
648
|
-
buffer.len -= res;
|
|
649
|
-
|
|
650
|
-
read += res;
|
|
651
|
-
if (res == 0 || read == len) break;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
uv_fs_close(loop, &req, fd, NULL);
|
|
655
|
-
uv_fs_req_cleanup(&req);
|
|
998
|
+
err = js_get_module_namespace(env, module, &result);
|
|
999
|
+
if (err < 0) return NULL;
|
|
656
1000
|
|
|
657
1001
|
return result;
|
|
658
|
-
|
|
659
|
-
err:
|
|
660
|
-
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
|
|
661
|
-
assert(err == 0);
|
|
662
|
-
|
|
663
|
-
return NULL;
|
|
664
1002
|
}
|
|
665
1003
|
|
|
666
1004
|
static js_value_t *
|
|
@@ -676,31 +1014,16 @@ bare_module_exports(js_env_t *env, js_value_t *exports) {
|
|
|
676
1014
|
assert(err == 0); \
|
|
677
1015
|
}
|
|
678
1016
|
|
|
679
|
-
V("
|
|
1017
|
+
V("createContext", bare_module_create_context)
|
|
680
1018
|
|
|
681
1019
|
V("createFunction", bare_module_create_function)
|
|
1020
|
+
V("getFunctionID", bare_module_get_function_id)
|
|
1021
|
+
|
|
682
1022
|
V("createModule", bare_module_create_module)
|
|
683
1023
|
V("createSyntheticModule", bare_module_create_synthetic_module)
|
|
684
|
-
V("
|
|
1024
|
+
V("setModuleExport", bare_module_set_module_export)
|
|
685
1025
|
V("runModule", bare_module_run_module)
|
|
686
|
-
V("
|
|
687
|
-
|
|
688
|
-
V("exists", bare_module_exists)
|
|
689
|
-
V("realpath", bare_module_realpath)
|
|
690
|
-
V("read", bare_module_read)
|
|
691
|
-
#undef V
|
|
692
|
-
|
|
693
|
-
#define V(name, n) \
|
|
694
|
-
{ \
|
|
695
|
-
js_value_t *val; \
|
|
696
|
-
err = js_create_uint32(env, n, &val); \
|
|
697
|
-
assert(err == 0); \
|
|
698
|
-
err = js_set_named_property(env, exports, name, val); \
|
|
699
|
-
assert(err == 0); \
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
V("FILE", S_IFREG)
|
|
703
|
-
V("DIR", S_IFDIR)
|
|
1026
|
+
V("getModuleNamespace", bare_module_get_module_namespace)
|
|
704
1027
|
#undef V
|
|
705
1028
|
|
|
706
1029
|
return exports;
|