Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/jsc/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2337,13 +2337,19 @@ extern "C" JSC::EncodedJSValue JSC__JSValue__unwrapBoxedPrimitive(JSGlobalObject
return JSValue::encode(value);
}

auto scope = DECLARE_THROW_SCOPE(globalObject->vm());
JSObject* object = asObject(value);

if (object->inherits<NumberObject>()) {
return JSValue::encode(jsNumber(object->toNumber(globalObject)));
double number = object->toNumber(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(jsNumber(number));
}
if (object->inherits<StringObject>()) {
JSString* string = object->toString(globalObject);
RETURN_IF_EXCEPTION(scope, {});
return JSValue::encode(string);
}
if (object->inherits<StringObject>())
return JSValue::encode(object->toString(globalObject));
if (object->inherits<BooleanObject>() || object->inherits<BigIntObject>())
return JSValue::encode(uncheckedDowncast<JSWrapperObject>(object)->internalValue());

Expand Down
31 changes: 31 additions & 0 deletions test/js/bun/yaml/yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4273,6 +4273,37 @@ refs:
expect(YAML.stringify(obj, null, 2)).toBe("normal: value");
});

// Unwrapping a String/Number wrapper re-enters JS via Symbol.toPrimitive,
// which can throw; debug builds abort if that exception is dropped, so
// this must run in a subprocess.
test("boxed primitive whose Symbol.toPrimitive throws propagates the error", async () => {
await using proc = Bun.spawn({
cmd: [
bunExe(),
"-e",
`const s = new String();
s[Symbol.toPrimitive] = () => String;
try { Bun.YAML.stringify(s); } catch (e) { console.log("string:", e.message); }
const n = new Number(1);
n[Symbol.toPrimitive] = () => Number;
try { Bun.YAML.stringify({ a: n }); } catch (e) { console.log("number:", e.message); }
try { Bun.YAML.stringify({ a: 1 }, null, s); } catch (e) { console.log("space:", e.message); }`,
],
env: bunEnv,
stderr: "pipe",
});

const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

expect(stdout).toBe(
"string: Symbol.toPrimitive returned an object\n" +
"number: Symbol.toPrimitive returned an object\n" +
"space: Symbol.toPrimitive returned an object\n",
);
expect(stderr).toBe("");
expect(exitCode).toBe(0);
});

test("handles Intl objects", () => {
const dateFormat = new Intl.DateTimeFormat("en-US");
const numberFormat = new Intl.NumberFormat("en-US");
Expand Down