this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

add remap function (#2453)

* add remap function (fix #1848)

* [skip ci] update zig template

* use compatible signature

uses compatible signature. note that i was able to get a segfault from
Zig by using an pointer to an optional instead of an optional pointer.

* fix pocketpy(?)

authored by

Bulby and committed by
GitHub
8c55996e 3cf27c5e

+128 -14
+29 -3
demos/bunny/wasmmark/src/tic80.zig
··· 97 97 // import the RAW api 98 98 99 99 pub const raw = struct { 100 + pub const Flip = enum(u32) { 101 + no = 0, 102 + horizontal = 1, 103 + vertical = 2, 104 + both = 3, 105 + }; 106 + pub const Rotate = enum(u32) { 107 + no = 0, 108 + by90 = 1, 109 + by180 = 2, 110 + by270 = 3, 111 + }; 100 112 pub extern fn btn(id: i32) i32; 101 113 pub extern fn btnp(id: i32, hold: i32, period: i32) bool; 102 114 pub extern fn clip(x: i32, y: i32, w: i32, h: i32) void; ··· 112 124 pub extern fn key(keycode: i32) bool; 113 125 pub extern fn keyp(keycode: i32, hold: i32, period: i32) bool; 114 126 pub extern fn line(x0: i32, y0: i32, x1: i32, y1: i32, color: i32) void; 115 - pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: i32) void; 127 + // pass struct by pointer SHOULD:tm JUST WORK 128 + pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: ?*const RemapArgs) void; 116 129 pub extern fn memcpy(to: u32, from: u32, length: u32) void; 117 130 pub extern fn memset(addr: u32, value: u8, length: u32) void; 118 131 pub extern fn mget(x: i32, y: i32) i32; ··· 198 211 // TODO: remap should be what???? 199 212 // pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]u8, color_count: i32, scale: i32, remap: i32) void; 200 213 214 + const RemapArgs = extern struct { remap: *const fn (?*anyopaque, i32, i32, *RemapInfo) callconv(.C) void, data: ?*anyopaque, res_ptr: *RemapInfo }; 215 + pub const RemapInfo = extern struct { 216 + index: u8, 217 + flip: raw.Flip, 218 + rotate: raw.Rotate, 219 + }; 201 220 const MapArgs = struct { 202 221 x: i32 = 0, 203 222 y: i32 = 0, ··· 207 226 sy: i32 = 0, 208 227 transparent: []const u8 = &.{}, 209 228 scale: u8 = 1, 210 - remap: i32 = -1, // TODO 229 + remap: ?*const fn (i32, i32, *RemapInfo) void = null, // TODO 211 230 }; 212 231 232 + fn remap_wrapper(data: ?*anyopaque, x: i32, y: i32, info: *RemapInfo) callconv(.C) void { 233 + const fun: *const fn (i32, i32, *RemapInfo) void = @ptrCast(data orelse return); 234 + fun(x, y, info); 235 + } 213 236 pub fn map(args: MapArgs) void { 214 237 const color_count = @as(u8, @intCast(args.transparent.len)); 215 238 const colors = args.transparent.ptr; 216 239 std.debug.assert(color_count < 16); 217 - raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, args.remap); 240 + // why? 241 + var remapinfo = .{ .index = undefined, .flip = undefined, .rotate = undefined }; 242 + const remap_args: ?RemapArgs = if (args.remap) |it| .{ .remap = &remap_wrapper, .data = @ptrCast(@constCast(it)), .res_ptr = &remapinfo } else null; 243 + raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, if (remap_args) |it| &it else null); 218 244 } 219 245 220 246 pub fn pix(x: i32, y: i32, color: u8) void {
+29 -3
demos/wasm/src/tic80.zig
··· 97 97 // import the RAW api 98 98 99 99 pub const raw = struct { 100 + pub const Flip = enum(u32) { 101 + no = 0, 102 + horizontal = 1, 103 + vertical = 2, 104 + both = 3, 105 + }; 106 + pub const Rotate = enum(u32) { 107 + no = 0, 108 + by90 = 1, 109 + by180 = 2, 110 + by270 = 3, 111 + }; 100 112 pub extern fn btn(id: i32) i32; 101 113 pub extern fn btnp(id: i32, hold: i32, period: i32) bool; 102 114 pub extern fn clip(x: i32, y: i32, w: i32, h: i32) void; ··· 112 124 pub extern fn key(keycode: i32) bool; 113 125 pub extern fn keyp(keycode: i32, hold: i32, period: i32) bool; 114 126 pub extern fn line(x0: i32, y0: i32, x1: i32, y1: i32, color: i32) void; 115 - pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: i32) void; 127 + // pass struct by pointer SHOULD:tm JUST WORK 128 + pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: ?*const RemapArgs) void; 116 129 pub extern fn memcpy(to: u32, from: u32, length: u32) void; 117 130 pub extern fn memset(addr: u32, value: u8, length: u32) void; 118 131 pub extern fn mget(x: i32, y: i32) i32; ··· 198 211 // TODO: remap should be what???? 199 212 // pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]u8, color_count: i32, scale: i32, remap: i32) void; 200 213 214 + const RemapArgs = extern struct { remap: *const fn (?*anyopaque, i32, i32, *RemapInfo) callconv(.C) void, data: ?*anyopaque, res_ptr: *RemapInfo }; 215 + pub const RemapInfo = extern struct { 216 + index: u8, 217 + flip: raw.Flip, 218 + rotate: raw.Rotate, 219 + }; 201 220 const MapArgs = struct { 202 221 x: i32 = 0, 203 222 y: i32 = 0, ··· 207 226 sy: i32 = 0, 208 227 transparent: []const u8 = &.{}, 209 228 scale: u8 = 1, 210 - remap: i32 = -1, // TODO 229 + remap: ?*const fn (i32, i32, *RemapInfo) void = null, // TODO 211 230 }; 212 231 232 + fn remap_wrapper(data: ?*anyopaque, x: i32, y: i32, info: *RemapInfo) callconv(.C) void { 233 + const fun: *const fn (i32, i32, *RemapInfo) void = @ptrCast(data orelse return); 234 + fun(x, y, info); 235 + } 213 236 pub fn map(args: MapArgs) void { 214 237 const color_count = @as(u8, @intCast(args.transparent.len)); 215 238 const colors = args.transparent.ptr; 216 239 std.debug.assert(color_count < 16); 217 - raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, args.remap); 240 + // why? 241 + var remapinfo = .{ .index = undefined, .flip = undefined, .rotate = undefined }; 242 + const remap_args: ?RemapArgs = if (args.remap) |it| .{ .remap = &remap_wrapper, .data = @ptrCast(@constCast(it)), .res_ptr = &remapinfo } else null; 243 + raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, if (remap_args) |it| &it else null); 218 244 } 219 245 220 246 pub fn pix(x: i32, y: i32, color: u8) void {
+41 -5
src/api/wasm.c
··· 42 42 #include "m3_exec_defs.h" 43 43 #include "m3_exception.h" 44 44 #include "m3_env.h" 45 + #include "m3_core.h" 45 46 46 47 static const char TicCore[] = "_TIC80"; 47 48 ··· 661 662 m3ApiSuccess(); 662 663 } 663 664 665 + struct MapData { 666 + int32_t func_index; 667 + uint32_t user_data; 668 + uint32_t res_ptr; 669 + }; 670 + struct RemapInfo { 671 + uint32_t data; 672 + IM3Function fun; 673 + tic_core* core; 674 + uint8_t* mem; 675 + RemapResult* wasm_result; 676 + }; 677 + static void wasm_remap(void* data, s32 x, s32 y, RemapResult* result) { 678 + if (data == NULL) return; 679 + struct RemapInfo* rdata = (struct RemapInfo*) data; 680 + IM3Function fun = rdata->fun; 681 + // : ) 682 + uint8_t* _mem = rdata->mem; 683 + 684 + *rdata->wasm_result = *result; 685 + M3Result res = m3_CallV(fun, rdata->data, x, y, m3ApiPtrToOffset(rdata->wasm_result)); 686 + if (res) { 687 + rdata->core->data->error(rdata->core->data->data, res); 688 + return; 689 + } 690 + 691 + *result = *rdata->wasm_result; 692 + } 664 693 m3ApiRawFunction(wasmtic_map) 665 694 { 666 695 m3ApiGetArg (int32_t, x) ··· 674 703 if (trans_colors == NULL) { 675 704 colorCount = 0; 676 705 } m3ApiGetArg (int8_t, scale) 677 - // TODO: actually test that this works 678 - m3ApiGetArg (int32_t, remap) 706 + m3ApiGetArg (i32, map_data_ptr) 707 + 679 708 709 + tic_mem* tic = (tic_mem*)getWasmCore(runtime); 710 + // depends on their only being 1 module, which SHOULD:tm: be true 711 + struct RemapInfo info = { .mem = _mem, .core = (tic_core*)tic }; 712 + if (map_data_ptr > 0) { 713 + struct MapData* map_data = (struct MapData*)m3ApiOffsetToPtr(map_data_ptr); 714 + m3_GetTableFunction(&info.fun, runtime->modules, map_data->func_index); 715 + info.data = map_data->user_data; 716 + info.wasm_result = (RemapResult*)m3ApiOffsetToPtr(map_data->res_ptr); 717 + } 680 718 // defaults 681 719 if (x == -1) { x = 0; } 682 720 if (y == -1) { y = 0; } ··· 684 722 if (h == -1) { h = 17; } 685 723 if (scale == -1) { scale = 1; } 686 724 687 - tic_mem* tic = (tic_mem*)getWasmCore(runtime); 688 - 689 - tic_api_map(tic, x, y, w, h, sx, sy, trans_colors, colorCount, scale, NULL, NULL); 725 + tic_api_map(tic, x, y, w, h, sx, sy, trans_colors, colorCount, scale, map_data_ptr > 0 ? &wasm_remap : NULL, map_data_ptr > 0 ? &info : NULL); 690 726 691 727 m3ApiSuccess(); 692 728 }
+29 -3
templates/zig/src/tic80.zig
··· 97 97 // import the RAW api 98 98 99 99 pub const raw = struct { 100 + pub const Flip = enum(u32) { 101 + no = 0, 102 + horizontal = 1, 103 + vertical = 2, 104 + both = 3, 105 + }; 106 + pub const Rotate = enum(u32) { 107 + no = 0, 108 + by90 = 1, 109 + by180 = 2, 110 + by270 = 3, 111 + }; 100 112 pub extern fn btn(id: i32) i32; 101 113 pub extern fn btnp(id: i32, hold: i32, period: i32) bool; 102 114 pub extern fn clip(x: i32, y: i32, w: i32, h: i32) void; ··· 112 124 pub extern fn key(keycode: i32) bool; 113 125 pub extern fn keyp(keycode: i32, hold: i32, period: i32) bool; 114 126 pub extern fn line(x0: i32, y0: i32, x1: i32, y1: i32, color: i32) void; 115 - pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: i32) void; 127 + // pass struct by pointer SHOULD:tm JUST WORK 128 + pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]const u8, color_count: i32, scale: i32, remap: ?*const RemapArgs) void; 116 129 pub extern fn memcpy(to: u32, from: u32, length: u32) void; 117 130 pub extern fn memset(addr: u32, value: u8, length: u32) void; 118 131 pub extern fn mget(x: i32, y: i32) i32; ··· 198 211 // TODO: remap should be what???? 199 212 // pub extern fn map(x: i32, y: i32, w: i32, h: i32, sx: i32, sy: i32, trans_colors: ?[*]u8, color_count: i32, scale: i32, remap: i32) void; 200 213 214 + const RemapArgs = extern struct { remap: *const fn (?*anyopaque, i32, i32, *RemapInfo) callconv(.C) void, data: ?*anyopaque, res_ptr: *RemapInfo }; 215 + pub const RemapInfo = extern struct { 216 + index: u8, 217 + flip: raw.Flip, 218 + rotate: raw.Rotate, 219 + }; 201 220 const MapArgs = struct { 202 221 x: i32 = 0, 203 222 y: i32 = 0, ··· 207 226 sy: i32 = 0, 208 227 transparent: []const u8 = &.{}, 209 228 scale: u8 = 1, 210 - remap: i32 = -1, // TODO 229 + remap: ?*const fn (i32, i32, *RemapInfo) void = null, // TODO 211 230 }; 212 231 232 + fn remap_wrapper(data: ?*anyopaque, x: i32, y: i32, info: *RemapInfo) callconv(.C) void { 233 + const fun: *const fn (i32, i32, *RemapInfo) void = @ptrCast(data orelse return); 234 + fun(x, y, info); 235 + } 213 236 pub fn map(args: MapArgs) void { 214 237 const color_count = @as(u8, @intCast(args.transparent.len)); 215 238 const colors = args.transparent.ptr; 216 239 std.debug.assert(color_count < 16); 217 - raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, args.remap); 240 + // why? 241 + var remapinfo = .{ .index = undefined, .flip = undefined, .rotate = undefined }; 242 + const remap_args: ?RemapArgs = if (args.remap) |it| .{ .remap = &remap_wrapper, .data = @ptrCast(@constCast(it)), .res_ptr = &remapinfo } else null; 243 + raw.map(args.x, args.y, args.w, args.h, args.sx, args.sy, colors, color_count, args.scale, if (remap_args) |it| &it else null); 218 244 } 219 245 220 246 pub fn pix(x: i32, y: i32, color: u8) void {