const std = @import("std");
const Interop = @import("../Interop.zig");
const zlua = @import("zlua");
const lua = zlua.Lua;

pub fn ImportFns() void {
    Interop.AddFnToLib(add_numbers, "add");
    Interop.AddFnToLib(greet, "greet");
}

fn add_numbers(luaState: ?*zlua.LuaState ) callconv(.c) c_int {
    const context: *zlua.Lua = @ptrCast(luaState.?);                      // Cast the LuaState to a Lua Runtime Object
    const num1 = lua.checkNumber(context, 1);               // Get arguemnt 1 from the lua stack
    const num2 = lua.checkNumber(context, 2);               // Get arguemnt 2 from the lua stack
    lua.pushNumber(context, num1 + num2);                          // Push result onto the lua stack
    return 1;                                                             // Number of results on lua stack
}

fn greet(luaState: ?*zlua.LuaState ) callconv(.c) c_int {
    const context: *zlua.Lua = @ptrCast(luaState.?);                      // Cast the LuaState to a Lua Runtime Object
    const num1 = lua.checkString(context, 1);      // Get arguemnt 1 from the lua stack
    std.debug.print("{s}", .{num1});
    return 0;                                                             // Number of results on lua stack
}