Xbase++ Requires Chilkat v11.4.0+
Xbase++
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Xbase++ Downloads
LOCAL nSuccess
LOCAL oSbScript
LOCAL oJs
LOCAL oResult
LOCAL oFuncCall
LOCAL nCount
LOCAL i
nSuccess := 0
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
oSbScript := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbScript:LoadFile("js_function_returning_array.js", "utf-8")
IF (nSuccess == 0)
? oSbScript:LastErrorText
oSbScript:destroy()
RETURN
ENDIF
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 each function
oFuncCall := CreateObject("Chilkat.JsonObject")
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
oFuncCall:UpdateString("name", "getDays")
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": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
nCount := oResult:SizeOfArray("value")
i := 0
DO WHILE i < nCount
oResult:I := i
? oResult:StringOf("value[i]")
i := i + 1
ENDDO
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
oFuncCall:Clear()
oFuncCall:UpdateString("name", "getRange")
oFuncCall:UpdateInt("args[0]", 14)
oFuncCall:UpdateInt("args[1]", 21)
nSuccess := oJs:CallFunction(oFuncCall, oResult)
? oResult:Emit()
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
nCount := oResult:SizeOfArray("value")
i := 0
DO WHILE i < nCount
oResult:I := i
? Str(oResult:IntOf("value[i]"))
i := i + 1
ENDDO
// ------------------------------------------------------------------------------
// Call the getEmployees() function
oFuncCall:Clear()
oFuncCall:UpdateString("name", "getEmployees")
nSuccess := oJs:CallFunction(oFuncCall, oResult)
? oResult:Emit()
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
nCount := oResult:SizeOfArray("value")
i := 0
DO WHILE i < nCount
oResult:I := i
? "name: " + oResult:StringOf("value[i].name")
? "role: " + oResult:StringOf("value[i].role")
? "id: " + Str(oResult:IntOf("value[i].id"))
? ""
i := i + 1
ENDDO
oSbScript:destroy()
oJs:destroy()
oResult:destroy()
oFuncCall:destroy()
JavaScript
function getDays() {
return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}
function getRange(start, end) {
var arr = [];
for (var i = start; i <= end; i++) {
arr.push(i);
}
return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]
function getEmployees() {
return [
{ id: 101, name: "Alice", role: "Dev" },
{ id: 102, name: "Bob", role: "Manager" }
];
}