예제 #1
0
파일: v7.go 프로젝트: botvs/go-v7
// Call JS function with arguments.
// Arguments is converted as following table.
// Go: Javascript
// uint, int, float: number
// []byte, string: string
// bool: boolean
func (fc *JSFunction) Call(args ...interface{}) (interface{}, error) {
	var cargs *C.v7_val_t

	if len(args) == 0 {
		_cargs := C.v7_create_undefined()
		cargs = &_cargs
	} else {
		var cobj *C.v7_val_t
		_cargs := C.v7_create_array(fc.ctx.rctx)
		for _, arg := range args {
			switch arg.(type) {
			case uint:
				obj := C.v7_create_number(C.double(arg.(uint)))
				cobj = &obj
			case uint32:
				obj := C.v7_create_number(C.double(arg.(uint32)))
				cobj = &obj
			case uint64:
				obj := C.v7_create_number(C.double(arg.(uint64)))
				cobj = &obj
			case int:
				obj := C.v7_create_number(C.double(arg.(int)))
				cobj = &obj
			case int32:
				obj := C.v7_create_number(C.double(arg.(int32)))
				cobj = &obj
			case int64:
				obj := C.v7_create_number(C.double(arg.(int64)))
				cobj = &obj
			case float32:
				obj := C.v7_create_number(C.double(arg.(float32)))
				cobj = &obj
			case float64:
				obj := C.v7_create_number(C.double(arg.(float64)))
				cobj = &obj
			case []byte, string:
				ptr := C.CString(arg.(string))
				defer C.free(unsafe.Pointer(ptr))
				obj := C.v7_create_string(fc.ctx.rctx, ptr, C.size_t(len(arg.(string))), 0)
				cobj = &obj
			case bool:
				obj := C.v7_create_boolean(arg.(C.int))
				cobj = &obj
			default:
				return nil, fmt.Errorf("Unsupported type %T", arg)
			}
			C.v7_array_push(fc.ctx.rctx, _cargs, *cobj)
		}
		cargs = &_cargs
	}

	var result C.v7_val_t
	err := wrapCError(C.v7_apply(fc.ctx.rctx, &result, fc.value, C.v7_create_undefined(), *cargs))
	if err != nil {
		return nil, err
	}

	return toValue(fc.ctx, result)
}
예제 #2
0
파일: gov7.go 프로젝트: edvakf/gov7
func (v7 *V7) Apply(fn Val, thisObj Val, args Val) (Val, error) {
	var result C.v7_val_t

	e := C.v7_apply((*C.struct_v7)(v7), &result, C.v7_val_t(fn), C.v7_val_t(thisObj), C.v7_val_t(args))

	v := Val(result)

	// DEBUG
	//fmt.Println(v7.ToJSON(v, 2048))

	return v, v7.convertError(v, e)
}