aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/ModuleLoader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bun.js/bindings/ModuleLoader.cpp')
-rw-r--r--src/bun.js/bindings/ModuleLoader.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/bun.js/bindings/ModuleLoader.cpp b/src/bun.js/bindings/ModuleLoader.cpp
index 4e2de9294..252c446b1 100644
--- a/src/bun.js/bindings/ModuleLoader.cpp
+++ b/src/bun.js/bindings/ModuleLoader.cpp
@@ -516,6 +516,29 @@ JSValue fetchCommonJSModule(
RELEASE_AND_RETURN(scope, {});
}
+ // The JSONForObjectLoader tag is source code returned from Bun that needs
+ // to go through the JSON parser in JSC.
+ //
+ // We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
+ // object to be accessible as named imports.
+ //
+ // We don't use Bun's JSON parser because JSON.parse is faster and
+ // handles stack overflow better.
+ //
+ // When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
+ // parser instead to support comments and trailing commas.
+ if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
+ JSC::JSValue value = JSC::JSONParse(globalObject, Bun::toWTFString(res->result.value.source_code));
+ if (!value) {
+ JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Failed to parse JSON"_s));
+ RELEASE_AND_RETURN(scope, {});
+ }
+
+ target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, value.isCell() && value.isCallable() ? JSC::PropertyAttribute::Function | 0 : 0);
+ target->hasEvaluated = true;
+ RELEASE_AND_RETURN(scope, target);
+ }
+
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value);
globalObject->moduleLoader()->provideFetch(globalObject, specifierValue, JSC::SourceCode(provider));
RETURN_IF_EXCEPTION(scope, {});
@@ -647,6 +670,34 @@ static JSValue fetchESMSourceCode(
return reject(exception);
}
+ // The JSONForObjectLoader tag is source code returned from Bun that needs
+ // to go through the JSON parser in JSC.
+ //
+ // We don't use JSON.parse directly in JS because we want the top-level keys of the JSON
+ // object to be accessible as named imports.
+ //
+ // We don't use Bun's JSON parser because JSON.parse is faster and
+ // handles stack overflow better.
+ //
+ // When parsing tsconfig.*.json or jsconfig.*.json, we go through Bun's JSON
+ // parser instead to support comments and trailing commas.
+ if (res->result.value.tag == SyntheticModuleType::JSONForObjectLoader) {
+ JSC::JSValue value = JSC::JSONParse(globalObject, Bun::toWTFString(res->result.value.source_code));
+ if (!value) {
+ return reject(JSC::JSValue(JSC::createSyntaxError(globalObject, "Failed to parse JSON"_s)));
+ }
+
+ // JSON can become strings, null, numbers, booleans so we must handle "export default 123"
+ auto function = generateJSValueModuleSourceCode(
+ globalObject,
+ value);
+ auto source = JSC::SourceCode(
+ JSC::SyntheticSourceProvider::create(WTFMove(function),
+ JSC::SourceOrigin(), Bun::toWTFString(*specifier)));
+ JSC::ensureStillAliveHere(value);
+ return rejectOrResolve(JSSourceCode::create(globalObject->vm(), WTFMove(source)));
+ }
+
auto&& provider = Zig::SourceProvider::create(globalObject, res->result.value);
return rejectOrResolve(JSC::JSSourceCode::create(vm, JSC::SourceCode(provider)));
}
td>2023-10-16Fix formattingGravatar Ashcon Partovi 2-5/+4 2023-10-16Fix `Response.statusText` (#6151)Gravatar Chris Toshok 10-238/+269 2023-10-16fix-subprocess-argument-missing (#6407)Gravatar Nicolae-Rares Ailincai 4-2/+40 * fix-subprocess-argument-missing * fix-tests * nitpick, these should === not just be undefined --------- Co-authored-by: dave caruso <me@paperdave.net> 2023-10-16Add type parameter to `expect` (#6128)Gravatar Voldemat 1-3/+3 2023-10-16fix(node:worker_threads): ensure threadId property is exposed on ↵Gravatar Jérôme Benoit 6-15/+75 worker_threads instance (#6521) * fix: ensure threadId property is exposed on worker_threads instance Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * fix: rename lazy worker_threads module properties Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * fix: add getter for threadId Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * test: improve worker_threads UTs Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * test: fix lazy loading Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * test: fix worker_threads test Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org> * fix: return the worker threadId Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> * test: refine worker_threads expectation on threadId Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org> --------- Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com> Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org> 2023-10-16Fix use before define bug in sqliteGravatar Ashcon Partovi 2-5/+5 Fixes #6481 2023-10-16fix(jest): fix toStrictEqual on same URLs (#6528)Gravatar João Alisson 2-13/+16 Fixes #6492 2023-10-16Fix `toHaveBeenCalled` having wrong error signatureGravatar Ashcon Partovi 1-2/+2 Fixes #6527 2023-10-16Fix formattingGravatar Ashcon Partovi 1-2/+1 2023-10-16Add `reusePort` to `Bun.serve` typesGravatar Ashcon Partovi 1-0/+9 2023-10-16Fix `request.url` having incorrect portGravatar Ashcon Partovi 4-1/+92 Fixes #6443 2023-10-16Remove uWebSockets header from Bun.serve responsesGravatar Ashcon Partovi 1-6/+6 2023-10-16Rename some testsGravatar Ashcon Partovi 3-0/+0 2023-10-16Fix #6467Gravatar Ashcon Partovi 2-3/+10 2023-10-16Update InternalModuleRegistryConstants.hGravatar Dylan Conway 1-3/+3 2023-10-16Development -> Contributing (#6538)Gravatar Colin McDonnell 2-1/+1 Co-authored-by: Colin McDonnell <colin@KennyM1.local> 2023-10-14fix(net/tls) fix pg hang on end + hanging on query (#6487)Gravatar Ciro Spaciari 3-8/+36 * fix pg hang on end + hanging on query * remove dummy function * fix node-stream * add test * fix test * return error in test * fix test use once instead of on * fix OOM * generated * 💅 * 💅 2023-10-13fix installing dependencies that match workspace versions (#6494)Gravatar Dylan Conway 4-2/+64 * check if dependency matches workspace version * test * Update lockfile.zig * set resolution to workspace package id 2023-10-13fix lockfile struct padding (#6495)Gravatar Dylan Conway 3-3/+18 * integrity padding * error message for bytes at end of struct