Skip to content

Commit b5f53a2

Browse files
kraenhansenclaude
andcommitted
feat: port 17 easy js-native-api tests to CTS
Port all "Easy" difficulty engine-specific tests from Node.js: 3_callbacks, 4_object_factory, 5_function_factory, 7_factory_wrap, 8_passing_wrapped, test_array, test_bigint, test_dataview, test_date, test_handle_scope, test_instance_data, test_new_target, test_number, test_promise, test_properties, test_reference_double_free, test_symbol. C/C++ sources are copied from Node.js with minimal changes (entry_point.h macro). JS tests are adapted to use CTS globals (loadAddon, assert, gcUntil) instead of Node.js test harness (common.js, require). test_dataview: removed SharedArrayBuffer tests that depend on experimental node_api_is_sharedarraybuffer API (not in stable node-api-headers). Also updates node-api-headers from 1.7.0 to 1.8.0. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 126f092 commit b5f53a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2318
-5
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"devDependencies": {
1414
"@types/node": "^24.10.1",
1515
"eslint": "^9.39.1",
16-
"node-api-headers": "^1.7.0"
16+
"node-api-headers": "^1.8.0"
1717
},
1818
"dependencies": {
1919
"amaro": "^1.1.5"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <js_native_api.h>
2+
#include <string.h>
3+
#include "../common.h"
4+
#include "../entry_point.h"
5+
6+
static napi_value RunCallback(napi_env env, napi_callback_info info) {
7+
size_t argc = 2;
8+
napi_value args[2];
9+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
10+
11+
NODE_API_ASSERT(env, argc == 1,
12+
"Wrong number of arguments. Expects a single argument.");
13+
14+
napi_valuetype valuetype0;
15+
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0));
16+
NODE_API_ASSERT(env, valuetype0 == napi_function,
17+
"Wrong type of arguments. Expects a function as first argument.");
18+
19+
napi_valuetype valuetype1;
20+
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype1));
21+
NODE_API_ASSERT(env, valuetype1 == napi_undefined,
22+
"Additional arguments should be undefined.");
23+
24+
napi_value argv[1];
25+
const char* str = "hello world";
26+
size_t str_len = strlen(str);
27+
NODE_API_CALL(env, napi_create_string_utf8(env, str, str_len, argv));
28+
29+
napi_value global;
30+
NODE_API_CALL(env, napi_get_global(env, &global));
31+
32+
napi_value cb = args[0];
33+
NODE_API_CALL(env, napi_call_function(env, global, cb, 1, argv, NULL));
34+
35+
return NULL;
36+
}
37+
38+
static napi_value RunCallbackWithRecv(napi_env env, napi_callback_info info) {
39+
size_t argc = 2;
40+
napi_value args[2];
41+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
42+
43+
napi_value cb = args[0];
44+
napi_value recv = args[1];
45+
NODE_API_CALL(env, napi_call_function(env, recv, cb, 0, NULL, NULL));
46+
return NULL;
47+
}
48+
49+
EXTERN_C_START
50+
napi_value Init(napi_env env, napi_value exports) {
51+
napi_property_descriptor desc[2] = {
52+
DECLARE_NODE_API_PROPERTY("RunCallback", RunCallback),
53+
DECLARE_NODE_API_PROPERTY("RunCallbackWithRecv", RunCallbackWithRecv),
54+
};
55+
NODE_API_CALL(env, napi_define_properties(env, exports, 2, desc));
56+
return exports;
57+
}
58+
EXTERN_C_END
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_addon(3_callbacks 3_callbacks.c)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const addon = loadAddon('3_callbacks');
3+
4+
let called = false;
5+
addon.RunCallback((msg) => {
6+
assert.strictEqual(msg, 'hello world');
7+
called = true;
8+
});
9+
assert(called);
10+
11+
function testRecv(desiredRecv) {
12+
let recvCalled = false;
13+
addon.RunCallbackWithRecv(function() {
14+
assert.strictEqual(this, desiredRecv);
15+
recvCalled = true;
16+
}, desiredRecv);
17+
assert(recvCalled);
18+
}
19+
20+
testRecv(undefined);
21+
testRecv(null);
22+
testRecv(5);
23+
testRecv(true);
24+
testRecv('Hello');
25+
testRecv([]);
26+
testRecv({});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <js_native_api.h>
2+
#include "../common.h"
3+
#include "../entry_point.h"
4+
5+
static napi_value CreateObject(napi_env env, napi_callback_info info) {
6+
size_t argc = 1;
7+
napi_value args[1];
8+
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
9+
10+
napi_value obj;
11+
NODE_API_CALL(env, napi_create_object(env, &obj));
12+
13+
NODE_API_CALL(env, napi_set_named_property(env, obj, "msg", args[0]));
14+
15+
return obj;
16+
}
17+
18+
EXTERN_C_START
19+
napi_value Init(napi_env env, napi_value exports) {
20+
NODE_API_CALL(env,
21+
napi_create_function(env, "exports", -1, CreateObject, NULL, &exports));
22+
return exports;
23+
}
24+
EXTERN_C_END
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_addon(4_object_factory 4_object_factory.c)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
const addon = loadAddon('4_object_factory');
3+
4+
const obj1 = addon('hello');
5+
const obj2 = addon('world');
6+
assert.strictEqual(`${obj1.msg} ${obj2.msg}`, 'hello world');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <js_native_api.h>
2+
#include "../common.h"
3+
#include "../entry_point.h"
4+
5+
static napi_value MyFunction(napi_env env, napi_callback_info info) {
6+
napi_value str;
7+
NODE_API_CALL(env, napi_create_string_utf8(env, "hello world", -1, &str));
8+
return str;
9+
}
10+
11+
static napi_value CreateFunction(napi_env env, napi_callback_info info) {
12+
napi_value fn;
13+
NODE_API_CALL(env,
14+
napi_create_function(env, "theFunction", -1, MyFunction, NULL, &fn));
15+
return fn;
16+
}
17+
18+
EXTERN_C_START
19+
napi_value Init(napi_env env, napi_value exports) {
20+
NODE_API_CALL(env,
21+
napi_create_function(env, "exports", -1, CreateFunction, NULL, &exports));
22+
return exports;
23+
}
24+
EXTERN_C_END
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_node_api_cts_addon(5_function_factory 5_function_factory.c)

0 commit comments

Comments
 (0)