Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
4cdd33b3e0 Update napi.cpp 2023-11-05 01:10:19 -08:00
Jarred Sumner
c10ca0a569 Cleanup some napi code 2023-11-05 01:44:13 -07:00
2 changed files with 38 additions and 41 deletions

View File

@@ -177,17 +177,17 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(NapiRef);
static uint32_t getPropertyAttributes(napi_property_attributes attributes)
{
uint32_t result = 0;
if (!(attributes & napi_key_configurable)) {
if (!(static_cast<unsigned>(attributes) & static_cast<unsigned>(napi_key_configurable))) {
result |= JSC::PropertyAttribute::DontDelete;
}
if (!(attributes & napi_key_enumerable)) {
if (!(static_cast<unsigned>(attributes) & static_cast<unsigned>(napi_key_enumerable))) {
result |= JSC::PropertyAttribute::DontEnum;
}
if (!(attributes & napi_key_writable)) {
// result |= JSC::PropertyAttribute::ReadOnly;
}
// if (!(attributes & napi_key_writable)) {
// // result |= JSC::PropertyAttribute::ReadOnly;
// }
return result;
}
@@ -822,42 +822,36 @@ extern "C" napi_status napi_get_cb_info(
}
if (data != nullptr) {
JSC::JSValue callee = JSC::JSValue(callFrame->jsCallee());
if (!callFrame->callee().isNativeCallee()) {
JSC::JSValue callee = JSC::JSValue(callFrame->jsCallee());
if (Zig::JSFFIFunction* ffiFunction = JSC::jsDynamicCast<Zig::JSFFIFunction*>(callee)) {
*data = ffiFunction->dataPtr;
} else if (auto* proto = JSC::jsDynamicCast<NapiPrototype*>(callee)) {
NapiRef* ref = proto->napiRef;
if (ref) {
*data = ref->data;
}
} else if (auto* proto = JSC::jsDynamicCast<NapiClass*>(callee)) {
void* local = proto->dataPtr;
if (!local) {
NapiRef* ref = nullptr;
if (ref) {
*data = ref->data;
auto getData = [&](JSC::JSValue value) -> void* {
if (Zig::JSFFIFunction* ffiFunction = JSC::jsDynamicCast<Zig::JSFFIFunction*>(value)) {
return ffiFunction->dataPtr;
} else if (auto* proto = JSC::jsDynamicCast<NapiPrototype*>(value)) {
NapiRef* ref = proto->napiRef;
if (ref) {
return ref->data;
}
} else if (auto* klass = JSC::jsDynamicCast<NapiClass*>(value)) {
void* local = klass->dataPtr;
if (local) {
return local;
}
if (auto* ref = klass->napiRef) {
return ref->data;
}
} else if (auto* proto = JSC::jsDynamicCast<Bun::NapiExternal*>(thisValue)) {
return proto->value();
}
} else {
*data = local;
}
} else if (auto* proto = JSC::jsDynamicCast<NapiPrototype*>(thisValue)) {
NapiRef* ref = proto->napiRef;
if (ref) {
*data = ref->data;
}
} else if (auto* proto = JSC::jsDynamicCast<NapiClass*>(thisValue)) {
void* local = proto->dataPtr;
if (!local) {
NapiRef* ref = nullptr;
if (ref) {
*data = ref->data;
}
} else {
*data = local;
}
} else if (auto* proto = JSC::jsDynamicCast<Bun::NapiExternal*>(thisValue)) {
*data = proto->value();
return nullptr;
};
// https://github.com/nodejs/node/blob/1936160c31afc9780e4365de033789f39b7cbc0c/src/js_native_api_v8.cc#L853
// It only relies on the callee, not on the this value.
*data = getData(callee);
} else {
*data = nullptr;
}
@@ -1026,13 +1020,16 @@ extern "C" napi_status napi_reference_ref(napi_env env, napi_ref ref,
extern "C" napi_status napi_delete_reference(napi_env env, napi_ref ref)
{
if (!ref)
return napi_invalid_arg;
NapiRef* napiRef = toJS(ref);
napiRef->~NapiRef();
delete napiRef;
return napi_ok;
}
extern "C" void napi_delete_reference_internal(napi_ref ref)
{
ASSERT(ref);
NapiRef* napiRef = toJS(ref);
delete napiRef;
}

View File

@@ -61,7 +61,7 @@ public:
void call(JSC::JSGlobalObject* globalObject, void* data);
};
class NapiRef : public RefCounted<NapiRef>, public CanMakeWeakPtr<NapiRef> {
class NapiRef {
WTF_MAKE_ISO_ALLOCATED(NapiRef);
public: