FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/test/js-native-api/test_instance_data/test_instance_data.c at main · MoLow/node · GitHub
MoLow
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
2
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
test
/
js-native-api
/
test_instance_data
/
test_instance_data.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
96 lines (78 loc) · 2.78 KB
Breadcrumbs
node
/
test
/
js-native-api
/
test_instance_data
/
test_instance_data.c
Copy path
File metadata and controls
96 lines (78 loc) · 2.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include
<js_native_api.h>
#include
<stdio.h>
#include
<stdlib.h>
#include
"../common.h"
#include
"../entry_point.h"
typedef
struct
{
size_t
value
;
bool
print
;
napi_ref
js_cb_ref
;
}
AddonData
;
static
napi_value
Increment
(
napi_env
env
,
napi_callback_info
info
) {
AddonData
*
data
;
napi_value
result
;
NODE_API_CALL
(
env
,
napi_get_instance_data
(
env
, (
void
*
*
)
&
data
));
NODE_API_CALL
(
env
,
napi_create_uint32
(
env
,
++
data
->
value
,
&
result
));
return
result
;
}
static
void
DeleteAddonData
(
napi_env
env
,
void
*
raw_data
,
void
*
hint
) {
AddonData
*
data
=
raw_data
;
if
(
data
->
print
) {
printf
(
"deleting addon data\n"
);
}
if
(
data
->
js_cb_ref
!=
NULL
) {
NODE_API_CALL_RETURN_VOID
(
env
,
napi_delete_reference
(
env
,
data
->
js_cb_ref
));
}
free
(
data
);
}
static
napi_value
SetPrintOnDelete
(
napi_env
env
,
napi_callback_info
info
) {
AddonData
*
data
;
NODE_API_CALL
(
env
,
napi_get_instance_data
(
env
, (
void
*
*
)
&
data
));
data
->
print
=
true;
return
NULL
;
}
static
void
TestFinalizer
(
napi_env
env
,
void
*
raw_data
,
void
*
hint
) {
(
void
)
raw_data
;
(
void
)
hint
;
AddonData
*
data
;
NODE_API_CALL_RETURN_VOID
(
env
,
napi_get_instance_data
(
env
, (
void
*
*
)
&
data
));
napi_value
js_cb
,
undefined
;
NODE_API_CALL_RETURN_VOID
(
env
,
napi_get_reference_value
(
env
,
data
->
js_cb_ref
,
&
js_cb
));
NODE_API_CALL_RETURN_VOID
(
env
,
napi_get_undefined
(
env
,
&
undefined
));
NODE_API_CALL_RETURN_VOID
(
env
,
napi_call_function
(
env
,
undefined
,
js_cb
,
0
,
NULL
,
NULL
));
NODE_API_CALL_RETURN_VOID
(
env
,
napi_delete_reference
(
env
,
data
->
js_cb_ref
));
data
->
js_cb_ref
=
NULL
;
}
static
napi_value
ObjectWithFinalizer
(
napi_env
env
,
napi_callback_info
info
) {
AddonData
*
data
;
napi_value
result
,
js_cb
;
size_t
argc
=
1
;
NODE_API_CALL
(
env
,
napi_get_instance_data
(
env
, (
void
*
*
)
&
data
));
NODE_API_ASSERT
(
env
,
data
->
js_cb_ref
==
NULL
,
"reference must be NULL"
);
NODE_API_CALL
(
env
,
napi_get_cb_info
(
env
,
info
,
&
argc
,
&
js_cb
,
NULL
,
NULL
));
NODE_API_CALL
(
env
,
napi_create_object
(
env
,
&
result
));
NODE_API_CALL
(
env
,
napi_add_finalizer
(
env
,
result
,
NULL
,
TestFinalizer
,
NULL
,
NULL
));
NODE_API_CALL
(
env
,
napi_create_reference
(
env
,
js_cb
,
1
,
&
data
->
js_cb_ref
));
return
result
;
}
EXTERN_C_START
napi_value
Init
(
napi_env
env
,
napi_value
exports
) {
AddonData
*
data
=
malloc
(
sizeof
(
*
data
));
data
->
value
=
41
;
data
->
print
=
false;
data
->
js_cb_ref
=
NULL
;
NODE_API_CALL
(
env
,
napi_set_instance_data
(
env
,
data
,
DeleteAddonData
,
NULL
));
napi_property_descriptor
props
[]
=
{
DECLARE_NODE_API_PROPERTY
(
"increment"
,
Increment
),
DECLARE_NODE_API_PROPERTY
(
"setPrintOnDelete"
,
SetPrintOnDelete
),
DECLARE_NODE_API_PROPERTY
(
"objectWithFinalizer"
,
ObjectWithFinalizer
),
};
NODE_API_CALL
(
env
,
napi_define_properties
(
env
,
exports
,
sizeof
(
props
) /
sizeof
(
*
props
),
props
));
return
exports
;
}
EXTERN_C_END
Back
|
FazBrowse Home
|
New Git URL