-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnode.ts
More file actions
203 lines (167 loc) · 6.83 KB
/
node.ts
File metadata and controls
203 lines (167 loc) · 6.83 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { emnapiNodeBinding, emnapiCtx, emnapiEnv } from 'emnapi:shared'
import { from64, makeSetValue, makeGetValue, POINTER_SIZE } from 'emscripten:parse-tools'
import { $PREAMBLE, $CHECK_ARG, $GET_RETURN_STATUS } from './macro'
/** @__sig vppdp */
export function _emnapi_node_emit_async_init (
async_resource: napi_value,
async_resource_name: napi_value,
trigger_async_id: double,
result: Pointer<[double, double]>
): void {
if (!emnapiNodeBinding) return
const resource = emnapiCtx.jsValueFromNapiValue(async_resource)!
const resource_name = emnapiCtx.jsValueFromNapiValue<string>(async_resource_name)!
const asyncContext = emnapiNodeBinding.node.emitAsyncInit(resource, resource_name, trigger_async_id)
const asyncId = asyncContext.asyncId; const triggerAsyncId = asyncContext.triggerAsyncId
if (result) {
from64('result')
makeSetValue('result', 0, 'asyncId', 'double')
makeSetValue('result', 8, 'triggerAsyncId', 'double')
}
}
/** @__sig vdd */
export function _emnapi_node_emit_async_destroy (async_id: double, trigger_async_id: double): void {
if (!emnapiNodeBinding) return
emnapiNodeBinding.node.emitAsyncDestroy({
asyncId: async_id,
triggerAsyncId: trigger_async_id
})
}
/* vpddp export function _emnapi_node_open_callback_scope (async_resource: napi_value, async_id: double, trigger_async_id: double, result: Pointer<int64_t>): void {
if (!emnapiNodeBinding || !result) return
const resource = emnapiCtx.jsValueFromNapiValue(async_resource)!
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const nativeCallbackScopePointer = emnapiNodeBinding.node.openCallbackScope(resource, {
asyncId: async_id,
triggerAsyncId: trigger_async_id
})
from64('result')
$_TODO_makeSetValue('result', 0, 'nativeCallbackScopePointer', 'i64')
}
vp
export function _emnapi_node_close_callback_scope (scope: Pointer<int64_t>): void {
if (!emnapiNodeBinding || !scope) return
from64('scope')
const nativeCallbackScopePointer = $_TODO_makeGetValue('scope', 0, 'i64')
emnapiNodeBinding.node.closeCallbackScope(BigInt(nativeCallbackScopePointer))
} */
/** @__sig ipppppddp */
export function _emnapi_node_make_callback (env: napi_env, async_resource: napi_value, cb: napi_value, argv: Pointer<napi_value>, size: size_t, async_id: double, trigger_async_id: double, result: Pointer<napi_value>): void {
let i = 0
let v: napi_value
if (!emnapiNodeBinding) return
const resource = emnapiCtx.jsValueFromNapiValue(async_resource)!
const callback = emnapiCtx.jsValueFromNapiValue<any>(cb)!
from64('argv')
from64('size')
size = size >>> 0
const arr = Array(size)
for (; i < size; i++) {
const argVal = makeGetValue('argv', 'i * ' + POINTER_SIZE, '*')
arr[i] = emnapiCtx.jsValueFromNapiValue(argVal)!
}
const ret = emnapiNodeBinding.node.makeCallback(resource, callback, arr, {
asyncId: async_id,
triggerAsyncId: trigger_async_id
})
if (result) {
from64('result')
v = emnapiCtx.napiValueFromJsValue(ret)
makeSetValue('result', 0, 'v', '*')
}
}
/** @__sig ippp */
export function _emnapi_async_init_js (async_resource: napi_value, async_resource_name: napi_value, result: Pointer<int64_t>): napi_status {
if (!emnapiNodeBinding) {
return napi_status.napi_generic_failure
}
let resource: object | undefined
if (async_resource) {
resource = Object(emnapiCtx.jsValueFromNapiValue(async_resource)!)
}
const name = emnapiCtx.jsValueFromNapiValue<string>(async_resource_name)!
const ret = emnapiNodeBinding.napi.asyncInit(resource, name)
if (ret.status !== 0) return ret.status
let numberValue = ret.value
if (!((numberValue >= (BigInt(-1) * (BigInt(1) << BigInt(63)))) && (numberValue < (BigInt(1) << BigInt(63))))) {
numberValue = numberValue & ((BigInt(1) << BigInt(64)) - BigInt(1))
if (numberValue >= (BigInt(1) << BigInt(63))) {
numberValue = numberValue - (BigInt(1) << BigInt(64))
}
}
const low = Number(numberValue & BigInt(0xffffffff))
const high = Number(numberValue >> BigInt(32))
from64('result')
makeSetValue('result', 0, 'low', 'i32')
makeSetValue('result', 4, 'high', 'i32')
return napi_status.napi_ok
}
/** @__sig ip */
export function _emnapi_async_destroy_js (async_context: Pointer<int64_t>): napi_status {
if (!emnapiNodeBinding) {
return napi_status.napi_generic_failure
}
from64('async_context')
const low = makeGetValue('async_context', 0, 'i32')
const high = makeGetValue('async_context', 4, 'i32')
const pointer = BigInt((low as number) >>> 0) | (BigInt(high) << BigInt(32))
const ret = emnapiNodeBinding.napi.asyncDestroy(pointer)
if (ret.status !== 0) return ret.status
return napi_status.napi_ok
}
// https://github.com/nodejs/node-addon-api/pull/1283
/** @__sig ipppp */
export function napi_open_callback_scope (env: napi_env, ignored: napi_value, async_context_handle: number, result: number): napi_status {
throw new Error('napi_open_callback_scope has not been implemented yet')
}
/** @__sig ipp */
export function napi_close_callback_scope (env: napi_env, scope: number): napi_status {
throw new Error('napi_close_callback_scope has not been implemented yet')
}
/** @__sig ippppppp */
export function napi_make_callback (env: napi_env, async_context: Pointer<int64_t>, recv: napi_value, func: napi_value, argc: size_t, argv: Pointer<napi_value>, result: Pointer<napi_value>): napi_status {
let i = 0
let v: napi_value
return $PREAMBLE!(env, (envObject) => {
if (!emnapiNodeBinding) {
return envObject.setLastError(napi_status.napi_generic_failure)
}
$CHECK_ARG!(envObject, recv)
if (argc > 0) {
$CHECK_ARG!(envObject, argv)
}
const v8recv = Object(emnapiCtx.jsValueFromNapiValue(recv)!)
const v8func = emnapiCtx.jsValueFromNapiValue<any>(func)!
if (typeof v8func !== 'function') {
return envObject.setLastError(napi_status.napi_invalid_arg)
}
from64('async_context')
const low = makeGetValue('async_context', 0, 'i32')
const high = makeGetValue('async_context', 4, 'i32')
const ctx = BigInt((low as number) >>> 0) | (BigInt(high) << BigInt(32))
from64('argv')
from64('argc')
argc = argc >>> 0
const arr = Array(argc)
for (; i < argc; i++) {
const argVal = makeGetValue('argv', 'i * ' + POINTER_SIZE, '*')
arr[i] = emnapiCtx.jsValueFromNapiValue(argVal)!
}
const ret = emnapiNodeBinding.napi.makeCallback(ctx, v8recv, v8func, arr)
if (ret.error) {
throw ret.error
}
if (ret.status !== napi_status.napi_ok) return envObject.setLastError(ret.status)
if (result) {
from64('result')
v = emnapiCtx.napiValueFromJsValue(ret.value)
makeSetValue('result', 0, 'v', '*')
}
return $GET_RETURN_STATUS!(envObject)
})
}
/** @__sig vp */
export function _emnapi_env_check_gc_access (env: napi_env): void {
const envObject = emnapiEnv
envObject.checkGCAccess()
}