Skip to content

Commit 9b20b0b

Browse files
committed
WIP
1 parent 850eece commit 9b20b0b

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/native/index.cpp

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#define NAPI_VERSION 3
22

3+
#include <string.h>
34
#include <node_api.h>
45
#include <napi-macros.h>
56

@@ -58,7 +59,6 @@ napi_value addOne (napi_env env, napi_callback_info info) {
5859
return return_int32;
5960
}
6061

61-
6262
/**
6363
* Using n-api macros instead removes alot of boilerplate
6464
* This function is basically the same as `addOne`
@@ -90,7 +90,7 @@ NAPI_METHOD(createArr) {
9090
NAPI_METHOD(createObj) {
9191
napi_value obj;
9292
NAPI_STATUS_THROWS(napi_create_object(env, &obj))
93-
// we need to set a string
93+
// Set a value on the obj
9494
napi_value value;
9595
NAPI_STATUS_THROWS(napi_create_string_utf8(
9696
env,
@@ -104,9 +104,44 @@ NAPI_METHOD(createObj) {
104104

105105
/**
106106
* Takes an object and sets a property on it
107+
* Object must be { key1: "some string" }
108+
* Will return object new property key2
107109
*/
108-
// NAPI_METHOD(setProperty) {
109-
// }
110+
NAPI_METHOD(setProperty) {
111+
NAPI_ARGV(1)
112+
// Expect an object argument
113+
napi_value obj = argv[0];
114+
// Extract property key1, and expect it to be a string
115+
napi_value keyValue;
116+
NAPI_STATUS_THROWS(napi_get_named_property(env, obj, "key1", &keyValue))
117+
// Find the JS length of the string
118+
size_t keyValueLength;
119+
NAPI_STATUS_THROWS(napi_get_value_string_utf8(
120+
env,
121+
keyValue,
122+
NULL,
123+
0,
124+
&keyValueLength
125+
))
126+
// Convert the value to a C string
127+
NAPI_UTF8(keyValueString, keyValueLength + 1, keyValue)
128+
keyValueString[keyValueLength + 1] = '\0';
129+
// Concatenate the string
130+
char fullString[8 + keyValueLength + 1] = {0};
131+
strcat(fullString, "initial ");
132+
strcat(fullString, keyValueString);
133+
// Convert the C string back to JS string
134+
napi_value fullStringJS;
135+
NAPI_STATUS_THROWS(napi_create_string_utf8(
136+
env,
137+
fullString,
138+
NAPI_AUTO_LENGTH,
139+
&fullStringJS
140+
))
141+
// Set the the new string as new of property key2
142+
NAPI_STATUS_THROWS(napi_set_named_property(env, obj, "key2", fullStringJS));
143+
return obj;
144+
}
110145

111146
/**
112147
* All exported functions
@@ -138,4 +173,5 @@ NAPI_INIT() {
138173
NAPI_EXPORT_FUNCTION(timesTwo)
139174
NAPI_EXPORT_FUNCTION(createArr)
140175
NAPI_EXPORT_FUNCTION(createObj)
176+
NAPI_EXPORT_FUNCTION(setProperty)
141177
}

tests/lib/native.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ describe('native bindings', () => {
2323
// https://github.com/facebook/jest/issues/12814
2424
// expect(native.createObj()).toStrictEqual({ key: 'hello world' });
2525
});
26+
test('set property', () => {
27+
expect(
28+
native.setProperty({ key1: 'value1' })
29+
).toStrictEqual({ key1: 'value1', key2: 'initial value1'});
30+
});
2631
});

0 commit comments

Comments
 (0)