Skip to content

Commit 9d66a29

Browse files
committed
bump to latest mruby (3.4.0)
- had to remove custom allocator examples for now, since `mrb_open_allocf` was removed - upstream added `y.tab.c` back, so no need for a fork anymore (yay!)
1 parent 2c8f593 commit 9d66a29

File tree

5 files changed

+16
-153
lines changed

5 files changed

+16
-153
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ SPDX-License-Identifier: MIT
66

77
## [Unreleased]
88

9-
- TODO
9+
## [0.2.0] - 2025-06-01
10+
11+
- bump to latest mruby (3.4.0), use upstream instead of a fork
1012

1113
## [0.1.1] - 2025-02-20
1214

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ $ zig build mirb
9898
mirb - Embeddable Interactive Ruby Shell
9999

100100
> MRUBY_VERSION
101-
=> "3.3.0"
101+
=> "3.4.0"
102102
```
103103

104104
### Examples

build.zig

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ pub fn build(b: *std.Build) !void {
280280
mrbc_cmd.addArgs(&.{ "-B", "mrblib_irep", "-o" });
281281
const mrblib_rbfiles_c = mrbc_cmd.addOutputFileArg("mrblib_rbfiles.c");
282282
for ([_][]const u8{
283-
"mrblib/00class.rb",
284-
"mrblib/00kernel.rb",
285283
"mrblib/10error.rb",
286284
"mrblib/array.rb",
287285
"mrblib/compar.rb",
@@ -713,7 +711,7 @@ pub fn build(b: *std.Build) !void {
713711
// "mrbgems/mruby-compiler/core/y.tab.c",
714712
// "mrbgems/mruby-compiler/core/codegen.c",
715713

716-
"mrbgems/mruby-bin-strip/tools/mruby-strip/mruby-strip.c",
714+
"mrbgems/mruby-bin-strip/tools/mruby-strip/mruby_strip.c",
717715
},
718716
.flags = &cflags,
719717
});
@@ -870,7 +868,7 @@ pub fn build(b: *std.Build) !void {
870868

871869
if (gem.test_rbfiles.len > 0) {
872870
try gem_test_content.appendSlice(b.fmt(
873-
\\ mrb_state *mrb2 = mrb_open_core(mrb_default_allocf, NULL);
871+
\\ mrb_state *mrb2 = mrb_open_core();
874872
\\ if (mrb2 == NULL) {{
875873
\\ fprintf(stderr, "Invalid mrb_state, exiting %s", __func__);
876874
\\ exit(EXIT_FAILURE);
@@ -1150,8 +1148,8 @@ pub fn build(b: *std.Build) !void {
11501148
example_ruby.addArgs(&.{"-r"});
11511149
example_ruby.addFileArg(b.path("src/example.rb"));
11521150
example_ruby.expectStdOutEqual(
1153-
\\mruby 3.3.0 (2024-02-14)
1154-
\\mruby - Copyright (c) 2010-2024 mruby developers
1151+
\\mruby 3.4.0 (2025-04-20)
1152+
\\mruby - Copyright (c) 2010-2025 mruby developers
11551153
\\#<Example:42>
11561154
\\
11571155
);
@@ -1348,7 +1346,7 @@ const builtin_gems = [_]Mgem{
13481346
},
13491347
Mgem{
13501348
.name = "mruby_exit",
1351-
.srcs = &.{"mrbgems/mruby-exit/src/mruby-exit.c"},
1349+
.srcs = &.{"mrbgems/mruby-exit/src/mruby_exit.c"},
13521350
.test_rbfiles = &.{}, // TODO: missing spec coverage
13531351
},
13541352
Mgem{
@@ -1358,7 +1356,7 @@ const builtin_gems = [_]Mgem{
13581356
},
13591357
Mgem{
13601358
.name = "mruby_hash_ext",
1361-
.srcs = &.{"mrbgems/mruby-hash-ext/src/hash-ext.c"},
1359+
.srcs = &.{"mrbgems/mruby-hash-ext/src/hash_ext.c"},
13621360
.rbfiles = &.{"mrbgems/mruby-hash-ext/mrblib/hash.rb"},
13631361
.test_rbfiles = &.{"mrbgems/mruby-hash-ext/test/hash.rb"},
13641362
.dependencies = &.{"mruby_array_ext"},
@@ -1420,7 +1418,7 @@ const builtin_gems = [_]Mgem{
14201418
.srcs = &.{"mrbgems/mruby-object-ext/src/object.c"},
14211419
.rbfiles = &.{"mrbgems/mruby-object-ext/mrblib/object.rb"},
14221420
.test_rbfiles = &.{"mrbgems/mruby-object-ext/test/object.rb"},
1423-
.test_srcs = &.{"mrbgems/mruby-object-ext/test/object-ext.c"},
1421+
.test_srcs = &.{"mrbgems/mruby-object-ext/test/object_ext.c"},
14241422
},
14251423
Mgem{
14261424
.name = "mruby_objectspace",
@@ -1446,9 +1444,9 @@ const builtin_gems = [_]Mgem{
14461444
},
14471445
Mgem{
14481446
.name = "mruby_proc_binding",
1449-
.srcs = &.{"mrbgems/mruby-proc-binding/src/proc-binding.c"},
1450-
.test_rbfiles = &.{"mrbgems/mruby-proc-binding/test/proc-binding.rb"},
1451-
.test_srcs = &.{"mrbgems/mruby-proc-binding/test/proc-binding.c"},
1447+
.srcs = &.{"mrbgems/mruby-proc-binding/src/proc_binding.c"},
1448+
.test_rbfiles = &.{"mrbgems/mruby-proc-binding/test/proc_binding.rb"},
1449+
.test_srcs = &.{"mrbgems/mruby-proc-binding/test/proc_binding.c"},
14521450
.dependencies = &.{
14531451
"mruby_proc_ext",
14541452
"mruby_binding",

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
.path = "build/linenoise",
2121
},
2222
.mruby = .{
23-
.url = "git+https://github.com/jethrodaniel/mruby?ref=fork#3a52c2274a719bc17890a109fc8f8d710a06450c",
24-
.hash = "1220f4a8f9987496dd255d901bb75d0c4e17571e58971991a4850e595bbea63aaf9e",
23+
.url = "git+https://github.com/mruby/mruby#9c5d1cdaa9ee2d34cc05523e9eac243e7cb7b2c9",
24+
.hash = "12202faa23de993ff59945b8329b263eb885e18424f8a8b609f4c246840b188b37cc",
2525
},
2626
},
2727
}

src/example.zig

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -96,57 +96,6 @@ pub fn main() !void {
9696
logger.err("Error => '{s}'", .{str});
9797
}
9898

99-
{
100-
logger.info("Interpreter with a simple custom allocator...", .{});
101-
102-
const custom_mrb: [*c]mruby.mrb_state = mruby.mrb_open_allocf(mrb_simple_allocf, null);
103-
if (custom_mrb == null) return error.mrb_open;
104-
defer _ = mruby.mrb_close(custom_mrb);
105-
106-
_ = mruby.mrb_load_string(custom_mrb, "puts 'using a simple custom allocator!'");
107-
}
108-
109-
{
110-
logger.info("Interpreter with a more complex custom allocator...", .{});
111-
112-
var gpa = std.heap.GeneralPurposeAllocator(.{
113-
// .verbose_log = true,
114-
.enable_memory_limit = true,
115-
}){};
116-
gpa.setRequestedMemoryLimit(1024 * 700); // 700 KiB
117-
defer if (gpa.deinit() == .leak) @panic("found memory leaks");
118-
const allocator = gpa.allocator();
119-
120-
// const allocator = std.heap.page_allocator;
121-
122-
// Doesn't work, since fba is a bump pointer allocator
123-
// var buffer: [1024 * 1000]u8 = undefined;
124-
// var fba = std.heap.FixedBufferAllocator.init(&buffer);
125-
// const allocator = fba.allocator();
126-
127-
// var realloc_map = std.AutoHashMap(usize, usize).init(std.heap.page_allocator);
128-
var realloc_map = std.AutoHashMap(usize, usize).init(allocator);
129-
defer realloc_map.deinit();
130-
131-
const user_data = UserData{
132-
.allocator = &allocator,
133-
.realloc_map = &realloc_map,
134-
};
135-
136-
const custom_mrb: [*c]mruby.mrb_state = mruby.mrb_open_allocf(mrb_complex_allocf, @as(?*anyopaque, @ptrCast(@constCast(&user_data))));
137-
if (custom_mrb == null) return error.mrb_open;
138-
defer _ = mruby.mrb_close(custom_mrb);
139-
140-
logger.info("loaded interpreter...", .{});
141-
142-
_ = mruby.mrb_load_string(custom_mrb, "puts 'using a more complex custom allocator!'");
143-
_ = mruby.mrb_load_string(custom_mrb, "$t = []; puts $t.inspect");
144-
_ = mruby.mrb_load_string(custom_mrb, "$t << :a; puts $t.inspect");
145-
_ = mruby.mrb_load_string(custom_mrb, "$t += [:a, :b, :c]; puts $t.inspect");
146-
147-
if (custom_mrb.*.exc != null) mruby.mrb_print_error(custom_mrb);
148-
}
149-
15099
logger.info("Done", .{});
151100
}
152101

@@ -167,92 +116,6 @@ fn nil_method(mrb: [*c]mruby.mrb_state, self: mruby.mrb_value) callconv(.C) mrub
167116
return mruby.mrb_nil_value();
168117
}
169118

170-
fn mrb_simple_allocf(mrb: [*c]mruby.mrb_state, p: ?*anyopaque, size: usize, ud: ?*anyopaque) callconv(.C) ?*anyopaque {
171-
_ = mrb;
172-
_ = ud;
173-
174-
if (size == 0) {
175-
if (p) |ptr| std.c.free(ptr);
176-
return null;
177-
} else {
178-
return std.c.realloc(p, size);
179-
}
180-
}
181-
182-
const UserData = struct {
183-
allocator: *const std.mem.Allocator,
184-
realloc_map: *std.AutoHashMap(usize, usize),
185-
};
186-
187-
// NOTE: trying to pass a custom allocator here is difficult, because we have
188-
// to keep track of previous allocation sizes like realloc does internally.
189-
fn mrb_complex_allocf(mrb: [*c]mruby.mrb_state, p: ?*anyopaque, size: usize, ud: ?*anyopaque) callconv(.C) ?*anyopaque {
190-
_ = mrb;
191-
192-
const user_data = @as(*UserData, @ptrCast(@alignCast(ud))).*;
193-
const allocator = user_data.allocator;
194-
var realloc_map = @constCast(user_data.realloc_map);
195-
196-
std.log.debug("-- realloc({x}, {d})", .{ @intFromPtr(p), size });
197-
198-
if (size == 0) {
199-
if (p) |ptr| {
200-
if (realloc_map.getEntry(@intFromPtr(ptr))) |entry| {
201-
std.log.debug(" freeing existing pointer {x}", .{@intFromPtr(ptr)});
202-
203-
const old_size = entry.value_ptr.*;
204-
const raw_ptr = entry.key_ptr.*;
205-
const slice = @as([*]u8, @ptrFromInt(raw_ptr))[0..old_size];
206-
207-
allocator.free(slice);
208-
209-
_ = realloc_map.remove(raw_ptr);
210-
211-
return null;
212-
}
213-
std.log.err(" realloc({x}, {d}), but ptr is missing from realloc_map", .{ ptr, size });
214-
@panic("ptr missing from realloc map");
215-
}
216-
217-
std.log.debug(" realloc({any}, {any}), seems invalid", .{ p, size });
218-
219-
return null;
220-
} else {
221-
if (p) |ptr| {
222-
if (realloc_map.getEntry(@intFromPtr(ptr))) |entry| {
223-
const old_size = entry.value_ptr.*;
224-
const raw_ptr = entry.key_ptr.*;
225-
226-
std.log.debug(" existing {x}, old_size: {d}", .{ raw_ptr, old_size });
227-
228-
const slice = @as([*]u8, @ptrFromInt(raw_ptr))[0..old_size];
229-
const allocation = allocator.realloc(slice, size) catch @panic("oom");
230-
231-
realloc_map.put(@intFromPtr(allocation.ptr), size) catch |err| {
232-
std.log.err("{any}", .{err});
233-
@panic("failed to update existing pointer size");
234-
};
235-
236-
return allocation.ptr;
237-
}
238-
239-
std.log.err(" realloc({x}, {d}), but ptr is missing from realloc_map", .{ ptr, size });
240-
@panic("ptr missing from realloc map");
241-
}
242-
243-
const allocation = allocator.alloc(u8, size) catch @panic("oom");
244-
const ptr = allocation.ptr;
245-
246-
realloc_map.put(@intFromPtr(ptr), size) catch |err| {
247-
std.log.err("{any}", .{err});
248-
@panic("failed to put new pointer into realloc_map");
249-
};
250-
std.log.debug(" new {x}", .{@intFromPtr(ptr)});
251-
252-
return ptr;
253-
}
254-
}
255-
256119
//--
257120

258121
const Foo = struct {

0 commit comments

Comments
 (0)