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

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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;
    // }

    const sb_script = try chilkat.StringBuilder.init();
    defer sb_script.deinit();
    sb_script.loadFile("js_function_array_arg.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 the function calculateTotal(numbers)

    const func_call = try chilkat.JsonObject.init();
    defer func_call.deinit();
    func_call.setEmitCompact(false);

    // Create JSON specifying the function name and arguments
    // In this case, there is only 1 argument, and it is an array.

    func_call.updateString("name", "calculateTotal") catch {};

    // Create the arguments array.
    const args_array = try chilkat.JsonArray.init();
    defer args_array.deinit();

    // The 1st argument in the arguments array is itself an array.
    // Passing -1 indicates to append to the array.
    const arg = try chilkat.JsonArray.init();
    defer arg.deinit();
    args_array.addArrayAt2(-1, arg) catch {};

    // Fill in the values for the 1st argument.
    arg.addNumberAt(-1, "10.50") catch {};
    arg.addNumberAt(-1, "20.00") catch {};
    arg.addNumberAt(-1, "5.25") catch {};

    // Add the "args" array to the funcCall.
    func_call.appendArrayCopy("args", args_array) catch {};

    std.debug.print("{s}\n", .{try func_call.emit(alloc)});

    // The funcCall is as follows.  Notice that the 1st (and only) argument is an array.

    // {
    //   "name": "calculateTotal",
    //   "args": [
    //     [
    //       10.50,
    //       20.00,
    //       5.25
    //     ]
    //   ]
    // }

    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)});

    // Result:
    // {
    //   "type": "double",
    //   "value": 35.75
    // }

    // The function also emitted text to the console.

    const sb_out = try chilkat.StringBuilder.init();
    defer sb_out.deinit();
    js.consoleOutputSb(sb_out);
    std.debug.print("{s}\n", .{try sb_out.getAsString(alloc)});

    // 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:

    func_call.clear();
    func_call.updateString("name", "calculateTotal") catch {};
    func_call.updateNumber("args[0][0]", "10.50") catch {};
    func_call.updateNumber("args[0][1]", "20.00") catch {};
    func_call.updateNumber("args[0][2]", "5.25") catch {};
    std.debug.print("{s}\n", .{try func_call.emit(alloc)});
}
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;
}