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,60 @@
#include <node_api.h>
#include <assert.h>
#include "../../js-native-api/common.h"
#define MAX_ARGUMENTS 10
#define RESERVED_ARGS 3
static napi_value MakeCallback(napi_env env, napi_callback_info info) {
size_t argc = MAX_ARGUMENTS;
size_t n;
napi_value args[MAX_ARGUMENTS];
// NOLINTNEXTLINE (readability/null_usage)
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc > 0, "Wrong number of arguments");
napi_value resource = args[0];
napi_value recv = args[1];
napi_value func = args[2];
napi_value argv[MAX_ARGUMENTS - RESERVED_ARGS];
for (n = RESERVED_ARGS; n < argc; n += 1) {
argv[n - RESERVED_ARGS] = args[n];
}
napi_valuetype func_type;
NODE_API_CALL(env, napi_typeof(env, func, &func_type));
napi_value resource_name;
NODE_API_CALL(env, napi_create_string_utf8(
env, "test", NAPI_AUTO_LENGTH, &resource_name));
napi_async_context context;
NODE_API_CALL(env, napi_async_init(env, resource, resource_name, &context));
napi_value result;
if (func_type == napi_function) {
NODE_API_CALL(env, napi_make_callback(
env, context, recv, func, argc - RESERVED_ARGS, argv, &result));
} else {
NODE_API_ASSERT(env, false, "Unexpected argument type");
}
NODE_API_CALL(env, napi_async_destroy(env, context));
return result;
}
static
napi_value Init(napi_env env, napi_value exports) {
napi_value fn;
NODE_API_CALL(env, napi_create_function(
// NOLINTNEXTLINE (readability/null_usage)
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
NODE_API_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
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,53 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const async_hooks = require('async_hooks');
const binding = require(`./build/${common.buildType}/binding`);
const makeCallback = binding.makeCallback;
// Check async hooks integration using async context.
const hook_result = {
id: null,
init_called: false,
before_called: false,
after_called: false,
destroy_called: false,
};
const test_hook = async_hooks.createHook({
init: (id, type) => {
if (type === 'test') {
hook_result.id = id;
hook_result.init_called = true;
}
},
before: (id) => {
if (id === hook_result.id) hook_result.before_called = true;
},
after: (id) => {
if (id === hook_result.id) hook_result.after_called = true;
},
destroy: (id) => {
if (id === hook_result.id) hook_result.destroy_called = true;
},
});
test_hook.enable();
/**
* Resource should be able to be arbitrary objects without special internal
* slots. Testing with plain object here.
*/
const resource = {};
makeCallback(resource, process, function cb() {
assert.strictEqual(this, process);
assert.strictEqual(async_hooks.executionAsyncResource(), resource);
});
assert.strictEqual(hook_result.init_called, true);
assert.strictEqual(hook_result.before_called, true);
assert.strictEqual(hook_result.after_called, true);
setImmediate(() => {
assert.strictEqual(hook_result.destroy_called, true);
test_hook.disable();
});

View File

@ -0,0 +1,86 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const vm = require('vm');
const binding = require(`./build/${common.buildType}/binding`);
const makeCallback = binding.makeCallback;
function myMultiArgFunc(arg1, arg2, arg3) {
assert.strictEqual(arg1, 1);
assert.strictEqual(arg2, 2);
assert.strictEqual(arg3, 3);
return 42;
}
/**
* Resource should be able to be arbitrary objects without special internal
* slots. Testing with plain object here.
*/
const resource = {};
assert.strictEqual(makeCallback(resource, process, common.mustCall(function() {
assert.strictEqual(arguments.length, 0);
assert.strictEqual(this, process);
return 42;
})), 42);
assert.strictEqual(makeCallback(resource, process, common.mustCall(function(x) {
assert.strictEqual(arguments.length, 1);
assert.strictEqual(this, process);
assert.strictEqual(x, 1337);
return 42;
}), 1337), 42);
assert.strictEqual(makeCallback(resource, this,
common.mustCall(myMultiArgFunc), 1, 2, 3), 42);
// TODO(node-api): napi_make_callback needs to support
// strings passed for the func argument
//
// const recv = {
// one: common.mustCall(function() {
// assert.strictEqual(0, arguments.length);
// assert.strictEqual(this, recv);
// return 42;
// }),
// two: common.mustCall(function(x) {
// assert.strictEqual(1, arguments.length);
// assert.strictEqual(this, recv);
// assert.strictEqual(x, 1337);
// return 42;
// }),
// };
//
// assert.strictEqual(makeCallback(recv, 'one'), 42);
// assert.strictEqual(makeCallback(recv, 'two', 1337), 42);
//
// // Check that callbacks on a receiver from a different context works.
// const foreignObject = vm.runInNewContext('({ fortytwo() { return 42; } })');
// assert.strictEqual(makeCallback(foreignObject, 'fortytwo'), 42);
// Check that the callback is made in the context of the receiver.
const target = vm.runInNewContext(`
(function($Object) {
if (Object === $Object)
throw new Error('bad');
return Object;
})
`);
assert.notStrictEqual(makeCallback(resource, process, target, Object), Object);
// Runs in inner context.
const forward = vm.runInNewContext(`
(function(forward) {
return forward(Object);
})
`);
// Runs in outer context.
function endpoint($Object) {
if (Object === $Object)
throw new Error('bad');
return Object;
}
assert.strictEqual(makeCallback(resource, process, forward, endpoint), Object);