robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40924 from justinmk/optdicte533ec5160

master
4.3 KiB136 linesraw
1//! "nlua" is an abbreviation for nvim flavored lua, i e lua with the
2//! extended standard library functionality added by nvim such as json, mpack
3//! and libuv and a range of vim.* utility functions.
4//!
5//! nlua0 is an interpreter for the "bootstrap" lua code we need to run before
6//! nvim can be built, in order to run lua scripts which process and generate
7//! more .c code, which still need these extensions.
8const std = @import("std");
9const ziglua = @import("ziglua");
10const options = @import("options");
11
12const embedded_data = @import("embedded_data");
13
14// these are common dependencies used by many generators
15const hashy = @embedFile("gen/hashy.lua");
16const keyset = @embedFile("gen/keyset.lua");
17const c_grammar = @embedFile("gen/c_grammar.lua");
18
19const Lua = ziglua.Lua;
20
21extern "c" fn luaopen_mpack(ptr: *anyopaque) c_int;
22extern "c" fn luaopen_lpeg(ptr: *anyopaque) c_int;
23extern "c" fn luaopen_bit(ptr: *anyopaque) c_int;
24extern "c" fn luaopen_luv(ptr: *anyopaque) c_int;
25
26fn init_lua() !*Lua {
27    // Initialize the Lua vm
28    var lua = try Lua.init(std.heap.c_allocator);
29    lua.openLibs();
30
31    // this sets _G.vim by itself, so we don't need to
32    try lua.loadBuffer(embedded_data.shared_module, "shared.lua");
33    lua.call(.{ .results = 1 });
34
35    try lua.loadBuffer(embedded_data.inspect_module, "inspect.lua");
36    lua.call(.{ .results = 1 });
37    lua.setField(-2, "inspect");
38
39    try lua.loadBuffer(embedded_data.iter_module, "iter.lua");
40    lua.call(.{ .results = 1 });
41    lua.setField(-2, "iter");
42
43    _ = lua.getGlobal("package");
44    _ = lua.getField(-1, "preload");
45    try lua.loadBuffer(hashy, "hashy.lua"); // [package, preload, hashy]
46    lua.setField(-2, "gen.hashy");
47    try lua.loadBuffer(keyset, "keyset.lua"); // [package, preload, keyset]
48    lua.setField(-2, "gen.keyset");
49    try lua.loadBuffer(c_grammar, "c_grammar.lua"); // [package, preload, c_grammar]
50    lua.setField(-2, "gen.c_grammar");
51    lua.pop(2);
52
53    const retval = luaopen_mpack(lua);
54    if (retval != 1) return error.LoadError;
55    _ = lua.getField(-1, "NIL"); // [vim, mpack, NIL]
56    lua.setField(-3, "NIL"); // vim.NIL = mpack.NIL (wow BOB wow)
57    lua.setField(-2, "mpack");
58
59    const retval2 = luaopen_lpeg(lua);
60    if (retval2 != 1) return error.LoadError;
61    lua.setField(-3, "lpeg");
62
63    const retval3 = luaopen_luv(lua);
64    if (retval3 != 1) return error.LoadError;
65    lua.setField(-3, "uv");
66
67    lua.pop(2);
68
69    if (!options.use_luajit) {
70        lua.pop(luaopen_bit(lua));
71    }
72    return lua;
73}
74
75pub fn main(init: std.process.Init) !void {
76    const args = init.minimal.args;
77
78    const lua = try init_lua();
79    defer lua.deinit();
80
81    if (args.vector.len < 2) {
82        std.debug.print("USAGE: nlua0 script.lua args...\n\n", .{});
83        return;
84    }
85    lua.createTable(@intCast(args.vector.len - 2), 1);
86
87    var iter = try init.minimal.args.iterateAllocator(init.arena.allocator());
88    _ = iter.skip();
89    var i: u32 = 0;
90    var firstarg: [:0]const u8 = undefined;
91    while (iter.next()) |val| : (i += 1) {
92        _ = lua.pushString(val);
93        if (i == 0) {
94            firstarg = try lua.toString(-1); // preserved on lua heap..
95        }
96        lua.setIndexRaw(-2, @intCast(i));
97    }
98    lua.setGlobal("arg");
99
100    _ = lua.getGlobal("debug");
101    _ = lua.getField(-1, "traceback");
102    try lua.loadFile(firstarg);
103    lua.protectedCall(.{ .msg_handler = -2 }) catch |e| {
104        if (e == error.LuaRuntime) {
105            const msg = try lua.toString(-1);
106            std.debug.print("{s}\n", .{msg});
107        }
108        return e;
109    };
110}
111
112fn do_ret1(lua: *Lua, str: [:0]const u8) !void {
113    try lua.loadString(str);
114    try lua.protectedCall(.{ .results = 1 });
115}
116
117test "simple test" {
118    const lua = try init_lua();
119    defer lua.deinit();
120
121    try do_ret1(lua, "return vim.isarray({2,3})");
122    try std.testing.expectEqual(true, lua.toBoolean(-1));
123    lua.pop(1);
124
125    try do_ret1(lua, "return vim.isarray({a=2,b=3})");
126    try std.testing.expectEqual(false, lua.toBoolean(-1));
127    lua.pop(1);
128
129    try do_ret1(lua, "return vim.inspect(vim.mpack.decode('\\146\\42\\69'))");
130    try std.testing.expectEqualStrings("{ 42, 69 }", try lua.toString(-1));
131    lua.pop(1);
132
133    try do_ret1(lua, "return require'bit'.band(7,12)");
134    try std.testing.expectEqualStrings("4", try lua.toString(-1));
135    lua.pop(1);
136}