Started to make things work: show the board.
[tagua.git] / src / luaapi / genericwrapper.cpp
blobc6957934538fadbbe6857ca0ade77444e7ddfe31
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include <QDir>
12 #include "genericwrapper.h"
14 namespace LuaApi {
16 QString GenericWrapperBase::file_path(lua_State* l, const QString& f) {
17 StackCheck check(l);
19 lua_getfield(l, LUA_REGISTRYINDEX, CURRENT_DIRECTORY);
20 if(lua_isnil(l, -1)) {
21 lua_pop(l, 1);
22 return f;
24 QDir* dir = reinterpret_cast<QDir*>(lua_touserdata(l, -1));
25 lua_pop(l, 1);
26 return QDir::cleanPath(QDir::isAbsolutePath(f) ? f : dir->filePath(f));
29 Loader::Context* GenericWrapperBase::retrieve_context(lua_State* l) {
30 StackCheck check(l);
32 lua_getfield(l, LUA_REGISTRYINDEX, LOADING_CONTEXT);
33 Loader::Context* retv = reinterpret_cast<Loader::Context*>(lua_touserdata(l, -1));
34 lua_pop(l, 1);
35 return retv;
38 void GenericWrapperBase::set_property(lua_State* l, const char* name, lua_CFunction get,
39 lua_CFunction set, int index) {
40 StackCheck check(l);
42 lua_getfield(l,index,".get");
43 lua_pushcfunction(l, get);
44 lua_setfield(l, -2, name);
45 lua_pop(l, 1);
47 if(set) {
48 lua_getfield(l,index,".set");
49 lua_pushcfunction(l, set);
50 lua_setfield(l, -2, name);
51 lua_pop(l, 1);
55 void GenericWrapperBase::set_method(lua_State* l, lua_CFunction f, const char* name,
56 int index) {
57 StackCheck check(l);
59 lua_getfield(l,index,".methods");
60 lua_pushcfunction(l, f);
61 lua_setfield(l, -2, name);
62 lua_pop(l, 1);
65 void GenericWrapperBase::set_meta_method(lua_State* l, lua_CFunction f,
66 const char* name, int index) {
67 StackCheck check(l);
69 lua_pushcfunction(l, f);
70 lua_setfield(l, index-1, name);
73 int GenericWrapperBase::object_meta_index_event(lua_State* l) {
75 /* should i check the object before the meta table? no... */
76 lua_getmetatable(l,-2);
78 //key, MT
79 lua_pushstring(l,".methods");
80 lua_rawget(l,-2);
81 lua_pushvalue(l,-3);
82 lua_gettable(l,-2);
84 //key, MT, .methods, method
85 if(!lua_isnil(l,-1)) {
86 lua_remove(l,-2);
87 lua_remove(l,-2);
88 lua_remove(l,-2);
89 return 1;
91 lua_pop(l,2);
93 //key, MT
94 lua_pushstring(l,".get");
95 lua_rawget(l,-2);
96 lua_pushvalue(l,-3);
97 lua_gettable(l,-2);
99 //key, MT, .get, getter
100 if(!lua_isnil(l,-1)) {
101 lua_remove(l,-2);
102 lua_remove(l,-2);
103 lua_remove(l,-2);
104 lua_pushvalue(l,-2);
105 lua_call(l, 1, 1);
106 return 1;
109 luaL_error(l, "Can't get unexisting method/property %s\n", lua_tostring(l,-4));
110 lua_pop(l,4);
111 return 0;
114 int GenericWrapperBase::object_meta_newindex_event(lua_State* l) {
115 StackCheck check(l, -2);
117 /* should i check the object before the meta table? no... */
118 lua_getmetatable(l,-3);
120 //key, value, MT
121 lua_pushstring(l,".set");
122 lua_rawget(l,-2);
123 lua_pushvalue(l,-4);
124 lua_gettable(l,-2);
126 //key, value, MT, .set, setter
127 if(!lua_isnil(l,-1)) {
128 lua_remove(l,-2);
129 lua_remove(l,-2);
130 lua_remove(l,-3);
131 lua_pushvalue(l,-3);
132 lua_pushvalue(l,-3);
133 lua_remove(l,-4);
134 lua_call(l, 2, 0);
135 return 0;
138 luaL_error(l, "Can't set unexisting property %s\n", lua_tostring(l,-5));
139 lua_pop(l, 5);
140 return 0;
144 } //end namespace LuaApi