Skip to content

Commit

Permalink
fix 10610 (#10618)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway authored Apr 28, 2024
1 parent c7aed7e commit 697f37e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/bun.js/bindings/BunString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ extern "C" void JSC__JSValue__putBunString(
JSC::JSObject* target = JSC::JSValue::decode(encodedTarget).getObject();
JSC::JSValue value = JSC::JSValue::decode(encodedValue);
auto& vm = global->vm();
WTF::String str = key->toWTFString();
WTF::String str = key->tag == BunStringTag::Empty ? WTF::String(""_s) : key->toWTFString();
Identifier id = Identifier::fromString(vm, str);
target->putDirect(vm, id, value, 0);
}
7 changes: 4 additions & 3 deletions src/bun.js/bindings/JSPropertyIterator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub fn JSPropertyIterator(comptime options: JSPropertyIteratorOptions) type {
pub fn next(this: *@This()) ?bun.String {
const i: usize = this.iter_i;
if (i >= this.len) {
this.i = this.iter_i;
return null;
}

Expand All @@ -58,7 +59,7 @@ pub fn JSPropertyIterator(comptime options: JSPropertyIteratorOptions) type {
if (comptime options.include_value) {
const current = Bun__JSPropertyIterator__getNameAndValue(this.impl, this.globalObject, this.object, &name, i);
if (current.isEmpty()) {
return null;
return this.next();
}
current.ensureStillAlive();
this.value = current;
Expand All @@ -67,12 +68,12 @@ pub fn JSPropertyIterator(comptime options: JSPropertyIteratorOptions) type {
}

if (name.tag == .Dead) {
return null;
return this.next();
}

if (comptime options.skip_empty_name) {
if (name.isEmpty()) {
return null;
return this.next();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/bindings/JSWrappingFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ JS_EXPORT_PRIVATE JSWrappingFunction* JSWrappingFunction::create(
JSC::JSFunction* wrappedFn = jsCast<JSC::JSFunction*>(wrappedFnValue.asCell());
ASSERT(wrappedFn != nullptr);

auto nameStr = symbolName->toWTFString();
auto nameStr = symbolName->tag == BunStringTag::Empty ? WTF::String(""_s) : symbolName->toWTFString();
auto name = Identifier::fromString(vm, nameStr);
NativeExecutable* executable = vm.getHostFunction(functionPointer, ImplementationVisibility::Public, nullptr, nameStr);

Expand Down
8 changes: 5 additions & 3 deletions src/bun.js/bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3262,7 +3262,7 @@ extern "C" void JSC__JSValue__putMayBeIndex(JSC__JSValue target, JSC__JSGlobalOb
JSC::VM& vm = globalObject->vm();
ThrowScope scope = DECLARE_THROW_SCOPE(vm);

WTF::String keyStr = key->toWTFString();
WTF::String keyStr = key->tag == BunStringTag::Empty ? WTF::String(""_s) : key->toWTFString();
JSC::Identifier identifier = JSC::Identifier::fromString(vm, keyStr);

JSC::JSObject* object = JSC::JSValue::decode(target).asCell()->getObject();
Expand Down Expand Up @@ -3591,7 +3591,8 @@ extern "C" JSC__JSValue JSC__JSValue__getIfPropertyExistsImplString(JSC__JSValue

JSC::VM& vm = globalObject->vm();
JSC::JSObject* object = value.getObject();
auto identifier = JSC::Identifier::fromString(vm, propertyName->toWTFString(BunString::ZeroCopy));
WTF::String propertyNameString = propertyName->tag == BunStringTag::Empty ? WTF::String(""_s) : propertyName->toWTFString(BunString::ZeroCopy);
auto identifier = JSC::Identifier::fromString(vm, propertyNameString);
auto property = JSC::PropertyName(identifier);

return JSC::JSValue::encode(object->getIfPropertyExists(globalObject, property));
Expand All @@ -3601,7 +3602,8 @@ extern "C" JSC__JSValue JSC__JSValue__getOwn(JSC__JSValue JSValue0, JSC__JSGloba
{
VM& vm = globalObject->vm();
JSValue value = JSC::JSValue::decode(JSValue0);
auto identifier = JSC::Identifier::fromString(vm, propertyName->toWTFString(BunString::ZeroCopy));
WTF::String propertyNameString = propertyName->tag == BunStringTag::Empty ? WTF::String(""_s) : propertyName->toWTFString(BunString::ZeroCopy);
auto identifier = JSC::Identifier::fromString(vm, propertyNameString);
auto property = JSC::PropertyName(identifier);
PropertySlot slot(value, PropertySlot::InternalMethodType::GetOwnProperty);
if (value.getOwnPropertySlot(globalObject, property, slot)) {
Expand Down
2 changes: 1 addition & 1 deletion src/bun.js/test/expect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4042,7 +4042,7 @@ pub const Expect = struct {
const matchers_to_register = args[0];
{
var iter = JSC.JSPropertyIterator(.{
.skip_empty_name = true,
.skip_empty_name = false,
.include_value = true,
}).init(globalObject, matchers_to_register);
defer iter.deinit();
Expand Down
7 changes: 7 additions & 0 deletions test/js/bun/test/expect-extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ expect.extend({

return { message, pass: 42 };
},
[""](actual, expected) {
return { pass: actual === expected };
},
_toBeDivisibleBy(actual, expected) {
const pass = typeof actual === "number" && actual % expected === 0;
const message = pass
Expand Down Expand Up @@ -118,6 +121,10 @@ it("is ok if there is no message specified", () => {
expect(() => expect(true)._toFailWithoutMessage())._toThrowErrorMatchingSnapshot();
});

it("works with empty matcher name", () => {
expect(1)[""](1);
});

it("exposes an equality function to custom matchers", () => {
// expect and expect share the same global state
//expect.assertions(3);
Expand Down
1 change: 1 addition & 0 deletions test/js/bun/test/expect-extend.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface CustomMatchersForTest {
_toAllowOverridingExistingMatcher(): any;
_toCustomA(): any;
_toCustomB(): any;
[""](value: any): any;

_toThrowErrorMatchingSnapshot(): any; // TODO: remove when implemented
_toHaveMessageThatThrows(a: any): any;
Expand Down
10 changes: 10 additions & 0 deletions test/transpiler/macro-test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ test("ascii string", () => {
expect(identity("abc")).toBe("abc");
});

test("type coercion", () => {
expect(identity({ a: 1 })).toEqual({ a: 1 });
expect(identity([1, 2, 3])).toEqual([1, 2, 3]);
expect(identity(undefined)).toBe(undefined);
expect(identity(null)).toBe(null);
expect(identity(1.5)).toBe(1.5);
expect(identity(1)).toBe(1);
expect(identity(true)).toBe(true);
});

test("escaping", () => {
expect(identity("\\")).toBe("\\");
expect(identity("\f")).toBe("\f");
Expand Down

0 comments on commit 697f37e

Please sign in to comment.