diff options
author | 2023-05-30 02:53:28 -0700 | |
---|---|---|
committer | 2023-05-30 02:53:28 -0700 | |
commit | 756e15f6e283fd7e21fe91e060d63440ce5c72de (patch) | |
tree | b3bcdc08eac3518ae3788ebbfe8f1625730b7c99 /src/bun.js/bindings/CommonJSModuleRecord.cpp | |
parent | 10be26f011bca5c715d9403ea56626f30171b342 (diff) | |
download | bun-756e15f6e283fd7e21fe91e060d63440ce5c72de.tar.gz bun-756e15f6e283fd7e21fe91e060d63440ce5c72de.tar.zst bun-756e15f6e283fd7e21fe91e060d63440ce5c72de.zip |
Add another comment
Diffstat (limited to 'src/bun.js/bindings/CommonJSModuleRecord.cpp')
-rw-r--r-- | src/bun.js/bindings/CommonJSModuleRecord.cpp | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/bun.js/bindings/CommonJSModuleRecord.cpp b/src/bun.js/bindings/CommonJSModuleRecord.cpp index d7fe6161a..e3bf3a0db 100644 --- a/src/bun.js/bindings/CommonJSModuleRecord.cpp +++ b/src/bun.js/bindings/CommonJSModuleRecord.cpp @@ -363,14 +363,27 @@ JSC::SourceCode createCommonJSModule( auto catchScope = DECLARE_CATCH_SCOPE(vm); - JSC::JSWithScope* withScope = JSC::JSWithScope::create(vm, globalObject, globalObject->globalScope(), scopeExtensionObject); - - vm.interpreter.executeEval(executable, globalObject, withScope); - - if (UNLIKELY(catchScope.exception())) { - auto returnedException = catchScope.exception(); - catchScope.clearException(); - JSC::throwException(globalObject, throwScope, returnedException); + // Where the magic happens. + // + // A `with` scope is created containing { module, exports, require }. + // We eval() the CommonJS module code + // with that scope. + // + // Doing it that way saves us a roundtrip through C++ <> JS. + // + // Sidenote: another implementation could use + // FunctionExecutable. It looks like there are lots of arguments + // to pass to that and it isn't used directly much, so that + // seems harder to do correctly. + { + JSWithScope* withScope = JSWithScope::create(vm, globalObject, globalObject->globalScope(), scopeExtensionObject); + vm.interpreter.executeEval(executable, globalObject, withScope); + + if (UNLIKELY(catchScope.exception())) { + auto returnedException = catchScope.exception(); + catchScope.clearException(); + JSC::throwException(globalObject, throwScope, returnedException); + } } if (throwScope.exception()) { @@ -439,7 +452,10 @@ JSC::SourceCode createCommonJSModule( return true; exportNames.append(Identifier::fromUid(vm, key)); - exportValues.append(exports->getDirect(entry.offset())); + + JSValue value = exports->getDirect(entry.offset()); + + exportValues.append(value); return true; }); } else { |