bare-module 6.4.0 → 7.0.1
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 +623 -333
- package/index.d.ts +156 -63
- package/index.js +87 -1002
- 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 +18 -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,7 +371,7 @@ 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;
|
|
@@ -38,21 +382,17 @@ bare_module__on_static_import(js_env_t *env, js_value_t *specifier, js_value_t *
|
|
|
38
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
|
-
assert(err == 0);
|
|
45
|
-
|
|
46
|
-
err = js_get_boolean(env, false, &args[4]);
|
|
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[
|
|
88
|
-
|
|
89
|
-
err = js_get_boolean(env, true, &args[4]);
|
|
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,7 +456,7 @@ 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;
|
|
@@ -157,7 +494,7 @@ bare_module__on_meta(js_env_t *env, js_module_t *module, js_value_t *meta, void
|
|
|
157
494
|
assert(err == 0);
|
|
158
495
|
|
|
159
496
|
js_value_t *ctx;
|
|
160
|
-
err =
|
|
497
|
+
err = js_get_undefined(env, &ctx);
|
|
161
498
|
assert(err == 0);
|
|
162
499
|
|
|
163
500
|
js_value_t *on_meta;
|
|
@@ -185,111 +522,148 @@ err:
|
|
|
185
522
|
}
|
|
186
523
|
|
|
187
524
|
static void
|
|
188
|
-
|
|
525
|
+
bare_module__destroy_context(js_env_t *env, bare_module_context_t *context) {
|
|
189
526
|
int err;
|
|
190
527
|
|
|
191
|
-
bare_module_context_t *context = data;
|
|
192
|
-
|
|
193
|
-
js_env_t *env = context->env;
|
|
194
|
-
|
|
195
528
|
err = js_delete_reference(env, context->on_import);
|
|
196
529
|
assert(err == 0);
|
|
197
530
|
|
|
531
|
+
err = js_delete_reference(env, context->on_dynamic_import);
|
|
532
|
+
assert(err == 0);
|
|
533
|
+
|
|
198
534
|
err = js_delete_reference(env, context->on_evaluate);
|
|
199
535
|
assert(err == 0);
|
|
200
536
|
|
|
201
537
|
err = js_delete_reference(env, context->on_meta);
|
|
202
538
|
assert(err == 0);
|
|
203
539
|
|
|
204
|
-
|
|
205
|
-
|
|
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);
|
|
206
561
|
}
|
|
207
562
|
|
|
208
563
|
static js_value_t *
|
|
209
|
-
|
|
564
|
+
bare_module_create_context(js_env_t *env, js_callback_info_t *info) {
|
|
210
565
|
int err;
|
|
211
566
|
|
|
212
|
-
size_t argc =
|
|
213
|
-
js_value_t *argv[
|
|
567
|
+
size_t argc = 5;
|
|
568
|
+
js_value_t *argv[5];
|
|
214
569
|
|
|
215
570
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
216
571
|
assert(err == 0);
|
|
217
572
|
|
|
218
|
-
|
|
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;
|
|
219
578
|
|
|
220
|
-
bare_module_context_t *context;
|
|
579
|
+
bare_module_context_t *context = malloc(sizeof(bare_module_context_t));
|
|
221
580
|
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
581
|
+
if (context == NULL) {
|
|
582
|
+
err = js_throw_error(env, uv_err_name(UV_ENOMEM), uv_strerror(UV_ENOMEM));
|
|
583
|
+
assert(err == 0);
|
|
225
584
|
|
|
226
|
-
|
|
585
|
+
return NULL;
|
|
586
|
+
}
|
|
227
587
|
|
|
228
|
-
|
|
229
|
-
assert(err == 0);
|
|
588
|
+
context->refs = 1; // Held by the wrap installed below.
|
|
230
589
|
|
|
231
590
|
err = js_create_reference(env, argv[1], 1, &context->on_import);
|
|
232
591
|
assert(err == 0);
|
|
233
592
|
|
|
234
|
-
err = js_create_reference(env, argv[2], 1, &context->
|
|
593
|
+
err = js_create_reference(env, argv[2], 1, &context->on_dynamic_import);
|
|
235
594
|
assert(err == 0);
|
|
236
595
|
|
|
237
|
-
err = js_create_reference(env, argv[3], 1, &context->
|
|
596
|
+
err = js_create_reference(env, argv[3], 1, &context->on_evaluate);
|
|
238
597
|
assert(err == 0);
|
|
239
598
|
|
|
240
|
-
err =
|
|
599
|
+
err = js_create_reference(env, argv[4], 1, &context->on_meta);
|
|
241
600
|
assert(err == 0);
|
|
242
601
|
|
|
243
|
-
err =
|
|
244
|
-
assert(err == 0);
|
|
602
|
+
err = js_wrap(env, argv[0], (void *) context, bare_module__on_finalize_context, NULL, NULL);
|
|
245
603
|
|
|
246
|
-
|
|
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;
|
|
247
617
|
}
|
|
248
618
|
|
|
249
619
|
static js_value_t *
|
|
250
620
|
bare_module_create_function(js_env_t *env, js_callback_info_t *info) {
|
|
251
621
|
int err;
|
|
252
622
|
|
|
253
|
-
size_t argc =
|
|
254
|
-
js_value_t *argv[
|
|
623
|
+
size_t argc = 5;
|
|
624
|
+
js_value_t *argv[5];
|
|
255
625
|
|
|
256
626
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
257
627
|
assert(err == 0);
|
|
258
628
|
|
|
259
|
-
|
|
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;
|
|
260
631
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
264
|
-
if (err < 0) return NULL;
|
|
632
|
+
int32_t offset;
|
|
633
|
+
if (!bare_module__get_offset(env, argv[4], &offset)) return NULL;
|
|
265
634
|
|
|
266
|
-
|
|
267
|
-
err =
|
|
635
|
+
bare_module_context_t *context;
|
|
636
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
268
637
|
if (err < 0) return NULL;
|
|
269
638
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
if (err < 0) goto err;
|
|
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;
|
|
274
642
|
|
|
275
|
-
|
|
643
|
+
uint32_t args_len;
|
|
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;
|
|
276
646
|
|
|
277
|
-
|
|
278
|
-
err = js_get_value_int32(env, argv[3], &offset);
|
|
279
|
-
if (err < 0) goto err;
|
|
647
|
+
js_value_t *source = argv[3];
|
|
280
648
|
|
|
281
649
|
js_value_t *result;
|
|
282
|
-
err = js_create_function_with_source(env, NULL, 0, (char *) file, file_len, args, args_len,
|
|
283
|
-
if (err < 0)
|
|
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;
|
|
284
652
|
|
|
285
|
-
|
|
653
|
+
err = js_on_function_dynamic_import(env, result, bare_module__on_dynamic_import, (void *) context);
|
|
654
|
+
if (err < 0) return NULL;
|
|
286
655
|
|
|
287
|
-
|
|
656
|
+
bare_module__retain_context(context);
|
|
288
657
|
|
|
289
|
-
err
|
|
290
|
-
free(args);
|
|
658
|
+
err = js_add_finalizer(env, result, (void *) context, bare_module__on_finalize_unit, NULL, NULL);
|
|
291
659
|
|
|
292
|
-
|
|
660
|
+
if (err < 0) {
|
|
661
|
+
bare_module__release_context(env, context);
|
|
662
|
+
|
|
663
|
+
return NULL;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
return result;
|
|
293
667
|
}
|
|
294
668
|
|
|
295
669
|
static js_value_t *
|
|
@@ -302,6 +676,8 @@ bare_module_get_function_id(js_env_t *env, js_callback_info_t *info) {
|
|
|
302
676
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
303
677
|
assert(err == 0);
|
|
304
678
|
|
|
679
|
+
if (!bare_module__check_function(env, argv[0], "Receiver must be a function")) return NULL;
|
|
680
|
+
|
|
305
681
|
js_value_t *result;
|
|
306
682
|
err = js_get_function_id(env, argv[0], &result);
|
|
307
683
|
if (err < 0) return NULL;
|
|
@@ -313,45 +689,105 @@ static void
|
|
|
313
689
|
bare_module__on_finalize(js_env_t *env, void *data, void *finalize_hint) {
|
|
314
690
|
int err;
|
|
315
691
|
|
|
316
|
-
|
|
692
|
+
bare_module_record_t *handle = (bare_module_record_t *) data;
|
|
317
693
|
|
|
318
|
-
err = js_delete_module(env, module);
|
|
694
|
+
err = js_delete_module(env, handle->module);
|
|
319
695
|
assert(err == 0);
|
|
696
|
+
|
|
697
|
+
bare_module__release_context(env, handle->context);
|
|
698
|
+
|
|
699
|
+
free(handle);
|
|
320
700
|
}
|
|
321
701
|
|
|
322
702
|
static js_value_t *
|
|
323
703
|
bare_module_create_module(js_env_t *env, js_callback_info_t *info) {
|
|
324
704
|
int err;
|
|
325
705
|
|
|
326
|
-
size_t argc =
|
|
327
|
-
js_value_t *argv[
|
|
706
|
+
size_t argc = 5;
|
|
707
|
+
js_value_t *argv[5];
|
|
328
708
|
|
|
329
709
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
330
710
|
assert(err == 0);
|
|
331
711
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
utf8_t file[1024];
|
|
336
|
-
err = js_get_value_string_utf8(env, argv[0], file, 1024, &file_len);
|
|
337
|
-
if (err < 0) return NULL;
|
|
338
|
-
|
|
339
|
-
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;
|
|
340
715
|
|
|
341
716
|
int32_t offset;
|
|
342
|
-
|
|
343
|
-
if (err < 0) return NULL;
|
|
717
|
+
if (!bare_module__get_offset(env, argv[4], &offset)) return NULL;
|
|
344
718
|
|
|
345
719
|
bare_module_context_t *context;
|
|
346
|
-
err =
|
|
720
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
347
721
|
if (err < 0) return NULL;
|
|
348
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
|
+
|
|
349
738
|
js_module_t *module;
|
|
350
739
|
err = js_create_module(env, (char *) file, file_len, offset, source, bare_module__on_meta, (void *) context, &module);
|
|
351
|
-
|
|
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
|
+
}
|
|
352
788
|
|
|
353
789
|
js_value_t *result;
|
|
354
|
-
err =
|
|
790
|
+
err = js_get_module_id(env, module, &result);
|
|
355
791
|
if (err < 0) return NULL;
|
|
356
792
|
|
|
357
793
|
return result;
|
|
@@ -361,67 +797,115 @@ static js_value_t *
|
|
|
361
797
|
bare_module_create_synthetic_module(js_env_t *env, js_callback_info_t *info) {
|
|
362
798
|
int err;
|
|
363
799
|
|
|
364
|
-
size_t argc =
|
|
365
|
-
js_value_t *argv[
|
|
800
|
+
size_t argc = 4;
|
|
801
|
+
js_value_t *argv[4];
|
|
366
802
|
|
|
367
803
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
368
804
|
assert(err == 0);
|
|
369
805
|
|
|
370
|
-
|
|
806
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
371
807
|
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
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);
|
|
375
810
|
if (err < 0) return NULL;
|
|
376
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;
|
|
377
817
|
uint32_t names_len;
|
|
378
|
-
|
|
379
|
-
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;
|
|
380
819
|
|
|
381
|
-
|
|
820
|
+
if (!bare_module__check_unclaimed(env, argv[1])) {
|
|
821
|
+
free(export_names);
|
|
382
822
|
|
|
383
|
-
|
|
384
|
-
|
|
823
|
+
return NULL;
|
|
824
|
+
}
|
|
385
825
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
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
|
+
}
|
|
389
836
|
|
|
390
837
|
js_module_t *module;
|
|
391
838
|
err = js_create_synthetic_module(env, (char *) file, file_len, export_names, names_len, bare_module__on_evaluate, (void *) context, &module);
|
|
392
|
-
if (err < 0) goto err;
|
|
393
|
-
|
|
394
|
-
js_value_t *result;
|
|
395
|
-
err = js_create_external(env, (void *) module, bare_module__on_finalize, NULL, &result);
|
|
396
|
-
if (err < 0) goto err;
|
|
397
839
|
|
|
398
840
|
free(export_names);
|
|
399
841
|
|
|
400
|
-
|
|
842
|
+
if (err < 0) {
|
|
843
|
+
free(handle);
|
|
401
844
|
|
|
402
|
-
|
|
403
|
-
|
|
845
|
+
return NULL;
|
|
846
|
+
}
|
|
404
847
|
|
|
405
|
-
|
|
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;
|
|
406
884
|
}
|
|
407
885
|
|
|
408
886
|
static js_value_t *
|
|
409
887
|
bare_module_set_module_export(js_env_t *env, js_callback_info_t *info) {
|
|
410
888
|
int err;
|
|
411
889
|
|
|
412
|
-
size_t argc =
|
|
413
|
-
js_value_t *argv[
|
|
890
|
+
size_t argc = 4;
|
|
891
|
+
js_value_t *argv[4];
|
|
414
892
|
|
|
415
893
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
416
894
|
assert(err == 0);
|
|
417
895
|
|
|
418
|
-
|
|
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;
|
|
419
899
|
|
|
420
|
-
|
|
421
|
-
err =
|
|
900
|
+
bare_module_context_t *context;
|
|
901
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
422
902
|
if (err < 0) return NULL;
|
|
423
903
|
|
|
424
|
-
|
|
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;
|
|
425
909
|
|
|
426
910
|
return NULL;
|
|
427
911
|
}
|
|
@@ -436,17 +920,18 @@ bare_module_run_module(js_env_t *env, js_callback_info_t *info) {
|
|
|
436
920
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
437
921
|
assert(err == 0);
|
|
438
922
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
err = js_get_value_external(env, argv[0], (void **) &module);
|
|
443
|
-
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;
|
|
444
926
|
|
|
445
927
|
bare_module_context_t *context;
|
|
446
|
-
err =
|
|
928
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
447
929
|
if (err < 0) return NULL;
|
|
448
930
|
|
|
449
|
-
|
|
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);
|
|
450
935
|
if (err < 0) return NULL;
|
|
451
936
|
|
|
452
937
|
js_value_t *promise;
|
|
@@ -476,13 +961,14 @@ bare_module_run_module(js_env_t *env, js_callback_info_t *info) {
|
|
|
476
961
|
err = js_get_and_clear_last_exception(env, &exception);
|
|
477
962
|
assert(err == 0);
|
|
478
963
|
|
|
479
|
-
js_value_t *
|
|
480
|
-
err =
|
|
964
|
+
js_value_t *undefined;
|
|
965
|
+
err = js_get_undefined(env, &undefined);
|
|
481
966
|
assert(err == 0);
|
|
482
967
|
|
|
483
968
|
js_value_t *args[3] = {reason, promise, exception};
|
|
484
969
|
|
|
485
|
-
js_call_function(env,
|
|
970
|
+
err = js_call_function(env, undefined, argv[2], 3, args, NULL);
|
|
971
|
+
if (err < 0) return NULL;
|
|
486
972
|
}
|
|
487
973
|
|
|
488
974
|
return promise;
|
|
@@ -492,207 +978,29 @@ static js_value_t *
|
|
|
492
978
|
bare_module_get_module_namespace(js_env_t *env, js_callback_info_t *info) {
|
|
493
979
|
int err;
|
|
494
980
|
|
|
495
|
-
size_t argc =
|
|
496
|
-
js_value_t *argv[
|
|
981
|
+
size_t argc = 2;
|
|
982
|
+
js_value_t *argv[2];
|
|
497
983
|
|
|
498
984
|
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
499
985
|
assert(err == 0);
|
|
500
986
|
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
if (err < 0) return NULL;
|
|
987
|
+
if (!bare_module__check_context(env, argv[0])) return NULL;
|
|
988
|
+
if (!bare_module__check_module(env, argv[1])) return NULL;
|
|
504
989
|
|
|
505
|
-
|
|
506
|
-
err =
|
|
990
|
+
bare_module_context_t *context;
|
|
991
|
+
err = js_unwrap(env, argv[0], (void **) &context);
|
|
507
992
|
if (err < 0) return NULL;
|
|
508
993
|
|
|
509
|
-
return result;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
static js_value_t *
|
|
513
|
-
bare_module_get_module_id(js_env_t *env, js_callback_info_t *info) {
|
|
514
|
-
int err;
|
|
515
|
-
|
|
516
|
-
size_t argc = 1;
|
|
517
|
-
js_value_t *argv[1];
|
|
518
|
-
|
|
519
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
520
|
-
assert(err == 0);
|
|
521
|
-
|
|
522
994
|
js_module_t *module;
|
|
523
|
-
|
|
524
|
-
if (err < 0) return NULL;
|
|
995
|
+
if (!bare_module__get_module(env, argv[1], context, &module)) return NULL;
|
|
525
996
|
|
|
526
997
|
js_value_t *result;
|
|
527
|
-
err =
|
|
998
|
+
err = js_get_module_namespace(env, module, &result);
|
|
528
999
|
if (err < 0) return NULL;
|
|
529
1000
|
|
|
530
1001
|
return result;
|
|
531
1002
|
}
|
|
532
1003
|
|
|
533
|
-
static js_value_t *
|
|
534
|
-
bare_module_exists(js_env_t *env, js_callback_info_t *info) {
|
|
535
|
-
int err;
|
|
536
|
-
|
|
537
|
-
uv_loop_t *loop;
|
|
538
|
-
err = js_get_env_loop(env, &loop);
|
|
539
|
-
assert(err == 0);
|
|
540
|
-
|
|
541
|
-
js_value_t *argv[2];
|
|
542
|
-
size_t argc = 2;
|
|
543
|
-
|
|
544
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
545
|
-
assert(err == 0);
|
|
546
|
-
|
|
547
|
-
assert(argc == 2);
|
|
548
|
-
|
|
549
|
-
utf8_t path[4096];
|
|
550
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
551
|
-
assert(err == 0);
|
|
552
|
-
|
|
553
|
-
uint32_t mode;
|
|
554
|
-
err = js_get_value_uint32(env, argv[1], &mode);
|
|
555
|
-
assert(err == 0);
|
|
556
|
-
|
|
557
|
-
uv_fs_t req;
|
|
558
|
-
uv_fs_stat(loop, &req, (char *) path, NULL);
|
|
559
|
-
|
|
560
|
-
uv_stat_t *st = req.result < 0 ? NULL : req.ptr;
|
|
561
|
-
|
|
562
|
-
js_value_t *result;
|
|
563
|
-
err = js_get_boolean(env, st && st->st_mode & mode, &result);
|
|
564
|
-
assert(err == 0);
|
|
565
|
-
|
|
566
|
-
uv_fs_req_cleanup(&req);
|
|
567
|
-
|
|
568
|
-
return result;
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
static js_value_t *
|
|
572
|
-
bare_module_realpath(js_env_t *env, js_callback_info_t *info) {
|
|
573
|
-
int err;
|
|
574
|
-
|
|
575
|
-
uv_loop_t *loop;
|
|
576
|
-
err = js_get_env_loop(env, &loop);
|
|
577
|
-
assert(err == 0);
|
|
578
|
-
|
|
579
|
-
js_value_t *argv[1];
|
|
580
|
-
size_t argc = 1;
|
|
581
|
-
|
|
582
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
583
|
-
assert(err == 0);
|
|
584
|
-
|
|
585
|
-
assert(argc == 1);
|
|
586
|
-
|
|
587
|
-
utf8_t path[4096];
|
|
588
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
589
|
-
assert(err == 0);
|
|
590
|
-
|
|
591
|
-
uv_fs_t req;
|
|
592
|
-
uv_fs_realpath(loop, &req, (char *) path, NULL);
|
|
593
|
-
|
|
594
|
-
int res = req.result;
|
|
595
|
-
|
|
596
|
-
if (res < 0) {
|
|
597
|
-
uv_fs_req_cleanup(&req);
|
|
598
|
-
err = res;
|
|
599
|
-
goto err;
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
js_value_t *result;
|
|
603
|
-
err = js_create_string_utf8(env, (utf8_t *) req.ptr, -1, &result);
|
|
604
|
-
assert(err == 0);
|
|
605
|
-
|
|
606
|
-
uv_fs_req_cleanup(&req);
|
|
607
|
-
|
|
608
|
-
return result;
|
|
609
|
-
|
|
610
|
-
err:
|
|
611
|
-
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
|
|
612
|
-
assert(err == 0);
|
|
613
|
-
|
|
614
|
-
return NULL;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
static js_value_t *
|
|
618
|
-
bare_module_read(js_env_t *env, js_callback_info_t *info) {
|
|
619
|
-
int err;
|
|
620
|
-
|
|
621
|
-
uv_loop_t *loop;
|
|
622
|
-
err = js_get_env_loop(env, &loop);
|
|
623
|
-
assert(err == 0);
|
|
624
|
-
|
|
625
|
-
js_value_t *argv[1];
|
|
626
|
-
size_t argc = 1;
|
|
627
|
-
|
|
628
|
-
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
629
|
-
assert(err == 0);
|
|
630
|
-
|
|
631
|
-
assert(argc == 1);
|
|
632
|
-
|
|
633
|
-
utf8_t path[4096];
|
|
634
|
-
err = js_get_value_string_utf8(env, argv[0], path, 4096, NULL);
|
|
635
|
-
assert(err == 0);
|
|
636
|
-
|
|
637
|
-
uv_fs_t req;
|
|
638
|
-
uv_fs_open(loop, &req, (char *) path, UV_FS_O_RDONLY, 0, NULL);
|
|
639
|
-
|
|
640
|
-
int fd = req.result;
|
|
641
|
-
uv_fs_req_cleanup(&req);
|
|
642
|
-
|
|
643
|
-
if (fd < 0) {
|
|
644
|
-
err = fd;
|
|
645
|
-
goto err;
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
uv_fs_fstat(loop, &req, fd, NULL);
|
|
649
|
-
uv_stat_t *st = req.ptr;
|
|
650
|
-
|
|
651
|
-
size_t len = st->st_size;
|
|
652
|
-
char *base;
|
|
653
|
-
|
|
654
|
-
js_value_t *result;
|
|
655
|
-
err = js_create_arraybuffer(env, len, (void **) &base, &result);
|
|
656
|
-
assert(err == 0);
|
|
657
|
-
|
|
658
|
-
uv_buf_t buffer = uv_buf_init(base, len);
|
|
659
|
-
|
|
660
|
-
uv_fs_req_cleanup(&req);
|
|
661
|
-
|
|
662
|
-
int64_t read = 0;
|
|
663
|
-
|
|
664
|
-
while (true) {
|
|
665
|
-
uv_fs_read(loop, &req, fd, &buffer, 1, read, NULL);
|
|
666
|
-
|
|
667
|
-
int res = req.result;
|
|
668
|
-
uv_fs_req_cleanup(&req);
|
|
669
|
-
|
|
670
|
-
if (res < 0) {
|
|
671
|
-
uv_fs_close(loop, &req, fd, NULL);
|
|
672
|
-
uv_fs_req_cleanup(&req);
|
|
673
|
-
err = res;
|
|
674
|
-
goto err;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
buffer.base += res;
|
|
678
|
-
buffer.len -= res;
|
|
679
|
-
|
|
680
|
-
read += res;
|
|
681
|
-
if (res == 0 || read == len) break;
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
uv_fs_close(loop, &req, fd, NULL);
|
|
685
|
-
uv_fs_req_cleanup(&req);
|
|
686
|
-
|
|
687
|
-
return result;
|
|
688
|
-
|
|
689
|
-
err:
|
|
690
|
-
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
|
|
691
|
-
assert(err == 0);
|
|
692
|
-
|
|
693
|
-
return NULL;
|
|
694
|
-
}
|
|
695
|
-
|
|
696
1004
|
static js_value_t *
|
|
697
1005
|
bare_module_exports(js_env_t *env, js_value_t *exports) {
|
|
698
1006
|
int err;
|
|
@@ -706,7 +1014,7 @@ bare_module_exports(js_env_t *env, js_value_t *exports) {
|
|
|
706
1014
|
assert(err == 0); \
|
|
707
1015
|
}
|
|
708
1016
|
|
|
709
|
-
V("
|
|
1017
|
+
V("createContext", bare_module_create_context)
|
|
710
1018
|
|
|
711
1019
|
V("createFunction", bare_module_create_function)
|
|
712
1020
|
V("getFunctionID", bare_module_get_function_id)
|
|
@@ -715,25 +1023,7 @@ bare_module_exports(js_env_t *env, js_value_t *exports) {
|
|
|
715
1023
|
V("createSyntheticModule", bare_module_create_synthetic_module)
|
|
716
1024
|
V("setModuleExport", bare_module_set_module_export)
|
|
717
1025
|
V("runModule", bare_module_run_module)
|
|
718
|
-
V("getModuleID", bare_module_get_module_id)
|
|
719
1026
|
V("getModuleNamespace", bare_module_get_module_namespace)
|
|
720
|
-
|
|
721
|
-
V("exists", bare_module_exists)
|
|
722
|
-
V("realpath", bare_module_realpath)
|
|
723
|
-
V("read", bare_module_read)
|
|
724
|
-
#undef V
|
|
725
|
-
|
|
726
|
-
#define V(name, n) \
|
|
727
|
-
{ \
|
|
728
|
-
js_value_t *val; \
|
|
729
|
-
err = js_create_uint32(env, n, &val); \
|
|
730
|
-
assert(err == 0); \
|
|
731
|
-
err = js_set_named_property(env, exports, name, val); \
|
|
732
|
-
assert(err == 0); \
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
V("FILE", S_IFREG)
|
|
736
|
-
V("DIR", S_IFDIR)
|
|
737
1027
|
#undef V
|
|
738
1028
|
|
|
739
1029
|
return exports;
|