Register and Buffers

This commit is contained in:
Nathan Craddock 2023-12-30 20:52:48 -07:00
parent b1ca88c477
commit 9f63b1e90d
2 changed files with 83 additions and 62 deletions

View File

@ -703,14 +703,13 @@ pub const Lua = struct {
c.lua_rawseti(lua.state, index, i);
}
// /// Sets the C function f as the new value of global name
// /// See https://www.lua.org/manual/5.1/manual.html#lua_register
// pub fn register(lua: *Lua, name: [:0]const u8, c_fn: CFn) void {
// // translate-c failure
// // c.lua_register(lua.state, name, c_fn);
// lua.pushFunction(c_fn);
// lua.setGlobal(name);
// }
/// Sets the C function f as the new value of global name
/// See https://www.lua.org/manual/5.1/manual.html#lua_register
pub fn register(lua: *Lua, name: [:0]const u8, c_fn: CFn) void {
// translate-c failure
lua.pushFunction(c_fn, name);
lua.setGlobal(name);
}
/// Removes the element at the given valid `index` shifting down elements to fill the gap
/// See https://www.lua.org/manual/5.1/manual.html#lua_remove
@ -1321,10 +1320,17 @@ pub const Buffer = struct {
/// Internal Lua type for a string buffer
pub const LuaBuffer = c.luaL_Strbuf;
// pub const buffer_size = c.LUAL_BUFFERSIZE;
pub const buffer_size = 10;
pub const buffer_size = c.LUA_BUFFERSIZE;
// TODO: addchar (defined as a macro)
/// Adds `byte` to the buffer
/// See https://www.lua.org/manual/5.1/manual.html#luaL_addchar
pub fn addChar(buf: *Buffer, byte: u8) void {
// could not be translated by translate-c
var lua_buf = &buf.b;
if (lua_buf.p > &lua_buf.buffer[buffer_size - 1]) _ = buf.prep();
lua_buf.p.* = byte;
lua_buf.p += 1;
}
/// Adds the string to the buffer
/// See https://www.lua.org/manual/5.1/manual.html#luaL_addlstring
@ -1341,27 +1347,40 @@ pub const Buffer = struct {
lua_buf.p += length;
}
/// Adds the zero-terminated string pointed to by `str` to the buffer
/// See https://www.lua.org/manual/5.1/manual.html#luaL_addstring
pub fn addString(buf: *Buffer, str: [:0]const u8) void {
c.luaL_addlstring(&buf.b, str.ptr, str.len);
}
/// Adds the value on the top of the stack to the buffer and pops the value
/// See https://www.lua.org/manual/5.1/manual.html#luaL_addvalue
pub fn addValue(buf: *Buffer) void {
c.luaL_addvalue(&buf.b);
}
/// TODO: addvalueany
/// Adds the value at the given index to the buffer
pub fn addValueAny(buf: *Buffer, idx: i32) void {
c.luaL_addvalueany(&buf.b, idx);
}
/// Equivalent to prepSize with a buffer size of Buffer.buffer_size
/// See https://www.lua.org/manual/5.1/manual.html#luaL_prepbuffer
// pub fn prep(buf: *Buffer) []u8 {
// return c.luaL_prepbuffsize(&buf.b, buffer_size);
// }
pub fn prep(buf: *Buffer) []u8 {
return c.luaL_prepbuffsize(&buf.b, buffer_size)[0..buffer_size];
}
/// TODO: prepbuffsize
/// Finishes the use of the buffer leaving the final string on the top of the stack
/// See https://www.lua.org/manual/5.1/manual.html#luaL_pushresult
pub fn pushResult(buf: *Buffer) void {
c.luaL_pushresult(&buf.b);
}
// TODO: pushresultsize
/// Equivalent to `Buffer.addSize()` followed by `Buffer.pushResult()`
/// See https://www.lua.org/manual/5.2/manual.html#luaL_pushresultsize
pub fn pushResultSize(buf: *Buffer, size: usize) void {
c.luaL_pushresultsize(&buf.b, size);
}
};
// Helper functions to make the ziglua API easier to use

View File

@ -312,60 +312,62 @@ test "stack manipulation" {
try expectEqual(@as(i32, 0), lua.getTop());
}
// test "calling a function" {
// var lua = try Lua.init(testing.allocator);
// defer lua.deinit();
test "calling a function" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();
// lua.register("zigadd", ziglua.wrap(add));
// lua.getGlobal("zigadd");
// lua.pushInteger(10);
// lua.pushInteger(32);
lua.register("zigadd", ziglua.wrap(add));
_ = lua.getGlobal("zigadd");
lua.pushInteger(10);
lua.pushInteger(32);
// // protectedCall is safer, but we might as well exercise call when
// // we know it should be safe
// lua.call(2, 1);
// protectedCall is safer, but we might as well exercise call when
// we know it should be safe
lua.call(2, 1);
// try expectEqual(@as(i64, 42), try lua.toInteger(1));
// }
try expectEqual(@as(i64, 42), try lua.toInteger(1));
}
// test "string buffers" {
// var lua = try Lua.init(testing.allocator);
// defer lua.deinit();
test "string buffers" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();
// var buffer: Buffer = undefined;
// buffer.init(lua);
var buffer: Buffer = undefined;
buffer.init(lua);
// buffer.addChar('z');
// buffer.addChar('i');
// buffer.addChar('g');
// buffer.addString("l");
buffer.addChar('z');
buffer.addChar('i');
buffer.addChar('g');
buffer.addString("l");
// var str = buffer.prep();
// str[0] = 'u';
// str[1] = 'a';
// buffer.addSize(2);
var str = buffer.prep();
str[0] = 'u';
str[1] = 'a';
buffer.addSize(2);
// buffer.addBytes(" api ");
// lua.pushNumber(5.1);
// buffer.addValue();
// buffer.pushResult();
// try expectEqualStrings("ziglua api 5.1", try lua.toBytes(-1));
buffer.addBytes(" api ");
lua.pushNumber(5.1);
buffer.addValue();
// // now test a small buffer
// buffer = undefined;
// buffer.init(lua);
// var b = buffer.prep();
// b[0] = 'a';
// b[1] = 'b';
// b[2] = 'c';
// buffer.addSize(3);
// b = buffer.prep();
// @memcpy(b, "defghijklmnopqrstuvwxyz");
// buffer.addSize(23);
// buffer.pushResult();
// try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toBytes(-1));
// lua.pop(1);
// }
lua.pushBytes(" luau");
buffer.addValueAny(-1);
buffer.pushResult();
try expectEqualStrings("ziglua api 5.1 luau", try lua.toBytes(-1));
// now test a small buffer
buffer = undefined;
buffer.init(lua);
var b = buffer.prep();
b[0] = 'a';
b[1] = 'b';
b[2] = 'c';
buffer.addSize(3);
b = buffer.prep();
@memcpy(b[0..23], "defghijklmnopqrstuvwxyz");
buffer.pushResultSize(23);
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toBytes(-1));
lua.pop(1);
}
test "function registration" {
var lua = try Lua.init(testing.allocator);