From 51f506d084452c274f962baad0ab54b2d66e2e42 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Fri, 15 Sep 2023 00:48:17 -0700 Subject: [PATCH 1/2] Fixes #4622 --- src/bun.js/bindings/napi.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index 72ca50dcb98669..ca26ff8d06b402 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -317,6 +317,10 @@ static void defineNapiProperty(Zig::GlobalObject* globalObject, JSC::JSObject* t extern "C" napi_status napi_set_property(napi_env env, napi_value target, napi_value key, napi_value value) { + if (UNLIKELY(!env || !target || !key)) { + return napi_invalid_arg; + } + auto globalObject = toJS(env); auto& vm = globalObject->vm(); auto* object = toJS(target).getObject(); @@ -327,7 +331,8 @@ extern "C" napi_status napi_set_property(napi_env env, napi_value target, auto keyProp = toJS(key); auto scope = DECLARE_CATCH_SCOPE(vm); - object->putDirect(globalObject->vm(), keyProp.toPropertyKey(globalObject), toJS(value)); + PutPropertySlot slot(object, true); + object->put(object, globalObject, keyProp.toPropertyKey(globalObject), toJS(value), slot); RETURN_IF_EXCEPTION(scope, napi_generic_failure); scope.clearException(); @@ -336,6 +341,10 @@ extern "C" napi_status napi_set_property(napi_env env, napi_value target, extern "C" napi_status napi_has_property(napi_env env, napi_value object, napi_value key, bool* result) { + if (UNLIKELY(!object || !env)) { + return napi_invalid_arg; + } + auto globalObject = toJS(env); auto& vm = globalObject->vm(); auto* target = toJS(object).getObject(); @@ -345,8 +354,7 @@ extern "C" napi_status napi_has_property(napi_env env, napi_value object, auto keyProp = toJS(key); auto scope = DECLARE_CATCH_SCOPE(vm); - // TODO: use the slot directly? - *result = !!target->getIfPropertyExists(globalObject, keyProp.toPropertyKey(globalObject)); + *result = target->hasProperty(globalObject, keyProp.toPropertyKey(globalObject)); RETURN_IF_EXCEPTION(scope, napi_generic_failure); scope.clearException(); @@ -1557,15 +1565,15 @@ extern "C" napi_status napi_define_class(napi_env env, extern "C" napi_status napi_coerce_to_string(napi_env env, napi_value value, napi_value* result) { - if (UNLIKELY(result == nullptr)) { + if (UNLIKELY(result == nullptr || value == nullptr || env == nullptr)) { return napi_invalid_arg; } Zig::GlobalObject* globalObject = toJS(env); JSC::VM& vm = globalObject->vm(); - auto scope = DECLARE_CATCH_SCOPE(vm); - JSC::JSValue jsValue = JSC::JSValue::decode(reinterpret_cast(value)); + auto scope = DECLARE_THROW_SCOPE(vm); + JSC::JSValue jsValue = toJS(value); JSC::EnsureStillAliveScope ensureStillAlive(jsValue); // .toString() can throw @@ -1717,7 +1725,7 @@ extern "C" napi_status napi_get_element(napi_env env, napi_value objectValue, uint32_t index, napi_value* result) { JSValue jsValue = toJS(objectValue); - if (!jsValue || !jsValue.isObject()) { + if (UNLIKELY(!env || !jsValue || !jsValue.isObject())) { return napi_invalid_arg; } @@ -1737,7 +1745,7 @@ extern "C" napi_status napi_get_element(napi_env env, napi_value objectValue, extern "C" napi_status napi_create_object(napi_env env, napi_value* result) { - if (UNLIKELY(result == nullptr)) { + if (UNLIKELY(result == nullptr || env == nullptr)) { return napi_invalid_arg; } From 4bc95b04f98ef535938c254a3f3b6f625403968e Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Fri, 15 Sep 2023 03:26:19 -0700 Subject: [PATCH 2/2] cleanup --- src/bun.js/bindings/napi.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/bun.js/bindings/napi.cpp b/src/bun.js/bindings/napi.cpp index ca26ff8d06b402..e500fae38eb387 100644 --- a/src/bun.js/bindings/napi.cpp +++ b/src/bun.js/bindings/napi.cpp @@ -1036,7 +1036,7 @@ extern "C" napi_status napi_fatal_exception(napi_env env, napi_value err) { auto globalObject = toJS(env); - JSC::JSValue value = JSC::JSValue::decode(reinterpret_cast(err)); + JSC::JSValue value = toJS(err); JSC::JSObject* obj = value.getObject(); if (UNLIKELY(obj == nullptr || !obj->isErrorInstance())) { return napi_invalid_arg; @@ -1053,7 +1053,7 @@ extern "C" napi_status napi_throw(napi_env env, napi_value error) JSC::VM& vm = globalObject->vm(); auto throwScope = DECLARE_THROW_SCOPE(vm); - JSC::JSValue value = JSC::JSValue::decode(reinterpret_cast(error)); + JSC::JSValue value = toJS(error); if (value) { JSC::throwException(globalObject, throwScope, value); } else { @@ -1139,8 +1139,8 @@ extern "C" napi_status napi_create_type_error(napi_env env, napi_value code, Zig::GlobalObject* globalObject = toJS(env); JSC::VM& vm = globalObject->vm(); - JSC::JSValue codeValue = JSC::JSValue::decode(reinterpret_cast(code)); - JSC::JSValue messageValue = JSC::JSValue::decode(reinterpret_cast(msg)); + JSC::JSValue codeValue = toJS(code); + JSC::JSValue messageValue = toJS(msg); auto error = JSC::createTypeError(globalObject, messageValue.toWTFString(globalObject)); if (codeValue) { @@ -1158,10 +1158,11 @@ extern "C" napi_status napi_create_error(napi_env env, napi_value code, Zig::GlobalObject* globalObject = toJS(env); JSC::VM& vm = globalObject->vm(); - JSC::JSValue codeValue = JSC::JSValue::decode(reinterpret_cast(code)); - JSC::JSValue messageValue = JSC::JSValue::decode(reinterpret_cast(msg)); + JSC::JSValue codeValue = toJS(code); + JSC::JSValue messageValue = toJS(msg); - auto error = JSC::createError(globalObject, messageValue.toWTFString(globalObject)); + WTF::String message = messageValue.toWTFString(globalObject); + auto* error = JSC::createError(globalObject, message); if (codeValue) { error->putDirect(vm, WebCore::builtinNames(vm).codePublicName(), codeValue, 0); } @@ -1596,7 +1597,7 @@ extern "C" napi_status napi_get_property_names(napi_env env, napi_value object, Zig::GlobalObject* globalObject = toJS(env); JSC::VM& vm = globalObject->vm(); - JSC::JSValue jsValue = JSC::JSValue::decode(reinterpret_cast(object)); + JSC::JSValue jsValue = toJS(object); if (!jsValue || !jsValue.isObject()) { return napi_invalid_arg; }