From 0c15c1e61303e3d80152cecfaf3c0712c381de77 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sun, 18 Dec 2022 03:10:26 +0000 Subject: [PATCH] implement test262 json module loading --- scripts/test262.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/scripts/test262.js b/scripts/test262.js index f846568497d..30977ca7a51 100644 --- a/scripts/test262.js +++ b/scripts/test262.js @@ -552,7 +552,14 @@ async function runCodeInHarness(yaml, code, importDir) { let module = moduleCache.get(modulePath) if (!module) { const code = fs.readFileSync(modulePath, 'utf8') - module = new vm.SourceTextModule(code + unique(), { context, importModuleDynamically }) + if (modulePath.endsWith('json')) { + const evaluate = function () { + this.setExport('default', vm.runInContext('JSON.parse', context)(code)) + } + module = new vm.SyntheticModule(['default'], evaluate, { context }) + } else { + module = new vm.SourceTextModule(code + unique(), { context, importModuleDynamically }) + } moduleCache.set(modulePath, module) } return module @@ -567,9 +574,13 @@ async function runCodeInHarness(yaml, code, importDir) { let promise = dynamicImportCache.get(where) if (!promise) { const module = findModule(where, context) - promise = module.link(linker) - .then(() => module.evaluate()) - .then(() => module) + if (module.status === 'unlinked') { + promise = module.link(linker) + .then(() => module.evaluate()) + .then(() => module) + } else { + promise = Promise.resolve(module) + } dynamicImportCache.set(where, promise) } return promise