@@ -140,7 +140,15 @@ static int pico8_rotr(lua_State *l) {
140
140
static int pico8_tostr (lua_State * l ) {
141
141
char buffer [20 ];
142
142
char const * s = buffer ;
143
- auto hex = lua_toboolean (l , 2 );
143
+
144
+ uint16_t flags = 0 ;
145
+ if (lua_isboolean (l , 2 )) {
146
+ flags = lua_toboolean (l , 2 ) ? 1 : 0 ;
147
+ }
148
+ else if (lua_isnumber (l , 2 )) {
149
+ flags = lua_tointeger (l , 2 );
150
+ }
151
+
144
152
switch (lua_type (l , 1 ))
145
153
{
146
154
case LUA_TNONE :
@@ -149,9 +157,17 @@ static int pico8_tostr(lua_State *l) {
149
157
break ;
150
158
case LUA_TNUMBER : {
151
159
lua_Number x = lua_tonumber (l , 1 );
152
- if (hex ) {
160
+ if (flags ) {
153
161
uint32_t b = (uint32_t )x .bits ();
154
- sprintf (buffer , "0x%04x.%04x" , (b >> 16 ) & 0xffff , b & 0xffff );
162
+ if ((flags & 0x3 ) == 0x3 ) {
163
+ sprintf (buffer , "0x%04x%04x" , (b >> 16 ) & 0xffff , b & 0xffff );
164
+ }
165
+ else if ((flags & 0x2 ) == 0x2 ) {
166
+ sprintf (buffer , "%d" , b );
167
+ }
168
+ else {
169
+ sprintf (buffer , "0x%04x.%04x" , (b >> 16 ) & 0xffff , b & 0xffff );
170
+ }
155
171
} else {
156
172
lua_number2str (buffer , x );
157
173
}
@@ -172,7 +188,7 @@ static int pico8_tostr(lua_State *l) {
172
188
case LUA_TFUNCTION :
173
189
// PICO-8 0.1.12d changelog: “tostr(x,true) can also be used to view
174
190
// the hex value of functions and tables (uses Lua's tostring)”
175
- if (hex ) {
191
+ if (flags ) {
176
192
luaL_tolstring (l , 1 , NULL );
177
193
return 1 ;
178
194
}
@@ -189,6 +205,25 @@ static int pico8_tonum(lua_State *l) {
189
205
{
190
206
case LUA_TSTRING : {
191
207
char const * s = lua_tostring (l , 1 );
208
+ uint16_t flags = lua_gettop (l ) >= 2 ? lua_tointeger (l , 2 ) : 0 ;
209
+ if (flags & 0x1 ) {
210
+ char buffer [9 ];
211
+ size_t i = 0 ;
212
+ while (s [i ] != '\0' ) {
213
+ buffer [i ] = isxdigit (s [i ]) ? s [i ] : '0' ;
214
+ i ++ ;
215
+ }
216
+ uint32_t bits = strtol (buffer , NULL , 16 );
217
+ if (flags & 0x2 ) bits >>= 16 ;
218
+ lua_pushnumber (l , (lua_Number )bits );
219
+ return 1 ;
220
+ }
221
+ else if (flags & 0x2 ) {
222
+ uint32_t bits = strtol (s , NULL , 10 );
223
+ bits >>= 16 ;
224
+ lua_pushnumber (l , (lua_Number )bits );
225
+ return 1 ;
226
+ }
192
227
// If parsing failed, PICO-8 returns nothing
193
228
if (!luaO_str2d (s , strlen (s ), & ret )) return 0 ;
194
229
break ;
@@ -203,14 +238,20 @@ static int pico8_tonum(lua_State *l) {
203
238
}
204
239
205
240
static int pico8_chr (lua_State * l ) {
206
- char s [2 ] = { (char )(uint8_t )lua_tonumber (l , 1 ), '\0' };
207
- lua_pushlstring (l , s , 1 );
241
+ // PICO-8 seems to top out at allowing 248 arguments
242
+ char s [248 ];
243
+ size_t numargs = lua_gettop (l ) < sizeof (s ) ? lua_gettop (l ) : sizeof (s );
244
+ for (size_t i = 0 ; i < numargs ; i ++ ) {
245
+ s [i ] = (char )(uint8_t )lua_tonumber (l , i + 1 );
246
+ }
247
+ lua_pushlstring (l , s , numargs );
208
248
return 1 ;
209
249
}
210
250
211
251
static int pico8_ord (lua_State * l ) {
212
252
size_t len ;
213
- int n = 0 , count = 1 ;
253
+ int n = 0 ;
254
+ int count = 1 ;
214
255
char const * s = luaL_checklstring (l , 1 , & len );
215
256
if (!lua_isnone (l , 3 )) {
216
257
if (!lua_isnumber (l , 3 )) return 0 ;
@@ -230,6 +271,10 @@ static int pico8_ord(lua_State *l) {
230
271
}
231
272
232
273
static int pico8_split (lua_State * l ) {
274
+ if (lua_isnil (l , 1 )) {
275
+ return 0 ;
276
+ }
277
+
233
278
size_t count = 0 , hlen ;
234
279
char const * haystack = luaL_checklstring (l , 1 , & hlen );
235
280
if (!haystack )
0 commit comments