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_object.go
351 lines (297 loc) · 9.36 KB
/
v8_object.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package v8
/*
#include "v8_wrap.h"
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "reflect"
type PropertyAttribute int
const (
PA_None PropertyAttribute = 0
PA_ReadOnly = 1 << 0
PA_DontEnum = 1 << 1
PA_DontDelete = 1 << 2
)
type External struct {
*Value
data interface{}
}
func (e *Engine) NewExternal(value interface{}) *External {
if value == nil {
panic("value is nil")
}
external := &External{
data: value,
}
external.Value = newValue(e, C.V8_NewExternal(
e.self, unsafe.Pointer(&(external.data)),
))
external.setOwner(external)
return external
}
func (ex *External) GetValue() interface{} {
if ex.data == nil {
ptr := C.V8_External_Value(ex.self)
ex.data = *(*interface{})(ptr)
}
return ex.data
}
// A JavaScript object (ECMA-262, 4.3.3)
//
type Object struct {
*Value
internalFields []interface{}
accessor *accessorInfo
}
func (e *Engine) NewObject() *Value {
return newValue(e, C.V8_NewObject(e.self))
}
func (o *Object) SetProperty(key string, value *Value) bool {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_SetProperty(
o.self, (*C.char)(keyPtr), C.int(len(key)), value.self,
) == 1
}
func (o *Object) GetProperty(key string) *Value {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return newValue(o.engine, C.V8_Object_GetProperty(
o.self, (*C.char)(keyPtr), C.int(len(key)),
))
}
func (o *Object) SetElement(index int, value *Value) bool {
return C.V8_Object_SetElement(
o.self, C.uint32_t(index), value.self,
) == 1
}
func (o *Object) GetElement(index int) *Value {
return newValue(o.engine, C.V8_Object_GetElement(o.self, C.uint32_t(index)))
}
func (o *Object) GetPropertyAttributes(key string) PropertyAttribute {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return PropertyAttribute(C.V8_Object_GetPropertyAttributes(
o.self, (*C.char)(keyPtr), C.int(len(key)),
))
}
func (o *Object) InternalFieldCount() int {
return int(C.V8_Object_InternalFieldCount(o.self))
}
func (o *Object) GetInternalField(index int) interface{} {
data := C.V8_Object_GetInternalField(o.self, C.int(index))
if data == nil {
return nil
}
return *(*interface{})(data)
}
func (o *Object) SetInternalField(index int, value interface{}) {
C.V8_Object_SetInternalField(
o.self,
C.int(index),
unsafe.Pointer(&value),
)
// the value reference by object so the value can't destory by GC
o.internalFields = append(o.internalFields, value)
o.setOwner(o)
}
func (o *Object) SetAccessor(
key string,
getter AccessorGetterCallback,
setter AccessorSetterCallback,
data interface{},
attribs PropertyAttribute,
) {
o.setAccessor(&accessorInfo{
key: key,
getter: getter,
setter: setter,
data: data,
attribs: attribs,
})
}
func (o *Object) setAccessor(info *accessorInfo) {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&info.key)).Data)
var getterPointer, setterPointer unsafe.Pointer
if info.getter != nil {
getterPointer = unsafe.Pointer(&info.getter)
}
if info.setter != nil {
setterPointer = unsafe.Pointer(&info.setter)
}
o.accessor = info
o.setOwner(o)
C.V8_Object_SetAccessor(
o.self,
(*C.char)(keyPtr), C.int(len(info.key)),
getterPointer,
setterPointer,
unsafe.Pointer(&info.data),
C.int(info.attribs),
)
}
// Sets a local property on this object bypassing interceptors and
// overriding accessors or read-only properties.
//
// Note that if the object has an interceptor the property will be set
// locally, but since the interceptor takes precedence the local property
// will only be returned if the interceptor doesn't return a value.
//
// Note also that this only works for named properties.
func (o *Object) ForceSetProperty(key string, value *Value, attribs PropertyAttribute) bool {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_ForceSetProperty(o.self,
(*C.char)(keyPtr), C.int(len(key)), value.self, C.int(attribs),
) == 1
}
func (o *Object) HasProperty(key string) bool {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_HasProperty(
o.self, (*C.char)(keyPtr), C.int(len(key)),
) == 1
}
func (o *Object) DeleteProperty(key string) bool {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_DeleteProperty(
o.self, (*C.char)(keyPtr), C.int(len(key)),
) == 1
}
// Delete a property on this object bypassing interceptors and
// ignoring dont-delete attributes.
func (o *Object) ForceDeleteProperty(key string) bool {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_ForceDeleteProperty(
o.self, (*C.char)(keyPtr), C.int(len(key)),
) == 1
}
func (o *Object) HasElement(index int) bool {
return C.V8_Object_HasElement(
o.self, C.uint32_t(index),
) == 1
}
func (o *Object) DeleteElement(index int) bool {
return C.V8_Object_DeleteElement(
o.self, C.uint32_t(index),
) == 1
}
// Returns an array containing the names of the enumerable properties
// of this object, including properties from prototype objects. The
// array returned by this method contains the same values as would
// be enumerated by a for-in statement over this object.
//
func (o *Object) GetPropertyNames() *Array {
return newValue(o.engine, C.V8_Object_GetPropertyNames(o.self)).ToArray()
}
// This function has the same functionality as GetPropertyNames but
// the returned array doesn't contain the names of properties from
// prototype objects.
//
func (o *Object) GetOwnPropertyNames() *Array {
return newValue(o.engine, C.V8_Object_GetOwnPropertyNames(o.self)).ToArray()
}
// Get the prototype object. This does not skip objects marked to
// be skipped by __proto__ and it does not consult the security
// handler.
//
func (o *Object) GetPrototype() *Object {
return newValue(o.engine, C.V8_Object_GetPrototype(o.self)).ToObject()
}
// Set the prototype object. This does not skip objects marked to
// be skipped by __proto__ and it does not consult the security
// handler.
//
func (o *Object) SetPrototype(proto *Object) bool {
return C.V8_Object_SetPrototype(o.self, proto.self) == 1
}
func (o *Object) SetHiddenValue(key string, value *Value) bool{
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_SetHiddenValue(o.self, (*C.char)(keyPtr), value.self) == 1
}
func (o *Object) GetHiddenValue(key string) *Value {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return newValue(o.engine, C.V8_Object_GetHiddenValue(o.self, (*C.char)(keyPtr)))
}
func (o *Object) DeleteHiddenValue(key string) bool{
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_DeleteHiddenValue(o.self, (*C.char)(keyPtr)) == 1
}
func (o *Object) GetConstructorName() string{
return newValue(o.engine, C.V8_Object_GetConstructorName(o.self)).ToString()
}
func (o *Object) SetAlignedPointerInInternalField(index int, val_ptr unsafe.Pointer) {
C.V8_Object_SetAlignedPointerInInternalField(o.self, C.int(index), val_ptr)
}
func (o *Object) GetAlignedPointerFromInternalField(index int) unsafe.Pointer{
return C.V8_Object_GetAlignedPointerFromInternalField(o.self, C.int(index))
}
func (o *Object) GetRealNamedProperty(key string) *Value {
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return newValue(o.engine, C.V8_Object_GetRealNamedProperty(o.self, (*C.char)(keyPtr)))
}
func (o *Object) HasRealNamedProperty(key string) bool{
keyPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&key)).Data)
return C.V8_Object_HasRealNamedProperty(o.self,(*C.char)(keyPtr)) == 1
}
// An instance of the built-in array constructor (ECMA-262, 15.4.2).
//
type Array struct {
*Object
}
func (e *Engine) NewArray(length int) *Value {
return newValue(e, C.V8_NewArray(
e.self, C.int(length),
))
}
func (a *Array) Length() int {
return int(C.V8_Array_Length(a.self))
}
type RegExpFlags int
// Regular expression flag bits. They can be or'ed to enable a set
// of flags.
//
const (
RF_None RegExpFlags = 0
RF_Global = 1
RF_IgnoreCase = 2
RF_Multiline = 4
)
type RegExp struct {
*Object
pattern string
patternCached bool
flags RegExpFlags
flagsCached bool
}
// Creates a regular expression from the given pattern string and
// the flags bit field. May throw a JavaScript exception as
// described in ECMA-262, 15.10.4.1.
//
// For example,
// NewRegExp("foo", RF_Global | RF_Multiline)
//
// is equivalent to evaluating "/foo/gm".
//
func (e *Engine) NewRegExp(pattern string, flags RegExpFlags) *Value {
patternPtr := unsafe.Pointer((*reflect.StringHeader)(unsafe.Pointer(&pattern)).Data)
return newValue(e, C.V8_NewRegExp(
e.self, (*C.char)(patternPtr), C.int(len(pattern)), C.int(flags),
))
}
// Returns the value of the source property: a string representing
// the regular expression.
func (r *RegExp) Pattern() string {
if !r.patternCached {
cstring := C.V8_RegExp_Pattern(r.self)
r.pattern = C.GoString(cstring)
r.patternCached = true
C.free(unsafe.Pointer(cstring))
}
return r.pattern
}
// Returns the flags bit field.
//
func (r *RegExp) Flags() RegExpFlags {
if !r.flagsCached {
r.flags = RegExpFlags(C.V8_RegExp_Flags(r.self))
r.flagsCached = true
}
return r.flags
}