@@ -2497,6 +2497,44 @@ pub const Lua = opaque {
24972497 }
24982498 }
24992499
2500+ /// Adds the zero-terminated string to the buffer.
2501+ /// The string may not contain embedded zeros.
2502+ ///
2503+ /// From: `void luaL_addstring(luaL_Buffer *B, const char *s);`
2504+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addstring
2505+ /// Stack Behavior: `[-0, +0, m]`
2506+ pub fn addString (buffer : * Buffer , string : [* :0 ]const u8 ) void {
2507+ assert (buffer .p != null and buffer .L != null ); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addString()`.
2508+
2509+ return c .luaL_addstring (@ptrCast (buffer ), string );
2510+ }
2511+
2512+ /// Adds the string to the buffer.
2513+ /// The string may contain embedded zeros.
2514+ ///
2515+ /// From: `void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l);`
2516+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addlstring
2517+ /// Stack Behavior: `[-0, +0, m]`
2518+ pub fn addLString (buffer : * Buffer , string : [:0 ]const u8 ) void {
2519+ assert (buffer .p != null and buffer .L != null ); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addLString()`.
2520+
2521+ return c .luaL_addlstring (@ptrCast (buffer ), string .ptr , string .len );
2522+ }
2523+
2524+ /// Adds the value at the top of the stack to the buffer (see https://www.lua.org/manual/5.1/manual.html#luaL_Buffer).
2525+ /// Pops the value. This is the only function on string buffers that can (and must) be called with an extra
2526+ /// element on the stack, which is the value to be added to the buffer.
2527+ ///
2528+ /// From: `void luaL_addvalue(luaL_Buffer *B);`
2529+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#luaL_addvalue
2530+ /// Stack Behavior: `[-1, +0, m]`
2531+ pub fn addValue (buffer : * Buffer ) void {
2532+ assert (buffer .p != null and buffer .L != null ); // You must use `Lua.initBuffer(&Lua.Buffer)` before calling `Lua.Buffer.addLString()`.
2533+ assert (buffer .L .? .getTop () > 0 );
2534+
2535+ return c .luaL_addvalue (@ptrCast (buffer ));
2536+ }
2537+
25002538 /// Prepares writable space in the buffer.
25012539 ///
25022540 /// Used to provide direct writing of data. The caller **MUST** call `buffer.addSize()` after writing content
@@ -4854,3 +4892,57 @@ test "Buffer can be created by direct writes to the buffer" {
48544892 try std .testing .expect (lua .isString (-1 ));
48554893 try std .testing .expectEqualStrings ("Hello" , try lua .toLString (-1 ));
48564894}
4895+
4896+ test "Buffer can be created by adding strings to the buffer" {
4897+ const lua = try Lua .init (std .testing .allocator );
4898+ defer lua .deinit ();
4899+
4900+ var b : Lua.Buffer = .{};
4901+ lua .initBuffer (& b );
4902+ b .addString ("Hello, world!" );
4903+ b .pushResult ();
4904+
4905+ try std .testing .expectEqual (1 , lua .getTop ());
4906+ try std .testing .expect (lua .isString (-1 ));
4907+ try std .testing .expectEqualStrings ("Hello, world!" , try lua .toLString (-1 ));
4908+ }
4909+
4910+ test "Buffer can be created by adding literal strings to the buffer" {
4911+ const lua = try Lua .init (std .testing .allocator );
4912+ defer lua .deinit ();
4913+
4914+ var b : Lua.Buffer = .{};
4915+ lua .initBuffer (& b );
4916+ b .addLString ("Hello, world!" );
4917+ b .pushResult ();
4918+
4919+ try std .testing .expectEqual (1 , lua .getTop ());
4920+ try std .testing .expect (lua .isString (-1 ));
4921+ try std .testing .expectEqualStrings ("Hello, world!" , try lua .toLString (-1 ));
4922+ }
4923+
4924+ test "Buffer can be created by adding values on the stack to the buffer" {
4925+ const lua = try Lua .init (std .testing .allocator );
4926+ defer lua .deinit ();
4927+
4928+ var b : Lua.Buffer = .{};
4929+ lua .initBuffer (& b );
4930+
4931+ lua .pushInteger (42 );
4932+ b .addValue ();
4933+ try std .testing .expectEqual (0 , lua .getTop ());
4934+
4935+ lua .pushLString ("AAA" );
4936+ b .addValue ();
4937+ try std .testing .expectEqual (0 , lua .getTop ());
4938+
4939+ lua .pushNumber (99.2 );
4940+ b .addValue ();
4941+ try std .testing .expectEqual (0 , lua .getTop ());
4942+
4943+ b .pushResult ();
4944+
4945+ try std .testing .expectEqual (1 , lua .getTop ());
4946+ try std .testing .expect (lua .isString (-1 ));
4947+ try std .testing .expectEqualStrings ("42AAA99.2" , try lua .toLString (-1 ));
4948+ }
0 commit comments