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

Call a JavaScript Function Passing an Object Argument

See more JavaScript Examples

Demonstrates how to call a JavaScript function with an argument that is an object.

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 describeCar(car) {
    //     console.log(`This is a ${car.year} ${car.make} ${car.model}.`);
    // }

    const sb_script = try chilkat.StringBuilder.init();
    defer sb_script.deinit();
    sb_script.append("function describeCar(car) { console.log(`This is a ${car.year} ${car.make} ${car.model}.`); }") catch {};

    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 describeCar(car)

    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 object.

    // {
    //   "name": "describeCar",
    //   "args": [
    //     {
    //       "make": "Toyota",
    //       "model": "Corolla",
    //       "year": 2022
    //     }
    //   ]
    // }

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

    // Create the JSON object that is the argument.
    const arg = try chilkat.JsonObject.init();
    defer arg.deinit();
    arg.updateString("make", "Toyota") catch {};
    arg.updateString("model", "Corolla") catch {};
    arg.updateInt("year", 2022) catch {};

    // Create the arguments array.
    const args_array = try chilkat.JsonArray.init();
    defer args_array.deinit();
    args_array.addObjectCopyAt(0, arg) 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)});

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

    // The describeCar JavaScript function returns nothing.
    // Therefore, the result is "undefined".

    // {
    //   "type": "undefined",
    //   "value": "undefined"
    // }

    // However, the function 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:
    // This is a 2022 Toyota Corolla.

    // -----------------------------------------------------------
    // Note: If the object argument is simple, this is an alternative
    // and simpler way of creating the funcCall:

    func_call.clear();
    func_call.updateString("name", "describeCar") catch {};
    func_call.updateString("args[0].make", "Toyota") catch {};
    func_call.updateString("args[0].model", "Corolla") catch {};
    func_call.updateInt("args[0].year", 2022) catch {};
    std.debug.print("{s}\n", .{try func_call.emit(alloc)});
}