diff options
author | cedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-06-14 15:06:06 +0200 |
---|---|---|
committer | cedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-06-14 15:06:06 +0200 |
commit | 92c59963f82f81aa3202657e7fdbb2592924ede3 (patch) | |
tree | b7eb2474528a4998fa102e3ec9119b908cee08b4 /source/tolua_base.h | |
parent | Added HOOK_WEATHER_CHANGE. (diff) | |
download | cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.gz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.bz2 cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.lz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.xz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.zst cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.zip |
Diffstat (limited to 'source/tolua_base.h')
-rw-r--r-- | source/tolua_base.h | 256 |
1 files changed, 128 insertions, 128 deletions
diff --git a/source/tolua_base.h b/source/tolua_base.h index 28708eb59..4f1038c09 100644 --- a/source/tolua_base.h +++ b/source/tolua_base.h @@ -1,128 +1,128 @@ -#ifndef TOLUA_BASE_H
-#define TOLUA_BASE_H
-
-#pragma warning(disable:4800) // This file is ONLY included by Bindings.cpp and it throws lots of C4800 warnings
-
-#include "tolua++.h"
-
-
-
-
-
-class ToluaBase {
-
- int lua_instance;
-
-protected:
-
- lua_State* lua_state;
-
- void lua_stacktrace(lua_State* L) const
- {
- lua_Debug entry;
- int depth = 0;
-
- while (lua_getstack(L, depth, &entry))
- {
- lua_getinfo(L, "Sln", &entry);
-
- LOGERROR("%s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?");
- depth++;
- }
- }
-
-
- bool report_errors(int status) const
- {
- if ( status!=0 )
- {
- const char* s = lua_tostring(lua_state, -1);
- LOGERROR("-- %s", s );
- //lua_pop(lua_state, 1);
- LOGERROR("Stack:");
- lua_stacktrace( lua_state );
- return true;
- }
- return false;
- }
-
- bool push_method(const char* name, lua_CFunction f) const {
-
- if (!lua_state) return false;
-
- lua_getref(lua_state, lua_instance);
- lua_pushstring(lua_state, name);
- //LOGINFO("1. push_method() Stack size: %i", lua_gettop( lua_state ) );
- lua_gettable(lua_state, -2);
- //LOGINFO("2. push_method() Stack size: %i", lua_gettop( lua_state ) );
-
- if (lua_isnil(lua_state, -1)) {
-
- // pop the table
- lua_pop(lua_state, 2);
- return false;
-
- } else {
-
- if (f) {
- if (lua_iscfunction(lua_state, -1)) {
- lua_pop(lua_state, 2);
- return false;
- };
- /* // not for now
- lua_pushcfunction(lua_state, f);
- if (lua_rawequal(lua_state, -1, -2)) {
-
- // avoid recursion, pop both functions and the table
- lua_pop(lua_state, 3);
- return false;
- };
-
- // pop f
- lua_pop(lua_state, 1);
- */
- };
-
- // swap table with function
- lua_insert(lua_state, -2);
- };
-
- return true;
- };
-
- void dbcall(lua_State* L, int nargs, int nresults) const {
-
- // using lua_call for now
- int s = lua_pcall(L, nargs, nresults, 0);
- report_errors( s );
- };
-public:
-
- int GetInstance() { return lua_instance; }
- lua_State* GetLuaState() { return lua_state; }
-
- void tolua__set_instance(lua_State* L, lua_Object lo) {
-
- lua_state = L;
-
- lua_pushvalue(L, lo);
- lua_instance = lua_ref(lua_state, 1);
- };
-
- ToluaBase() {
-
- lua_state = NULL;
- };
-
- ~ToluaBase() {
-
- if (lua_state) {
-
- lua_unref(lua_state, lua_instance);
- };
- };
-};
-
-#endif
-
-
+#ifndef TOLUA_BASE_H +#define TOLUA_BASE_H + +#pragma warning(disable:4800) // This file is ONLY included by Bindings.cpp and it throws lots of C4800 warnings + +#include "tolua++.h" + + + + + +class ToluaBase { + + int lua_instance; + +protected: + + lua_State* lua_state; + + void lua_stacktrace(lua_State* L) const + { + lua_Debug entry; + int depth = 0; + + while (lua_getstack(L, depth, &entry)) + { + lua_getinfo(L, "Sln", &entry); + + LOGERROR("%s(%d): %s", entry.short_src, entry.currentline, entry.name ? entry.name : "?"); + depth++; + } + } + + + bool report_errors(int status) const + { + if ( status!=0 ) + { + const char* s = lua_tostring(lua_state, -1); + LOGERROR("-- %s", s ); + //lua_pop(lua_state, 1); + LOGERROR("Stack:"); + lua_stacktrace( lua_state ); + return true; + } + return false; + } + + bool push_method(const char* name, lua_CFunction f) const { + + if (!lua_state) return false; + + lua_getref(lua_state, lua_instance); + lua_pushstring(lua_state, name); + //LOGINFO("1. push_method() Stack size: %i", lua_gettop( lua_state ) ); + lua_gettable(lua_state, -2); + //LOGINFO("2. push_method() Stack size: %i", lua_gettop( lua_state ) ); + + if (lua_isnil(lua_state, -1)) { + + // pop the table + lua_pop(lua_state, 2); + return false; + + } else { + + if (f) { + if (lua_iscfunction(lua_state, -1)) { + lua_pop(lua_state, 2); + return false; + }; + /* // not for now + lua_pushcfunction(lua_state, f); + if (lua_rawequal(lua_state, -1, -2)) { + + // avoid recursion, pop both functions and the table + lua_pop(lua_state, 3); + return false; + }; + + // pop f + lua_pop(lua_state, 1); + */ + }; + + // swap table with function + lua_insert(lua_state, -2); + }; + + return true; + }; + + void dbcall(lua_State* L, int nargs, int nresults) const { + + // using lua_call for now + int s = lua_pcall(L, nargs, nresults, 0); + report_errors( s ); + }; +public: + + int GetInstance() { return lua_instance; } + lua_State* GetLuaState() { return lua_state; } + + void tolua__set_instance(lua_State* L, lua_Object lo) { + + lua_state = L; + + lua_pushvalue(L, lo); + lua_instance = lua_ref(lua_state, 1); + }; + + ToluaBase() { + + lua_state = NULL; + }; + + ~ToluaBase() { + + if (lua_state) { + + lua_unref(lua_state, lua_instance); + }; + }; +}; + +#endif + + |