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

Call a JavaScript Function Returning an Integer

See more JavaScript Examples

Demonstrates how to call a JavaScript function that returns an integer.

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

    // ----------------------------------------------------------------------------------
    // The Javascript function called in this example is shown at the bottom of this page.
    // -----------------------------------------------------------------------------------

    // In this example, we'll load the Javascript function definition from a file.
    // It doesn't need to come from a file.  It could just as easily be loaded from a string.
    const sb_script = try chilkat.StringBuilder.init();
    defer sb_script.deinit();
    sb_script.loadFile("js_call_function.js", "utf-8") catch {
        std.debug.print("{s}\n", .{try sb_script.getLastErrorText(alloc)});
        return;
    };

    // Note: Each instance of a Chilkat Js object automatically establishes
    // its own internal runtime and context.  Applications do not need to explicitly create
    // the JavaScript runtime or context.
    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 (shown at the bottom of this page) 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;
    };

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

    // The expected output is "undefined":

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

    //    When Eval processes a script containing only a function declaration,
    //    it successfully performs the action (the function becomes defined).
    //    However, since the script consists of a statement that produces no value,
    //    the script's overall completion value is empty. In JavaScript, the
    //    absence of a value is represented by `undefined`.
    //
    //    Therefore, the Eval call returns `undefined`.

    // ------------------------------------------------------------------------------
    // Call the function calculateScore("Player1", 10, 20)

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

    // Create JSON defining the function call:

    // {
    //   "name": "calculateScore",
    //   "args": [ "Player1", 10, 20 ]
    // }

    func_call.updateString("name", "calculateScore") catch {};
    func_call.updateString("args[0]", "Player1") catch {};
    func_call.updateInt("args[1]", 10) catch {};
    func_call.updateInt("args[2]", 20) catch {};

    func_call.setEmitCompact(false);
    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)});

    // Output:

    // {
    //   "type": "int",
    //   "value": 37
    // }

    const retval = result.intOf("value");
    std.debug.print("retval = {d}\n", .{retval});
}
JavaScript
function calculateScore(name, pointsA, pointsB) {
    // Return length of name + both scores
    return name.length + pointsA + pointsB;
    }