bare-module 1.9.6 → 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, &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
 
@@ -95,9 +110,9 @@ const Module = module.exports = class Module {
95
110
  const bundle = this._bundleFor(path.dirname(specifier), protocol, source)
96
111
 
97
112
  if (bundle) {
98
- imports = { ...imports, ...bundle.imports }
99
-
100
113
  protocol = new Protocol({
114
+ imports: bundle.imports,
115
+
101
116
  exists (filename) {
102
117
  return bundle.exists(filename)
103
118
  },
@@ -152,9 +167,9 @@ const Module = module.exports = class Module {
152
167
  const bundle = this._bundleFor(path.dirname(specifier), protocol)
153
168
 
154
169
  if (bundle) {
155
- imports = { ...imports, ...bundle.imports }
156
-
157
170
  protocol = new Protocol({
171
+ imports: bundle.imports,
172
+
158
173
  exists (filename) {
159
174
  return bundle.exists(filename)
160
175
  },
@@ -180,6 +195,7 @@ const Module = module.exports = class Module {
180
195
 
181
196
  static * _resolve (specifier, dirname, protocol, imports) {
182
197
  if (specifier in imports) specifier = imports[specifier]
198
+ else if (specifier in protocol.imports) specifier = protocol.imports[specifier]
183
199
 
184
200
  protocol = this._protocolFor(specifier, protocol)
185
201
 
@@ -296,6 +312,22 @@ const Module = module.exports = class Module {
296
312
 
297
313
  if (bundle) return bundle
298
314
 
315
+ const parent = this._bundleFor(path.dirname(name), protocol)
316
+
317
+ if (parent) {
318
+ protocol = new Protocol({
319
+ imports: parent.imports,
320
+
321
+ exists (filename) {
322
+ return parent.exists(filename)
323
+ },
324
+
325
+ read (filename) {
326
+ return parent.read(filename)
327
+ }
328
+ })
329
+ }
330
+
299
331
  if (source === null || name !== specifier) source = protocol.read(name)
300
332
 
301
333
  bundle = this._bundles[name] = Bundle.from(source).mount(name)
@@ -416,7 +448,7 @@ Module._extensions['.mjs'] = function (module, source, referrer, protocol, impor
416
448
  module._protocol = protocol
417
449
  module._imports = imports
418
450
 
419
- module._handle = binding.createModule(module.filename, source, 0)
451
+ module._handle = binding.createModule(module.filename, source, 0, this._context)
420
452
  }
421
453
 
422
454
  Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
package/lib/protocol.js CHANGED
@@ -1,12 +1,15 @@
1
1
  module.exports = class Protocol {
2
2
  constructor (opts = {}) {
3
3
  const {
4
+ imports = Object.create(null),
4
5
  map = null,
5
6
  resolve = null,
6
7
  exists = null,
7
8
  read = null
8
9
  } = opts
9
10
 
11
+ this.imports = imports
12
+
10
13
  if (map) this.map = map.bind(this)
11
14
  if (resolve) this.resolve = resolve.bind(this)
12
15
  else this.resolve = null
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.9.6",
3
+ "version": "1.10.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -12,9 +12,7 @@
12
12
  "prebuilds"
13
13
  ],
14
14
  "scripts": {
15
- "test": "standard && bare test.js",
16
- "install": "bare-dev rebuild",
17
- "prebuild": "bare-dev prebuild"
15
+ "test": "standard && bare test.js"
18
16
  },
19
17
  "repository": {
20
18
  "type": "git",
Binary file