Zig Requires Chilkat v11.4.0+
Zig
Call a JavaScript Function Returning an Array
See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// ----------------------------------------------------------------------------------
// The Javascript functions called in this example are shown at the bottom of this page.
// -----------------------------------------------------------------------------------
const sb_script = try chilkat.StringBuilder.init();
defer sb_script.deinit();
sb_script.loadFile("js_function_returning_array.js", "utf-8") catch {
std.debug.print("{s}\n", .{try sb_script.getLastErrorText(alloc)});
return;
};
const js = try chilkat.Js.init();
defer js.deinit();
const result = try chilkat.JsonObject.init();
defer result.deinit();
result.setEmitCompact(false);
// Call Eval to add the function to the context's global object
js.eval(sb_script, result) catch {
// Examine the result for an exception.
std.debug.print("{s}\n", .{try result.emit(alloc)});
// Also examine the LastErrorText.
std.debug.print("{s}\n", .{try js.getLastErrorText(alloc)});
return;
};
// ------------------------------------------------------------------------------
// Call each function
const func_call = try chilkat.JsonObject.init();
defer func_call.deinit();
// Create JSON specifying the function name and arguments
// The function has no arguments, so we only specify the name.
func_call.updateString("name", "getDays") catch {};
js.callFunction(func_call, result) catch {
// Examine the result for an exception.
std.debug.print("{s}\n", .{try result.emit(alloc)});
// Also examine the LastErrorText.
std.debug.print("{s}\n", .{try js.getLastErrorText(alloc)});
return;
};
std.debug.print("{s}\n", .{try result.emit(alloc)});
// Output:
// {
// "type": "array",
// "value": [
// "Monday",
// "Tuesday",
// "Wednesday",
// "Thursday",
// "Friday"
// ]
// }
// Access each array value..
var count: i32 = result.sizeOfArray("value");
var i: i32 = 0;
while (i < count) {
result.setI(i);
std.debug.print("{s}\n", .{try result.stringOf(alloc, "value[i]")});
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getRange(start,end) function
func_call.clear();
func_call.updateString("name", "getRange") catch {};
func_call.updateInt("args[0]", 14) catch {};
func_call.updateInt("args[1]", 21) catch {};
try js.callFunction(func_call, result);
std.debug.print("{s}\n", .{try result.emit(alloc)});
// Output:
// {
// "type": "array",
// "value": [
// 14,
// 15,
// 16,
// 17,
// 18,
// 19,
// 20,
// 21
// ]
// }
count = result.sizeOfArray("value");
i = 0;
while (i < count) {
result.setI(i);
std.debug.print("{d}\n", .{result.intOf("value[i]")});
i = i + 1;
}
// ------------------------------------------------------------------------------
// Call the getEmployees() function
func_call.clear();
func_call.updateString("name", "getEmployees") catch {};
try js.callFunction(func_call, result);
std.debug.print("{s}\n", .{try result.emit(alloc)});
// Output:
// {
// "type": "array",
// "value": [
// {
// "id": 101,
// "name": "Alice",
// "role": "Dev"
// },
// {
// "id": 102,
// "name": "Bob",
// "role": "Manager"
// }
// ]
// }
count = result.sizeOfArray("value");
i = 0;
while (i < count) {
result.setI(i);
std.debug.print("name: {s}\n", .{try result.stringOf(alloc, "value[i].name")});
std.debug.print("role: {s}\n", .{try result.stringOf(alloc, "value[i].role")});
std.debug.print("id: {d}\n", .{result.intOf("value[i].id")});
std.debug.print("\n", .{});
i = i + 1;
}
}
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" }
];
}