-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnode_api.go
137 lines (119 loc) · 2.78 KB
/
node_api.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package napi
/*
#include <node/node_api.h>
*/
import "C"
import (
"unsafe"
)
func CreateAsyncWork(
env Env,
asyncResource, asyncResourceName Value,
execute AsyncExecuteCallback,
complete AsyncCompleteCallback,
) (AsyncWork, Status) {
provider, status := getInstanceData(env)
if status != StatusOK || provider == nil {
return AsyncWork{}, status
}
return provider.GetAsyncWorkData().CreateAsyncWork(
env,
asyncResource, asyncResourceName,
execute,
complete,
)
}
func DeleteAsyncWork(env Env, work AsyncWork) Status {
provider, status := getInstanceData(env)
if status != StatusOK || provider == nil {
return status
}
defer provider.GetAsyncWorkData().DeleteAsyncWork(work.ID)
return Status(C.napi_delete_async_work(
C.napi_env(env),
C.napi_async_work(work.Handle),
))
}
func QueueAsyncWork(env Env, work AsyncWork) Status {
return Status(C.napi_queue_async_work(
C.napi_env(env),
C.napi_async_work(work.Handle),
))
}
func CancelAsyncWork(env Env, work AsyncWork) Status {
return Status(C.napi_cancel_async_work(
C.napi_env(env),
C.napi_async_work(work.Handle),
))
}
func GetNodeVersion(env Env) (NodeVersion, Status) {
var cresult *C.napi_node_version
status := Status(C.napi_get_node_version(
C.napi_env(env),
(**C.napi_node_version)(&cresult),
))
if status != StatusOK {
return NodeVersion{}, status
}
return NodeVersion{
Major: uint(cresult.major),
Minor: uint(cresult.minor),
Patch: uint(cresult.patch),
Release: C.GoString(cresult.release),
}, status
}
func GetModuleFileName(env Env) (string, Status) {
var cresult *C.char
status := Status(C.node_api_get_module_file_name(
C.napi_env(env),
(**C.char)(&cresult),
))
if status != StatusOK {
return "", status
}
return C.GoString(cresult), status
}
func CreateThreadsafeFunction(
env Env,
fn Value,
asyncResource, asyncResourceName Value,
maxQueueSize, initialThreadCount int,
) (ThreadsafeFunction, Status) {
var result ThreadsafeFunction
status := Status(C.napi_create_threadsafe_function(
C.napi_env(env),
C.napi_value(fn),
C.napi_value(asyncResource),
C.napi_value(asyncResourceName),
C.size_t(maxQueueSize),
C.size_t(initialThreadCount),
nil,
nil,
nil,
nil,
(*C.napi_threadsafe_function)(unsafe.Pointer(&result)),
))
return result, status
}
func CallThreadsafeFunction(
fn ThreadsafeFunction,
) Status {
return Status(C.napi_call_threadsafe_function(
C.napi_threadsafe_function(fn),
nil,
C.napi_tsfn_blocking,
))
}
func AcquireThreadsafeFunction(fn ThreadsafeFunction) Status {
return Status(C.napi_acquire_threadsafe_function(
C.napi_threadsafe_function(fn),
))
}
func ReleaseThreadsafeFunction(
fn ThreadsafeFunction,
) Status {
return Status(C.napi_release_threadsafe_function(
C.napi_threadsafe_function(fn),
C.napi_tsfn_release,
))
}