Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
Took updated node_api.cc from node
  • Loading branch information
kfarnung committed Jul 5, 2017
1 parent fb85578 commit 010ed9d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdarg>
#include <vector>
#include "uv.h"
#include "node_api.h"
Expand Down Expand Up @@ -1008,6 +1007,28 @@ napi_status napi_get_property(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status napi_delete_property(napi_env env,
napi_value object,
napi_value key,
bool* result) {
NAPI_PREAMBLE(env);
CHECK_ARG(env, key);

v8::Isolate* isolate = env->isolate;
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Value> k = v8impl::V8LocalValueFromJsValue(key);
v8::Local<v8::Object> obj;

CHECK_TO_OBJECT(env, context, obj, object);
v8::Maybe<bool> delete_maybe = obj->Delete(context, k);
CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure);

if (result != NULL)
*result = delete_maybe.FromMaybe(false);

return GET_RETURN_STATUS(env);
}

napi_status napi_set_named_property(napi_env env,
napi_value object,
const char* utf8name,
Expand Down Expand Up @@ -1145,6 +1166,26 @@ napi_status napi_get_element(napi_env env,
return GET_RETURN_STATUS(env);
}

napi_status napi_delete_element(napi_env env,
napi_value object,
uint32_t index,
bool* result) {
NAPI_PREAMBLE(env);

v8::Isolate* isolate = env->isolate;
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Object> obj;

CHECK_TO_OBJECT(env, context, obj, object);
v8::Maybe<bool> delete_maybe = obj->Delete(context, index);
CHECK_MAYBE_NOTHING(env, delete_maybe, napi_generic_failure);

if (result != NULL)
*result = delete_maybe.FromMaybe(false);

return GET_RETURN_STATUS(env);
}

napi_status napi_define_properties(napi_env env,
napi_value object,
size_t property_count,
Expand Down Expand Up @@ -1973,16 +2014,16 @@ napi_status napi_wrap(napi_env env,

// Create a wrapper object with an internal field to hold the wrapped pointer.
v8::Local<v8::ObjectTemplate> wrapperTemplate =
v8::ObjectTemplate::New(isolate);
v8::ObjectTemplate::New(isolate);
wrapperTemplate->SetInternalFieldCount(1);
v8::Local<v8::Object> wrapper =
wrapperTemplate->NewInstance(context).ToLocalChecked();
wrapperTemplate->NewInstance(context).ToLocalChecked();
wrapper->SetInternalField(0, v8::External::New(isolate, native_object));

// Insert the wrapper into the object's prototype chain.
v8::Local<v8::Value> proto = obj->GetPrototype();
wrapper->SetPrototype(proto);
obj->SetPrototype(wrapper);
CHECK(wrapper->SetPrototype(context, proto).FromJust());
CHECK(obj->SetPrototype(context, wrapper).FromJust());

if (result != nullptr) {
// The returned reference should be deleted via napi_delete_reference()
Expand Down Expand Up @@ -2287,7 +2328,7 @@ napi_status napi_instanceof(napi_env env,
}

if (env->has_instance_available) {
napi_value value, js_result, has_instance = nullptr;
napi_value value, js_result = nullptr, has_instance = nullptr;
napi_status status = napi_generic_failure;
napi_valuetype value_type;

Expand Down
8 changes: 8 additions & 0 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ NAPI_EXTERN napi_status napi_get_property(napi_env env,
napi_value object,
napi_value key,
napi_value* result);
NAPI_EXTERN napi_status napi_delete_property(napi_env env,
napi_value object,
napi_value key,
bool* result);
NAPI_EXTERN napi_status napi_set_named_property(napi_env env,
napi_value object,
const char* utf8name,
Expand All @@ -259,6 +263,10 @@ NAPI_EXTERN napi_status napi_get_element(napi_env env,
napi_value object,
uint32_t index,
napi_value* result);
NAPI_EXTERN napi_status napi_delete_element(napi_env env,
napi_value object,
uint32_t index,
bool* result);
NAPI_EXTERN napi_status
napi_define_properties(napi_env env,
napi_value object,
Expand Down

0 comments on commit 010ed9d

Please sign in to comment.