-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnative.go
507 lines (433 loc) · 14.1 KB
/
native.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
package gojs
// #include <stdlib.h>
// #include <JavaScriptCore/JSStringRef.h>
// #include <JavaScriptCore/JSObjectRef.h>
// #include "callback.h"
import "C"
import (
"errors"
"fmt"
"log"
"reflect"
"syscall"
"unsafe"
)
type object_data struct {
typ reflect.Type
val reflect.Value
method int
}
var (
nativecallback C.JSClassRef
nativefunction C.JSClassRef
nativeobject C.JSClassRef
nativemethod C.JSClassRef
objects map[uintptr]*object_data
)
type Stringer interface {
String() string
}
func init() {
// Create the class definition for JavaScriptCore
nativecallback = C.JSClassDefinition_NativeCallback()
if nativecallback == nil {
panic(syscall.ENOMEM)
}
// Create the class definition for JavaScriptCore
nativeobject = C.JSClassDefinition_NativeObject()
if nativeobject == nil {
panic(syscall.ENOMEM)
}
// Create the class definition for JavaScriptCore
nativefunction = C.JSClassDefinition_NativeFunction()
if nativefunction == nil {
panic(syscall.ENOMEM)
}
// Create the class definition for JavaScriptCore
nativemethod = C.JSClassDefinition_NativeMethod()
if nativemethod == nil {
panic(syscall.ENOMEM)
}
// Create map for native objects
objects = make(map[uintptr]*object_data)
}
// Given a slice of go-style Values, this function allocates a new array of c-style values and returns a pointer to the first element in the array, along with the length of the array.
func (ctx *Context) newCValueArray(val []*Value) (*C.JSValueRef, C.size_t) {
if len(val) == 0 {
return nil, 0
}
arr := make([]C.JSValueRef, len(val))
for i := 0; i < len(val); i++ {
arr[i] = val[i].ref
}
return &arr[0], C.size_t(len(arr))
}
func (ctx *Context) ptrValue(ptr unsafe.Pointer) *Value {
return ctx.newValue(*(*C.JSValueRef)(ptr))
}
func (ctx *Context) newGoValueArray(ptr unsafe.Pointer, size uint) []*Value {
// TODO(sqs): use technique from https://code.google.com/p/go-wiki/wiki/cgo
if uintptr(ptr) == 0x00000000 {
return nil
}
ptrs := unsafe.Sizeof(uintptr(0))
goarr := make([]*Value, size)
for i := uint(0); i < size; i++ {
goarr[i] = ctx.ptrValue(ptr)
// Increment the pointer by one space
ptr = unsafe.Pointer(uintptr(ptr) + ptrs)
}
return goarr
}
// Given a reflect.Value, this function examines the type and returns a javascript value that best represents the given value. If no acceptable conversion can be found, it panics.
func (ctx *Context) reflectToJSValue(value reflect.Value) *Value {
// Allows functions to return JavaScriptCore values and objects
// directly. These we can return without conversion.
if value.Type() == reflect.TypeOf((*Value)(nil)) {
// Type is already a JavaScriptCore value
return value.Interface().(*Value)
}
if value.Type() == reflect.TypeOf((*Object)(nil)) {
// Type is already a JavaScriptCore object
// nearly there
return value.Interface().(*Object).ToValue()
}
// Handle simple types directly. These can be identified by their
// types in the package 'reflect'.
switch value.Kind() {
case (reflect.Int):
r := value.Int()
return ctx.NewNumberValue(float64(r))
case (reflect.Uint):
r := value.Uint()
return ctx.NewNumberValue(float64(r))
case (reflect.Float64), (reflect.Float32):
r := value.Float()
return ctx.NewNumberValue(r)
case (reflect.String):
r := value.String()
return ctx.NewStringValue(r)
case (reflect.Func):
r := value.Interface()
return ctx.NewFunctionWithNative(r).ToValue()
//case (reflect.Struct):
// r := value.Interface()
// return ctx.NewNativeObject(r).ToValue()
case (reflect.Ptr):
if value.IsNil() {
return ctx.NewNullValue()
}
r := value.Elem()
if r.Kind() == reflect.Struct {
ret := ctx.NewNativeObject(value.Interface())
return ret.ToValue()
}
if r.Kind() == reflect.Array {
panic("Called reflectToJSValue() with a pointer to an array or slice. Most likely, this is a native javascript object you are passing by accident, and meant to pass to newValue() rather than NewValue(). If you really are trying to convert a pointer to an array into a native javascript object, dereference it first (slices are pointers internally anyway, so no significant loss in efficiency).")
//log.Println("About to make new native object from *[0]uint8")
//ret := ctx.NewNativeObject(value.Interface())
//log.Println("Made new native object from *[0]uint8")
//return ret.ToValue()
}
}
// No acceptable conversion found.
panic("Parameter can not be converted from Go native type. Type is " + value.Kind().String() + ", value is " + value.String())
}
func panicArgToJSString(ctx *Context, r interface{}) *Value {
var msg string
switch r := r.(type) {
case error:
msg = r.Error()
case string:
msg = r
default:
msg = fmt.Sprintf("unhandled Go panic: %v", r)
}
return ctx.NewStringValue(msg)
}
func (ctx *Context) jsValuesToReflect(param []*Value) []reflect.Value {
ret := make([]reflect.Value, len(param))
for index, item := range param {
var goval interface{}
log.Println(index, item)
switch item.Type() {
case TypeBoolean:
goval = item.ToBoolean()
case TypeNumber:
goval = item.ToNumberOrDie()
case TypeString:
goval = item.ToStringOrDie()
default:
panic("Parameter can not be converted to Go native type.")
}
ret[index] = reflect.ValueOf(goval)
}
return ret
}
func setNativeFieldFromJSValue(field reflect.Value, ctx *Context, value *Value) (err error) {
switch field.Kind() {
case reflect.String:
var str string
str, err = value.ToString()
if err == nil {
field.SetString(str)
} else {
return
}
case reflect.Float32, reflect.Float64:
var flt float64
flt, err = value.ToNumber()
if err == nil {
field.SetFloat(flt)
} else {
return
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
var flt float64
flt, err = value.ToNumber()
if err == nil {
field.SetInt(int64(flt))
} else {
return
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
log.Println("Dealing with uint type of some sort...")
var flt float64
flt, err = value.ToNumber()
log.Println("Got value!")
if err != nil {
return
}
if flt >= 0 {
field.SetUint(uint64(flt))
} else {
err = errors.New("number must be greater than or equal to zero")
return
}
default:
panic("Parameter can not be converted to Go native type.")
}
return
}
//=========================================================
// Finalizer from JavaScriptCore for all native objects
//---------------------------------------------------------
func register(data *object_data) {
id := uintptr(unsafe.Pointer(data))
objects[id] = data
}
//export finalize_go
func finalize_go(data unsafe.Pointer) {
// Called from JavaScriptCore finalizer methods
id := uintptr(data)
delete(objects, id)
}
//=========================================================
// Native Callback
//---------------------------------------------------------
type GoFunctionCallback func(ctx *Context, obj *Object, thisObject *Object, arguments []*Value) (ret *Value)
func (ctx *Context) NewFunctionWithCallback(callback GoFunctionCallback) *Object {
// Register the native Go object
data := &object_data{
reflect.TypeOf(callback),
reflect.ValueOf(callback),
0}
register(data)
ret := C.JSObjectMake(ctx.ref, nativecallback, unsafe.Pointer(data))
return ctx.newObject(ret)
}
//export nativecallback_CallAsFunction_go
func nativecallback_CallAsFunction_go(data_ptr unsafe.Pointer, rawCtx C.JSContextRef, function C.JSObjectRef, thisObject C.JSObjectRef, argumentCount uint, arguments unsafe.Pointer, exception *C.JSValueRef) unsafe.Pointer {
ctx := NewContextFrom(RawContext(rawCtx))
defer func() {
if r := recover(); r != nil {
*exception = panicArgToJSString(ctx, r).ref
}
}()
data := (*object_data)(data_ptr)
ret := data.val.Interface().(GoFunctionCallback)(
ctx, ctx.newObject(function), ctx.newObject(thisObject), ctx.newGoValueArray(arguments, argumentCount) /*(*[1 << 14]*Value)(arguments)[0:argumentCount]*/)
if ret == nil {
return unsafe.Pointer(nil)
}
return unsafe.Pointer(ret.ref)
}
//=========================================================
// Native Function
//---------------------------------------------------------
func (ctx *Context) NewFunctionWithNative(fn interface{}) *Object {
// Sanity checks on the function
if typ := reflect.TypeOf(fn); typ.NumOut() > 1 {
panic("Bad native function: too many output parameters")
}
// Create Go-side registration
data := &object_data{
reflect.TypeOf(fn),
reflect.ValueOf(fn),
0}
register(data)
ret := C.JSObjectMake(ctx.ref, nativefunction, unsafe.Pointer(data))
return ctx.newObject(ret)
}
func docall(ctx *Context, val reflect.Value, argumentCount uint, arguments unsafe.Pointer) *Value {
// Step one, convert the JavaScriptCore array of arguments to
// an array of reflect.Values.
var in []reflect.Value
if argumentCount != 0 {
valarr := ctx.newGoValueArray(arguments, argumentCount)
log.Println("Converted pointer to go-style array", valarr)
log.Println("Converting to relfect.Value s")
in = ctx.jsValuesToReflect(valarr)
}
log.Println("Converted arguments to native go reflect types. About to actually call the callback function...")
log.Println(in)
// Step two, perform the call
out := val.Call(in)
// Step three, convert the function return value back to JavaScriptCore
if len(out) == 0 {
return nil
}
// len(out) should be equal to 1
return ctx.reflectToJSValue(out[0])
}
//export nativefunction_CallAsFunction_go
func nativefunction_CallAsFunction_go(data_ptr unsafe.Pointer, rawCtx C.JSContextRef, _ unsafe.Pointer, _ unsafe.Pointer, argumentCount uint, arguments unsafe.Pointer, exception *C.JSValueRef) unsafe.Pointer {
ctx := NewContextFrom(RawContext(rawCtx))
defer func() {
if r := recover(); r != nil {
*exception = panicArgToJSString(ctx, r).ref
}
}()
// recover the object
data := (*object_data)(data_ptr)
typ := data.typ
val := data.val
// Do the number of input parameters match?
if typ.NumIn() != int(argumentCount) {
panic("Incorrect number of function arguments")
}
log.Println("About to docall()!")
ret := docall(ctx, val, argumentCount, arguments)
if ret == nil {
return nil
}
return unsafe.Pointer(ret.ref)
}
//=========================================================
// Native Object
//---------------------------------------------------------
func (ctx *Context) NewNativeObject(obj interface{}) *Object {
// The obj must be a pointer to a struct
// TODO: add error checking code
data := &object_data{
reflect.TypeOf(obj),
reflect.ValueOf(obj),
0}
register(data)
ret := C.JSObjectMake(ctx.ref, nativeobject, unsafe.Pointer(data))
return ctx.newObject(ret)
}
//export nativeobject_GetProperty_go
func nativeobject_GetProperty_go(data_ptr, uctx, _, propertyName unsafe.Pointer, exception *unsafe.Pointer) unsafe.Pointer {
ctx := NewContextFrom(RawContext(uctx))
// Get name of property as a go string
name := (*String)(propertyName).String()
// Reconstruct the object interface
data := (*object_data)(data_ptr)
// Drill down through reflect to find the property
val := data.val
if ptrvalue := val; ptrvalue.Kind() == reflect.Ptr {
val = ptrvalue.Elem()
}
struct_val := val
if struct_val.Kind() != reflect.Struct {
return nil
}
// Can we locate a field with the proper name?
field := struct_val.FieldByName(name)
if field.IsValid() {
return unsafe.Pointer(ctx.reflectToJSValue(field).ref)
}
// Can we locate a method with the proper name?
typ := data.typ
for lp := 0; lp < typ.NumMethod(); lp++ {
if typ.Method(lp).Name == name {
ret := newNativeMethod(ctx, data, lp)
return unsafe.Pointer(ret.ref)
}
}
// No matches found
return nil
}
//export nativeobject_SetProperty_go
func nativeobject_SetProperty_go(data_ptr unsafe.Pointer, rawCtx C.JSContextRef, _, propertyName C.JSStringRef, value C.JSValueRef, exception *C.JSValueRef) C.char {
ctx := NewContextFrom(RawContext(rawCtx))
// Get name of property as a go string
name := newStringFromRef(propertyName).String()
// Reconstruct the object interface
data := (*object_data)(data_ptr)
// Drill down through reflect to find the property
val := data.val
if ptrvalue := val; ptrvalue.Kind() == reflect.Ptr {
val = ptrvalue.Elem()
}
struct_val := val
if struct_val.Kind() != reflect.Struct {
*exception = ctx.newErrorOrPanic("object is not a Go struct")
return 0
}
field := struct_val.FieldByName(name)
if !field.IsValid() {
return 0
}
err := setNativeFieldFromJSValue(field, ctx, ctx.newValue(C.JSValueRef(value)))
if err != nil {
*exception = ctx.newErrorOrPanic(err.Error())
return 0
}
return 1
}
//export nativeobject_ConvertToString_go
func nativeobject_ConvertToString_go(data_ptr, ctx, obj unsafe.Pointer) unsafe.Pointer {
// Reconstruct the object interface
data := (*object_data)(data_ptr)
// Can we get a string?
if stringer, ok := data.val.Interface().(Stringer); ok {
str := stringer.String()
ret := NewString(str)
return unsafe.Pointer(ret)
}
return nil
}
//=========================================================
// Native Method
//---------------------------------------------------------
func newNativeMethod(ctx *Context, obj *object_data, method int) *Object {
data := &object_data{
obj.typ,
obj.val,
method}
register(data)
ret := C.JSObjectMake(ctx.ref, nativemethod, unsafe.Pointer(data))
return ctx.newObject(ret)
}
//export nativemethod_CallAsFunction_go
func nativemethod_CallAsFunction_go(data_ptr unsafe.Pointer, rawCtx C.JSContextRef, function C.JSObjectRef, thisObject C.JSObjectRef, argumentCount uint, arguments unsafe.Pointer, exception *C.JSValueRef) unsafe.Pointer {
ctx := NewContextFrom(RawContext(rawCtx))
defer func() {
if r := recover(); r != nil {
*exception = panicArgToJSString(ctx, r).ref
}
}()
// Reconstruct the object interface
data := (*object_data)(data_ptr)
// Get the method
method := data.val.Method(data.method)
// Do the number of input parameters match?
if method.Type().NumIn() != int(argumentCount) {
panic(fmt.Sprintf("Incorrect number of function arguments! Got %d, expected %d!", method.Type().NumIn(), int(argumentCount)))
}
// Perform the call
ret := docall(ctx, method, argumentCount, arguments)
return unsafe.Pointer(ret.ref)
}