Esempio n. 1
0
// DoString ...
func (state *JsState) DoString(text string) (JsValue, JsError) {
	source := C.CString(text)
	defer C.free(unsafe.Pointer(source))

	if rc := C.js_ploadstring(state.vm, C.CString("[string]"), source); rc != 0 {
		return nil, newJsError(state)
	}

	C.js_pushglobal(state.vm)

	if rc := C.js_pcall(state.vm, 0); rc != 0 {
		return nil, newJsError(state)
	}

	return newJsValue(state), nil
}
Esempio n. 2
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
}