From 7c4c360431e0ef78d3f2fa8a5e2a5ce6d6e48975 Mon Sep 17 00:00:00 2001 From: pfg Date: Fri, 1 Aug 2025 19:26:55 -0700 Subject: [PATCH] Make getIfPropertyValueExistsImpl accept a slice (#21554) Previously it accepted `property: anytype` but now it's `[]const u8` because that was the only allowed value, so it makes it easier to see what type it accepts in autocomplete. Also updates the doc comment, switches it to use ZIG_EXPORT, and updates the cppbind doc comment --- src/bun.js/bindings/JSValue.zig | 15 +++++---------- src/bun.js/bindings/bindings.cpp | 4 ++-- src/bun.js/bindings/headers.h | 1 - src/codegen/cppbind.ts | 6 +++--- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/bun.js/bindings/JSValue.zig b/src/bun.js/bindings/JSValue.zig index 03c9a634c3..a018612c0c 100644 --- a/src/bun.js/bindings/JSValue.zig +++ b/src/bun.js/bindings/JSValue.zig @@ -1408,18 +1408,18 @@ pub const JSValue = enum(i64) { /// Equivalent to `target[property]`. Calls userland getters/proxies. Can /// throw. Null indicates the property does not exist. JavaScript undefined /// and JavaScript null can exist as a property and is different than zig - /// `null` (property does not exist). + /// `null` (property does not exist), however javascript undefined will return + /// zig null. /// - /// `property` must be either `[]const u8`. A comptime slice may defer to + /// `property` must be `[]const u8`. A comptime slice may defer to /// calling `fastGet`, which use a more optimal code path. This function is /// marked `inline` to allow Zig to determine if `fastGet` should be used /// per invocation. /// /// Cannot handle property names that are numeric indexes. (For this use `getPropertyValue` instead.) /// - pub inline fn get(target: JSValue, global: *JSGlobalObject, property: anytype) JSError!?JSValue { + pub inline fn get(target: JSValue, global: *JSGlobalObject, property_slice: []const u8) JSError!?JSValue { bun.debugAssert(target.isObject()); - const property_slice: []const u8 = property; // must be a slice! // This call requires `get` to be `inline` if (bun.isComptimeKnown(property_slice)) { @@ -1428,12 +1428,7 @@ pub const JSValue = enum(i64) { } } - return switch (try fromJSHostCall(global, @src(), JSC__JSValue__getIfPropertyExistsImpl, .{ - target, - global, - property_slice.ptr, - @intCast(property_slice.len), - })) { + return switch (try bun.cpp.JSC__JSValue__getIfPropertyExistsImpl(target, global, property_slice.ptr, property_slice.len)) { .zero => unreachable, // handled by fromJSHostCall .property_does_not_exist_on_object => null, diff --git a/src/bun.js/bindings/bindings.cpp b/src/bun.js/bindings/bindings.cpp index 1a588e39c0..47561da6ca 100644 --- a/src/bun.js/bindings/bindings.cpp +++ b/src/bun.js/bindings/bindings.cpp @@ -3971,9 +3971,9 @@ JSC::EncodedJSValue JSC__JSValue__createObject2(JSC::JSGlobalObject* globalObjec // Returns empty for exception, returns deleted if not found. // Be careful when handling the return value. // Cannot handle numeric index property names! If it is possible that this will be a integer index, use JSC__JSValue__getPropertyValue instead -JSC::EncodedJSValue JSC__JSValue__getIfPropertyExistsImpl(JSC::EncodedJSValue JSValue0, +[[ZIG_EXPORT(zero_is_throw)]] JSC::EncodedJSValue JSC__JSValue__getIfPropertyExistsImpl(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* globalObject, - const unsigned char* arg1, uint32_t arg2) + const unsigned char* arg1, size_t arg2) { ASSERT_NO_PENDING_EXCEPTION(globalObject); JSValue value = JSC::JSValue::decode(JSValue0); diff --git a/src/bun.js/bindings/headers.h b/src/bun.js/bindings/headers.h index db641387cc..d9f3418338 100644 --- a/src/bun.js/bindings/headers.h +++ b/src/bun.js/bindings/headers.h @@ -227,7 +227,6 @@ CPP_DECL JSC::EncodedJSValue JSC__JSValue__bigIntSum(JSC::JSGlobalObject* arg0, CPP_DECL void JSC__JSValue__getClassName(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC::EncodedJSValue JSC__JSValue__getErrorsProperty(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1); CPP_DECL JSC::EncodedJSValue JSC__JSValue__getIfPropertyExistsFromPath(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1, JSC::EncodedJSValue JSValue2); -CPP_DECL JSC::EncodedJSValue JSC__JSValue__getIfPropertyExistsImpl(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1, const unsigned char* arg2, uint32_t arg3); CPP_DECL double JSC__JSValue__getLengthIfPropertyExistsInternal(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1); CPP_DECL void JSC__JSValue__getNameProperty(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1, ZigString* arg2); CPP_DECL JSC::EncodedJSValue JSC__JSValue__getPrototype(JSC::EncodedJSValue JSValue0, JSC::JSGlobalObject* arg1); diff --git a/src/codegen/cppbind.ts b/src/codegen/cppbind.ts index 2ed5ba1fc5..46f17a46f7 100644 --- a/src/codegen/cppbind.ts +++ b/src/codegen/cppbind.ts @@ -14,7 +14,7 @@ To run manually: 1. **nothrow** - Function that never throws exceptions: ```cpp - [[ZIG_EXPORT(nothrow)]] void hello_world() { + extern "C" [[ZIG_EXPORT(nothrow)]] void hello_world() { printf("hello world\n"); } ``` @@ -22,7 +22,7 @@ To run manually: 2. **zero_is_throw** - Function returns JSValue, where .zero indicates an exception: ```cpp - [[ZIG_EXPORT(zero_is_throw)]] JSValue create_object(JSGlobalObject* globalThis) { + extern "C" [[ZIG_EXPORT(zero_is_throw)]] JSValue create_object(JSGlobalObject* globalThis) { auto scope = DECLARE_THROW_SCOPE(); // ... RETURN_IF_EXCEPTION(scope, {}); @@ -33,7 +33,7 @@ To run manually: 3. **check_slow** - Function that may throw, performs runtime exception checking: ```cpp - [[ZIG_EXPORT(check_slow)]] void process_data(JSGlobalObject* globalThis) { + extern "C" [[ZIG_EXPORT(check_slow)]] void process_data(JSGlobalObject* globalThis) { auto scope = DECLARE_THROW_SCOPE(); // ... RETURN_IF_EXCEPTION(scope, );