Пример #1
0
func (value *basicValue) check(f unsafe.Pointer) bool {
	// put the reference on stack
	C.js_getregistry(value.state.vm, value.ref)
	defer C.js_pop(value.state.vm, 1)

	// use the 'check' wrapper to call the validation function
	return C.check(f, value.state.vm) == 1
}
Пример #2
0
// Set ...
func (state *JsState) Set(name string, value JsValue) {
	property := C.CString(name)
	defer C.free(unsafe.Pointer(property))

	// push value
	C.js_getregistry(state.vm, value.reference())

	// set property
	C.js_setglobal(state.vm, property)
}
Пример #3
0
// Call ...
func (callable *JsCallable) Call(args ...interface{}) (JsValue, JsError) {
	// push function
	C.js_getregistry(callable.state.vm, callable.ref)

	// push the this value to be used by the function
	C.js_pushundefined(callable.state.vm) // for now, just pushing undefined

	// push the arguments
	argsCount := 0
	for _, arg := range args {
		if arg == nil {
			C.js_pushnull(callable.state.vm)
		} else {
			switch arg := arg.(type) {
			case int:
				C.js_pushnumber(callable.state.vm, C.double(arg))
			case float64:
				C.js_pushnumber(callable.state.vm, C.double(arg))
			case bool:
				value := 0
				if arg {
					value = 1
				}
				C.js_pushboolean(callable.state.vm, C.int(value))
			case string:
				value := C.CString(arg)
				defer C.free(unsafe.Pointer(value))
				C.js_pushstring(callable.state.vm, value)
			}
		}
		argsCount++
	}

	// call the function
	if rc := C.js_pcall(callable.state.vm, C.int(argsCount)); rc != 0 {
		return nil, newJsError(callable.state)
	}

	// return the result
	return newJsValue(callable.state), nil
}
Пример #4
0
// Bool ...
func (value *basicValue) Bool() bool {
	C.js_getregistry(value.state.vm, value.ref)
	defer C.js_pop(value.state.vm, 1)
	return C.js_toboolean(value.state.vm, 0) == 1
}
Пример #5
0
// String ...
func (value *basicValue) String() string {
	C.js_getregistry(value.state.vm, value.ref)
	defer C.js_pop(value.state.vm, 1)
	return C.GoString(C.js_tostring(value.state.vm, 0))
}
Пример #6
0
// Integer ...
func (value *basicValue) Integer() int {
	C.js_getregistry(value.state.vm, value.ref)
	defer C.js_pop(value.state.vm, 1)
	return int(C.js_toint32(value.state.vm, 0))
}
Пример #7
0
// Float ...
func (value *basicValue) Float() float64 {
	C.js_getregistry(value.state.vm, value.ref)
	defer C.js_pop(value.state.vm, 1)
	return float64(C.js_tonumber(value.state.vm, 0))
}