Skip to content
Closed
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
16 changes: 16 additions & 0 deletions test/js/bun/yaml/yaml.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3484,6 +3484,22 @@ config:
expect(YAML.stringify(obj, null, 2)).toBe("num: \n 3.14\nstr: world\nbool: \n false");
});

test("propagates exceptions thrown while unwrapping boxed primitives", () => {
const boxedString = new String("x");
boxedString.toString = () => {
throw new Error("boom string");
};
expect(() => YAML.stringify(boxedString)).toThrow("boom string");
expect(() => YAML.stringify({ a: boxedString })).toThrow("boom string");

const boxedNumber = new Number(5);
boxedNumber.valueOf = () => {
throw new Error("boom number");
};
expect(() => YAML.stringify(boxedNumber)).toThrow("boom number");
expect(() => YAML.stringify([boxedNumber])).toThrow("boom number");
});

test("handles Date objects", () => {
// Date objects get converted to ISO string via toString()
const date = new Date("2024-01-15T10:30:00Z");
Expand Down