/
js_callable.go
54 lines (47 loc) · 1.17 KB
/
js_callable.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
package jsgo
// #include "mujs.h"
// #include <stdlib.h>
import "C"
import "unsafe"
// JsCallable ...
type JsCallable struct {
*JsObject
}
// Call ...
func (callable *JsCallable) Call(args ...interface{}) (JsValue, JsError) {
// push function
C.js_getregistry(callable.state.vm, callable.ref)
// push the this value to be used by the function
C.js_pushundefined(callable.state.vm) // for now, just pushing undefined
// push the arguments
argsCount := 0
for _, arg := range args {
if arg == nil {
C.js_pushnull(callable.state.vm)
} else {
switch arg := arg.(type) {
case int:
C.js_pushnumber(callable.state.vm, C.double(arg))
case float64:
C.js_pushnumber(callable.state.vm, C.double(arg))
case bool:
value := 0
if arg {
value = 1
}
C.js_pushboolean(callable.state.vm, C.int(value))
case string:
value := C.CString(arg)
defer C.free(unsafe.Pointer(value))
C.js_pushstring(callable.state.vm, value)
}
}
argsCount++
}
// call the function
if rc := C.js_pcall(callable.state.vm, C.int(argsCount)); rc != 0 {
return nil, newJsError(callable.state)
}
// return the result
return newJsValue(callable.state), nil
}