Move print_override to luax;

It makes more sense for it to live there since it's a Lua override.
I misunderstood something when communicating about this initially.
This commit is contained in:
bjorn 2018-11-13 18:31:11 -08:00 committed by Bjorn Swenson
parent 784d61ef4a
commit c6126bed59
6 changed files with 33 additions and 44 deletions

View File

@ -575,7 +575,7 @@ elseif(EMSCRIPTEN)
target_sources(lovr PRIVATE src/platform/web.c)
elseif(ANDROID)
target_link_libraries(lovr log EGL GLESv3)
target_sourceS(lovr PRIVATE src/platform/linux.c src/platform/android.c src/platform/print_override.c)
target_sources(lovr PRIVATE src/platform/linux.c src/platform/android.c)
elseif(UNIX)
target_sourceS(lovr PRIVATE src/platform/linux.c)
target_sources(lovr PRIVATE src/platform/linux.c)
endif()

View File

@ -358,14 +358,8 @@ void bridgeLovrInit(BridgeLovrInitData *initData) {
lovrSetErrorCallback((lovrErrorHandler) android_vthrow, L);
// Install custom print
static const struct luaL_Reg printHack [] = {
{"print", lovr_luaB_print_override},
{NULL, NULL} /* end of array */
};
lua_getglobal(L, "_G");
luaL_register(L, NULL, printHack); // "for Lua versions < 5.2"
//luaL_setfuncs(L, printlib, 0); // "for Lua versions 5.2 or greater"
lua_pop(L, 1);
lua_pushcfunction(L, luax_print);
lua_setglobal(L, "print");
glfwSetTime(0);

View File

@ -1,5 +1,7 @@
#include "luax.h"
#include "platform.h"
#include "util.h"
#include "lib/sds/sds.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdbool.h>
@ -25,6 +27,32 @@ static int luax_module__gc(lua_State* L) {
return 0;
}
// A version of print that uses lovrLog, for platforms that need it (Android)
int luax_print(lua_State* L) {
sds str = sdsempty();
int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++) {
const char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, LUA_QL("tostring") " must return a string to "
LUA_QL("print"));
if (i>1) str = sdscat(str, "\t");
str = sdscat(str, s);
lua_pop(L, 1); /* pop result */
}
lovrLog("%s", str);
sdsfree(str);
return 0;
}
void luax_atexit(lua_State* L, luax_destructor destructor) {
lua_getfield(L, LUA_REGISTRYINDEX, "_lovrmodules");

View File

@ -10,6 +10,7 @@
#define luax_checktype(L, i, T) ((T*) _luax_checktype(L, i, #T))
typedef void (*luax_destructor)(void);
int luax_print(lua_State* L);
void luax_atexit(lua_State* L, luax_destructor destructor);
void luax_registerloader(lua_State* L, lua_CFunction loader, int index);
void luax_registertype(lua_State* L, const char* name, const luaL_Reg* functions);

View File

@ -1,30 +0,0 @@
#include "luax.h"
#include "print_override.h"
#include "platform.h"
#include "lib/sds/sds.h"
// THIS FUNCTION IS SUBSTANTIALLY BASED ON luaB_print FROM LUA SOURCE AND IS LIKELY LICENSE ENCUMBERED
int lovr_luaB_print_override (lua_State *L) {
sds str = sdsempty();
int n = lua_gettop(L); /* number of arguments */
int i;
lua_getglobal(L, "tostring");
for (i=1; i<=n; i++) {
const char *s;
lua_pushvalue(L, -1); /* function to be called */
lua_pushvalue(L, i); /* value to print */
lua_call(L, 1, 1);
s = lua_tostring(L, -1); /* get result */
if (s == NULL)
return luaL_error(L, LUA_QL("tostring") " must return a string to "
LUA_QL("print"));
if (i>1) str = sdscat(str, "\t");
str = sdscat(str, s);
lua_pop(L, 1); /* pop result */
}
lovrLog("%s", str);
sdsfree(str);
return 0;
}

View File

@ -1,4 +0,0 @@
#pragma once
// A helper, currently used only by the Android platform, that acts like print() but directs to lovrLog
int lovr_luaB_print_override (lua_State *L);