summaryrefslogtreecommitdiff
path: root/test/node-api
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2019-02-15 11:48:56 +0100
committerAnna Henningsen <anna@addaleax.net>2019-02-17 18:09:58 +0100
commit441ef4d7f03131ddfd6e82e405bdd3640ec5bf5a (patch)
treef1e87015ee36ee209f85ee6eb9bde76be9705120 /test/node-api
parent783c65ebc4143ed9afa723f26eaebdecf5f98691 (diff)
downloadnode-new-441ef4d7f03131ddfd6e82e405bdd3640ec5bf5a.tar.gz
n-api: do not call into JS when that is not allowed
Check whether calling into JS is allowed before doing so. PR-URL: https://github.com/nodejs/node/pull/26127 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Diffstat (limited to 'test/node-api')
-rw-r--r--test/node-api/test_worker_terminate/binding.gyp8
-rw-r--r--test/node-api/test_worker_terminate/test.js23
-rw-r--r--test/node-api/test_worker_terminate/test_worker_terminate.c39
3 files changed, 70 insertions, 0 deletions
diff --git a/test/node-api/test_worker_terminate/binding.gyp b/test/node-api/test_worker_terminate/binding.gyp
new file mode 100644
index 0000000000..3a9465c745
--- /dev/null
+++ b/test/node-api/test_worker_terminate/binding.gyp
@@ -0,0 +1,8 @@
+{
+ "targets": [
+ {
+ "target_name": "test_worker_terminate",
+ "sources": [ "test_worker_terminate.c" ]
+ }
+ ]
+}
diff --git a/test/node-api/test_worker_terminate/test.js b/test/node-api/test_worker_terminate/test.js
new file mode 100644
index 0000000000..2bfaab8e8e
--- /dev/null
+++ b/test/node-api/test_worker_terminate/test.js
@@ -0,0 +1,23 @@
+'use strict';
+const common = require('../../common');
+const assert = require('assert');
+const { Worker, isMainThread, workerData } = require('worker_threads');
+
+if (isMainThread) {
+ const counter = new Int32Array(new SharedArrayBuffer(4));
+ const worker = new Worker(__filename, { workerData: { counter } });
+ worker.on('exit', common.mustCall(() => {
+ assert.strictEqual(counter[0], 1);
+ }));
+ worker.on('error', common.mustNotCall());
+} else {
+ const { Test } = require(`./build/${common.buildType}/test_worker_terminate`);
+
+ const { counter } = workerData;
+ // Test() tries to call a function twice and asserts that the second call does
+ // not work because of a pending exception.
+ Test(() => {
+ Atomics.add(counter, 0, 1);
+ process.exit();
+ });
+}
diff --git a/test/node-api/test_worker_terminate/test_worker_terminate.c b/test/node-api/test_worker_terminate/test_worker_terminate.c
new file mode 100644
index 0000000000..a88d936293
--- /dev/null
+++ b/test/node-api/test_worker_terminate/test_worker_terminate.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <node_api.h>
+#include <assert.h>
+#include "../../js-native-api/common.h"
+
+napi_value Test(napi_env env, napi_callback_info info) {
+ size_t argc = 1;
+ napi_value recv;
+ napi_value argv[1];
+ napi_status status;
+
+ NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &recv, NULL));
+ NAPI_ASSERT(env, argc >= 1, "Not enough arguments, expected 1.");
+
+ napi_valuetype t;
+ NAPI_CALL(env, napi_typeof(env, argv[0], &t));
+ NAPI_ASSERT(env, t == napi_function,
+ "Wrong first argument, function expected.");
+
+ status = napi_call_function(env, recv, argv[0], 0, NULL, NULL);
+ assert(status == napi_ok);
+ status = napi_call_function(env, recv, argv[0], 0, NULL, NULL);
+ assert(status == napi_pending_exception);
+
+ return NULL;
+}
+
+napi_value Init(napi_env env, napi_value exports) {
+ napi_property_descriptor properties[] = {
+ DECLARE_NAPI_PROPERTY("Test", Test)
+ };
+
+ NAPI_CALL(env, napi_define_properties(
+ env, exports, sizeof(properties) / sizeof(*properties), properties));
+
+ return exports;
+}
+
+NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)