forked from lua/lua
-
Notifications
You must be signed in to change notification settings - Fork 12
/
lpico8lib.c
382 lines (346 loc) · 11.3 KB
/
lpico8lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//
// ZEPTO-8 — Fantasy console emulator
//
// Copyright © 2016–2024 Sam Hocevar <sam@hocevar.net>
//
// This program is free software. It comes without any warranty, to
// the extent permitted by applicable law. You can redistribute it
// and/or modify it under the terms of the Do What the Fuck You Want
// to Public License, Version 2, as published by the WTFPL Task Force.
// See http://www.wtfpl.net/ for more details.
//
#include <cctype>
#include <cstring>
#define lpico8lib_c
#define LUA_LIB
#include "lua.h"
#include "lauxlib.h"
#include "llimits.h"
#include "lobject.h"
#include "trigtables.h"
static int pico8_max(lua_State *l) {
lua_pushnumber(l, lua_Number::max(lua_tonumber(l, 1), lua_tonumber(l, 2)));
return 1;
}
static int pico8_min(lua_State *l) {
lua_pushnumber(l, lua_Number::min(lua_tonumber(l, 1), lua_tonumber(l, 2)));
return 1;
}
static int pico8_mid(lua_State *l) {
lua_Number x = lua_tonumber(l, 1);
lua_Number y = lua_tonumber(l, 2);
lua_Number z = lua_tonumber(l, 3);
lua_pushnumber(l, x > y ? y > z ? y : lua_Number::min(x, z)
: x > z ? x : lua_Number::min(y, z));
return 1;
}
static int pico8_ceil(lua_State *l) {
lua_pushnumber(l, lua_Number::ceil(lua_tonumber(l, 1)));
return 1;
}
static int pico8_flr(lua_State *l) {
lua_pushnumber(l, lua_Number::floor(lua_tonumber(l, 1)));
return 1;
}
static lua_Number sin_helper(lua_Number x) {
// Reduce angle to 0x0 … 0x0.3fff, with the following PICO-8 rules:
// - sin(x) = -sin(x + 0.5)
// - sin(x) equals sin(~x) rather than sin(-x)
// - the last two bits are rounded
// We use a lookup table of sin(x)-4x generated by PICO-8 to ensure
// that we get the exact same results.
auto a = ((x.bits() & 0x4000 ? ~x : x).bits() & 0x3fff) + 2;
auto ret = lua_Number::frombits((a >> 2 << 4) + sintable[a >> 2]);
return x.bits() & 0x8000 ? ret : -ret;
}
static int pico8_cos(lua_State *l) {
lua_pushnumber(l, sin_helper(lua_tonumber(l, 1) - lua_Number::frombits(0x4000)));
return 1;
}
static int pico8_sin(lua_State *l) {
lua_pushnumber(l, sin_helper(lua_tonumber(l, 1)));
return 1;
}
static int pico8_atan2(lua_State *l) {
lua_Number x = lua_tonumber(l, 1);
lua_Number y = lua_tonumber(l, 2);
int32_t bits = 0x4000;
if (x) {
// Use std::abs() instead of fix32::abs() to emulate PICO-8’s behaviour
// with e.g. atan2(0x8000, 0x8000.0001)
auto q = (std::abs(int64_t(y.bits())) << 16) / std::abs(int64_t(x.bits()));
if (q > 0x10000)
bits -= atantable[(int64_t(1) << 32) / q >> 5];
else
bits = atantable[q >> 5];
}
if (x.bits() < 0) bits = 0x8000 - bits;
if (y.bits() > 0) bits = -bits & 0xffff;
// Emulate a bug in PICO-8 with e.g. atan2(1, 0x8000)
if (x && y.bits() == int32_t(0x80000000)) bits = -bits & 0xffff;
lua_pushnumber(l, lua_Number::frombits(bits));
return 1;
}
static int pico8_sqrt(lua_State *l) {
int64_t root = 0, x = int64_t(lua_tonumber(l, 1).bits()) << 16;
if (x > 0) {
// Square root by abacus algorithm, Martin Guy @ UKC, June 1985.
// From a book on programming abaci by Mr C. Woo.
// Observing a PICO-8 JavaScript export shows the same algorithm.
for (int64_t a = int64_t(1) << 46; a; a >>= 2, root >>= 1) {
if (x >= a + root) {
x -= a + root;
root += a << 1;
}
}
}
lua_pushnumber(l, lua_Number::frombits(root));
return 1;
}
static int pico8_abs(lua_State *l) {
lua_pushnumber(l, lua_Number::abs(lua_tonumber(l, 1)));
return 1;
}
static int pico8_sgn(lua_State *l) {
lua_pushnumber(l, cast_num(lua_tonumber(l, 1).bits() >= 0 ? 1.0 : -1.0));
return 1;
}
static int pico8_band(lua_State *l) {
lua_pushnumber(l, lua_tonumber(l, 1) & lua_tonumber(l, 2));
return 1;
}
static int pico8_bor(lua_State *l) {
lua_pushnumber(l, lua_tonumber(l, 1) | lua_tonumber(l, 2));
return 1;
}
static int pico8_bxor(lua_State *l) {
lua_pushnumber(l, lua_tonumber(l, 1) ^ lua_tonumber(l, 2));
return 1;
}
static int pico8_bnot(lua_State *l) {
lua_pushnumber(l, ~lua_tonumber(l, 1));
return 1;
}
static int pico8_shl(lua_State *l) {
lua_pushnumber(l, lua_tonumber(l, 1) << int(lua_tonumber(l, 2)));
return 1;
}
static int pico8_lshr(lua_State *l) {
lua_pushnumber(l, lua_Number::lshr(lua_tonumber(l, 1), int(lua_tonumber(l, 2))));
return 1;
}
static int pico8_shr(lua_State *l) {
lua_pushnumber(l, lua_tonumber(l, 1) >> int(lua_tonumber(l, 2)));
return 1;
}
static int pico8_rotl(lua_State *l) {
lua_pushnumber(l, lua_Number::rotl(lua_tonumber(l, 1), int(lua_tonumber(l, 2))));
return 1;
}
static int pico8_rotr(lua_State *l) {
lua_pushnumber(l, lua_Number::rotr(lua_tonumber(l, 1), int(lua_tonumber(l, 2))));
return 1;
}
static int pico8_tostr(lua_State *l) {
char buffer[20];
char const *s = buffer;
uint16_t flags = 0;
if (lua_isboolean(l, 2)) {
flags = lua_toboolean(l, 2) ? 1 : 0;
}
else if (lua_isnumber(l, 2)) {
flags = lua_tointeger(l, 2);
}
switch (lua_type(l, 1))
{
case LUA_TNONE:
// PICO-8 0.2.2 changelog: tostr() returns "" instead of nil
buffer[0] = '\0';
break;
case LUA_TNUMBER: {
lua_Number x = lua_tonumber(l, 1);
if (flags) {
uint32_t b = (uint32_t)x.bits();
if ((flags & 0x3) == 0x3) {
sprintf(buffer, "0x%04x%04x", (b >> 16) & 0xffff, b & 0xffff);
}
else if ((flags & 0x2) == 0x2) {
sprintf(buffer, "%d", b);
}
else {
sprintf(buffer, "0x%04x.%04x", (b >> 16) & 0xffff, b & 0xffff);
}
} else {
lua_number2str(buffer, x);
}
break;
}
case LUA_TSTRING:
lua_pushvalue(l, 1);
return 1;
case LUA_TBOOLEAN: s = lua_toboolean(l, 1) ? "true" : "false"; break;
case LUA_TTABLE:
// PICO-8 0.1.12d changelog: “__tostring metatable method
// observed by tostr() / print() / printh()”
if (luaL_callmeta(l, 1, "__tostring")) {
luaL_tolstring(l, 1, NULL);
return 1;
}
[[fallthrough]];
case LUA_TFUNCTION:
// PICO-8 0.1.12d changelog: “tostr(x,true) can also be used to view
// the hex value of functions and tables (uses Lua's tostring)”
if (flags) {
luaL_tolstring(l, 1, NULL);
return 1;
}
[[fallthrough]];
default: sprintf(buffer, "[%s]", luaL_typename(l, 1)); break;
}
lua_pushstring(l, s);
return 1;
}
static int pico8_tonum(lua_State *l) {
lua_Number ret;
switch (lua_type(l, 1))
{
case LUA_TSTRING: {
char const *s = lua_tostring(l, 1);
uint16_t flags = lua_gettop(l) >= 2 ? lua_tointeger(l, 2) : 0;
if (flags & 0x1) {
char buffer[9];
for (size_t i = 0; s[i] != '\0'; ++i) {
buffer[i] = isxdigit(s[i]) ? s[i] : '0';
}
uint32_t bits = strtol(buffer, NULL, 16);
if (flags & 0x2) bits >>= 16;
lua_pushnumber(l, (lua_Number)bits);
return 1;
}
else if (flags & 0x2) {
uint32_t bits = strtol(s, NULL, 10);
bits >>= 16;
lua_pushnumber(l, (lua_Number)bits);
return 1;
}
// If parsing failed, PICO-8 returns nothing
if (!luaO_str2d(s, strlen(s), &ret)) return 0;
break;
}
case LUA_TNUMBER: ret = lua_tonumber(l, 1); break;
// PICO-8 0.2.3 changelog: “tonum(boolean_value) returns 1 or 0 instead of nil”
case LUA_TBOOLEAN: ret = lua_toboolean(l, 1) ? 1 : 0; break;
default: return 0;
}
lua_pushnumber(l, ret);
return 1;
}
static int pico8_chr(lua_State *l) {
// PICO-8 seems to top out at allowing 248 arguments
char s[248];
size_t numargs = lua_gettop(l);
if (numargs > sizeof(s)) numargs = sizeof(s);
for (size_t i = 0; i < numargs; i++) {
s[i] = (char)(uint8_t)lua_tonumber(l, i + 1);
}
lua_pushlstring(l, s, numargs);
return 1;
}
static int pico8_ord(lua_State *l) {
size_t len;
int n = 0;
int count = 1;
char const *s = luaL_checklstring(l, 1, &len);
if (!lua_isnone(l, 3)) {
if (!lua_isnumber(l, 3)) return 0;
count = int(lua_tonumber(l, 3));
}
if (!lua_isnone(l, 2)) {
if (!lua_isnumber(l, 2)) return 0;
n = int(lua_tonumber(l, 2)) - 1;
}
if (n < 0 || size_t(n) >= len || count < 1)
return 0;
if (size_t(n + count) > len)
count = len - n;
// min stack is only 20. This could be a much longer string
lua_checkstack(l, count);
for (int i = 0; i < count; ++i)
lua_pushnumber(l, uint8_t(s[n + i]));
return count;
}
static int pico8_split(lua_State *l) {
if (lua_isnil(l, 1)) {
return 0;
}
size_t count = 0, hlen;
char const *haystack = luaL_checklstring(l, 1, &hlen);
if (!haystack)
return 0;
lua_newtable(l);
// Split either by chunk size or by needle position
int size = 0;
char needle = ',';
if (lua_isnumber(l, 2)) {
size = int(lua_tonumber(l, 2));
if (size <= 0)
size = 1;
} else if (lua_isstring(l, 2)) {
needle = *lua_tostring(l, 2);
}
auto convert = lua_isnone(l, 3) || lua_toboolean(l, 3);
char const *end = haystack + hlen + (!size && needle);
for (char const *parser = haystack; parser < end; ) {
lua_Number num;
char const *next = size ? parser + size
: needle ? (char const*)memchr(parser, needle, end - parser) : parser + 1;
if (!next || next > end)
next = haystack + hlen;
char saved = *next; // temporarily put a null terminator here
*(char *)next = '\0';
if (convert && luaO_str2d(parser, next - parser, &num))
lua_pushnumber(l, num);
else
lua_pushlstring(l, parser, next - parser);
*(char *)next = saved;
lua_rawseti(l, -2, int(++count));
parser = next + (!size && needle);
}
return 1;
}
static const luaL_Reg pico8lib[] = {
{"max", pico8_max},
{"min", pico8_min},
{"mid", pico8_mid},
{"ceil", pico8_ceil},
{"flr", pico8_flr},
{"cos", pico8_cos},
{"sin", pico8_sin},
{"atan2", pico8_atan2},
{"sqrt", pico8_sqrt},
{"abs", pico8_abs},
{"sgn", pico8_sgn},
{"band", pico8_band},
{"bor", pico8_bor},
{"bxor", pico8_bxor},
{"bnot", pico8_bnot},
{"shl", pico8_shl},
{"shr", pico8_shr},
{"lshr", pico8_lshr},
{"rotl", pico8_rotl},
{"rotr", pico8_rotr},
{"tostr", pico8_tostr},
{"tonum", pico8_tonum},
{"chr", pico8_chr},
{"ord", pico8_ord},
{"split", pico8_split},
{NULL, NULL}
};
/*
** Register PICO-8 functions in global table
*/
LUAMOD_API int luaopen_pico8 (lua_State *L) {
lua_pushglobaltable(L);
luaL_setfuncs(L, pico8lib, 0);
return 1;
}