Rename preinit to init and add more instructions

This commit is contained in:
luccie-cmd 2025-08-06 18:51:48 +02:00
parent 8c740f3793
commit 8282a299b1
5 changed files with 34 additions and 37 deletions

Binary file not shown.

View file

@ -1,19 +0,0 @@
pub extern fn logErr(
string: *const u8,
len: u64,
) callconv(.c) void;
pub extern fn logWarn(
string: *const u8,
len: u64,
) callconv(.c) void;
pub extern fn logInfo(
string: *const u8,
len: u64,
) callconv(.c) void;
pub extern fn logDebug(
string: *const u8,
len: u64,
) callconv(.c) void;

View file

@ -318,8 +318,8 @@ pub fn parseModule(self: *Parser) !void {
else => return Error.invalid_section,
};
}
if (self.exports.preinit != null and self.exports.preinit.? != 0){
self.exports.preinit.? -= self.importCount;
if (self.exports.init != null and self.exports.init.? != 0){
self.exports.init.? -= self.importCount;
}
}
@ -385,7 +385,7 @@ fn parseImportsec(self: *Parser) !void {
} else if (std.mem.eql(u8, i.name, "logErr")) {
self.exports.logErr = index;
} else {
std.log.warn("imported function {s} not supported\n", .{i.name});
std.debug.panic("imported function {s} not supported\n", .{i.name});
}
index += 1;
},
@ -546,8 +546,8 @@ fn parseExportsec(self: *Parser) !void {
for (exports) |e| {
switch (e.exportdesc) {
.func => {
if (std.mem.eql(u8, e.name, "preinit")) {
self.exports.preinit = e.exportdesc.func;
if (std.mem.eql(u8, e.name, "init")) {
self.exports.init = e.exportdesc.func;
} else {
std.log.warn("exported function {s} not supported\n", .{e.name});
}

View file

@ -33,14 +33,14 @@ pub const Function = struct { func_type: Functype, typ: union(enum) {
} };
pub const ExportFunction = enum {
preinit,
init,
logErr,
logWarn,
logInfo,
logDebug,
};
pub const Exports = struct {
preinit: ?u32 = null,
init: ?u32 = null,
logErr: ?u32 = null,
logWarn: ?u32 = null,
logInfo: ?u32 = null,
@ -372,13 +372,21 @@ pub const Runtime = struct {
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b > a))) });
},
.i32_le_s => @panic("UNIMPLEMENTED"),
.i32_le_s => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b <= a))) });
},
.i32_le_u => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b <= a))) });
},
.i32_ge_s => @panic("UNIMPLEMENTED"),
.i32_ge_s => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = @intCast(@as(u1, @bitCast(b >= a))) });
},
.i32_ge_u => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
@ -471,8 +479,16 @@ pub const Runtime = struct {
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = (b << @as(u5, @intCast(a))) });
},
.i32_shr_s => @panic("UNIMPLEMENTED"),
.i32_shr_u => @panic("UNIMPLEMENTED"),
.i32_shr_s => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = (b >> @as(u5, @intCast(a))) });
},
.i32_shr_u => {
const a = self.stack.pop().?.i32;
const b = self.stack.pop().?.i32;
try self.stack.append(Value{ .i32 = (b >> @as(u5, @intCast(a))) });
},
.i32_rotl => @panic("UNIMPLEMENTED"),
.i32_rotr => @panic("UNIMPLEMENTED"),
@ -581,11 +597,11 @@ pub const Runtime = struct {
// TODO: Do name resolution at parseTime
pub fn callExternal(self: *Runtime, allocator: Allocator, name: ExportFunction, parameters: []Value) !void {
switch (name) {
.preinit => {
if (self.module.exports.preinit) |func| {
.init => {
if (self.module.exports.init) |func| {
try self.call(allocator, func, parameters);
} else {
std.debug.panic("Function preinit unavailable\n", .{});
std.debug.panic("Function init unavailable\n", .{});
}
},
else => {

View file

@ -18,8 +18,8 @@ fn init_mods() void {
var global_runtime = mods.GlobalRuntime.init(allocator);
defer global_runtime.deinit();
// const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm");
const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm");
const file = std.fs.cwd().openFile("assets/mods/core.wasm", .{}) catch @panic("Couldn't open assets/mods/core.wasm");
// const file = std.fs.cwd().openFile("./test.wasm", .{}) catch @panic("Couldn't open test.wasm");
const all = file.readToEndAlloc(allocator, 1_000_000) catch @panic("Unable to read the file"); // 1 MB
defer allocator.free(all);
var parser = mods.Parser.init(allocator, all) catch @panic("Failed to init parser");
@ -38,9 +38,9 @@ fn init_mods() void {
defer runtime.deinit(allocator);
var parameters = [_]mods.VM.Value{.{ .i32 = 17 }};
runtime.callExternal(allocator, .preinit, &parameters) catch @panic("Failed to call to preinit");
runtime.callExternal(allocator, .init, &parameters) catch @panic("Failed to call to init");
const result = runtime.stack.pop().?;
std.debug.print("Result of preinit: {any}\n", .{result});
std.debug.print("Result of init: {any}\n", .{result});
}
export fn sideros_init(init: api.GameInit) callconv(.c) void {