Skip to content

Commit

Permalink
Does not fix oven-sh#4622 (oven-sh#5452)
Browse files Browse the repository at this point in the history
* Fixes oven-sh#4622

* cleanup

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
  • Loading branch information
2 people authored and paperdave committed Sep 18, 2023
1 parent 8768c06 commit 1bea5f9
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions src/bun.js/bindings/napi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -1028,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<JSC::EncodedJSValue>(err));
JSC::JSValue value = toJS(err);
JSC::JSObject* obj = value.getObject();
if (UNLIKELY(obj == nullptr || !obj->isErrorInstance())) {
return napi_invalid_arg;
Expand All @@ -1045,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<JSC::EncodedJSValue>(error));
JSC::JSValue value = toJS(error);
if (value) {
JSC::throwException(globalObject, throwScope, value);
} else {
Expand Down Expand Up @@ -1131,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<JSC::EncodedJSValue>(code));
JSC::JSValue messageValue = JSC::JSValue::decode(reinterpret_cast<JSC::EncodedJSValue>(msg));
JSC::JSValue codeValue = toJS(code);
JSC::JSValue messageValue = toJS(msg);

auto error = JSC::createTypeError(globalObject, messageValue.toWTFString(globalObject));
if (codeValue) {
Expand All @@ -1150,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<JSC::EncodedJSValue>(code));
JSC::JSValue messageValue = JSC::JSValue::decode(reinterpret_cast<JSC::EncodedJSValue>(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);
}
Expand Down Expand Up @@ -1557,15 +1566,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<JSC::EncodedJSValue>(value));
auto scope = DECLARE_THROW_SCOPE(vm);
JSC::JSValue jsValue = toJS(value);
JSC::EnsureStillAliveScope ensureStillAlive(jsValue);

// .toString() can throw
Expand All @@ -1588,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<JSC::EncodedJSValue>(object));
JSC::JSValue jsValue = toJS(object);
if (!jsValue || !jsValue.isObject()) {
return napi_invalid_arg;
}
Expand Down Expand Up @@ -1717,7 +1726,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;
}

Expand All @@ -1737,7 +1746,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;
}

Expand Down

0 comments on commit 1bea5f9

Please sign in to comment.