bare-module 1.10.0 → 1.10.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/binding.c CHANGED
@@ -12,6 +12,7 @@ typedef struct {
12
12
  js_ref_t *ctx;
13
13
  js_ref_t *on_import;
14
14
  js_ref_t *on_evaluate;
15
+ js_ref_t *on_meta;
15
16
  } bare_module_context_t;
16
17
 
17
18
  static js_module_t *
@@ -109,17 +110,47 @@ on_evaluate (js_env_t *env, js_module_t *module, void *data) {
109
110
  if (err < 0) return;
110
111
  }
111
112
 
113
+ static void
114
+ on_meta (js_env_t *env, js_module_t *module, js_value_t *meta, void *data) {
115
+ bare_module_context_t *context = (bare_module_context_t *) data;
116
+
117
+ int err;
118
+
119
+ js_value_t *ctx;
120
+ err = js_get_reference_value(env, context->ctx, &ctx);
121
+ assert(err == 0);
122
+
123
+ js_value_t *on_meta;
124
+ err = js_get_reference_value(env, context->on_meta, &on_meta);
125
+ assert(err == 0);
126
+
127
+ const char *name;
128
+ err = js_get_module_name(env, module, &name);
129
+ assert(err == 0);
130
+
131
+ js_value_t *args[2];
132
+
133
+ err = js_create_string_utf8(env, (utf8_t *) name, -1, &args[0]);
134
+ if (err < 0) return;
135
+
136
+ args[1] = meta;
137
+
138
+ js_value_t *result;
139
+ err = js_call_function(env, ctx, on_meta, 2, args, &result);
140
+ if (err < 0) return;
141
+ }
142
+
112
143
  static js_value_t *
113
144
  bare_module_init (js_env_t *env, js_callback_info_t *info) {
114
145
  int err;
115
146
 
116
- size_t argc = 3;
117
- js_value_t *argv[3];
147
+ size_t argc = 4;
148
+ js_value_t *argv[4];
118
149
 
119
150
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
120
151
  assert(err == 0);
121
152
 
122
- assert(argc == 3);
153
+ assert(argc == 4);
123
154
 
124
155
  bare_module_context_t *context;
125
156
 
@@ -136,6 +167,9 @@ bare_module_init (js_env_t *env, js_callback_info_t *info) {
136
167
  err = js_create_reference(env, argv[2], 1, &context->on_evaluate);
137
168
  assert(err == 0);
138
169
 
170
+ err = js_create_reference(env, argv[3], 1, &context->on_meta);
171
+ assert(err == 0);
172
+
139
173
  err = js_on_dynamic_import(env, on_dynamic_import, (void *) context);
140
174
  assert(err == 0);
141
175
 
@@ -222,13 +256,13 @@ static js_value_t *
222
256
  bare_module_create_module (js_env_t *env, js_callback_info_t *info) {
223
257
  int err;
224
258
 
225
- size_t argc = 3;
226
- js_value_t *argv[3];
259
+ size_t argc = 4;
260
+ js_value_t *argv[4];
227
261
 
228
262
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
229
263
  assert(err == 0);
230
264
 
231
- assert(argc == 3);
265
+ assert(argc == 4);
232
266
 
233
267
  size_t file_len;
234
268
  utf8_t file[1024];
@@ -241,8 +275,12 @@ bare_module_create_module (js_env_t *env, js_callback_info_t *info) {
241
275
  err = js_get_value_int32(env, argv[2], &offset);
242
276
  if (err < 0) return NULL;
243
277
 
278
+ bare_module_context_t *context;
279
+ err = js_get_arraybuffer_info(env, argv[3], (void **) &context, NULL);
280
+ if (err < 0) return NULL;
281
+
244
282
  js_module_t *module;
245
- err = js_create_module(env, (char *) file, file_len, offset, source, NULL, NULL, &module);
283
+ err = js_create_module(env, (char *) file, file_len, offset, source, on_meta, (void *) context, &module);
246
284
  if (err < 0) return NULL;
247
285
 
248
286
  js_value_t *result;
package/index.js CHANGED
@@ -22,7 +22,7 @@ const Module = module.exports = class Module {
22
22
  return path.dirname(this.filename)
23
23
  }
24
24
 
25
- static _context = binding.init(this, this._onimport, this._onevaluate)
25
+ static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
26
26
 
27
27
  static _extensions = Object.create(null)
28
28
  static _protocols = Object.create(null)
@@ -70,6 +70,21 @@ const Module = module.exports = class Module {
70
70
  }
71
71
  }
72
72
 
73
+ static _onmeta (specifier, meta) {
74
+ const module = this._cache[specifier]
75
+
76
+ const resolve = (specifier) => {
77
+ return this.resolve(specifier, module.dirname, {
78
+ protocol: this._protocolFor(specifier, module._protocol),
79
+ imports: module._imports,
80
+ referrer: module
81
+ })
82
+ }
83
+
84
+ meta.url = module.filename
85
+ meta.resolve = resolve
86
+ }
87
+
73
88
  static Protocol = Protocol
74
89
  static Bundle = Bundle
75
90
 
@@ -433,7 +448,7 @@ Module._extensions['.mjs'] = function (module, source, referrer, protocol, impor
433
448
  module._protocol = protocol
434
449
  module._imports = imports
435
450
 
436
- module._handle = binding.createModule(module.filename, source, 0)
451
+ module._handle = binding.createModule(module.filename, source, 0, this._context)
437
452
  }
438
453
 
439
454
  Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
Binary file