@emnapi/node-binding 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ See [https://github.com/toyobayashi/emnapi](https://github.com/toyobayashi/emnapi)
package/binding.gyp ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "variables": {
3
+ "module_name": "emnapi_node_binding",
4
+ "module_path": "./dist"
5
+ },
6
+ 'targets': [
7
+ {
8
+ 'target_name': '<(module_name)',
9
+ 'sources': [
10
+ 'src/binding.cpp',
11
+ ],
12
+ },
13
+ {
14
+ 'target_name': '<(module_name)_napi',
15
+ 'sources': [
16
+ 'src/napi.c',
17
+ ],
18
+ },
19
+ {
20
+ "target_name": "action_after_build",
21
+ "type": "none",
22
+ "dependencies": [ "<(module_name)", '<(module_name)_napi' ],
23
+ "copies": [
24
+ {
25
+ "files": [ "<(PRODUCT_DIR)/<(module_name).node", "<(PRODUCT_DIR)/<(module_name)_napi.node" ],
26
+ "destination": "<(module_path)"
27
+ }
28
+ ]
29
+ }
30
+ ]
31
+ }
package/index.d.ts ADDED
@@ -0,0 +1,29 @@
1
+ export declare interface AsyncContext {
2
+ asyncId: number
3
+ triggerAsyncId: number
4
+ }
5
+
6
+ export declare interface NapiResult<T> {
7
+ status: number
8
+ value: T
9
+ error?: any
10
+ }
11
+
12
+ export declare namespace node {
13
+ export function emitAsyncInit (resource: object, name: string, triggerAsyncId: number): AsyncContext
14
+ export function emitAsyncDestroy (asyncContext: AsyncContext): void
15
+ // export function openCallbackScope (resource: object, asyncContext: AsyncContext): bigint
16
+ // export function closeCallbackScope (callbackScope: bigint): void
17
+ export function makeCallback<P extends any[], T> (resource: object, cb: (...args: P) => T, argv: P, asyncContext: AsyncContext): T
18
+ }
19
+
20
+ export declare namespace napi {
21
+ export function asyncInit (resource: object | undefined | null, name: string): NapiResult<bigint>
22
+ export function asyncDestroy (asyncContextPointer: bigint): NapiResult<undefined>
23
+ export function makeCallback<P extends any[], T> (
24
+ asyncContextPointer: bigint,
25
+ recv: any,
26
+ func: (...args: P) => T,
27
+ argv: P
28
+ ): NapiResult<T>
29
+ }
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ node: require('./dist/emnapi_node_binding.node'),
5
+ napi: require('./dist/emnapi_node_binding_napi.node')
6
+ }
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@emnapi/node-binding",
3
+ "version": "0.31.0",
4
+ "description": "Bridge connecting emnapi and Node.js native implementation",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
+ "scripts": {
8
+ "build": "node-gyp rebuild"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/toyobayashi/emnapi.git"
13
+ },
14
+ "author": "toyobayashi",
15
+ "license": "MIT",
16
+ "bugs": {
17
+ "url": "https://github.com/toyobayashi/emnapi/issues"
18
+ },
19
+ "homepage": "https://github.com/toyobayashi/emnapi#readme",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ }
23
+ }
@@ -0,0 +1,98 @@
1
+ #include <vector>
2
+ #include <node.h>
3
+
4
+ namespace emnapi_node_binding {
5
+
6
+ namespace {
7
+
8
+ void EmitAsyncInit(const v8::FunctionCallbackInfo<v8::Value>& args) {
9
+ v8::Isolate* isolate = args.GetIsolate();
10
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
11
+ v8::Local<v8::Object> resource = args[0].As<v8::Object>();
12
+ v8::Local<v8::String> name = args[1].As<v8::String>();
13
+ double trigger_async_id = args[2].As<v8::Number>()->Value();
14
+
15
+ node::async_context result_context = node::EmitAsyncInit(isolate, resource, name, trigger_async_id);
16
+
17
+ v8::Local<v8::Object> result = v8::Object::New(isolate);
18
+ v8::Local<v8::String> async_id_key = v8::String::NewFromUtf8(isolate, "asyncId").ToLocalChecked();
19
+ v8::Local<v8::String> trigger_async_id_key = v8::String::NewFromUtf8(isolate, "triggerAsyncId").ToLocalChecked();
20
+ v8::Local<v8::Number> result_async_id = v8::Number::New(isolate, result_context.async_id);
21
+ v8::Local<v8::Number> result_trigger_async_id = v8::Number::New(isolate, result_context.trigger_async_id);
22
+ result->Set(context, async_id_key, result_async_id).ToChecked();
23
+ result->Set(context, trigger_async_id_key, result_trigger_async_id).ToChecked();
24
+
25
+ args.GetReturnValue().Set(result);
26
+ }
27
+
28
+ inline node::async_context FromAsyncContextObject(v8::Isolate* isolate,
29
+ v8::Local<v8::Object> async_context_object) {
30
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
31
+ double async_id = async_context_object
32
+ ->Get(context, v8::String::NewFromUtf8(isolate, "asyncId").ToLocalChecked()).ToLocalChecked()
33
+ .As<v8::Number>()->Value();
34
+ double trigger_async_id = async_context_object
35
+ ->Get(context, v8::String::NewFromUtf8(isolate, "triggerAsyncId").ToLocalChecked()).ToLocalChecked()
36
+ .As<v8::Number>()->Value();
37
+
38
+ return { async_id, trigger_async_id };
39
+ }
40
+
41
+ void EmitAsyncDestroy(const v8::FunctionCallbackInfo<v8::Value>& args) {
42
+ v8::Isolate* isolate = args.GetIsolate();
43
+ v8::Local<v8::Object> async_context_object = args[0].As<v8::Object>();
44
+ node::EmitAsyncDestroy(isolate, FromAsyncContextObject(isolate, async_context_object));
45
+ args.GetReturnValue().Set(v8::Undefined(isolate));
46
+ }
47
+
48
+ /* void OpenCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
49
+ v8::Isolate* isolate = args.GetIsolate();
50
+ v8::Local<v8::Object> resource = args[0].As<v8::Object>();
51
+ v8::Local<v8::Object> async_context_object = args[1].As<v8::Object>();
52
+
53
+ node::CallbackScope* callback_scope = new node::CallbackScope(
54
+ isolate, resource, FromAsyncContextObject(isolate, async_context_object));
55
+
56
+ args.GetReturnValue().Set(v8::BigInt::New(isolate, reinterpret_cast<int64_t>(callback_scope)));
57
+ }
58
+
59
+ void CloseCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
60
+ v8::Isolate* isolate = args.GetIsolate();
61
+ v8::Local<v8::BigInt> scope = args[0].As<v8::BigInt>();
62
+
63
+ node::CallbackScope* callback_scope = reinterpret_cast<node::CallbackScope*>(scope->Int64Value());
64
+ delete callback_scope;
65
+
66
+ args.GetReturnValue().Set(v8::Undefined(isolate));
67
+ } */
68
+
69
+ void MakeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) {
70
+ v8::Isolate* isolate = args.GetIsolate();
71
+ v8::Local<v8::Context> context = isolate->GetCurrentContext();
72
+ v8::Local<v8::Object> resource = args[0].As<v8::Object>();
73
+ v8::Local<v8::Function> cb = args[1].As<v8::Function>();
74
+ v8::Local<v8::Array> argv = args[2].As<v8::Array>();
75
+ v8::Local<v8::Object> async_context_object = args[3].As<v8::Object>();
76
+
77
+ uint32_t argc = argv->Length();
78
+ std::vector<v8::Local<v8::Value>> vec_argv(argc);
79
+ for (uint32_t i = 0; i < argc; ++i) {
80
+ vec_argv[i] = argv->Get(context, i).ToLocalChecked();
81
+ }
82
+
83
+ v8::MaybeLocal<v8::Value> ret = node::MakeCallback(isolate, resource, cb, argc, vec_argv.data(), FromAsyncContextObject(isolate, async_context_object));
84
+
85
+ args.GetReturnValue().Set(ret.FromMaybe(v8::Local<v8::Value>()));
86
+ }
87
+
88
+ }
89
+
90
+ NODE_MODULE_INIT() {
91
+ NODE_SET_METHOD(exports, "emitAsyncInit", EmitAsyncInit);
92
+ NODE_SET_METHOD(exports, "emitAsyncDestroy", EmitAsyncDestroy);
93
+ // NODE_SET_METHOD(exports, "openCallbackScope", OpenCallbackScope);
94
+ // NODE_SET_METHOD(exports, "closeCallbackScope", CloseCallbackScope);
95
+ NODE_SET_METHOD(exports, "makeCallback", MakeCallback);
96
+ }
97
+
98
+ }
package/src/napi.c ADDED
@@ -0,0 +1,130 @@
1
+ #include <assert.h>
2
+ #include <stdlib.h>
3
+ #include <node_api.h>
4
+
5
+ #ifdef NDEBUG
6
+ #define NAPI_ASSERT(the_call) (the_call)
7
+ #else
8
+ #define NAPI_ASSERT(the_call) (assert(napi_ok == (the_call)))
9
+ #endif
10
+
11
+ static napi_value napi_async_init_js(napi_env env, napi_callback_info info) {
12
+ napi_value argv[2];
13
+ size_t argc = 2;
14
+ napi_async_context ctx;
15
+ napi_value ret;
16
+ napi_status status;
17
+ napi_value status_value, ret_value;
18
+ napi_valuetype type;
19
+ napi_value resource = NULL;
20
+ NAPI_ASSERT(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
21
+ NAPI_ASSERT(napi_typeof(env, argv[0], &type));
22
+ if (type != napi_undefined && type != napi_null) {
23
+ resource = argv[0];
24
+ }
25
+ status = napi_async_init(env, resource, argv[1], &ctx);
26
+ NAPI_ASSERT(napi_create_object(env, &ret));
27
+ NAPI_ASSERT(napi_create_int32(env, status, &status_value));
28
+ NAPI_ASSERT(napi_create_bigint_int64(env, (int64_t) ctx, &ret_value));
29
+ NAPI_ASSERT(napi_set_named_property(env, ret, "status", status_value));
30
+ NAPI_ASSERT(napi_set_named_property(env, ret, "value", ret_value));
31
+ return ret;
32
+ }
33
+
34
+ static napi_value napi_async_destroy_js(napi_env env, napi_callback_info info) {
35
+ napi_value argv[1];
36
+ size_t argc = 1;
37
+ int64_t ctx;
38
+ napi_value ret;
39
+ napi_status status;
40
+ napi_value status_value, ret_value;
41
+ bool lossless;
42
+ NAPI_ASSERT(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
43
+ NAPI_ASSERT(napi_get_value_bigint_int64(env, *argv, &ctx, &lossless));
44
+ status = napi_async_destroy(env, (napi_async_context) ctx);
45
+ NAPI_ASSERT(napi_create_object(env, &ret));
46
+ NAPI_ASSERT(napi_create_int32(env, status, &status_value));
47
+ NAPI_ASSERT(napi_get_undefined(env, &ret_value));
48
+ NAPI_ASSERT(napi_set_named_property(env, ret, "status", status_value));
49
+ NAPI_ASSERT(napi_set_named_property(env, ret, "value", ret_value));
50
+ return ret;
51
+ }
52
+
53
+ static napi_value napi_make_callback_js(napi_env env, napi_callback_info info) {
54
+ napi_value argv[4];
55
+ size_t argc = 4;
56
+ int64_t ctx = 0;
57
+ napi_value ret;
58
+ napi_status status;
59
+ napi_value status_value, ret_value;
60
+ bool lossless;
61
+ napi_value arr;
62
+ uint32_t len = 0;
63
+ napi_value* callback_argv = NULL;
64
+ napi_value err = NULL;
65
+
66
+ NAPI_ASSERT(napi_get_cb_info(env, info, &argc, argv, NULL, NULL));
67
+ NAPI_ASSERT(napi_get_value_bigint_int64(env, *argv, &ctx, &lossless));
68
+ arr = argv[3];
69
+ NAPI_ASSERT(napi_get_array_length(env, arr, &len));
70
+ if (len != 0) {
71
+ callback_argv = (napi_value*) malloc(len * sizeof(napi_value));
72
+ }
73
+ for (uint32_t i = 0; i < len; ++i) {
74
+ NAPI_ASSERT(napi_get_element(env, arr, i, callback_argv + i));
75
+ }
76
+ NAPI_ASSERT(napi_get_and_clear_last_exception(env, &err));
77
+ err = NULL;
78
+ status = napi_make_callback(env, (napi_async_context) ctx, argv[1], argv[2], len, callback_argv, &ret_value);
79
+ if (callback_argv != NULL) {
80
+ free(callback_argv);
81
+ }
82
+ if (status == napi_pending_exception) {
83
+ NAPI_ASSERT(napi_get_and_clear_last_exception(env, &err));
84
+ }
85
+ NAPI_ASSERT(napi_create_object(env, &ret));
86
+ NAPI_ASSERT(napi_create_int32(env, status, &status_value));
87
+ NAPI_ASSERT(napi_set_named_property(env, ret, "status", status_value));
88
+ if (err != NULL) {
89
+ NAPI_ASSERT(napi_get_undefined(env, &ret_value));
90
+ NAPI_ASSERT(napi_set_named_property(env, ret, "value", ret_value));
91
+ NAPI_ASSERT(napi_set_named_property(env, ret, "error", err));
92
+ } else {
93
+ NAPI_ASSERT(napi_set_named_property(env, ret, "value", ret_value));
94
+ }
95
+ return ret;
96
+ }
97
+
98
+ /* static napi_value napi_open_callback_scope_js(napi_env env, napi_callback_info info) {
99
+ napi_value argv[2];
100
+ size_t argc = 2;
101
+ napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
102
+ bool lossless;
103
+ int64_t ctx;
104
+ napi_get_value_bigint_int64(env, argv[1], &ctx, &lossless);
105
+ napi_callback_scope scope;
106
+ napi_open_callback_scope(env, argv[0], (napi_async_context) ctx, &scope);
107
+ napi_value ret;
108
+ napi_create_bigint_int64(env, (int64_t) scope, &ret);
109
+ return ret;
110
+ } */
111
+
112
+ NAPI_MODULE_INIT() {
113
+ napi_value ai;
114
+ NAPI_ASSERT(napi_create_function(env, "asyncInit", NAPI_AUTO_LENGTH, napi_async_init_js, NULL, &ai));
115
+ NAPI_ASSERT(napi_set_named_property(env, exports, "asyncInit", ai));
116
+
117
+ napi_value ad;
118
+ NAPI_ASSERT(napi_create_function(env, "asyncDestroy", NAPI_AUTO_LENGTH, napi_async_destroy_js, NULL, &ad));
119
+ NAPI_ASSERT(napi_set_named_property(env, exports, "asyncDestroy", ad));
120
+
121
+ napi_value mc;
122
+ NAPI_ASSERT(napi_create_function(env, "makeCallback", NAPI_AUTO_LENGTH, napi_make_callback_js, NULL, &mc));
123
+ NAPI_ASSERT(napi_set_named_property(env, exports, "makeCallback", mc));
124
+
125
+ /* napi_value ocs;
126
+ napi_create_function(env, "napiOpenCallbackScope", NAPI_AUTO_LENGTH, napi_open_callback_scope_js, NULL, &ocs);
127
+ napi_set_named_property(env, exports, "napiOpenCallbackScope", ocs); */
128
+
129
+ return exports;
130
+ }