Upload Kmake

This commit is contained in:
Gorochu
2026-05-26 23:36:42 -07:00
parent ba051b2f74
commit 555ec72358
41615 changed files with 13344630 additions and 1 deletions

View File

@ -0,0 +1,119 @@
#include <stdlib.h>
#include "node_api.h"
#include "uv.h"
#include "../../js-native-api/common.h"
static napi_value RunInCallbackScope(napi_env env, napi_callback_info info) {
size_t argc;
napi_value args[3];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
NODE_API_ASSERT(env, argc == 3 , "Wrong number of arguments");
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
napi_valuetype valuetype;
NODE_API_CALL(env, napi_typeof(env, args[0], &valuetype));
NODE_API_ASSERT(env, valuetype == napi_object,
"Wrong type of arguments. Expects an object as first argument.");
NODE_API_CALL(env, napi_typeof(env, args[1], &valuetype));
NODE_API_ASSERT(env, valuetype == napi_string,
"Wrong type of arguments. Expects a string as second argument.");
NODE_API_CALL(env, napi_typeof(env, args[2], &valuetype));
NODE_API_ASSERT(env, valuetype == napi_function,
"Wrong type of arguments. Expects a function as third argument.");
napi_async_context context;
NODE_API_CALL(env, napi_async_init(env, args[0], args[1], &context));
napi_callback_scope scope = NULL;
NODE_API_CALL(env,
napi_open_callback_scope(env, args[0], context, &scope));
// If the function has an exception pending after the call that is ok
// so we don't use NODE_API_CALL as we must close the callback scope
// regardless.
napi_value result = NULL;
napi_status function_call_result =
napi_call_function(env, args[0], args[2], 0, NULL, &result);
if (function_call_result != napi_ok) {
GET_AND_THROW_LAST_ERROR((env));
}
NODE_API_CALL(env, napi_close_callback_scope(env, scope));
NODE_API_CALL(env, napi_async_destroy(env, context));
return result;
}
static napi_env shared_env = NULL;
static napi_deferred deferred = NULL;
static void Callback(uv_work_t* req, int ignored) {
napi_env env = shared_env;
napi_handle_scope handle_scope = NULL;
NODE_API_CALL_RETURN_VOID(env, napi_open_handle_scope(env, &handle_scope));
napi_value resource_name;
NODE_API_CALL_RETURN_VOID(env, napi_create_string_utf8(
env, "test", NAPI_AUTO_LENGTH, &resource_name));
napi_async_context context;
NODE_API_CALL_RETURN_VOID(env,
napi_async_init(env, NULL, resource_name, &context));
napi_value resource_object;
NODE_API_CALL_RETURN_VOID(env, napi_create_object(env, &resource_object));
napi_value undefined_value;
NODE_API_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined_value));
napi_callback_scope scope = NULL;
NODE_API_CALL_RETURN_VOID(env,
napi_open_callback_scope(env, resource_object, context, &scope));
NODE_API_CALL_RETURN_VOID(env,
napi_resolve_deferred(env, deferred, undefined_value));
NODE_API_CALL_RETURN_VOID(env, napi_close_callback_scope(env, scope));
NODE_API_CALL_RETURN_VOID(env, napi_close_handle_scope(env, handle_scope));
NODE_API_CALL_RETURN_VOID(env, napi_async_destroy(env, context));
free(req);
}
static void NoopWork(uv_work_t* work) { (void) work; }
static napi_value TestResolveAsync(napi_env env, napi_callback_info info) {
napi_value promise = NULL;
if (deferred == NULL) {
shared_env = env;
NODE_API_CALL(env, napi_create_promise(env, &deferred, &promise));
uv_loop_t* loop = NULL;
NODE_API_CALL(env, napi_get_uv_event_loop(env, &loop));
uv_work_t* req = malloc(sizeof(*req));
uv_queue_work(loop,
req,
NoopWork,
Callback);
}
return promise;
}
static napi_value Init(napi_env env, napi_value exports) {
napi_property_descriptor descriptors[] = {
DECLARE_NODE_API_PROPERTY("runInCallbackScope", RunInCallbackScope),
DECLARE_NODE_API_PROPERTY("testResolveAsync", TestResolveAsync)
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors));
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)

View File

@ -0,0 +1,9 @@
{
'targets': [
{
'target_name': 'binding',
'defines': [ 'V8_DEPRECATION_WARNINGS=1' ],
'sources': [ 'binding.c' ]
}
]
}

View File

@ -0,0 +1,39 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
// The async_hook that we enable would register the process.emitWarning()
// call from loading the N-API addon as asynchronous activity because
// it contains a process.nextTick() call. Monkey patch it to be a no-op
// before we load the addon in order to avoid this.
process.emitWarning = () => {};
const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
const expectedResource = {};
const expectedResourceType = 'test-resource';
let insideHook = false;
let expectedId;
async_hooks.createHook({
init: common.mustCall((id, type, triggerAsyncId, resource) => {
if (type !== expectedResourceType) {
return;
}
assert.strictEqual(resource, expectedResource);
expectedId = id;
}),
before: common.mustCall((id) => {
assert.strictEqual(id, expectedId);
insideHook = true;
}),
after: common.mustCall((id) => {
assert.strictEqual(id, expectedId);
insideHook = false;
}),
}).enable();
runInCallbackScope(expectedResource, expectedResourceType, () => {
assert(insideHook);
});

View File

@ -0,0 +1,6 @@
'use strict';
const common = require('../../common');
const { testResolveAsync } = require(`./build/${common.buildType}/binding`);
testResolveAsync().then(common.mustCall());

View File

@ -0,0 +1,17 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const { runInCallbackScope } = require(`./build/${common.buildType}/binding`);
assert.strictEqual(runInCallbackScope({}, 'test-resource', () => 42), 42);
{
process.once('uncaughtException', common.mustCall((err) => {
assert.strictEqual(err.message, 'foo');
}));
runInCallbackScope({}, 'test-resource', () => {
throw new Error('foo');
});
}