Lua Requires Chilkat v11.4.0+
Lua
Call a JavaScript Function Passing an Array Argument
See more JavaScript Examples
Demonstrates how to call a JavaScript function with an argument that is an array.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This is the JavaScript function we'll call:
-- function calculateTotal(numbers) {
-- console.log(numbers);
-- let total = 0;
--
-- // Loop through every number in the array
-- for (const num of numbers) {
-- console.log(num);
-- total += num;
-- }
--
-- return total;
-- }
local sbScript = chilkat.StringBuilder.new()
local success = sbScript:load_file("js_function_array_arg.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 the function calculateTotal(numbers)
local funcCall = chilkat.JsonObject.new()
funcCall.emit_compact = false
-- Create JSON specifying the function name and arguments
-- In this case, there is only 1 argument, and it is an array.
funcCall:update_string("name", "calculateTotal")
-- Create the arguments array.
local argsArray = chilkat.JsonArray.new()
-- The 1st argument in the arguments array is itself an array.
-- Passing -1 indicates to append to the array.
local arg = chilkat.JsonArray.new()
argsArray:add_array_at2(-1, arg)
-- Fill in the values for the 1st argument.
arg:add_number_at(-1, "10.50")
arg:add_number_at(-1, "20.00")
arg:add_number_at(-1, "5.25")
-- Add the "args" array to the funcCall.
funcCall:append_array_copy("args", argsArray)
print(funcCall:emit())
-- The funcCall is as follows. Notice that the 1st (and only) argument is an array.
-- {
-- "name": "calculateTotal",
-- "args": [
-- [
-- 10.50,
-- 20.00,
-- 5.25
-- ]
-- ]
-- }
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())
-- Result:
-- {
-- "type": "double",
-- "value": 35.75
-- }
-- The function also emitted text to the console.
local sbOut = chilkat.StringBuilder.new()
js:console_output_sb(sbOut)
print(sbOut:get_as_string())
-- Output:
-- 10.5,20,5.25
-- 10.5
-- 20
-- 5.25
-- -----------------------------------------------------------
-- Note: If the array argument is simple, this is an alternative
-- and simpler way of creating the funcCall:
funcCall:clear()
funcCall:update_string("name", "calculateTotal")
funcCall:update_number("args[0][0]", "10.50")
funcCall:update_number("args[0][1]", "20.00")
funcCall:update_number("args[0][2]", "5.25")
print(funcCall:emit())
JavaScript
function calculateTotal(numbers) {
console.log(numbers);
let total = 0;
// Loop through every number in the array
for (const num of numbers) {
console.log(num);
total += num;
}
return total;
}