Skip to content

Commit

Permalink
Merge pull request nodejs#150 from boingoing/remove_napi_propertyname
Browse files Browse the repository at this point in the history
napi: Remove napi_propertyname
  • Loading branch information
boingoing authored Mar 16, 2017
2 parents dc3ea48 + 4fe8681 commit 2791cf9
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 270 deletions.
353 changes: 202 additions & 151 deletions src/node_jsvmapi.cc

Large diffs are not rendered by default.

99 changes: 58 additions & 41 deletions src/node_jsvmapi.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/******************************************************************************
/******************************************************************************
* Experimental prototype for demonstrating VM agnostic and ABI stable API
* for native modules to use instead of using Nan and V8 APIs directly.
*
Expand Down Expand Up @@ -120,102 +120,111 @@ NODE_EXTERN napi_status napi_create_range_error(napi_env e,

// Methods to get the the native napi_value from Primitive type
NODE_EXTERN napi_status napi_get_type_of_value(napi_env e,
napi_value vv,
napi_value value,
napi_valuetype* result);
NODE_EXTERN napi_status napi_get_value_double(napi_env e,
napi_value v,
napi_value value,
double* result);
NODE_EXTERN napi_status napi_get_value_int32(napi_env e,
napi_value v,
napi_value value,
int32_t* result);
NODE_EXTERN napi_status napi_get_value_uint32(napi_env e,
napi_value v,
napi_value value,
uint32_t* result);
NODE_EXTERN napi_status napi_get_value_int64(napi_env e,
napi_value v,
napi_value value,
int64_t* result);
NODE_EXTERN napi_status napi_get_value_bool(napi_env e,
napi_value v,
napi_value value,
bool* result);

// Gets the number of CHARACTERS in the string.
NODE_EXTERN napi_status napi_get_value_string_length(napi_env e,
napi_value v,
napi_value value,
int* result);

// Gets the number of BYTES in the UTF-8 encoded representation of the string.
NODE_EXTERN napi_status napi_get_value_string_utf8_length(napi_env e,
napi_value v,
napi_value value,
int* result);

// Copies UTF-8 encoded bytes from a string into a buffer.
NODE_EXTERN napi_status napi_get_value_string_utf8(napi_env e,
napi_value v,
napi_value value,
char* buf,
int bufsize,
int* result);

// Gets the number of 2-byte code units in the UTF-16 encoded
// representation of the string.
NODE_EXTERN napi_status napi_get_value_string_utf16_length(napi_env e,
napi_value v,
napi_value value,
int* result);

// Copies UTF-16 encoded bytes from a string into a buffer.
NODE_EXTERN napi_status napi_get_value_string_utf16(napi_env e,
napi_value v,
napi_value value,
char16_t* buf,
int bufsize,
int* result);

// Methods to coerce values
// These APIs may execute user script
NODE_EXTERN napi_status napi_coerce_to_bool(napi_env e,
napi_value v,
napi_value value,
napi_value* result);
NODE_EXTERN napi_status napi_coerce_to_number(napi_env e,
napi_value v,
napi_value value,
napi_value* result);
NODE_EXTERN napi_status napi_coerce_to_object(napi_env e,
napi_value v,
napi_value value,
napi_value* result);
NODE_EXTERN napi_status napi_coerce_to_string(napi_env e,
napi_value v,
napi_value value,
napi_value* result);

// Methods to work with Objects
NODE_EXTERN napi_status napi_get_prototype(napi_env e,
napi_value object,
napi_value* result);
NODE_EXTERN napi_status napi_property_name(napi_env e,
const char* utf8name,
napi_propertyname* result);
NODE_EXTERN napi_status napi_get_propertynames(napi_env e,
napi_value object,
napi_value* result);
NODE_EXTERN napi_status napi_set_property(napi_env e,
napi_value object,
napi_propertyname name,
napi_value v);
napi_value key,
napi_value value);
NODE_EXTERN napi_status napi_has_property(napi_env e,
napi_value object,
napi_propertyname name,
napi_value key,
bool* result);
NODE_EXTERN napi_status napi_get_property(napi_env e,
napi_value object,
napi_propertyname name,
napi_value key,
napi_value* result);
NODE_EXTERN napi_status napi_set_named_property(napi_env e,
napi_value object,
const char* utf8name,
napi_value value);
NODE_EXTERN napi_status napi_has_named_property(napi_env e,
napi_value object,
const char* utf8name,
bool* result);
NODE_EXTERN napi_status napi_get_named_property(napi_env e,
napi_value object,
const char* utf8name,
napi_value* result);
NODE_EXTERN napi_status napi_set_element(napi_env e,
napi_value object,
uint32_t i,
napi_value v);
uint32_t index,
napi_value value);
NODE_EXTERN napi_status napi_has_element(napi_env e,
napi_value object,
uint32_t i,
uint32_t index,
bool* result);
NODE_EXTERN napi_status napi_get_element(napi_env e,
napi_value object,
uint32_t i,
uint32_t index,
napi_value* result);
NODE_EXTERN napi_status
napi_define_properties(napi_env e,
Expand All @@ -224,9 +233,11 @@ napi_define_properties(napi_env e,
const napi_property_descriptor* properties);

// Methods to work with Arrays
NODE_EXTERN napi_status napi_is_array(napi_env e, napi_value v, bool* result);
NODE_EXTERN napi_status napi_is_array(napi_env e,
napi_value value,
bool* result);
NODE_EXTERN napi_status napi_get_array_length(napi_env e,
napi_value v,
napi_value value,
uint32_t* result);

// Methods to compare values
Expand All @@ -243,13 +254,13 @@ NODE_EXTERN napi_status napi_call_function(napi_env e,
const napi_value* argv,
napi_value* result);
NODE_EXTERN napi_status napi_new_instance(napi_env e,
napi_value cons,
napi_value constructor,
int argc,
const napi_value* argv,
napi_value* result);
NODE_EXTERN napi_status napi_instanceof(napi_env e,
napi_value obj,
napi_value cons,
napi_value object,
napi_value constructor,
bool* result);

// Napi version of node::MakeCallback(...)
Expand Down Expand Up @@ -295,7 +306,7 @@ NODE_EXTERN napi_status napi_is_construct_call(napi_env e,
bool* result);
NODE_EXTERN napi_status napi_set_return_value(napi_env e,
napi_callback_info cbinfo,
napi_value v);
napi_value value);
NODE_EXTERN napi_status
napi_define_class(napi_env e,
const char* utf8name,
Expand All @@ -319,7 +330,7 @@ NODE_EXTERN napi_status napi_create_external(napi_env e,
napi_finalize finalize_cb,
napi_value* result);
NODE_EXTERN napi_status napi_get_value_external(napi_env e,
napi_value v,
napi_value value,
void** result);

// Methods to control object lifespan
Expand Down Expand Up @@ -361,23 +372,27 @@ NODE_EXTERN napi_status napi_get_reference_value(napi_env e,
NODE_EXTERN napi_status napi_open_handle_scope(napi_env e,
napi_handle_scope* result);
NODE_EXTERN napi_status napi_close_handle_scope(napi_env e,
napi_handle_scope s);
napi_handle_scope scope);
NODE_EXTERN napi_status
napi_open_escapable_handle_scope(napi_env e,
napi_escapable_handle_scope* result);
NODE_EXTERN napi_status
napi_close_escapable_handle_scope(napi_env e, napi_escapable_handle_scope s);
napi_close_escapable_handle_scope(napi_env e,
napi_escapable_handle_scope scope);

NODE_EXTERN napi_status napi_escape_handle(napi_env e,
napi_escapable_handle_scope s,
napi_value v,
napi_escapable_handle_scope scope,
napi_value escapee,
napi_value* result);

// Methods to support error handling
NODE_EXTERN napi_status napi_throw(napi_env e, napi_value error);
NODE_EXTERN napi_status napi_throw_error(napi_env e, const char* msg);
NODE_EXTERN napi_status napi_throw_type_error(napi_env e, const char* msg);
NODE_EXTERN napi_status napi_throw_range_error(napi_env e, const char* msg);
NODE_EXTERN napi_status napi_is_error(napi_env e, napi_value v, bool* result);
NODE_EXTERN napi_status napi_is_error(napi_env e,
napi_value value,
bool* result);

// Methods to support catching exceptions
NODE_EXTERN napi_status napi_is_exception_pending(napi_env e, bool* result);
Expand All @@ -398,9 +413,11 @@ NODE_EXTERN napi_status napi_create_buffer_copy(napi_env e,
const void* data,
size_t size,
napi_value* result);
NODE_EXTERN napi_status napi_is_buffer(napi_env e, napi_value v, bool* result);
NODE_EXTERN napi_status napi_is_buffer(napi_env e,
napi_value value,
bool* result);
NODE_EXTERN napi_status napi_get_buffer_info(napi_env e,
napi_value v,
napi_value value,
void** data,
size_t* length);

Expand Down
3 changes: 1 addition & 2 deletions src/node_jsvmapi_types.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#ifndef SRC_NODE_JSVMAPI_TYPES_H_
#ifndef SRC_NODE_JSVMAPI_TYPES_H_
#define SRC_NODE_JSVMAPI_TYPES_H_

#include <stddef.h>
Expand All @@ -11,7 +11,6 @@ typedef struct napi_value__ *napi_value;
typedef struct napi_ref__ *napi_ref;
typedef struct napi_handle_scope__ *napi_handle_scope;
typedef struct napi_escapable_handle_scope__ *napi_escapable_handle_scope;
typedef struct napi_propertyname__ *napi_propertyname;
typedef struct napi_callback_info__ *napi_callback_info;

typedef void (*napi_callback)(napi_env, napi_callback_info);
Expand Down
6 changes: 1 addition & 5 deletions test/addons-abi/4_object_factory/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ void CreateObject(napi_env env, const napi_callback_info info) {
status = napi_create_object(env, &obj);
if (status != napi_ok) return;

napi_propertyname msgprop;
status = napi_property_name(env, "msg", &msgprop);
if (status != napi_ok) return;

status = napi_set_property(env, obj, msgprop, args[0]);
status = napi_set_named_property(env, obj, "msg", args[0]);
if (status != napi_ok) return;

status = napi_set_return_value(env, info, obj);
Expand Down
6 changes: 1 addition & 5 deletions test/addons-abi/6_object_wrap/myobject.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ void MyObject::Init(napi_env env, napi_value exports) {
status = napi_create_reference(env, cons, 1, &constructor);
if (status != napi_ok) return;

napi_propertyname name;
status = napi_property_name(env, "MyObject", &name);
if (status != napi_ok) return;

status = napi_set_property(env, exports, name, cons);
status = napi_set_named_property(env, exports, "MyObject", cons);
if (status != napi_ok) return;
}

Expand Down
8 changes: 3 additions & 5 deletions test/addons-abi/test_buffer/test_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,11 @@ void staticBuffer(napi_env env, napi_callback_info info) {
}

void Init(napi_env env, napi_value exports, napi_value module) {
napi_propertyname propName;
napi_value theValue;

NAPI_CALL(env, napi_property_name(env, "theText", &propName));
NAPI_CALL(env,
napi_create_string_utf8(env, theText, sizeof(theText), &theValue));
NAPI_CALL(env, napi_set_property(env, exports, propName, theValue));
NAPI_CALL(env, napi_create_string_utf8(env,
theText, sizeof(theText), &theValue));
NAPI_CALL(env, napi_set_named_property(env, exports, "theText", theValue));

napi_property_descriptor methods[] = {
{"newBuffer", newBuffer},
Expand Down
6 changes: 1 addition & 5 deletions test/addons-abi/test_constructor/test_constructor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ void Init(napi_env env, napi_value exports, napi_value module) {
nullptr, sizeof(properties)/sizeof(*properties), properties, &cons);
if (status != napi_ok) return;

napi_propertyname name;
status = napi_property_name(env, "exports", &name);
if (status != napi_ok) return;

status = napi_set_property(env, module, name, cons);
status = napi_set_named_property(env, module, "exports", cons);
if (status != napi_ok) return;

status = napi_create_reference(env, cons, 1, &constructor_);
Expand Down
6 changes: 1 addition & 5 deletions test/addons-abi/test_function/test_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ void Test(napi_env env, napi_callback_info info) {
void Init(napi_env env, napi_value exports, napi_value module) {
napi_status status;

napi_propertyname name;
status = napi_property_name(env, "Test", &name);
if (status != napi_ok) return;

napi_value fn;
status = napi_create_function(env, nullptr, Test, nullptr, &fn);
if (status != napi_ok) return;

status = napi_set_property(env, exports, name, fn);
status = napi_set_named_property(env, exports, "Test", fn);
if (status != napi_ok) return;
}

Expand Down
20 changes: 20 additions & 0 deletions test/addons-abi/test_object/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,23 @@ assert.deepStrictEqual(test_object.Inflate(cube), {x: 12, y: 12, z: 12});
assert.deepStrictEqual(test_object.Inflate(cube), {x: 13, y: 13, z: 13});
cube.t = 13;
assert.deepStrictEqual(test_object.Inflate(cube), {x: 14, y: 14, z: 14, t: 14});

const sym1 = Symbol('1');
const sym2 = Symbol('2');
const sym3 = Symbol('3');
const sym4 = Symbol('4');
const object2 = {
[sym1]: '@@iterator',
[sym2]: sym3
};

assert(test_object.Has(object2, sym1));
assert(test_object.Has(object2, sym2));
assert.strictEqual(test_object.Get(object2, sym1), '@@iterator');
assert.strictEqual(test_object.Get(object2, sym2), sym3);
assert(test_object.Set(object2, 'string', 'value'));
assert(test_object.Set(object2, sym4, 123));
assert(test_object.Has(object2, 'string'));
assert(test_object.Has(object2, sym4));
assert.strictEqual(test_object.Get(object2, 'string'), 'value');
assert.strictEqual(test_object.Get(object2, sym4), 123);
Loading

0 comments on commit 2791cf9

Please sign in to comment.