Skip to content

Commit 1f362be

Browse files
committed
tests: add getExtraSpace test
1 parent db385a4 commit 1f362be

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/ziglua.zig

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,10 +434,10 @@ pub const Lua = struct {
434434
return c.lua_getallocf(lua.state, @ptrCast([*c]?*anyopaque, data)).?;
435435
}
436436

437-
/// Returns a pointer to a raw memory area associated with the given Lua state
437+
/// Returns a slice of a raw memory area associated with the given Lua state
438438
/// The application may use this area for any purpose; Lua does not use it for anything
439-
pub fn getExtraSpace(lua: *Lua) *anyopaque {
440-
return c.lua_getextraspace(lua.state).?;
439+
pub fn getExtraSpace(lua: *Lua) []u8 {
440+
return @ptrCast([*]u8, c.lua_getextraspace(lua.state).?)[0..@sizeOf(isize)];
441441
}
442442

443443
/// Pushes onto the stack the value t[key] where t is the value at the given `index`
@@ -2311,6 +2311,17 @@ test "garbage collector" {
23112311
try expect(!lua.gc(.set_incremental, .{0, 0, 0}));
23122312
}
23132313

2314+
test "extra space" {
2315+
var lua = try Lua.init(testing.allocator);
2316+
defer lua.deinit();
2317+
2318+
var space = @ptrCast(*align(1) usize, lua.getExtraSpace().ptr);
2319+
space.* = 1024;
2320+
// each new thread is initialized with a copy of the extra space from the main thread
2321+
var thread = lua.newThread();
2322+
try expectEqual(@as(usize, 1024), @ptrCast(*align(1) usize, thread.getExtraSpace()).*);
2323+
}
2324+
23142325
test "refs" {
23152326
// temporary test that includes a reference to all functions so
23162327
// they will be type-checked
@@ -2321,8 +2332,6 @@ test "refs" {
23212332
_ = Lua.createTable;
23222333
_ = Lua.dump;
23232334
_ = Lua.raiseError;
2324-
_ = Lua.gc;
2325-
_ = Lua.getExtraSpace;
23262335
_ = Lua.getI;
23272336
_ = Lua.getIUserValue;
23282337
_ = Lua.getMetatable;

0 commit comments

Comments
 (0)