Lua Requires Chilkat v11.4.0+
Lua
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- ----------------------------------------------------------------------------------
-- The Javascript functions called in this example are shown at the bottom of this page.
-- -----------------------------------------------------------------------------------
local sbScript = chilkat.StringBuilder.new()
local success = sbScript:load_file("js_function_returning_array.js", "utf-8")
if not success then
print(sbScript.last_error_text)
return
end
local js = chilkat.Js.new()
local result = chilkat.JsonObject.new()
result.emit_compact = false
-- Call Eval to add the function to the context's global object
success = js:eval(sbScript, result)
if not success then
-- Examine the result for an exception.
print(result:emit())
-- Also examine the LastErrorText.
print(js.last_error_text)
return
end
-- ------------------------------------------------------------------------------
-- Call each function
local funcCall = chilkat.JsonObject.new()
-- Create JSON specifying the function name and arguments
-- The function has no arguments, so we only specify the name.
funcCall:update_string("name", "getDays")
success = js:call_function(funcCall, result)
if not success then
-- Examine the result for an exception.
print(result:emit())
-- Also examine the LastErrorText.
print(js.last_error_text)
return
end
print(result:emit())
-- Output:
-- {
-- "type": "array",
-- "value": [
-- "Monday",
-- "Tuesday",
-- "Wednesday",
-- "Thursday",
-- "Friday"
-- ]
-- }
-- Access each array value..
local count = result:size_of_array("value")
local i = 0
while i < count do
result.i = i
print(result:string_of("value[i]"))
i = i + 1
end
-- ------------------------------------------------------------------------------
-- Call the getRange(start,end) function
funcCall:clear()
funcCall:update_string("name", "getRange")
funcCall:update_int("args[0]", 14)
funcCall:update_int("args[1]", 21)
success = js:call_function(funcCall, result)
print(result:emit())
-- Output:
-- {
-- "type": "array",
-- "value": [
-- 14,
-- 15,
-- 16,
-- 17,
-- 18,
-- 19,
-- 20,
-- 21
-- ]
-- }
count = result:size_of_array("value")
i = 0
while i < count do
result.i = i
print(result:int_of("value[i]"))
i = i + 1
end
-- ------------------------------------------------------------------------------
-- Call the getEmployees() function
funcCall:clear()
funcCall:update_string("name", "getEmployees")
success = js:call_function(funcCall, result)
print(result:emit())
-- Output:
-- {
-- "type": "array",
-- "value": [
-- {
-- "id": 101,
-- "name": "Alice",
-- "role": "Dev"
-- },
-- {
-- "id": 102,
-- "name": "Bob",
-- "role": "Manager"
-- }
-- ]
-- }
count = result:size_of_array("value")
i = 0
while i < count do
result.i = i
print("name: " .. result:string_of("value[i].name"))
print("role: " .. result:string_of("value[i].role"))
print("id: " .. result:int_of("value[i].id"))
print("")
i = i + 1
end
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" }
];
}