Avoid creating temporary strings when throwing errors (#17179)

Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
This commit is contained in:
Jarred Sumner
2025-02-20 12:09:27 -08:00
committed by GitHub
parent 6e45e3bf1e
commit 1b271fd45e
19 changed files with 726 additions and 303 deletions

View File

@@ -669,11 +669,9 @@ JSValue fetchCommonJSModule(
// 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, res->result.value.source_code.toWTFString(BunString::ZeroCopy));
if (!value) {
JSC::throwException(globalObject, scope, JSC::createSyntaxError(globalObject, "Failed to parse JSON"_s));
RELEASE_AND_RETURN(scope, {});
}
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::ZeroCopy);
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
RETURN_IF_EXCEPTION(scope, {});
target->putDirect(vm, WebCore::clientData(vm)->builtinNames().exportsPublicName(), value, 0);
target->hasEvaluated = true;
@@ -861,10 +859,9 @@ static JSValue fetchESMSourceCode(
// 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, res->result.value.source_code.toWTFString(BunString::ZeroCopy));
if (!value) {
return reject(JSC::JSValue(JSC::createSyntaxError(globalObject, "Failed to parse JSON"_s)));
}
WTF::String jsonSource = res->result.value.source_code.toWTFString(BunString::ZeroCopy);
JSC::JSValue value = JSC::JSONParseWithException(globalObject, jsonSource);
RETURN_IF_EXCEPTION(scope, {});
// JSON can become strings, null, numbers, booleans so we must handle "export default 123"
auto function = generateJSValueModuleSourceCode(