@@ -15,8 +15,6 @@ const Lua = struct {
15
15
allocator : ? * Allocator = null ,
16
16
state : * c.lua_State ,
17
17
18
- // Ziggified wrapper functions
19
-
20
18
/// Allows Lua to allocate memory using a Zig allocator passed in via data.
21
19
/// See https://www.lua.org/manual/5.4/manual.html#lua_Alloc for more details
22
20
fn alloc (data : ? * anyopaque , ptr : ? * anyopaque , osize : usize , nsize : usize ) callconv (.C ) ? * anyopaque {
@@ -102,6 +100,89 @@ const Lua = struct {
102
100
}
103
101
104
102
// Standard library loading functions
103
+ // TODO: opening libs can run arbitrary Lua code and can throw any error
104
+
105
+ pub const Libs = packed struct {
106
+ base : bool = false ,
107
+ coroutine : bool = false ,
108
+ package : bool = false ,
109
+ string : bool = false ,
110
+ utf8 : bool = false ,
111
+ table : bool = false ,
112
+ math : bool = false ,
113
+ io : bool = false ,
114
+ os : bool = false ,
115
+ debug : bool = false ,
116
+ };
117
+
118
+ /// Opens the specified standard library functions
119
+ pub fn open (lua : * Lua , libs : Libs ) void {
120
+ if (libs .base ) lua .openBase ();
121
+ if (libs .coroutine ) lua .openCoroutine ();
122
+ if (libs .package ) lua .openPackage ();
123
+ if (libs .string ) lua .openString ();
124
+ if (libs .utf8 ) lua .openUtf8 ();
125
+ if (libs .table ) lua .openTable ();
126
+ if (libs .math ) lua .openMath ();
127
+ if (libs .io ) lua .openIO ();
128
+ if (libs .os ) lua .openOS ();
129
+ if (libs .debug ) lua .openDebug ();
130
+ }
131
+
132
+ /// Open all standard libraries
133
+ pub fn openLibs (lua : * Lua ) void {
134
+ c .luaL_openlibs (lua .state );
135
+ }
136
+
137
+ /// Open the basic standard library
138
+ pub fn openBase (lua : * Lua ) void {
139
+ _ = c .luaopen_base (lua .state );
140
+ }
141
+
142
+ /// Open the coroutine standard library
143
+ pub fn openCoroutine (lua : * Lua ) void {
144
+ _ = c .luaopen_coroutine (lua .state );
145
+ }
146
+
147
+ /// Open the package standard library
148
+ pub fn openPackage (lua : * Lua ) void {
149
+ _ = c .luaopen_package (lua .state );
150
+ }
151
+
152
+ /// Open the string standard library
153
+ pub fn openString (lua : * Lua ) void {
154
+ _ = c .luaopen_string (lua .state );
155
+ }
156
+
157
+ /// Open the UTF-8 standard library
158
+ pub fn openUtf8 (lua : * Lua ) void {
159
+ _ = c .luaopen_utf8 (lua .state );
160
+ }
161
+
162
+ /// Open the table standard library
163
+ pub fn openTable (lua : * Lua ) void {
164
+ _ = c .luaopen_table (lua .state );
165
+ }
166
+
167
+ /// Open the math standard library
168
+ pub fn openMath (lua : * Lua ) void {
169
+ _ = c .luaopen_math (lua .state );
170
+ }
171
+
172
+ /// Open the io standard library
173
+ pub fn openIO (lua : * Lua ) void {
174
+ _ = c .luaopen_io (lua .state );
175
+ }
176
+
177
+ /// Open the os standard library
178
+ pub fn openOS (lua : * Lua ) void {
179
+ _ = c .luaopen_os (lua .state );
180
+ }
181
+
182
+ /// Open the debug standard library
183
+ pub fn openDebug (lua : * Lua ) void {
184
+ _ = c .luaopen_debug (lua .state );
185
+ }
105
186
};
106
187
107
188
// Tests
@@ -136,3 +217,35 @@ test "initialization" {
136
217
lua = try Lua .auxNewState ();
137
218
lua .close ();
138
219
}
220
+
221
+ test "standard library loading" {
222
+ // open a subset of standard libraries with Zig wrapper
223
+ {
224
+ var lua = try Lua .init (testing .allocator );
225
+ defer lua .deinit ();
226
+ lua .open (.{ .base = true , .utf8 = true , .string = true });
227
+ }
228
+
229
+ // open all standard libraries
230
+ {
231
+ var lua = try Lua .init (testing .allocator );
232
+ defer lua .deinit ();
233
+ lua .openLibs ();
234
+ }
235
+
236
+ // open all standard libraries with individual functions
237
+ {
238
+ var lua = try Lua .init (testing .allocator );
239
+ defer lua .deinit ();
240
+ lua .openBase ();
241
+ lua .openCoroutine ();
242
+ lua .openPackage ();
243
+ lua .openString ();
244
+ lua .openUtf8 ();
245
+ lua .openTable ();
246
+ lua .openMath ();
247
+ lua .openIO ();
248
+ lua .openOS ();
249
+ lua .openDebug ();
250
+ }
251
+ }
0 commit comments