| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dc9c770 commit 646b81c
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + { | ||
| 2 | + 'targets': [ | ||
| 3 | + { | ||
| 4 | + 'target_name': 'binding', | ||
| 5 | + 'sources': [ 'test_worker_buffer_callback.c' ] | ||
| 6 | + } | ||
| 7 | + ] | ||
| 8 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../../common'); | ||
| 3 | + const path = require('path'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const { Worker } = require('worker_threads'); | ||
| 6 | + const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`); | ||
| 7 | + const { getFreeCallCount } = require(binding); | ||
| 8 | + | ||
| 9 | + // Test that buffers allocated with a free callback through our APIs are | ||
| 10 | + // released when a Worker owning it exits. | ||
| 11 | + | ||
| 12 | + const w = new Worker(`require(${JSON.stringify(binding)})`, { eval: true }); | ||
| 13 | + | ||
| 14 | + assert.strictEqual(getFreeCallCount(), 0); | ||
| 15 | + w.on('exit', common.mustCall(() => { | ||
| 16 | + assert.strictEqual(getFreeCallCount(), 1); | ||
| 17 | + })); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,15 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { MessageChannel } = require('worker_threads'); | ||
| 5 | + const { buffer } = require(`./build/${common.buildType}/binding`); | ||
| 6 | + | ||
| 7 | + // Test that buffers allocated with a free callback through our APIs are not | ||
| 8 | + // transferred. | ||
| 9 | + | ||
| 10 | + const { port1 } = new MessageChannel(); | ||
| 11 | + const origByteLength = buffer.byteLength; | ||
| 12 | + port1.postMessage(buffer, [buffer]); | ||
| 13 | + | ||
| 14 | + assert.strictEqual(buffer.byteLength, origByteLength); | ||
| 15 | + assert.notStrictEqual(buffer.byteLength, 0); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + #include <stdio.h> | ||
| 2 | + #include <node_api.h> | ||
| 3 | + #include <assert.h> | ||
| 4 | + #include "../../js-native-api/common.h" | ||
| 5 | + | ||
| 6 | + uint32_t free_call_count = 0; | ||
| 7 | + char data[] = "hello"; | ||
| 8 | + | ||
| 9 | + napi_value GetFreeCallCount(napi_env env, napi_callback_info info) { | ||
| 10 | + napi_value value; | ||
| 11 | + NAPI_CALL(env, napi_create_uint32(env, free_call_count, &value)); | ||
| 12 | + return value; | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + static void finalize_cb(napi_env env, void* finalize_data, void* hint) { | ||
| 16 | + assert(finalize_data == data); | ||
| 17 | + free_call_count++; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + NAPI_MODULE_INIT() { | ||
| 21 | + napi_property_descriptor properties[] = { | ||
| 22 | + DECLARE_NAPI_PROPERTY("getFreeCallCount", GetFreeCallCount) | ||
| 23 | + }; | ||
| 24 | + | ||
| 25 | + NAPI_CALL(env, napi_define_properties( | ||
| 26 | + env, exports, sizeof(properties) / sizeof(*properties), properties)); | ||
| 27 | + | ||
| 28 | + // This is a slight variation on the non-N-API test: We create an ArrayBuffer | ||
| 29 | + // rather than a Node.js Buffer, since testing the latter would only test | ||
| 30 | + // the same code paths and not the ones specific to N-API. | ||
| 31 | + napi_value buffer; | ||
| 32 | + NAPI_CALL(env, napi_create_external_arraybuffer( | ||
| 33 | + env, | ||
| 34 | + data, | ||
| 35 | + sizeof(data), | ||
| 36 | + finalize_cb, | ||
| 37 | + NULL, | ||
| 38 | + &buffer)); | ||
| 39 | + | ||
| 40 | + NAPI_CALL(env, napi_set_named_property(env, exports, "buffer", buffer)); | ||
| 41 | + | ||
| 42 | + return exports; | ||
| 43 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments