Skip to content

Commit

Permalink
Rename napi_throw_error to napi_throw
Browse files Browse the repository at this point in the history
napi_throw_error is misleading since you can legitimately throw any
napi_value (any javascript value).  So renamed to napi_throw to clarify.
This also leaves room for a napi_throw_error which would be a helper
that creates a new error from a string and throws it for you, to
collapse the otherwise three calls that would be needed into one.

Also renamed a bunch of parameters named napi_env to just env for
clarity.
  • Loading branch information
Ian Halliday committed Jul 14, 2016
1 parent 77e2a6a commit 0d92cf5
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 143 deletions.
2 changes: 1 addition & 1 deletion src/node_jsvmapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ napi_value napi_get_global_scope(napi_env e) {
return v8impl::JsValueFromV8LocalValue(context->Global());
}

void napi_throw_error(napi_env e, napi_value error) {
void napi_throw(napi_env e, napi_value error) {
v8::Isolate *isolate = v8impl::V8IsolateFromJsEnv(e);

isolate->ThrowException(
Expand Down
2 changes: 1 addition & 1 deletion src/node_jsvmapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ NODE_EXTERN napi_value napi_escape_handle(napi_env e, napi_escapable_handle_scop


// Methods to support error handling
NODE_EXTERN void napi_throw_error(napi_env e, napi_value error);
NODE_EXTERN void napi_throw(napi_env e, napi_value error);

// TODO (ianhall): APIs for handling try catch semantics need serious design analysis, this is just a quick hack
// One thing to note is that v8::TryCatch relies on the current stack pointer position, so it cannot be used
Expand Down
14 changes: 7 additions & 7 deletions test/addons-abi/1_hello_world/binding.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include <node_jsvmapi.h>

void Method(napi_env napi_env, napi_func_cb_info info) {
void Method(napi_env env, napi_func_cb_info info) {
napi_set_return_value(
napi_env,
env,
info,
napi_create_string(napi_env, "world"));
napi_create_string(env, "world"));
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, exports,
napi_property_name(napi_env, "hello"),
napi_create_function(napi_env, Method));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, exports,
napi_property_name(env, "hello"),
napi_create_function(env, Method));
}

NODE_MODULE_ABI(addon, Init)
Expand Down
42 changes: 21 additions & 21 deletions test/addons-abi/2_function_arguments/binding.cc
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
#include <node_jsvmapi.h>

void Add(napi_env napi_env, napi_func_cb_info info) {
if (napi_get_cb_args_length(napi_env, info) < 2) {
napi_throw_error(
napi_env,
void Add(napi_env env, napi_func_cb_info info) {
if (napi_get_cb_args_length(env, info) < 2) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong number of arguments")));
env,
napi_create_string(env, "Wrong number of arguments")));
return;
}

napi_value args[2];
napi_get_cb_args(napi_env, info, args, 2);
napi_get_cb_args(env, info, args, 2);

if (napi_get_type_of_value(napi_env, args[0]) != napi_number ||
napi_get_type_of_value(napi_env, args[1]) != napi_number) {
napi_throw_error(
napi_env,
if (napi_get_type_of_value(env, args[0]) != napi_number ||
napi_get_type_of_value(env, args[1]) != napi_number) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong arguments")));
env,
napi_create_string(env, "Wrong arguments")));
return;
}

double value = napi_get_number_from_value(napi_env, args[0])
+ napi_get_number_from_value(napi_env, args[1]);
napi_value num = napi_create_number(napi_env, value);
double value = napi_get_number_from_value(env, args[0])
+ napi_get_number_from_value(env, args[1]);
napi_value num = napi_create_number(env, value);

napi_set_return_value(napi_env, info, num);
napi_set_return_value(env, info, num);
}


void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, exports,
napi_property_name(napi_env, "add"),
napi_create_function(napi_env, Add));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, exports,
napi_property_name(env, "add"),
napi_create_function(env, Add));
}

NODE_MODULE_ABI(addon, Init)
16 changes: 8 additions & 8 deletions test/addons-abi/3_callbacks/binding.cc
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include <node_jsvmapi.h>

void RunCallback(napi_env napi_env, const napi_func_cb_info info) {
void RunCallback(napi_env env, const napi_func_cb_info info) {
napi_value args[1];
napi_get_cb_args(napi_env, info, args, 1);
napi_get_cb_args(env, info, args, 1);
napi_value cb = args[0];

napi_value argv[1];
argv[0] = napi_create_string(napi_env, "hello world");
napi_call_function(napi_env, napi_get_global_scope(napi_env) , cb, 1, argv);
argv[0] = napi_create_string(env, "hello world");
napi_call_function(env, napi_get_global_scope(env) , cb, 1, argv);
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, module,
napi_property_name(napi_env, "exports"),
napi_create_function(napi_env, RunCallback));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, module,
napi_property_name(env, "exports"),
napi_create_function(env, RunCallback));
}

NODE_MODULE_ABI(addon, Init)
18 changes: 9 additions & 9 deletions test/addons-abi/4_object_factory/binding.cc
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <node_jsvmapi.h>

void napi_create_object(napi_env napi_env, const napi_func_cb_info info) {
void napi_create_object(napi_env env, const napi_func_cb_info info) {
napi_value args[1];
napi_get_cb_args(napi_env, info, args, 1);
napi_get_cb_args(env, info, args, 1);

napi_value obj = napi_create_object(napi_env);
napi_set_property(napi_env, obj, napi_property_name(napi_env, "msg"),
napi_value obj = napi_create_object(env);
napi_set_property(env, obj, napi_property_name(env, "msg"),
args[0]);

napi_set_return_value(napi_env, info, obj);
napi_set_return_value(env, info, obj);
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, module,
napi_property_name(napi_env, "exports"),
napi_create_function(napi_env, napi_create_object));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, module,
napi_property_name(env, "exports"),
napi_create_function(env, napi_create_object));
}

NODE_MODULE_ABI(addon, Init)
20 changes: 10 additions & 10 deletions test/addons-abi/5_function_factory/binding.cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include <node_jsvmapi.h>

void MyFunction(napi_env napi_env, napi_func_cb_info info) {
napi_set_return_value(napi_env, info, napi_create_string(napi_env, "hello world"));
void MyFunction(napi_env env, napi_func_cb_info info) {
napi_set_return_value(env, info, napi_create_string(env, "hello world"));
}

void napi_create_function(napi_env napi_env, napi_func_cb_info info) {
napi_value fn = napi_create_function(napi_env, MyFunction);
void napi_create_function(napi_env env, napi_func_cb_info info) {
napi_value fn = napi_create_function(env, MyFunction);

// omit this to make it anonymous
napi_set_function_name(napi_env, fn, napi_property_name(napi_env, "theFunction"));
napi_set_function_name(env, fn, napi_property_name(env, "theFunction"));

napi_set_return_value(napi_env, info, fn);
napi_set_return_value(env, info, fn);
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, module,
napi_property_name(napi_env, "exports"),
napi_create_function(napi_env, napi_create_function));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, module,
napi_property_name(env, "exports"),
napi_create_function(env, napi_create_function));
}

NODE_MODULE_ABI(addon, Init)
Expand Down
4 changes: 2 additions & 2 deletions test/addons-abi/6_object_wrap/binding.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "myobject.h"

void Init(napi_env napi_env, napi_value exports, napi_value module) {
MyObject::Init(napi_env, exports);
void Init(napi_env env, napi_value exports, napi_value module) {
MyObject::Init(env, exports);
}

NODE_MODULE_ABI(addon, Init)
42 changes: 21 additions & 21 deletions test/addons-abi/test_function/test_function.cc
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
#include <node_jsvmapi.h>

void Test(napi_env napi_env, napi_func_cb_info info) {
if (napi_get_cb_args_length(napi_env, info) < 1) {
napi_throw_error(
napi_env,
void Test(napi_env env, napi_func_cb_info info) {
if (napi_get_cb_args_length(env, info) < 1) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong number of arguments")));
env,
napi_create_string(env, "Wrong number of arguments")));
return;
}

napi_value args[10];
napi_get_cb_args(napi_env, info, args, 10);
napi_get_cb_args(env, info, args, 10);

if (napi_get_type_of_value(napi_env, args[0]) != napi_function) {
napi_throw_error(
napi_env,
if (napi_get_type_of_value(env, args[0]) != napi_function) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong type of argments. Expects a function.")));
env,
napi_create_string(env, "Wrong type of argments. Expects a function.")));
return;
}

napi_value function = args[0];
int argc = napi_get_cb_args_length(napi_env, info) - 1;
int argc = napi_get_cb_args_length(env, info) - 1;
napi_value* argv = args + 1;

napi_escapable_handle_scope scope = napi_open_escapable_handle_scope(napi_env);
napi_escapable_handle_scope scope = napi_open_escapable_handle_scope(env);
napi_value scope_val = reinterpret_cast<napi_value> (scope);
napi_value output = napi_call_function(napi_env, scope_val, function, argc, argv);
napi_close_escapable_handle_scope(napi_env, scope);
napi_set_return_value(napi_env, info, output);
napi_value output = napi_call_function(env, scope_val, function, argc, argv);
napi_close_escapable_handle_scope(env, scope);
napi_set_return_value(env, info, output);
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, exports,
napi_property_name(napi_env, "Test"),
napi_create_function(napi_env, Test));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, exports,
napi_property_name(env, "Test"),
napi_create_function(env, Test));
}

NODE_MODULE_ABI(addon, Init)
38 changes: 19 additions & 19 deletions test/addons-abi/test_number/test_number.cc
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
#include <node_jsvmapi.h>

void Test(napi_env napi_env, napi_func_cb_info info) {
if (napi_get_cb_args_length(napi_env, info) < 1) {
napi_throw_error(
napi_env,
void Test(napi_env env, napi_func_cb_info info) {
if (napi_get_cb_args_length(env, info) < 1) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong number of arguments")));
env,
napi_create_string(env, "Wrong number of arguments")));
return;
}

napi_value args[1];
napi_get_cb_args(napi_env, info, args, 1);
napi_get_cb_args(env, info, args, 1);

if (napi_get_type_of_value(napi_env, args[0]) != napi_number) {
napi_throw_error(
napi_env,
if (napi_get_type_of_value(env, args[0]) != napi_number) {
napi_throw(
env,
napi_create_type_error(
napi_env,
napi_create_string(napi_env, "Wrong type of argments. Expects a number.")));
env,
napi_create_string(env, "Wrong type of argments. Expects a number.")));
return;
}

double input = napi_get_number_from_value(napi_env, args[0]);
napi_value output = napi_create_number(napi_env, input);
napi_set_return_value(napi_env, info, output);
double input = napi_get_number_from_value(env, args[0]);
napi_value output = napi_create_number(env, input);
napi_set_return_value(env, info, output);
}

void Init(napi_env napi_env, napi_value exports, napi_value module) {
napi_set_property(napi_env, exports,
napi_property_name(napi_env, "Test"),
napi_create_function(napi_env, Test));
void Init(napi_env env, napi_value exports, napi_value module) {
napi_set_property(env, exports,
napi_property_name(env, "Test"),
napi_create_function(env, Test));
}

NODE_MODULE_ABI(addon, Init)
Loading

0 comments on commit 0d92cf5

Please sign in to comment.