Xbase++ Requires Chilkat v11.4.0+
Xbase++
Call a JavaScript Function Returning a Boolean
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns a boolean.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSbScript
LOCAL oJs
LOCAL oResult
LOCAL oFuncCall
LOCAL nRetval
nSuccess := 0
// This is the JavaScript function we'll call:
// function isEven(number) {
// return number % 2 === 0;
// }
oSbScript := CreateObject("Chilkat.StringBuilder")
oSbScript:Append("function isEven(number) { return number % 2 === 0; }")
oJs := CreateObject("Chilkat.Js")
oResult := CreateObject("Chilkat.JsonObject")
oResult:EmitCompact := 0
// Call Eval to add the function to the context's global object
nSuccess := oJs:Eval(oSbScript, oResult)
IF (nSuccess == 0)
// Examine the result for an exception.
? oResult:Emit()
// Also examine the LastErrorText.
? oJs:LastErrorText
oSbScript:destroy()
oJs:destroy()
oResult:destroy()
RETURN
ENDIF
// ------------------------------------------------------------------------------
// Call the function isEven(8)
oFuncCall := CreateObject("Chilkat.JsonObject")
// Create JSON specifying the function name and arguments
// {
// "name": "isEven",
// "args": [ 8 ]
// }
oFuncCall:UpdateString("name", "isEven")
oFuncCall:UpdateInt("args[0]", 8)
nSuccess := oJs:CallFunction(oFuncCall, oResult)
IF (nSuccess == 0)
// Examine the result for an exception.
? oResult:Emit()
// Also examine the LastErrorText.
? oJs:LastErrorText
oSbScript:destroy()
oJs:destroy()
oResult:destroy()
oFuncCall:destroy()
RETURN
ENDIF
? oResult:Emit()
// Output:
// {
// "type": "bool",
// "value": true
// }
nRetval := oResult:BoolOf("value")
? Str(nRetval)
// Output:
// 1
oSbScript:destroy()
oJs:destroy()
oResult:destroy()
oFuncCall:destroy()