Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.4.0+

Call a JavaScript Function Returning a Boolean

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns a boolean.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This is the JavaScript function we'll call:

-- function isEven(number) {
--     return number % 2 === 0;
-- }

local sbScript = chilkat.StringBuilder.new()
sbScript:append("function isEven(number) { return number % 2 === 0; }")

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
local 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 isEven(8)

local funcCall = chilkat.JsonObject.new()

-- Create JSON specifying the function name and arguments

-- {
--   "name": "isEven",
--   "args": [ 8 ]
-- }

funcCall:update_string("name", "isEven")
funcCall:update_int("args[0]", 8)

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": "bool",
--   "value": true
-- }

local retval = result:bool_of("value")
print(retval)

-- Output:
-- true