Skip to content

Commit 0587a26

Browse files
committed
feat(js_native_api): Add more missing js functions
1 parent c8ac488 commit 0587a26

File tree

1 file changed

+54
-40
lines changed

1 file changed

+54
-40
lines changed

js_native_api.go

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ func CreateSymbol(env Env, description Value) (Value, Status) {
110110
}
111111

112112
func CreateFunction(env Env, name string, cb Callback) (Value, Status) {
113-
provider, status := getInstanceData(env)
114-
if status != StatusOK || provider == nil {
115-
return nil, status
116-
}
113+
cname := C.CString(name)
114+
defer C.free(unsafe.Pointer(cname))
117115

118-
return provider.GetCallbackData().CreateCallback(env, name, cb)
116+
var result Value
117+
status := Status(C.napi_create_function(
118+
C.napi_env(env),
119+
cname,
120+
C.size_t(len(name)),
121+
C.napi_callback(cb),
122+
unsafe.Pointer(nil),
123+
(*C.napi_value)(unsafe.Pointer(&result)),
124+
))
125+
return result, status
119126
}
120127

121128
func CreateError(env Env, code, msg Value) (Value, Status) {
@@ -235,45 +242,17 @@ type GetCbInfoResult struct {
235242
This Value
236243
}
237244

238-
func GetCbInfo(env Env, info CallbackInfo) (GetCbInfoResult, Status) {
239-
// call napi_get_cb_info twice
240-
// first is to get total number of arguments
241-
// second is to populate the actual arguments
242-
argc := C.size_t(0)
245+
func GetCbInfo(env Env, info CallbackInfo, argc int, argv []Value, thisArg *Value) Status {
246+
cArgv := (*C.napi_value)(unsafe.Pointer(&argv[0]))
243247
status := Status(C.napi_get_cb_info(
244248
C.napi_env(env),
245249
C.napi_callback_info(info),
246-
&argc,
247-
nil,
248-
nil,
249-
nil,
250-
))
251-
252-
if status != StatusOK {
253-
return GetCbInfoResult{}, status
254-
}
255-
256-
argv := make([]Value, int(argc))
257-
var cArgv unsafe.Pointer
258-
if argc > 0 {
259-
cArgv = unsafe.Pointer(&argv[0]) // must pass element pointer
260-
}
261-
262-
var thisArg Value
263-
264-
status = Status(C.napi_get_cb_info(
265-
C.napi_env(env),
266-
C.napi_callback_info(info),
267-
&argc,
268-
(*C.napi_value)(cArgv),
269-
(*C.napi_value)(unsafe.Pointer(&thisArg)),
270-
nil,
250+
(*C.size_t)(unsafe.Pointer(&argc)),
251+
cArgv,
252+
(*C.napi_value)(unsafe.Pointer(thisArg)),
253+
unsafe.Pointer(nil),
271254
))
272-
273-
return GetCbInfoResult{
274-
Args: argv,
275-
This: thisArg,
276-
}, status
255+
return status
277256
}
278257

279258
func Throw(env Env, err Value) Status {
@@ -1163,3 +1142,38 @@ func CreateStringUtf16(env Env, str []uint16) (Value, Status) {
11631142
))
11641143
return result, status
11651144
}
1145+
1146+
func CallFunction(env Env, recv Value, fn Value, argc int, argv []Value) (Value, Status) {
1147+
var result Value
1148+
status := Status(C.napi_call_function(
1149+
C.napi_env(env),
1150+
C.napi_value(recv),
1151+
C.napi_value(fn),
1152+
C.size_t(argc),
1153+
(*C.napi_value)(unsafe.Pointer(&argv[0])),
1154+
(*C.napi_value)(unsafe.Pointer(&result)),
1155+
))
1156+
return result, status
1157+
}
1158+
1159+
func GetNewTarget(env Env, info CallbackInfo) (Value, Status) {
1160+
var result Value
1161+
status := Status(C.napi_get_new_target(
1162+
C.napi_env(env),
1163+
C.napi_callback_info(info),
1164+
(*C.napi_value)(unsafe.Pointer(&result)),
1165+
))
1166+
return result, status
1167+
}
1168+
1169+
func NewInstance(env Env, constructor Value, argc int, argv []Value) (Value, Status) {
1170+
var result Value
1171+
status := Status(C.napi_new_instance(
1172+
C.napi_env(env),
1173+
C.napi_value(constructor),
1174+
C.size_t(argc),
1175+
(*C.napi_value)(unsafe.Pointer(&argv[0])),
1176+
(*C.napi_value)(unsafe.Pointer(&result)),
1177+
))
1178+
return result, status
1179+
}

0 commit comments

Comments
 (0)