mirror of
https://github.com/oven-sh/bun
synced 2026-02-16 05:42:43 +00:00
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
This commit is contained in:
@@ -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,
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
1
src/bun.js/bindings/headers.h
generated
1
src/bun.js/bindings/headers.h
generated
@@ -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);
|
||||
|
||||
@@ -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, );
|
||||
|
||||
Reference in New Issue
Block a user