This repository has been archived by the owner on Oct 26, 2023. It is now read-only.
forked from kingland/go-v8
-
Notifications
You must be signed in to change notification settings - Fork 3
/
v8_function.go
152 lines (124 loc) · 3.64 KB
/
v8_function.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package v8
/*
#include "v8_wrap.h"
*/
import "C"
import "unsafe"
import "reflect"
// A JavaScript function object (ECMA-262, 15.3).
//
type Function struct {
*Object
callback FunctionCallback
data interface{}
}
type FunctionCallback func(FunctionCallbackInfo)
//export go_function_callback
func go_function_callback(info, callback, context, data unsafe.Pointer) {
callbackFunc := *(*func(FunctionCallbackInfo))(callback)
callbackFunc(FunctionCallbackInfo{
info,
ReturnValue{},
(*Context)(context),
*(*interface{})(data),
})
}
func (e *Engine) NewFunction(callback FunctionCallback, data interface{}) *Function {
function := new(Function)
function.data = data
function.callback = callback
function.Object = newValue(e, C.V8_NewFunction(
e.self, unsafe.Pointer(&function.callback), unsafe.Pointer(&function.data),
)).ToObject()
function.setOwner(function)
return function
}
func (f *Function) Call(args ...*Value) *Value {
argv := make([]unsafe.Pointer, len(args))
for i, arg := range args {
argv[i] = arg.self
}
return newValue(f.engine, C.V8_Function_Call(
f.self, C.int(len(args)),
unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&argv)).Data),
))
}
func (f *Function) NewInstance(args ...*Value) *Value {
argv := make([]unsafe.Pointer, len(args))
for i, arg := range args {
argv[i] = arg.self
}
return newValue(f.engine, C.V8_Function_NewInstance(
f.self, C.int(len(args)),
unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&argv)).Data),
))
}
// Function and property return value
//
type ReturnValue struct {
self unsafe.Pointer
}
func (rv ReturnValue) Set(value *Value) {
C.V8_ReturnValue_Set(rv.self, value.self)
}
func (rv ReturnValue) SetBoolean(value bool) {
valueInt := 0
if value {
valueInt = 1
}
C.V8_ReturnValue_SetBoolean(rv.self, C.int(valueInt))
}
func (rv ReturnValue) SetNumber(value float64) {
C.V8_ReturnValue_SetNumber(rv.self, C.double(value))
}
func (rv ReturnValue) SetInt32(value int32) {
C.V8_ReturnValue_SetInt32(rv.self, C.int32_t(value))
}
func (rv ReturnValue) SetUint32(value uint32) {
C.V8_ReturnValue_SetUint32(rv.self, C.uint32_t(value))
}
func (rv ReturnValue) SetString(value string) {
valuePtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&value)).Data)
C.V8_ReturnValue_SetString(rv.self, (*C.char)(valuePtr), C.int(len(value)))
}
func (rv ReturnValue) SetNull() {
C.V8_ReturnValue_SetNull(rv.self)
}
func (rv ReturnValue) SetUndefined() {
C.V8_ReturnValue_SetUndefined(rv.self)
}
// Function callback info
//
type FunctionCallbackInfo struct {
self unsafe.Pointer
returnValue ReturnValue
context *Context
data interface{}
}
func (fc FunctionCallbackInfo) CurrentScope() ContextScope {
return ContextScope{fc.context}
}
func (fc FunctionCallbackInfo) Get(i int) *Value {
return newValue(fc.context.engine, C.V8_FunctionCallbackInfo_Get(fc.self, C.int(i)))
}
func (fc FunctionCallbackInfo) Length() int {
return int(C.V8_FunctionCallbackInfo_Length(fc.self))
}
func (fc FunctionCallbackInfo) Callee() *Function {
return newValue(fc.context.engine, C.V8_FunctionCallbackInfo_Callee(fc.self)).ToFunction()
}
func (fc FunctionCallbackInfo) This() *Object {
return newValue(fc.context.engine, C.V8_FunctionCallbackInfo_This(fc.self)).ToObject()
}
func (fc FunctionCallbackInfo) Holder() *Object {
return newValue(fc.context.engine, C.V8_FunctionCallbackInfo_Holder(fc.self)).ToObject()
}
func (fc FunctionCallbackInfo) Data() interface{} {
return fc.data
}
func (fc *FunctionCallbackInfo) ReturnValue() ReturnValue {
if fc.returnValue.self == nil {
fc.returnValue.self = C.V8_FunctionCallbackInfo_ReturnValue(fc.self)
}
return fc.returnValue
}