-
-
Notifications
You must be signed in to change notification settings - Fork 12
port test_sharedarraybuffer to CTS #46
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
Open
bavulapati
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
bavulapati:feat/port-test-sharedarraybuffer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| add_node_api_cts_experimental_addon(test_sharedarraybuffer test_sharedarraybuffer.c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| "use strict"; | ||
|
|
||
| if (experimentalFeatures.sharedArrayBuffer) { | ||
| const test_sharedarraybuffer = loadAddon("test_sharedarraybuffer"); | ||
|
|
||
| { | ||
| const sab = new SharedArrayBuffer(16); | ||
| const ab = new ArrayBuffer(16); | ||
| const obj = {}; | ||
| const arr = []; | ||
|
|
||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(sab), | ||
| true, | ||
| ); | ||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(ab), | ||
| false, | ||
| ); | ||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(obj), | ||
| false, | ||
| ); | ||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(arr), | ||
| false, | ||
| ); | ||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(null), | ||
| false, | ||
| ); | ||
| assert.strictEqual( | ||
| test_sharedarraybuffer.TestIsSharedArrayBuffer(undefined), | ||
| false, | ||
| ); | ||
| } | ||
|
|
||
| // Test node_api_create_sharedarraybuffer | ||
| { | ||
| const sab = test_sharedarraybuffer.TestCreateSharedArrayBuffer(16); | ||
| assert(sab instanceof SharedArrayBuffer); | ||
| assert.strictEqual(sab.byteLength, 16); | ||
| } | ||
|
|
||
| // Test node_api_create_get_sharedarraybuffer_info | ||
| { | ||
| const sab = new SharedArrayBuffer(32); | ||
| const byteLength = test_sharedarraybuffer.TestGetSharedArrayBufferInfo(sab); | ||
| assert.strictEqual(byteLength, 32); | ||
| } | ||
|
|
||
| // Test data access | ||
| { | ||
| const sab = new SharedArrayBuffer(8); | ||
| const result = test_sharedarraybuffer.TestSharedArrayBufferData(sab); | ||
| assert.strictEqual(result, true); | ||
|
|
||
| // Check if data was written correctly | ||
| const view = new Uint8Array(sab); | ||
| for (let i = 0; i < 8; i++) { | ||
| assert.strictEqual(view[i], i % 256); | ||
| } | ||
| } | ||
|
|
||
| // Test data pointer from existing SharedArrayBuffer | ||
| { | ||
| const sab = new SharedArrayBuffer(16); | ||
| const result = test_sharedarraybuffer.TestSharedArrayBufferData(sab); | ||
| assert.strictEqual(result, true); | ||
| } | ||
|
|
||
| // Test zero-length SharedArrayBuffer | ||
| { | ||
| const sab = test_sharedarraybuffer.TestCreateSharedArrayBuffer(0); | ||
| assert(sab instanceof SharedArrayBuffer); | ||
| assert.strictEqual(sab.byteLength, 0); | ||
| } | ||
|
|
||
| // Test invalid arguments | ||
| { | ||
| assert.throws( | ||
| () => { | ||
| test_sharedarraybuffer.TestGetSharedArrayBufferInfo({}); | ||
| }, | ||
| { name: "Error", message: "Invalid argument" }, | ||
| ); | ||
| } | ||
| } |
130 changes: 130 additions & 0 deletions
130
tests/js-native-api/test_sharedarraybuffer/test_sharedarraybuffer.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #define NAPI_EXPERIMENTAL | ||
| #include <js_native_api.h> | ||
| #include <string.h> | ||
| #include "../common.h" | ||
| #include "../entry_point.h" | ||
|
|
||
| static napi_value TestIsSharedArrayBuffer(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); | ||
|
|
||
| bool is_sharedarraybuffer; | ||
| NODE_API_CALL( | ||
| env, node_api_is_sharedarraybuffer(env, args[0], &is_sharedarraybuffer)); | ||
|
|
||
| napi_value ret; | ||
| NODE_API_CALL(env, napi_get_boolean(env, is_sharedarraybuffer, &ret)); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static napi_value TestCreateSharedArrayBuffer(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); | ||
|
|
||
| napi_valuetype valuetype0; | ||
| NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype0)); | ||
|
|
||
| NODE_API_ASSERT( | ||
| env, | ||
| valuetype0 == napi_number, | ||
| "Wrong type of arguments. Expects a number as first argument."); | ||
|
|
||
| int32_t byte_length; | ||
| NODE_API_CALL(env, napi_get_value_int32(env, args[0], &byte_length)); | ||
|
|
||
| NODE_API_ASSERT(env, | ||
| byte_length >= 0, | ||
| "Invalid byte length. Expects a non-negative integer."); | ||
|
|
||
| napi_value ret; | ||
| void* data; | ||
| NODE_API_CALL( | ||
| env, node_api_create_sharedarraybuffer(env, byte_length, &data, &ret)); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static napi_value TestGetSharedArrayBufferInfo(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); | ||
|
|
||
| void* data; | ||
| size_t byte_length; | ||
| NODE_API_CALL(env, | ||
| napi_get_arraybuffer_info(env, args[0], &data, &byte_length)); | ||
|
|
||
| napi_value ret; | ||
| NODE_API_CALL(env, napi_create_uint32(env, byte_length, &ret)); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| static void WriteTestDataToBuffer(void* data, size_t byte_length) { | ||
| if (byte_length > 0 && data != NULL) { | ||
| uint8_t* bytes = (uint8_t*)data; | ||
| for (size_t i = 0; i < byte_length; i++) { | ||
| bytes[i] = i % 256; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static napi_value TestSharedArrayBufferData(napi_env env, | ||
| napi_callback_info info) { | ||
| size_t argc = 1; | ||
| napi_value args[1]; | ||
| NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); | ||
|
|
||
| NODE_API_ASSERT(env, argc >= 1, "Wrong number of arguments"); | ||
|
|
||
| void* data; | ||
| size_t byte_length; | ||
| NODE_API_CALL(env, | ||
| napi_get_arraybuffer_info(env, args[0], &data, &byte_length)); | ||
|
|
||
| WriteTestDataToBuffer(data, byte_length); | ||
|
|
||
| // Return the same data pointer validity | ||
| bool data_valid = (data != NULL) && (byte_length > 0); | ||
|
|
||
| napi_value ret; | ||
| NODE_API_CALL(env, napi_get_boolean(env, data_valid, &ret)); | ||
|
|
||
| return ret; | ||
| } | ||
|
|
||
| EXTERN_C_START | ||
| napi_value Init(napi_env env, napi_value exports) { | ||
| napi_property_descriptor descriptors[] = { | ||
| DECLARE_NODE_API_PROPERTY("TestIsSharedArrayBuffer", | ||
| TestIsSharedArrayBuffer), | ||
| DECLARE_NODE_API_PROPERTY("TestCreateSharedArrayBuffer", | ||
| TestCreateSharedArrayBuffer), | ||
| DECLARE_NODE_API_PROPERTY("TestGetSharedArrayBufferInfo", | ||
| TestGetSharedArrayBufferInfo), | ||
| DECLARE_NODE_API_PROPERTY("TestSharedArrayBufferData", | ||
| TestSharedArrayBufferData), | ||
| }; | ||
|
|
||
| NODE_API_CALL( | ||
| env, | ||
| napi_define_properties(env, | ||
| exports, | ||
| sizeof(descriptors) / sizeof(*descriptors), | ||
| descriptors)); | ||
|
|
||
| return exports; | ||
| } | ||
| EXTERN_C_END |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is same as in #39