diff options
author | chai <chaifix@163.com> | 2018-09-08 13:09:06 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2018-09-08 13:09:06 +0800 |
commit | 4798e70dcbbedb55de8e9f8e321e8fad7b9783f6 (patch) | |
tree | f4740d59b561163b260f2f29467cfffdd92c4d9e /src/lua | |
parent | 36f7e3e5542f3cfee11b34ce497fcb877b3462bf (diff) |
*update
Diffstat (limited to 'src/lua')
-rw-r--r-- | src/lua/common/common.h | 1 | ||||
-rw-r--r-- | src/lua/common/error.h | 28 | ||||
-rw-r--r-- | src/lua/libraries/luax/luax.h | 1 | ||||
-rw-r--r-- | src/lua/modules/audio/audio.cpp | 22 | ||||
-rw-r--r-- | src/lua/modules/core/core.cpp | 8 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 38 | ||||
-rw-r--r-- | src/lua/modules/luax.h | 7 |
7 files changed, 82 insertions, 23 deletions
diff --git a/src/lua/common/common.h b/src/lua/common/common.h index 536b897..0ee72cc 100644 --- a/src/lua/common/common.h +++ b/src/lua/common/common.h @@ -3,5 +3,6 @@ #include "Proxy.h" #include "Reference.hpp" +#include "error.h" #endif
\ No newline at end of file diff --git a/src/lua/common/error.h b/src/lua/common/error.h new file mode 100644 index 0000000..f8b62f2 --- /dev/null +++ b/src/lua/common/error.h @@ -0,0 +1,28 @@ +#ifndef __JIN_ERROR_H +#define __JIN_ERROR_H +#include "../../luax.h" +#include "../modules/jin.h" +#include <string.h> + +namespace jin +{ +namespace lua +{ + + static const int FORMAT_MSG_BUFFER_SIZE = 2048; + + inline void error(lua_State* L, const char* fmt, ...) + { + char err[FORMAT_MSG_BUFFER_SIZE + 1] = { 0 }; + va_list args; + va_start(args, fmt); + vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args); + va_end(args); + luax_getglobal(L, MODULE_NAME); + luax_setfield_string(L, "error", err); + } + +} +} + +#endif
\ No newline at end of file diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h index 56023b3..9b283bd 100644 --- a/src/lua/libraries/luax/luax.h +++ b/src/lua/libraries/luax/luax.h @@ -104,6 +104,7 @@ inline bool luax_checkbool(lua_State *L, int numArg) #define luax_pushinteger lua_pushinteger #define luax_pushboolean lua_pushboolean #define luax_pushlightuserdata lua_pushlightuserdata +#define luax_pushnil lua_pushnil //inline void luax_pushuserdata(lua_State* L, void* p) //{ diff --git a/src/lua/modules/audio/audio.cpp b/src/lua/modules/audio/audio.cpp index 3d29e17..7590104 100644 --- a/src/lua/modules/audio/audio.cpp +++ b/src/lua/modules/audio/audio.cpp @@ -64,17 +64,29 @@ namespace lua { Filesystem* fs = Filesystem::get(); const char* f = luax_checkstring(L, 1); + Buffer b; if (!fs->exists(f)) { - printf("Error: no such image %s\n", f); - exit(1); + error(L, "No such image %s", f); + goto fail; + } + if (!fs->read(f, &b)) + { + error(L, "Failed to read source file %s", f); + goto fail; } - Buffer b; - fs->read(f, &b); - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); Source* src = Source::createSource(b.data, b.size); + if (src == nullptr) + { + error(L, "Failed to decode source file %s", f); + goto fail; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_AUDIO_SOURCE, sizeof(Proxy)); proxy->bind(new Ref<Source>(src, JIN_AUDIO_SOURCE)); return 1; + fail: + luax_pushnil(L); + return 1; } static int l_destroy(lua_State* L) diff --git a/src/lua/modules/core/core.cpp b/src/lua/modules/core/core.cpp index 4a370db..bfd80b8 100644 --- a/src/lua/modules/core/core.cpp +++ b/src/lua/modules/core/core.cpp @@ -29,10 +29,10 @@ namespace lua } static const luaL_Reg f[] = { - {"running", l_running }, - {"stop", l_stop }, - {"quit", l_quit }, - {0, 0 } + { "running", l_running }, + { "stop", l_stop }, + { "quit", l_quit }, + { 0, 0 } }; int luaopen_core(lua_State* L) diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index 8a58ff0..ada4754 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -107,16 +107,28 @@ namespace lua Filesystem* fs = Filesystem::get(); if (!fs->exists(f)) { - printf("Error: no such texture %s\n", f); - exit(1); + error(L, "No such image file %s", f); + goto fail; } Buffer b; - fs->read(f, &b); + if (!fs->read(f, &b)) + { + error(L, "Failed to read image %s", f); + goto fail; + } bitmap = Bitmap::createBitmap(b.data, b.size); + if (bitmap == nullptr) + { + error(L, "Failed to decode image file %s", f); + goto fail; + } } Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_BITMAP, sizeof(Proxy)); proxy->bind(new Ref<Bitmap>(bitmap, JIN_GRAPHICS_BITMAP)); return 1; + fail: + luax_pushnil(L); + return 1; } /* jin.graphics.newTexture(bitmap) */ @@ -126,16 +138,22 @@ namespace lua Ref<Bitmap>& refBitmap = p->getRef<Bitmap>(); Bitmap* bitmap = refBitmap.getObject(); Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_TEXTURE, sizeof(Proxy)); - Texture* img = Texture::createTexture(bitmap); - proxy->bind(new Ref<Texture>(img, JIN_GRAPHICS_TEXTURE)); + Texture* tex = Texture::createTexture(bitmap); + proxy->bind(new Ref<Texture>(tex, JIN_GRAPHICS_TEXTURE)); return 1; } static int l_newShader(lua_State* L) { - Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); const char* program = luax_checkstring(L, 1); JSLProgram* jsl = JSLProgram::createJSLProgram(program); + if (jsl == nullptr) + { + error(L, "Failed to compile shader"); + luax_pushnil(L); + return 1; + } + Proxy* proxy = (Proxy*)luax_newinstance(L, JIN_GRAPHICS_SHADER, sizeof(Proxy)); proxy->bind(new Ref<JSLProgram>(jsl, JIN_GRAPHICS_SHADER)); return 1; } @@ -420,11 +438,10 @@ namespace lua luax_error(L, emsg, n * 2, tn); return 1; } - float* p = new float[2 * n]; + float* p = (float*)alloca(2 * n * sizeof(float)); for (int i = 1; i <= 2 * n; ++i) p[i - 1] = luax_rawgetnumber(L, 3, i); polygon(mode, p, n); - delete[] p; } else { @@ -445,8 +462,9 @@ namespace lua Buffer b = {}; if (!fs->exists(path)) { - printf("Error: no such font %s\n", path); - exit(1); + error(L, "No such font %s\n", path); + luax_pushnil(L); + return 1; } fs->read(path, &b); font->loadMemory((const unsigned char*)b.data); diff --git a/src/lua/modules/luax.h b/src/lua/modules/luax.h index 89e456e..24d74e4 100644 --- a/src/lua/modules/luax.h +++ b/src/lua/modules/luax.h @@ -1,7 +1,6 @@ -#ifndef __JIN_LUA_LUAX_H -#define __JIN_LUA_LUAX_H +#ifndef __JIN_MODULES_LUAX_H +#define __JIN_MODULES_LUAX_H -#include "LuaJIT/lua.hpp" -#include "lua/libraries/luax/luax.h" +#include "../../luax.h" #endif
\ No newline at end of file |