Skip to content
3 changes: 3 additions & 0 deletions src/js_native_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_coerce_to_string(napi_env env,
napi_value* result);

// Methods to work with Objects
NAPI_EXTERN napi_status NAPI_CDECL napi_set_prototype(napi_env env,
Comment thread
siaeyy marked this conversation as resolved.
Outdated
Comment thread
siaeyy marked this conversation as resolved.
Outdated
Comment thread
siaeyy marked this conversation as resolved.
Outdated
Comment thread
siaeyy marked this conversation as resolved.
Outdated
napi_value object,
napi_value value);
Comment thread
siaeyy marked this conversation as resolved.
Outdated
NAPI_EXTERN napi_status NAPI_CDECL napi_get_prototype(napi_env env,
napi_value object,
napi_value* result);
Expand Down
20 changes: 20 additions & 0 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,26 @@ napi_status NAPI_CDECL napi_strict_equals(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status NAPI_CDECL napi_set_prototype(napi_env env,
Comment thread
siaeyy marked this conversation as resolved.
Outdated
napi_value object,
napi_value value) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, value);

v8::Local<v8::Context> context = env->context();
v8::Local<v8::Object> obj;

CHECK_TO_OBJECT(env, context, obj, object);

v8::Local<v8::Value> val = v8impl::V8LocalValueFromJsValue(value);

v8::Maybe<bool> set_maybe = obj->SetPrototypeV2(context, val);

RETURN_STATUS_IF_FALSE_WITH_PREAMBLE(
env, set_maybe.FromMaybe(false), napi_generic_failure);
return GET_RETURN_STATUS(env);
}

napi_status NAPI_CDECL napi_get_prototype(napi_env env,
napi_value object,
napi_value* result) {
Expand Down
6 changes: 6 additions & 0 deletions test/js-native-api/test_general/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ class ExtendedClass extends BaseClass {

const baseObject = new BaseClass();
const extendedObject = new ExtendedClass();
const nullProtoObject = { __proto__: null };
Comment thread
siaeyy marked this conversation as resolved.

// Test napi_strict_equals
assert.ok(test_general.testStrictEquals(val1, val1));
assert.strictEqual(test_general.testStrictEquals(val1, val2), false);
assert.ok(test_general.testStrictEquals(val2, val3));

// Test napi_set_prototype
test_general.testSetPrototype(nullProtoObject, Object.prototype);
assert.strictEqual(Object.getPrototypeOf(nullProtoObject),
Object.prototype);

// Test napi_get_prototype
assert.strictEqual(test_general.testGetPrototype(baseObject),
Object.getPrototypeOf(baseObject));
Expand Down
11 changes: 11 additions & 0 deletions test/js-native-api/test_general/test_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ static napi_value testStrictEquals(napi_env env, napi_callback_info info) {
return result;
}

static napi_value testSetPrototype(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));

NODE_API_CALL(env, napi_set_prototype(env, args[0], args[1]));
Comment thread
siaeyy marked this conversation as resolved.
Outdated

return NULL;
}

static napi_value testGetPrototype(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1];
Expand Down Expand Up @@ -287,6 +297,7 @@ EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("testStrictEquals", testStrictEquals),
DECLARE_NODE_API_PROPERTY("testSetPrototype", testSetPrototype),
DECLARE_NODE_API_PROPERTY("testGetPrototype", testGetPrototype),
DECLARE_NODE_API_PROPERTY("testGetVersion", testGetVersion),
DECLARE_NODE_API_PROPERTY("testNapiRun", testNapiRun),
Expand Down