bare-module 3.1.12 → 3.1.14

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
@@ -442,13 +442,13 @@ static js_value_t *
442
442
  bare_module_run_module (js_env_t *env, js_callback_info_t *info) {
443
443
  int err;
444
444
 
445
- size_t argc = 2;
446
- js_value_t *argv[2];
445
+ size_t argc = 3;
446
+ js_value_t *argv[3];
447
447
 
448
448
  err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
449
449
  assert(err == 0);
450
450
 
451
- assert(argc == 2);
451
+ assert(argc == 3);
452
452
 
453
453
  js_module_t *module;
454
454
  err = js_get_value_external(env, argv[0], (void **) &module);
@@ -461,30 +461,41 @@ bare_module_run_module (js_env_t *env, js_callback_info_t *info) {
461
461
  err = js_instantiate_module(env, module, on_static_import, (void *) context);
462
462
  if (err < 0) return NULL;
463
463
 
464
- js_value_t *result;
465
- err = js_run_module(env, module, &result);
464
+ js_value_t *promise;
465
+ err = js_run_module(env, module, &promise);
466
466
  if (err < 0) return NULL;
467
467
 
468
468
  bool is_promise;
469
- err = js_is_promise(env, result, &is_promise);
469
+ err = js_is_promise(env, promise, &is_promise);
470
470
  assert(err == 0);
471
471
 
472
472
  if (is_promise) {
473
473
  js_promise_state_t state;
474
- err = js_get_promise_state(env, result, &state);
474
+ err = js_get_promise_state(env, promise, &state);
475
475
  assert(err == 0);
476
476
 
477
477
  if (state == js_promise_rejected) {
478
- js_value_t *error;
479
- err = js_get_promise_result(env, result, &error);
478
+ js_value_t *reason;
479
+ err = js_get_promise_result(env, promise, &reason);
480
480
  if (err < 0) return NULL;
481
481
 
482
- err = js_set_named_property(env, result, "error", error);
483
- if (err < 0) return NULL;
482
+ js_value_t *exception;
483
+ err = js_get_and_clear_last_exception(env, &exception);
484
+ assert(err == 0);
485
+
486
+ js_value_t *ctx;
487
+ err = js_get_reference_value(env, context->ctx, &ctx);
488
+ assert(err == 0);
489
+
490
+ js_value_t *args[3] = {reason, promise, exception};
491
+
492
+ js_call_function(env, ctx, argv[2], 3, args, NULL);
493
+
494
+ return NULL;
484
495
  }
485
496
  }
486
497
 
487
- return result;
498
+ return promise;
488
499
  }
489
500
 
490
501
  static js_value_t *
package/index.js CHANGED
@@ -179,13 +179,15 @@ const Module = module.exports = exports = class Module {
179
179
  this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
180
180
  }
181
181
 
182
+ _run () {
183
+ binding.runModule(this._handle, Module._handle, Module._onrejection)
184
+ }
185
+
182
186
  _evaluate (eagerRun = false) {
183
187
  if ((this._state & constants.states.EVALUATED) !== 0) return
184
188
 
185
189
  this._state |= constants.states.EVALUATED
186
190
 
187
- let result
188
-
189
191
  if (this._type === constants.types.SCRIPT) {
190
192
  const require = createRequire(this._url, { module: this })
191
193
 
@@ -201,23 +203,17 @@ const Module = module.exports = exports = class Module {
201
203
  urlToDirname(this._url)
202
204
  )
203
205
 
204
- if (eagerRun) result = binding.runModule(this._handle, Module._handle)
206
+ if (eagerRun) this._run()
205
207
  }
206
208
 
207
209
  if (this._type === constants.types.MODULE) {
208
- result = binding.runModule(this._handle, Module._handle)
210
+ this._run()
209
211
 
210
212
  this._exports = binding.getNamespace(this._handle)
211
213
  }
212
214
 
213
215
  if (this._type === constants.types.ADDON) {
214
- if (eagerRun) result = binding.runModule(this._handle, Module._handle)
215
- }
216
-
217
- if (result && result.error) {
218
- result.catch(() => {}) // Handle the promise rejection
219
-
220
- throw result.error
216
+ if (eagerRun) this._run()
221
217
  }
222
218
  }
223
219
 
@@ -347,6 +343,12 @@ const Module = module.exports = exports = class Module {
347
343
  }
348
344
  }
349
345
 
346
+ static _onrejection (reason, promise, err = reason) {
347
+ promise.catch(() => {}) // Don't leak the rejection
348
+
349
+ throw err
350
+ }
351
+
350
352
  static Protocol = Protocol
351
353
  static Bundle = Bundle
352
354
  static constants = constants
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "3.1.12",
3
+ "version": "3.1.14",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [