|
| 1 | +// Flags: --experimental-vm-modules |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +// This tests the result of evaluating a vm.SourceTextModule. |
| 5 | +const common = require('../common'); |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +// To make testing easier we just use the public inspect API. If the output format |
| 9 | +// changes, update this test accordingly. |
| 10 | +const { inspect } = require('util'); |
| 11 | +const vm = require('vm'); |
| 12 | + |
| 13 | +globalThis.callCount = {}; |
| 14 | +common.allowGlobals(globalThis.callCount); |
| 15 | + |
| 16 | +// Synchronous error during evaluation results in a synchronously rejected promise. |
| 17 | +{ |
| 18 | + globalThis.callCount.syncError = 0; |
| 19 | + const mod = new vm.SourceTextModule(` |
| 20 | + globalThis.callCount.syncError++; |
| 21 | + throw new Error("synchronous source text module"); |
| 22 | + export const a = 1; |
| 23 | + `); |
| 24 | + mod.linkRequests([]); |
| 25 | + mod.instantiate(); |
| 26 | + const promise = mod.evaluate(); |
| 27 | + assert.strictEqual(globalThis.callCount.syncError, 1); |
| 28 | + assert.match(inspect(promise), /rejected/); |
| 29 | + assert(mod.error, 'Expected mod.error to be set'); |
| 30 | + assert.strictEqual(mod.error.message, 'synchronous source text module'); |
| 31 | + |
| 32 | + promise.catch(common.mustCall((err) => { |
| 33 | + assert.strictEqual(err, mod.error); |
| 34 | + // Calling evaluate() again results in the same rejection synchronously. |
| 35 | + const promise2 = mod.evaluate(); |
| 36 | + assert.match(inspect(promise2), /rejected/); |
| 37 | + promise2.catch(common.mustCall((err2) => { |
| 38 | + assert.strictEqual(err, err2); |
| 39 | + // The module is only evaluated once. |
| 40 | + assert.strictEqual(globalThis.callCount.syncError, 1); |
| 41 | + })); |
| 42 | + })); |
| 43 | +} |
| 44 | + |
| 45 | +// Successful evaluation of a module without top-level await results in a |
| 46 | +// promise synchronously resolved to undefined. |
| 47 | +{ |
| 48 | + globalThis.callCount.syncNamedExports = 0; |
| 49 | + const mod = new vm.SourceTextModule(` |
| 50 | + globalThis.callCount.syncNamedExports++; |
| 51 | + export const a = 1, b = 2; |
| 52 | + `); |
| 53 | + mod.linkRequests([]); |
| 54 | + mod.instantiate(); |
| 55 | + const promise = mod.evaluate(); |
| 56 | + assert.match(inspect(promise), /Promise { undefined }/); |
| 57 | + assert.strictEqual(mod.namespace.a, 1); |
| 58 | + assert.strictEqual(mod.namespace.b, 2); |
| 59 | + assert.strictEqual(globalThis.callCount.syncNamedExports, 1); |
| 60 | + promise.then(common.mustCall((value) => { |
| 61 | + assert.strictEqual(value, undefined); |
| 62 | + |
| 63 | + // Calling evaluate() again results in the same resolved promise synchronously. |
| 64 | + const promise2 = mod.evaluate(); |
| 65 | + assert.match(inspect(promise2), /Promise { undefined }/); |
| 66 | + assert.strictEqual(mod.namespace.a, 1); |
| 67 | + assert.strictEqual(mod.namespace.b, 2); |
| 68 | + promise2.then(common.mustCall((value) => { |
| 69 | + assert.strictEqual(value, undefined); |
| 70 | + // The module is only evaluated once. |
| 71 | + assert.strictEqual(globalThis.callCount.syncNamedExports, 1); |
| 72 | + })); |
| 73 | + })); |
| 74 | +} |
| 75 | + |
| 76 | +{ |
| 77 | + globalThis.callCount.syncDefaultExports = 0; |
| 78 | + // Modules with either named and default exports have the same behaviors. |
| 79 | + const mod = new vm.SourceTextModule(` |
| 80 | + globalThis.callCount.syncDefaultExports++; |
| 81 | + export default 42; |
| 82 | + `); |
| 83 | + mod.linkRequests([]); |
| 84 | + mod.instantiate(); |
| 85 | + const promise = mod.evaluate(); |
| 86 | + assert.match(inspect(promise), /Promise { undefined }/); |
| 87 | + assert.strictEqual(mod.namespace.default, 42); |
| 88 | + assert.strictEqual(globalThis.callCount.syncDefaultExports, 1); |
| 89 | + |
| 90 | + promise.then(common.mustCall((value) => { |
| 91 | + assert.strictEqual(value, undefined); |
| 92 | + |
| 93 | + // Calling evaluate() again results in the same resolved promise synchronously. |
| 94 | + const promise2 = mod.evaluate(); |
| 95 | + assert.match(inspect(promise2), /Promise { undefined }/); |
| 96 | + assert.strictEqual(mod.namespace.default, 42); |
| 97 | + promise2.then(common.mustCall((value) => { |
| 98 | + assert.strictEqual(value, undefined); |
| 99 | + // The module is only evaluated once. |
| 100 | + assert.strictEqual(globalThis.callCount.syncDefaultExports, 1); |
| 101 | + })); |
| 102 | + })); |
| 103 | +} |
| 104 | + |
| 105 | +// Successful evaluation of a module with top-level await results in a promise |
| 106 | +// that is fulfilled asynchronously with undefined. |
| 107 | +{ |
| 108 | + globalThis.callCount.asyncEvaluation = 0; |
| 109 | + const mod = new vm.SourceTextModule(` |
| 110 | + globalThis.callCount.asyncEvaluation++; |
| 111 | + await Promise.resolve(); |
| 112 | + export const a = 1; |
| 113 | + `); |
| 114 | + mod.linkRequests([]); |
| 115 | + mod.instantiate(); |
| 116 | + const promise = mod.evaluate(); |
| 117 | + assert.match(inspect(promise), /<pending>/); |
| 118 | + // Accessing the namespace before the promise is fulfilled throws ReferenceError. |
| 119 | + assert.throws(() => mod.namespace.a, { name: 'ReferenceError' }); |
| 120 | + assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); |
| 121 | + promise.then(common.mustCall((value) => { |
| 122 | + assert.strictEqual(value, undefined); |
| 123 | + assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); |
| 124 | + |
| 125 | + // Calling evaluate() again results in a promise synchronously resolved to undefined. |
| 126 | + const promise2 = mod.evaluate(); |
| 127 | + assert.match(inspect(promise2), /Promise { undefined }/); |
| 128 | + assert.strictEqual(mod.namespace.a, 1); |
| 129 | + promise2.then(common.mustCall((value) => { |
| 130 | + assert.strictEqual(value, undefined); |
| 131 | + // The module is only evaluated once. |
| 132 | + assert.strictEqual(globalThis.callCount.asyncEvaluation, 1); |
| 133 | + })); |
| 134 | + })); |
| 135 | +} |
| 136 | + |
| 137 | +// Rejection of a top-level await promise results in a promise that is |
| 138 | +// rejected asynchronously with the same reason. |
| 139 | +{ |
| 140 | + globalThis.callCount.asyncRejection = 0; |
| 141 | + const mod = new vm.SourceTextModule(` |
| 142 | + globalThis.callCount.asyncRejection++; |
| 143 | + await Promise.reject(new Error("asynchronous source text module")); |
| 144 | + export const a = 1; |
| 145 | + `); |
| 146 | + mod.linkRequests([]); |
| 147 | + mod.instantiate(); |
| 148 | + const promise = mod.evaluate(); |
| 149 | + assert.match(inspect(promise), /<pending>/); |
| 150 | + // Accessing the namespace before the promise is fulfilled throws ReferenceError. |
| 151 | + assert.throws(() => mod.namespace.a, { name: 'ReferenceError' }); |
| 152 | + promise.catch(common.mustCall((err) => { |
| 153 | + assert.strictEqual(err, mod.error); |
| 154 | + assert.strictEqual(err.message, 'asynchronous source text module'); |
| 155 | + assert.strictEqual(globalThis.callCount.asyncRejection, 1); |
| 156 | + |
| 157 | + // Calling evaluate() again results in a promise synchronously rejected |
| 158 | + // with the same reason. |
| 159 | + const promise2 = mod.evaluate(); |
| 160 | + assert.match(inspect(promise2), /rejected/); |
| 161 | + promise2.catch(common.mustCall((err2) => { |
| 162 | + assert.strictEqual(err, err2); |
| 163 | + // The module is only evaluated once. |
| 164 | + assert.strictEqual(globalThis.callCount.asyncRejection, 1); |
| 165 | + })); |
| 166 | + })); |
| 167 | +} |
0 commit comments