Skip to content

Commit 85570dd

Browse files
authored
Merge pull request #1924 from nicolasnoble/psyqo-lua
Various psyqo-lua fixes & improvements
2 parents 0e6eda7 + 0367fe3 commit 85570dd

File tree

10 files changed

+107
-54
lines changed

10 files changed

+107
-54
lines changed

.github/workflows/linux-toolchain.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
- name: Build OpenBIOS with it
2727
run: make -C src/mips/openbios -j 6 all
2828
- name: Build psyqo examples with it
29-
run: for d in src/mips/psyqo/examples/* ; do make -C $d -j 6 all TEST=true ; done
29+
run: for d in src/mips/psyqo/examples/* src/mips/psyqo-paths/examples/* src/mips/psyqo-lua/examples/* ; do make -C $d -j 6 all TEST=true ; done

src/mips/common.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ DEPS += $(patsubst %.s, %.dep,$(filter %.s,$(SRCS)))
108108

109109
dep: $(DEPS)
110110

111-
clean: $(EXTRA_CLEAN)
111+
clean::
112112
rm -f $(OBJS) $(BINDIR)*.a $(BINDIR)Overlay.* $(BINDIR)*.elf $(BINDIR)*.ps-exe $(BINDIR)*.map $(DEPS)
113113

114114
ifneq ($(MAKECMDGOALS), clean)

src/mips/psyqo-lua/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ SRCS = \
77
src/lua.cpp \
88

99
CPPFLAGS += -I$(PSYQOLUADIR)../../../third_party/psxlua/src
10+
CPPFLAGS += -DLUA_TARGET_PSX
1011

1112
EXTRA_DEPS += $(PSYQOLUADIR)Makefile
1213

src/mips/psyqo-lua/examples/hello/hello.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ function factorial(n)
4545
end
4646
end
4747
48+
function printfactorial(n)
49+
local f = factorial(n)
50+
print('Factorial of ' .. tostring(n) .. ' is ' .. tostring(f))
51+
return f
52+
end
53+
4854
-- Calculate some values
4955
results = {
5056
factorial = factorial(5),
@@ -159,6 +165,8 @@ void LuaExample::createScene() {
159165
pushScene(&luaExampleScene);
160166
}
161167

168+
using namespace psyqo::fixed_point_literals;
169+
162170
void LuaExampleScene::frame() {
163171
auto& gpu = luaExample.gpu();
164172
auto& font = luaExample.m_font;
@@ -189,6 +197,20 @@ void LuaExampleScene::frame() {
189197
font.printf(gpu, {{.x = 16, .y = 144}}, textColor, "Calling factorial(7) from C++: %d", factorial7);
190198
}
191199
luaExample.L.pop();
200+
201+
// Example of using FixedPoint
202+
luaExample.L.getGlobal("printfactorial");
203+
luaExample.L.push(6.0_fp);
204+
if (luaExample.L.pcall(1, 1) != 0) {
205+
// Error occurred
206+
font.print(gpu, "Error calling printfactorial(6):", {{.x = 16, .y = 160}}, textColor);
207+
font.print(gpu, luaExample.L.toString(-1), {{.x = 16, .y = 176}}, textColor);
208+
} else {
209+
auto factorial6 = luaExample.L.toFixedPoint(-1);
210+
font.print(gpu, "Calling printfactorial(6.0_fp):", {{.x = 16, .y = 176}}, textColor);
211+
font.printf(gpu, {{.x = 16, .y = 192}}, textColor, "Factorial(6) is %.2f", factorial6);
212+
}
213+
luaExample.L.pop();
192214
} else {
193215
// Display error message
194216
font.print(gpu, "Lua script execution failed:", {{.x = 16, .y = 64}}, textColor);

src/mips/psyqo-lua/lua.hh

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SOFTWARE.
2929
extern "C" {
3030
#include "lauxlib.h"
3131
#include "lua.h"
32+
#include "lualib.h"
3233
}
3334

3435
#include "EASTL/string_view.h"
@@ -78,7 +79,8 @@ struct Lua {
7879
lua_pushlstring(L, s, S - 1);
7980
}
8081
void push(eastl::string_view s) { lua_pushlstring(L, s.data(), s.size()); }
81-
void push(const char* fmt, ...);
82+
void vpushf(const char* fmt, va_list ap) { lua_pushvfstring(L, fmt, ap); }
83+
void pushf(const char* fmt, ...);
8284
void push(lua_CFunction f, int closure = 0) { lua_pushcclosure(L, f, closure); }
8385
void push(lua_CPPFunction f);
8486
void push(void* p) { lua_pushlightuserdata(L, p); }
@@ -97,7 +99,10 @@ struct Lua {
9799
const char* toString(int idx, size_t* len = nullptr) { return lua_tolstring(L, idx, len); }
98100
size_t rawLen(int idx) { return lua_rawlen(L, idx); }
99101
lua_CFunction toCFunction(int idx) { return lua_tocfunction(L, idx); }
100-
void* toUserdata(int idx) { return lua_touserdata(L, idx); }
102+
template <typename T = void>
103+
T* toUserdata(int idx) {
104+
return reinterpret_cast<T*>(lua_touserdata(L, idx));
105+
}
101106
lua_State* toThread(int idx) { return lua_tothread(L, idx); }
102107
const void* toPointer(int idx) { return lua_topointer(L, idx); }
103108

@@ -141,8 +146,8 @@ struct Lua {
141146
int setMetatable(int objindex) { return lua_setmetatable(L, objindex); }
142147

143148
// Function calling
144-
void call(int nargs, int nresults) { lua_call(L, nargs, nresults); }
145-
int pcall(int nargs, int nresults, int errfunc = 0) { return lua_pcall(L, nargs, nresults, errfunc); }
149+
void call(int nargs, int nresults = LUA_MULTRET) { lua_call(L, nargs, nresults); }
150+
int pcall(int nargs, int nresults = LUA_MULTRET);
146151

147152
// Loading and executing
148153
int loadBuffer(const char* buff, size_t sz, const char* chunkname = nullptr) {
@@ -197,6 +202,7 @@ struct Lua {
197202
private:
198203
lua_State* L;
199204
void setupFixedPointMetatable();
205+
static int traceback(lua_State* L);
200206
};
201207

202208
} // namespace psyqo

src/mips/psyqo-lua/psyqo-lua.mk

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ PSYQOLUADIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
44
LIBRARIES += $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a
55

66
CPPFLAGS += -I$(PSYQOLUADIR)../../../third_party/psxlua/src
7+
CPPFLAGS += -DLUA_TARGET_PSX
78

89
LDFLAGS += \
910
-Wl,--defsym,luaI_sprintf=sprintf_for_Lua \
1011
-Wl,--defsym,luaI_realloc=psyqo_realloc \
1112
-Wl,--defsym,luaI_free=psyqo_free \
1213

13-
EXTRA_CLEAN += clean-psyqo-lua
14-
1514
include $(PSYQOLUADIR)../psyqo/psyqo.mk
1615

1716
$(PSYQOLUADIR)libpsyqo-lua.a:
@@ -20,9 +19,9 @@ $(PSYQOLUADIR)libpsyqo-lua.a:
2019
$(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a:
2120
$(MAKE) -C $(PSYQOLUADIR)../../../third_party/psxlua/ psx
2221

23-
clean-psyqo-lua:
22+
clean::
2423
$(MAKE) -C $(PSYQOLUADIR) clean
2524
$(MAKE) -C $(PSYQOLUADIR)../../../third_party/psxlua/ clean
2625

27-
.PHONY: clean-psyqo-lua $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a
26+
.PHONY: clean $(PSYQOLUADIR)libpsyqo-lua.a $(PSYQOLUADIR)../../../third_party/psxlua/src/liblua.a
2827
endif

src/mips/psyqo-lua/src/lua.cpp

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ SOFTWARE.
2626

2727
#include "psyqo-lua/lua.hh"
2828

29+
#include <EASTL/string.h>
30+
2931
#include "psyqo/kernel.hh"
3032
#include "psyqo/xprintf.h"
3133

@@ -111,6 +113,7 @@ psyqo::FixedPoint<> psyqo::Lua::optFixedPoint(int idx, FixedPoint<> def) {
111113
psyqo::Lua::Lua() : L(luaL_newstate()) {
112114
static_assert(sizeof(Lua) == sizeof(lua_State*));
113115
Kernel::assert(L, "Couldn't create Lua VM");
116+
luaL_openlibs(L);
114117
lua_atpanic(L, [](lua_State* L) -> int {
115118
const char* errorMsg = lua_tolstring(L, 1, nullptr);
116119
Kernel::abort(errorMsg);
@@ -153,7 +156,7 @@ int psyqo::Lua::error(const char* fmt, ...) {
153156
return lua_error(L);
154157
}
155158

156-
void psyqo::Lua::push(const char* fmt, ...) {
159+
void psyqo::Lua::pushf(const char* fmt, ...) {
157160
va_list argp;
158161
va_start(argp, fmt);
159162
lua_pushvfstring(L, fmt, argp);
@@ -238,10 +241,10 @@ void psyqo::Lua::setupFixedPointMetatable() {
238241
unsigned fraction = raw & 0xfff;
239242

240243
if (fraction == 0) {
241-
L.push("%d", integer);
244+
L.pushf("%d", integer);
242245
} else {
243246
unsigned decimal = (fraction * 1000) >> 12;
244-
L.push("%d.%03u", integer, decimal);
247+
L.pushf("%d.%03u", integer, decimal);
245248
}
246249

247250
return 1;
@@ -253,68 +256,80 @@ void psyqo::Lua::setupFixedPointMetatable() {
253256
return function(metatable)
254257
FixedPoint = metatable
255258
259+
-- Create a new FixedPoint from raw value
260+
local newFromRaw = function(raw_value)
261+
return setmetatable({_raw = raw_value}, FixedPoint)
262+
end
263+
264+
FixedPoint.newFromRaw = newFromRaw
265+
local lshift = bit32.lshift
266+
local rshift = bit32.rshift
267+
local err = function(op)
268+
error('Cannot ' .. op .. ' FixedPoint to this type')
269+
end
270+
256271
-- Simple operations can be done directly in Lua
257272
function FixedPoint.__add(a, b)
258273
local raw_a = a._raw
259-
if type(b) == "number" then
260-
-- FixedPoint + number (treated as integer)
261-
return FixedPoint.new(raw_a + bit.lshift(b, 12))
262-
elseif type(b) == "table" and b._raw then
274+
if type(b) == 'number' then
275+
-- FixedPoint + number
276+
return newFromRaw(raw_a + lshift(b, 12))
277+
elseif type(b) == 'table' and b._raw then
263278
-- FixedPoint + FixedPoint
264-
return FixedPoint.new(raw_a + b._raw)
279+
return newFromRaw(raw_a + b._raw)
265280
else
266-
error("Cannot add FixedPoint to this type")
281+
err('add')
267282
end
268283
end
269284
270285
function FixedPoint.__sub(a, b)
271286
local raw_a = a._raw
272-
if type(b) == "number" then
273-
-- FixedPoint - number (treated as integer)
274-
return FixedPoint.new(raw_a - bit.lshift(b, 12))
275-
elseif type(b) == "table" and b._raw then
287+
if type(b) == 'number' then
288+
-- FixedPoint - number
289+
return newFromRaw(raw_a - lshift(b, 12))
290+
elseif type(b) == 'table' and b._raw then
276291
-- FixedPoint - FixedPoint
277-
return FixedPoint.new(raw_a - b._raw)
292+
return newFromRaw(raw_a - b._raw)
278293
else
279-
error("Cannot subtract this type from FixedPoint")
294+
err('subtract')
280295
end
281296
end
282297
283298
function FixedPoint.__unm(a)
284299
-- Unary minus
285-
return FixedPoint.new(-a._raw)
300+
return newFromRaw(-a._raw)
286301
end
287302
288303
function FixedPoint.__eq(a, b)
289-
if type(b) == "table" and b._raw then
304+
if type(b) == 'table' and b._raw then
290305
return a._raw == b._raw
291-
elseif type(b) == "number" then
306+
elseif type(b) == 'number' then
292307
-- Compare with an integer number (shifted)
293-
return a._raw == bit.lshift(b, 12)
308+
return a._raw == lshift(b, 12)
294309
else
295310
return false
296311
end
297312
end
298313
299314
function FixedPoint.__lt(a, b)
300-
if type(b) == "table" and b._raw then
315+
if type(b) == 'table' and b._raw then
301316
return a._raw < b._raw
302-
elseif type(b) == "number" then
317+
elseif type(b) == 'number' then
303318
-- Compare with an integer number (shifted)
304-
return a._raw < bit.lshift(b, 12)
319+
return a._raw < lshift(b, 12)
305320
else
306-
error("Cannot compare FixedPoint with this type")
321+
err('compare')
307322
end
308323
end
309324
310325
function FixedPoint.__le(a, b)
311-
if type(b) == "table" and b._raw then
326+
if type(b) == 'table' and b._raw then
312327
return a._raw <= b._raw
313-
elseif type(b) == "number" then
328+
elseif type(b) == 'number' then
314329
-- Compare with an integer number (shifted)
315-
return a._raw <= bit.lshift(b, 12)
330+
return a._raw <= lshift(b, 12)
316331
else
317-
error("Cannot compare FixedPoint with this type")
332+
err('compare')
318333
end
319334
end
320335
@@ -325,24 +340,19 @@ void psyqo::Lua::setupFixedPointMetatable() {
325340
326341
-- Method to convert to a simple number (for simple calculations)
327342
function FixedPoint:toNumber()
328-
return (self._raw + 2048) / 4096
329-
end
330-
331-
-- Create a new FixedPoint from raw value
332-
function FixedPoint.newFromRaw(raw_value)
333-
return setmetatable({_raw = raw_value}, FixedPoint)
343+
return rshift((self._raw + 2048), 12)
334344
end
335345
336346
-- Create a new FixedPoint
337347
function FixedPoint.new(integer, fraction)
338348
if fraction == nil then fraction = 0 end
339-
return setmetatable({_raw = bit.lshift(integer, 12) + fraction}, FixedPoint)
349+
return setmetatable({_raw = lshift(integer, 12) + fraction}, FixedPoint)
340350
end
341351
end
342352
)lua";
343353

344354
// Load the Lua script
345-
if (loadBuffer(fixedPointScript, sizeof(fixedPointScript) - 1) != 0) {
355+
if (loadBuffer(fixedPointScript, sizeof(fixedPointScript) - 1, "buffer:fixedPointScript") != 0) {
346356
const char* errorMsg = toString(2);
347357
psyqo::Kernel::abort(errorMsg);
348358
}
@@ -362,3 +372,22 @@ void psyqo::Lua::setupFixedPointMetatable() {
362372

363373
pop();
364374
}
375+
376+
int psyqo::Lua::pcall(int nargs, int nresults) {
377+
int n = getTop();
378+
int errorfunc = n - nargs;
379+
lua_pushcfunction(L, traceback);
380+
insert(errorfunc);
381+
int r = lua_pcall(L, nargs, nresults, errorfunc);
382+
remove(errorfunc);
383+
return r;
384+
}
385+
386+
int psyqo::Lua::traceback(lua_State* L) {
387+
int n = lua_gettop(L);
388+
const char* msgPtr = n >= 1 ? lua_tostring(L, 1) : nullptr;
389+
eastl::string msg = msgPtr ? msgPtr : "no message";
390+
lua_settop(L, 0);
391+
luaL_traceback(L, L, msg.c_str(), 1);
392+
return 1;
393+
}

src/mips/psyqo-paths/psyqo-paths.mk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ PSYQOPATHSDIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
33

44
LIBRARIES += $(PSYQOPATHSDIR)libpsyqo-paths.a
55

6-
EXTRA_CLEAN += clean-psyqo-paths
7-
86
include $(PSYQOPATHSDIR)../psyqo/psyqo.mk
97

108
$(PSYQOPATHSDIR)libpsyqo-paths.a:
119
$(MAKE) -C $(PSYQOPATHSDIR) BUILD=$(BUILD)
1210

13-
clean-psyqo-paths:
11+
clean::
1412
$(MAKE) -C $(PSYQOPATHSDIR) clean
1513

16-
.PHONY: clean-psyqo-paths $(PSYQOPATHSDIR)libpsyqo-paths.a
14+
.PHONY: clean $(PSYQOPATHSDIR)libpsyqo-paths.a
1715
endif

src/mips/psyqo/psyqo.mk

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ LIBRARIES += $(PSYQODIR)libpsyqo.a
55
CPPFLAGS += -I$(PSYQODIR)../../../third_party/EASTL/include -I$(PSYQODIR)../../../third_party/EABase/include/Common
66
CXXFLAGS += -std=c++20
77

8-
EXTRA_CLEAN += clean-psyqo
9-
108
include $(PSYQODIR)../common.mk
119

1210
$(PSYQODIR)libpsyqo.a:
1311
$(MAKE) -C $(PSYQODIR) BUILD=$(BUILD) CPPFLAGS_$(BUILD)=$(CPPFLAGS_$(BUILD)) LDFLAGS_$(BUILD)=$(LDFLAGS_$(BUILD))
1412

15-
clean-psyqo:
13+
clean::
1614
$(MAKE) -C $(PSYQODIR) clean
1715

18-
.PHONY: clean-psyqo $(PSYQODIR)libpsyqo.a
16+
.PHONY: clean $(PSYQODIR)libpsyqo.a
1917
endif

third_party/psxlua

0 commit comments

Comments
 (0)