Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try catch chakracore #43

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
768006f
Makefile: Copy instructions related to test-addons to test-addons-abi
Jan 7, 2017
a44e1a8
Change naming addon-abi to addons-abi
Jan 7, 2017
0f1df31
Apply commits f6680a0...a5e7cf7 as pertaining to test/addons-abi
Jan 7, 2017
b0af536
Fix the free error due to malloc in napi_get_string
sampsongao Jul 12, 2016
9ace7fd
Apply commits Apply commits Apply commits db2a981...47d9a7d as pertai…
Jan 7, 2017
bef0dd8
Change variable name napi_env to env
sampsongao Jul 14, 2016
4f7c9ce
Add tests for symbol
sampsongao Jul 19, 2016
79941c7
Add more test cases to test number and modify the test format
sampsongao Jul 19, 2016
b239127
Replace type error throws api calls
sampsongao Jul 19, 2016
993140e
Add test for testing napi_create_object
sampsongao Jul 21, 2016
664015a
Add tests for object napi_get_propertynames
sampsongao Jul 25, 2016
e330002
modify tests for function
sampsongao Jul 25, 2016
157659b
Modify tests for string
sampsongao Jul 25, 2016
2d2010b
Add tests for string length
sampsongao Jul 26, 2016
c8e904a
Replace array get/set methods with object's
sampsongao Jul 26, 2016
1c9fbe0
Cleanup code to pass lint tests
sampsongao Jul 27, 2016
4233b35
Fix typo in test_object.cc
sampsongao Sep 7, 2016
4ecc5ff
test: remove reference to v8 in abi version of test so it an compile
mhdawson Sep 11, 2016
dbb3291
Fix some inconsistency addons tests build files
sampsongao Sep 13, 2016
b2f08e6
src/node_jsvmapi.h: Include node.h for the CTOR definition
Jan 7, 2017
5cd6adc
napi: implement exception handling
Jan 20, 2017
0c96f22
napi: add test for exception handling implementation
Jan 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ v8:

test: all
$(MAKE) build-addons
$(MAKE) build-addons-abi
$(MAKE) cctest
$(PYTHON) tools/test.py --mode=release -J \
addons doctool inspector known_issues message pseudo-tty parallel sequential
addons addons-abi doctool inspector known_issues message pseudo-tty parallel sequential
$(MAKE) lint

test-parallel: all
Expand Down Expand Up @@ -153,10 +154,18 @@ ADDONS_BINDING_GYPS := \
$(filter-out test/addons/??_*/binding.gyp, \
$(wildcard test/addons/*/binding.gyp))

ADDONS_ABI_BINDING_GYPS := \
$(filter-out test/addons-abi/??_*/binding.gyp, \
$(wildcard test/addons-abi/*/binding.gyp))

ADDONS_BINDING_SOURCES := \
$(filter-out test/addons/??_*/*.cc, $(wildcard test/addons/*/*.cc)) \
$(filter-out test/addons/??_*/*.h, $(wildcard test/addons/*/*.h))

ADDONS_ABI_BINDING_SOURCES := \
$(filter-out test/addons-abi/??_*/*.cc, $(wildcard test/addons-abi/*/*.cc)) \
$(filter-out test/addons-abi/??_*/*.h, $(wildcard test/addons-abi/*/*.h))

# Implicitly depends on $(NODE_EXE), see the build-addons rule for rationale.
# Depends on node-gyp package.json so that build-addons is (re)executed when
# node-gyp is updated as part of an npm update.
Expand All @@ -176,6 +185,24 @@ test/addons/.buildstamp: config.gypi \
done
touch $@

# Implicitly depends on $(NODE_EXE), see the build-addons rule for rationale.
# Depends on node-gyp package.json so that build-addons is (re)executed when
# node-gyp is updated as part of an npm update.
test/addons-abi/.buildstamp: config.gypi \
deps/npm/node_modules/node-gyp/package.json \
$(ADDONS_ABI_BINDING_GYPS) $(ADDONS_ABI_INDING_SOURCES) \
deps/uv/include/*.h deps/v8/include/*.h \
src/node.h src/node_buffer.h src/node_object_wrap.h
# Cannot use $(wildcard test/addons-abi/*/) here, it's evaluated before
# embedded addons have been generated from the documentation.
for dirname in test/addons-abi/*/; do \
$(NODE) deps/npm/node_modules/node-gyp/bin/node-gyp rebuild \
--python="$(PYTHON)" \
--directory="$$PWD/$$dirname" \
--nodedir="$$PWD" || exit 1 ; \
done
touch $@

# .buildstamp and .docbuildstamp need $(NODE_EXE) but cannot depend on it
# directly because it calls make recursively. The parent make cannot know
# if the subprocess touched anything so it pessimistically assumes that
Expand All @@ -184,22 +211,30 @@ test/addons/.buildstamp: config.gypi \
# TODO(bnoordhuis) Force rebuild after gyp update.
build-addons: $(NODE_EXE) test/addons/.buildstamp

# .buildstamp and .docbuildstamp need $(NODE_EXE) but cannot depend on it
# directly because it calls make recursively. The parent make cannot know
# if the subprocess touched anything so it pessimistically assumes that
# .buildstamp and .docbuildstamp are out of date and need a rebuild.
# Just goes to show that recursive make really is harmful...
# TODO(bnoordhuis) Force rebuild after gyp update.
build-addons-abi: $(NODE_EXE) test/addons-abi/.buildstamp

test-gc: all test/gc/node_modules/weak/build/Release/weakref.node
$(PYTHON) tools/test.py --mode=release gc

test-build: | all build-addons
test-build: | all build-addons build-addons-abi

test-all: test-build test/gc/node_modules/weak/build/Release/weakref.node
$(PYTHON) tools/test.py --mode=debug,release

test-all-valgrind: test-build
$(PYTHON) tools/test.py --mode=debug,release --valgrind

CI_NATIVE_SUITES := addons
CI_NATIVE_SUITES := addons addons-abi
CI_JS_SUITES := doctool inspector known_issues message parallel pseudo-tty sequential

# Build and test addons without building anything else
test-ci-native: | test/addons/.buildstamp
test-ci-native: | test/addons/.buildstamp test/addons-abi/.buildstamp
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
--mode=release --flaky-tests=$(FLAKY_TESTS) \
$(TEST_CI_ARGS) $(CI_NATIVE_SUITES)
Expand All @@ -210,7 +245,7 @@ test-ci-js:
--mode=release --flaky-tests=$(FLAKY_TESTS) \
$(TEST_CI_ARGS) $(CI_JS_SUITES)

test-ci: | build-addons
test-ci: | build-addons build-addons-abi
out/Release/cctest --gtest_output=tap:cctest.tap
$(PYTHON) tools/test.py $(PARALLEL_ARGS) -p tap --logfile test.tap \
--mode=release --flaky-tests=$(FLAKY_TESTS) \
Expand Down Expand Up @@ -252,6 +287,9 @@ test-npm-publish: $(NODE_EXE)
test-addons: test-build
$(PYTHON) tools/test.py --mode=release addons

test-addons-abi: test-build
$(PYTHON) tools/test.py --mode=release addons-abi

test-timers:
$(MAKE) --directory=tools faketime
$(PYTHON) tools/test.py --mode=release timers
Expand Down Expand Up @@ -704,14 +742,16 @@ CPPLINT_EXCLUDE ?=
CPPLINT_EXCLUDE += src/node_root_certs.h
CPPLINT_EXCLUDE += src/queue.h
CPPLINT_EXCLUDE += src/tree.h
CPPLINT_EXCLUDE += $(wildcard test/addons/??_*/*.cc test/addons/??_*/*.h)
CPPLINT_EXCLUDE += $(wildcard test/addons/??_*/*.cc test/addons/??_*/*.h test/addons-abi/??_*/*.cc test/addons-abi/??_*/*.h)

CPPLINT_FILES = $(filter-out $(CPPLINT_EXCLUDE), $(wildcard \
src/*.c \
src/*.cc \
src/*.h \
test/addons/*/*.cc \
test/addons/*/*.h \
test/addons-abi/*/*.cc \
test/addons-abi/*/*.h \
tools/icu/*.cc \
tools/icu/*.h \
))
Expand Down Expand Up @@ -748,4 +788,5 @@ endif
bench-all bench bench-misc bench-array bench-buffer bench-net \
bench-http bench-fs bench-tls cctest run-ci test-v8 test-v8-intl \
test-v8-benchmarks test-v8-all v8 lint-ci bench-ci jslint-ci doc-only \
$(TARBALL)-headers test-ci test-ci-native test-ci-js build-ci
$(TARBALL)-headers test-ci test-ci-native test-ci-js build-ci \
test-addons-abi build-addons-abi
55 changes: 12 additions & 43 deletions src/node_jsrtapi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -918,49 +918,6 @@ napi_value napi_make_callback(napi_env e, napi_value recv,
return reinterpret_cast<napi_value>(result);
}

bool napi_try_catch(napi_env e, napi_try_callback cbtry,
napi_catch_callback cbcatch, void* data) {
JsValueRef error = JS_INVALID_REFERENCE;
bool hasException;
cbtry(e, data);
if (error = JS_INVALID_REFERENCE) {
JsErrorCode errorCode = JsHasException(&hasException);
if (errorCode != JsNoError) {
// If script is in disabled state, no need to fail here.
// We will fail subsequent calls to Jsrt anyway.
assert(errorCode == JsErrorInDisabledState);
} else if (hasException) {
JsValueRef exceptionRef;
errorCode = JsGetAndClearException(&exceptionRef);
// We came here through JsHasException, so script shouldn't be in disabled
// state.
assert(errorCode != JsErrorInDisabledState);
if (errorCode == JsNoError) {
error = exceptionRef;
}
}
}
if(error != JS_INVALID_REFERENCE) {
cbcatch(e, data);
return true;
}
JsErrorCode errorCode = JsHasException(&hasException);
if (errorCode != JsNoError) {
if (errorCode == JsErrorInDisabledState) {
cbcatch(e, data);
return true;
}
// Should never get errorCode other than JsNoError/JsErrorInDisabledState
assert(false);
return false;
}
if (hasException) {
cbcatch(e, data);
return true;
}
return false;
}

struct ArrayBufferFinalizeInfo {
void *data;

Expand Down Expand Up @@ -1020,3 +977,15 @@ size_t napi_buffer_length(napi_env e, napi_value v) {
return 0;
return len;
}

bool napi_is_exception_pending(napi_env) {
bool returnValue = false;
JsErrorCode error = JsHasException(&returnValue);
return returnValue;
}

napi_value napi_get_and_clear_last_exception(napi_env e) {
JsValueRef lastException = napi_get_undefined_(e);
JsErrorCode error = JsGetAndClearException(&lastException);
return reinterpret_cast<napi_value>(lastException);
}
14 changes: 4 additions & 10 deletions src/node_jsvmapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct napi_module_struct {
struct node_module* nm_link;
};

#include <node.h>

This comment was marked as off-topic.


NODE_EXTERN void napi_module_register(void* mod);

Expand Down Expand Up @@ -241,16 +242,9 @@ NODE_EXTERN void napi_throw(napi_env e, napi_value error);
NODE_EXTERN void napi_throw_error(napi_env e, char* msg);
NODE_EXTERN void napi_throw_type_error(napi_env e, char* msg);

// 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
// with a wrapper class hack like we're doing for handle scopes.
typedef void (*napi_try_callback)(napi_env e, void* data);
typedef void (*napi_catch_callback)(napi_env e, void* data);
NODE_EXTERN bool napi_try_catch(napi_env e, napi_try_callback t,
napi_catch_callback c, void* data);

// Methods to support catching exceptions
NODE_EXTERN bool napi_is_exception_pending(napi_env e);
NODE_EXTERN napi_value napi_get_and_clear_last_exception(napi_env e);

// Methods to provide node::Buffer functionality with napi types
NODE_EXTERN napi_value napi_buffer_new(napi_env e, char* data, uint32_t size);
Expand Down
7 changes: 7 additions & 0 deletions test/addons-abi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.buildstamp
.docbuildstamp
Makefile
*.Makefile
*.mk
gyp-mac-tool
/*/build
17 changes: 17 additions & 0 deletions test/addons-abi/1_hello_world/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <node_jsvmapi.h>

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

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)

8 changes: 8 additions & 0 deletions test/addons-abi/1_hello_world/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
}
]
}
6 changes: 6 additions & 0 deletions test/addons-abi/1_hello_world/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
require('../../common');
var assert = require('assert');
var addon = require('./build/Release/binding');

assert.equal(addon.hello(), 'world');
32 changes: 32 additions & 0 deletions test/addons-abi/2_function_arguments/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <node_jsvmapi.h>

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

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

if (napi_get_type_of_value(env, args[0]) != napi_number ||
napi_get_type_of_value(env, args[1]) != napi_number) {
napi_throw_type_error(env, "Wrong arguments");
return;
}

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(env, info, num);
}


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)
8 changes: 8 additions & 0 deletions test/addons-abi/2_function_arguments/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
}
]
}
6 changes: 6 additions & 0 deletions test/addons-abi/2_function_arguments/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';
require('../../common');
var assert = require('assert');
var addon = require('./build/Release/binding');

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

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

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

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)
8 changes: 8 additions & 0 deletions test/addons-abi/3_callbacks/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
}
]
}
8 changes: 8 additions & 0 deletions test/addons-abi/3_callbacks/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
require('../../common');
var assert = require('assert');
var addon = require('./build/Release/binding');

addon(function(msg) {
assert.equal(msg, 'hello world');
});
20 changes: 20 additions & 0 deletions test/addons-abi/4_object_factory/binding.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <node_jsvmapi.h>

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

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

napi_set_return_value(env, info, obj);
}

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)
8 changes: 8 additions & 0 deletions test/addons-abi/4_object_factory/binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "binding",
"sources": [ "binding.cc" ]
}
]
}
8 changes: 8 additions & 0 deletions test/addons-abi/4_object_factory/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
require('../../common');
var assert = require('assert');
var addon = require('./build/Release/binding');

var obj1 = addon('hello');
var obj2 = addon('world');
assert.equal(obj1.msg + ' ' + obj2.msg, 'hello world');
Loading