-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.c
More file actions
41 lines (31 loc) · 1.04 KB
/
Copy pathhello.c
File metadata and controls
41 lines (31 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <assert.h>
#include <node_api.h>
#include <stdio.h>
typedef struct Vector2 {
float x; // Vector x component
float y; // Vector y component
} Vector2;
static napi_value hello(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value argv[argc];
napi_get_cb_info(env, info, &argc, argv, NULL, NULL);
if (argc < 1) {
napi_throw_error(env, "EINVAL", "Too few arguments to hello()");
return NULL;
}
int64_t* ptr = 0;
napi_get_value_int64(env, argv[0], ptr);
Vector2* v = (Vector2*) ptr;
printf("Vector2 in C: (%f, %f)\n", v->x, v->y);
return NULL;
}
#define DECLARE_NAPI_METHOD(name, func) \
{ name, 0, func, 0, 0, 0, napi_default, 0 }
static napi_value Init(napi_env env, napi_value exports) {
napi_status status;
napi_property_descriptor desc = DECLARE_NAPI_METHOD("hello", hello);
status = napi_define_properties(env, exports, 1, &desc);
assert(status == napi_ok);
return exports;
}
NAPI_MODULE(NODE_GYP_MODULE_NAME, Init)