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,10 @@
{
"targets": [
{
"target_name": "test_properties",
"sources": [
"test_properties.c"
]
}
]
}

View File

@ -0,0 +1,68 @@
'use strict';
const common = require('../../common');
const assert = require('assert');
const readonlyErrorRE =
/^TypeError: Cannot assign to read only property '.*' of object '#<Object>'$/;
const getterOnlyErrorRE =
/^TypeError: Cannot set property .* of #<Object> which has only a getter$/;
// Testing api calls for defining properties
const test_object = require(`./build/${common.buildType}/test_properties`);
assert.strictEqual(test_object.echo('hello'), 'hello');
test_object.readwriteValue = 1;
assert.strictEqual(test_object.readwriteValue, 1);
test_object.readwriteValue = 2;
assert.strictEqual(test_object.readwriteValue, 2);
assert.throws(() => { test_object.readonlyValue = 3; }, readonlyErrorRE);
assert.ok(test_object.hiddenValue);
// Properties with napi_enumerable attribute should be enumerable.
const propertyNames = [];
for (const name in test_object) {
propertyNames.push(name);
}
assert.ok(propertyNames.includes('echo'));
assert.ok(propertyNames.includes('readwriteValue'));
assert.ok(propertyNames.includes('readonlyValue'));
assert.ok(!propertyNames.includes('hiddenValue'));
assert.ok(propertyNames.includes('NameKeyValue'));
assert.ok(!propertyNames.includes('readwriteAccessor1'));
assert.ok(!propertyNames.includes('readwriteAccessor2'));
assert.ok(!propertyNames.includes('readonlyAccessor1'));
assert.ok(!propertyNames.includes('readonlyAccessor2'));
// Validate properties created with symbol
const propertySymbols = Object.getOwnPropertySymbols(test_object);
assert.strictEqual(propertySymbols[0].toString(), 'Symbol(NameKeySymbol)');
assert.strictEqual(propertySymbols[1].toString(), 'Symbol()');
assert.strictEqual(propertySymbols[2], Symbol.for('NameKeySymbolFor'));
// The napi_writable attribute should be ignored for accessors.
const readwriteAccessor1Descriptor =
Object.getOwnPropertyDescriptor(test_object, 'readwriteAccessor1');
const readonlyAccessor1Descriptor =
Object.getOwnPropertyDescriptor(test_object, 'readonlyAccessor1');
assert.ok(readwriteAccessor1Descriptor.get != null);
assert.ok(readwriteAccessor1Descriptor.set != null);
assert.ok(readwriteAccessor1Descriptor.value === undefined);
assert.ok(readonlyAccessor1Descriptor.get != null);
assert.ok(readonlyAccessor1Descriptor.set === undefined);
assert.ok(readonlyAccessor1Descriptor.value === undefined);
test_object.readwriteAccessor1 = 1;
assert.strictEqual(test_object.readwriteAccessor1, 1);
assert.strictEqual(test_object.readonlyAccessor1, 1);
assert.throws(() => { test_object.readonlyAccessor1 = 3; }, getterOnlyErrorRE);
test_object.readwriteAccessor2 = 2;
assert.strictEqual(test_object.readwriteAccessor2, 2);
assert.strictEqual(test_object.readonlyAccessor2, 2);
assert.throws(() => { test_object.readonlyAccessor2 = 3; }, getterOnlyErrorRE);
assert.strictEqual(test_object.hasNamedProperty(test_object, 'echo'), true);
assert.strictEqual(test_object.hasNamedProperty(test_object, 'hiddenValue'),
true);
assert.strictEqual(test_object.hasNamedProperty(test_object, 'doesnotexist'),
false);

View File

@ -0,0 +1,113 @@
#define NAPI_VERSION 9
#include <js_native_api.h>
#include "../common.h"
#include "../entry_point.h"
static double value_ = 1;
static napi_value GetValue(napi_env env, napi_callback_info info) {
size_t argc = 0;
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, NULL, NULL, NULL));
NODE_API_ASSERT(env, argc == 0, "Wrong number of arguments");
napi_value number;
NODE_API_CALL(env, napi_create_double(env, value_, &number));
return number;
}
static napi_value SetValue(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");
NODE_API_CALL(env, napi_get_value_double(env, args[0], &value_));
return NULL;
}
static napi_value Echo(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");
return args[0];
}
static napi_value HasNamedProperty(napi_env env, napi_callback_info info) {
size_t argc = 2;
napi_value args[2];
NODE_API_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL));
NODE_API_ASSERT(env, argc == 2, "Wrong number of arguments");
// Extract the name of the property to check
char buffer[128];
size_t copied;
NODE_API_CALL(env,
napi_get_value_string_utf8(env, args[1], buffer, sizeof(buffer), &copied));
// do the check and create the boolean return value
bool value;
napi_value result;
NODE_API_CALL(env, napi_has_named_property(env, args[0], buffer, &value));
NODE_API_CALL(env, napi_get_boolean(env, value, &result));
return result;
}
EXTERN_C_START
napi_value Init(napi_env env, napi_value exports) {
napi_value number;
NODE_API_CALL(env, napi_create_double(env, value_, &number));
napi_value name_value;
NODE_API_CALL(env,
napi_create_string_utf8(
env, "NameKeyValue", NAPI_AUTO_LENGTH, &name_value));
napi_value symbol_description;
napi_value name_symbol;
NODE_API_CALL(env,
napi_create_string_utf8(
env, "NameKeySymbol", NAPI_AUTO_LENGTH, &symbol_description));
NODE_API_CALL(env,
napi_create_symbol(env, symbol_description, &name_symbol));
napi_value name_symbol_descriptionless;
NODE_API_CALL(env,
napi_create_symbol(env, NULL, &name_symbol_descriptionless));
napi_value name_symbol_for;
NODE_API_CALL(env, node_api_symbol_for(env,
"NameKeySymbolFor",
NAPI_AUTO_LENGTH,
&name_symbol_for));
napi_property_descriptor properties[] = {
{ "echo", 0, Echo, 0, 0, 0, napi_enumerable, 0 },
{ "readwriteValue", 0, 0, 0, 0, number, napi_enumerable | napi_writable, 0 },
{ "readonlyValue", 0, 0, 0, 0, number, napi_enumerable, 0},
{ "hiddenValue", 0, 0, 0, 0, number, napi_default, 0},
{ NULL, name_value, 0, 0, 0, number, napi_enumerable, 0},
{ NULL, name_symbol, 0, 0, 0, number, napi_enumerable, 0},
{ NULL, name_symbol_descriptionless, 0, 0, 0, number, napi_enumerable, 0},
{ NULL, name_symbol_for, 0, 0, 0, number, napi_enumerable, 0},
{ "readwriteAccessor1", 0, 0, GetValue, SetValue, 0, napi_default, 0},
{ "readwriteAccessor2", 0, 0, GetValue, SetValue, 0, napi_writable, 0},
{ "readonlyAccessor1", 0, 0, GetValue, NULL, 0, napi_default, 0},
{ "readonlyAccessor2", 0, 0, GetValue, NULL, 0, napi_writable, 0},
{ "hasNamedProperty", 0, HasNamedProperty, 0, 0, 0, napi_default, 0 },
};
NODE_API_CALL(env, napi_define_properties(
env, exports, sizeof(properties) / sizeof(*properties), properties));
return exports;
}
EXTERN_C_END