diff options
Diffstat (limited to 'test/js-native-api/4_object_factory')
-rw-r--r-- | test/js-native-api/4_object_factory/binding.c | 23 | ||||
-rw-r--r-- | test/js-native-api/4_object_factory/binding.gyp | 11 | ||||
-rw-r--r-- | test/js-native-api/4_object_factory/test.js | 8 |
3 files changed, 42 insertions, 0 deletions
diff --git a/test/js-native-api/4_object_factory/binding.c b/test/js-native-api/4_object_factory/binding.c new file mode 100644 index 0000000000..5e1403a892 --- /dev/null +++ b/test/js-native-api/4_object_factory/binding.c @@ -0,0 +1,23 @@ +#include <js_native_api.h> +#include "../common.h" + +static napi_value CreateObject(napi_env env, napi_callback_info info) { + size_t argc = 1; + napi_value args[1]; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, NULL, NULL)); + + napi_value obj; + NAPI_CALL(env, napi_create_object(env, &obj)); + + NAPI_CALL(env, napi_set_named_property(env, obj, "msg", args[0])); + + return obj; +} + +EXTERN_C_START +napi_value Init(napi_env env, napi_value exports) { + NAPI_CALL(env, + napi_create_function(env, "exports", -1, CreateObject, NULL, &exports)); + return exports; +} +EXTERN_C_END diff --git a/test/js-native-api/4_object_factory/binding.gyp b/test/js-native-api/4_object_factory/binding.gyp new file mode 100644 index 0000000000..2a144bab42 --- /dev/null +++ b/test/js-native-api/4_object_factory/binding.gyp @@ -0,0 +1,11 @@ +{ + "targets": [ + { + "target_name": "binding", + "sources": [ + "../entry_point.c", + "binding.c" + ] + } + ] +} diff --git a/test/js-native-api/4_object_factory/test.js b/test/js-native-api/4_object_factory/test.js new file mode 100644 index 0000000000..15313c1547 --- /dev/null +++ b/test/js-native-api/4_object_factory/test.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../../common'); +const assert = require('assert'); +const addon = require(`./build/${common.buildType}/binding`); + +const obj1 = addon('hello'); +const obj2 = addon('world'); +assert.strictEqual(`${obj1.msg} ${obj2.msg}`, 'hello world'); |