-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose Luau bytecode loading API, add example
- Loading branch information
Showing
5 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//! Run Luau bytecode | ||
|
||
// How to recompile `test.luau.bin` bytecode binary: | ||
// | ||
// luau-compile --binary test.luau > test.bc | ||
// | ||
// This may be required if the Luau version gets upgraded. | ||
|
||
const std = @import("std"); | ||
|
||
// The ziglua module is made available in build.zig | ||
const ziglua = @import("ziglua"); | ||
|
||
pub fn main() anyerror!void { | ||
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
const allocator = gpa.allocator(); | ||
defer _ = gpa.deinit(); | ||
|
||
// Initialize The Lua vm and get a reference to the main thread | ||
var lua = try ziglua.Lua.init(allocator); | ||
defer lua.deinit(); | ||
|
||
// Open all Lua standard libraries | ||
lua.openLibs(); | ||
|
||
// Load bytecode | ||
const filename = "examples/test.luau.bin"; | ||
const bc = std.fs.cwd().readFileAlloc(allocator, filename, std.math.maxInt(u32)) catch { | ||
std.log.err("failed to open bytecode {s}", .{filename}); | ||
return; | ||
}; | ||
defer allocator.free(bc); | ||
try lua.loadBytecode("...", bc); | ||
try lua.protectedCall(0, 0, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--!strict | ||
|
||
function ispositive(x : number) : string | ||
if x > 0 then | ||
return "yes" | ||
else | ||
return "no" | ||
end | ||
end | ||
|
||
local result : string | ||
result = ispositive(1) | ||
print("result is positive:", result) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters