Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: passing NULL to napi_define_class #62

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ add_test("bigint" "./bigint/binding.c" ON OFF "")
add_test("fnwrap" "./fnwrap/myobject.cc;./fnwrap/binding.cc" ON OFF "")
add_test("passwrap" "./passwrap/myobject.cc;./passwrap/binding.cc" ON OFF "")
add_test("array" "./array/binding.c" ON OFF "")
add_test("constructor" "./constructor/binding.c" ON OFF "")
add_test("constructor" "./constructor/binding.c;./constructor/test_null.c" ON OFF "")
add_test("conversion" "./conversion/test_conversions.c;./conversion/test_null.c" ON OFF "")
add_test("dataview" "./dataview/binding.c" ON OFF "")
add_test("date" "./date/binding.c" ON OFF "")
Expand Down
3 changes: 3 additions & 0 deletions packages/test/constructor/binding.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <js_native_api.h>
#include "../common.h"
#include "test_null.h"

static double value_ = 1;
static double static_value_ = 10;
Expand Down Expand Up @@ -191,6 +192,8 @@ napi_value Init(napi_env env, napi_value exports) {
NAPI_CALL(env, napi_define_class(env, "MyObject", NAPI_AUTO_LENGTH, New,
NULL, sizeof(properties)/sizeof(*properties), properties, &cons));

init_test_null(env, cons);

return cons;
}
EXTERN_C_END
20 changes: 20 additions & 0 deletions packages/test/constructor/constructor-null.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable camelcase */
'use strict'
const assert = require('assert')
const { load } = require('../util')

// Test passing NULL to object-related N-APIs.
module.exports = load('constructor').then(({ testNull }) => {
const expectedResult = {
envIsNull: 'Invalid argument',
nameIsNull: 'Invalid argument',
lengthIsZero: 'napi_ok',
nativeSideIsNull: 'Invalid argument',
dataIsNull: 'napi_ok',
propsLengthIsZero: 'napi_ok',
propsIsNull: 'Invalid argument',
resultIsNull: 'Invalid argument'
}

assert.deepStrictEqual(testNull.testDefineClass(), expectedResult)
})
111 changes: 111 additions & 0 deletions packages/test/constructor/test_null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <js_native_api.h>

#include "../common.h"
#include "test_null.h"

static int some_data = 0;

static napi_value TestConstructor(napi_env env, napi_callback_info info) {
return NULL;
}

static napi_value TestDefineClass(napi_env env, napi_callback_info info) {
napi_value return_value, cons;

const napi_property_descriptor prop =
DECLARE_NAPI_PROPERTY("testConstructor", TestConstructor);

NAPI_CALL(env, napi_create_object(env, &return_value));
add_returned_status(env,
"envIsNull",
return_value,
"Invalid argument",
napi_invalid_arg,
napi_define_class(NULL,
"TestClass",
NAPI_AUTO_LENGTH,
TestConstructor,
&some_data,
1,
&prop,
&cons));

napi_define_class(env,
NULL,
NAPI_AUTO_LENGTH,
TestConstructor,
&some_data,
1,
&prop,
&cons);
add_last_status(env, "nameIsNull", return_value);

napi_define_class(
env, "TestClass", 0, TestConstructor, &some_data, 1, &prop, &cons);
add_last_status(env, "lengthIsZero", return_value);

napi_define_class(
env, "TestClass", NAPI_AUTO_LENGTH, NULL, &some_data, 1, &prop, &cons);
add_last_status(env, "nativeSideIsNull", return_value);

napi_define_class(env,
"TestClass",
NAPI_AUTO_LENGTH,
TestConstructor,
NULL,
1,
&prop,
&cons);
add_last_status(env, "dataIsNull", return_value);

napi_define_class(env,
"TestClass",
NAPI_AUTO_LENGTH,
TestConstructor,
&some_data,
0,
&prop,
&cons);
add_last_status(env, "propsLengthIsZero", return_value);

napi_define_class(env,
"TestClass",
NAPI_AUTO_LENGTH,
TestConstructor,
&some_data,
1,
NULL,
&cons);
add_last_status(env, "propsIsNull", return_value);

napi_define_class(env,
"TestClass",
NAPI_AUTO_LENGTH,
TestConstructor,
&some_data,
1,
&prop,
NULL);
add_last_status(env, "resultIsNull", return_value);

return return_value;
}

void init_test_null(napi_env env, napi_value exports) {
napi_value test_null;

const napi_property_descriptor test_null_props[] = {
DECLARE_NAPI_PROPERTY("testDefineClass", TestDefineClass),
};

NAPI_CALL_RETURN_VOID(env, napi_create_object(env, &test_null));
NAPI_CALL_RETURN_VOID(
env,
napi_define_properties(env,
test_null,
sizeof(test_null_props) / sizeof(*test_null_props),
test_null_props));

NAPI_CALL_RETURN_VOID(
env, napi_set_named_property(env, exports, "testNull", test_null));
}
8 changes: 8 additions & 0 deletions packages/test/constructor/test_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_
#define TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_

#include <js_native_api.h>

void init_test_null(napi_env env, napi_value exports);

#endif // TEST_JS_NATIVE_API_TEST_OBJECT_TEST_NULL_H_