Esempio n. 1
0
File: v7.go Progetto: 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)
}
Esempio n. 2
0
File: gov7.go Progetto: edvakf/gov7
func (v7 *V7) ArrayPush(ary Val, v Val) {
	C.v7_array_push((*C.struct_v7)(v7), C.v7_val_t(ary), C.v7_val_t(v))
}