Changed my email in the copyright statements.
[tagua/yd.git] / src / luaapi / genericwrapper.cpp
blob22994764ef72c4f408314a08cfae7c043328aac7
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
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 QDir* dir = reinterpret_cast<QDir*>(lua_touserdata(l, -1));
21 lua_pop(l, 1);
22 return QDir::cleanPath(QDir::isAbsolutePath(f) ? f : dir->filePath(f));
25 Loader::Context* GenericWrapperBase::retrieve_context(lua_State* l) {
26 StackCheck check(l);
28 lua_getfield(l, LUA_REGISTRYINDEX, LOADING_CONTEXT);
29 Loader::Context* retv = reinterpret_cast<Loader::Context*>(lua_touserdata(l, -1));
30 lua_pop(l, 1);
31 return retv;
34 void GenericWrapperBase::set_property(lua_State* l, const char* name, lua_CFunction get,
35 lua_CFunction set, int index) {
36 StackCheck check(l);
38 lua_getfield(l,index,".get");
39 lua_pushcfunction(l, get);
40 lua_setfield(l, -2, name);
41 lua_pop(l, 1);
43 if(set) {
44 lua_getfield(l,index,".set");
45 lua_pushcfunction(l, set);
46 lua_setfield(l, -2, name);
47 lua_pop(l, 1);
51 void GenericWrapperBase::set_method(lua_State* l, lua_CFunction f, const char* name,
52 int index) {
53 StackCheck check(l);
55 lua_getfield(l,index,".methods");
56 lua_pushcfunction(l, f);
57 lua_setfield(l, -2, name);
58 lua_pop(l, 1);
61 void GenericWrapperBase::set_meta_method(lua_State* l, lua_CFunction f,
62 const char* name, int index) {
63 StackCheck check(l);
65 lua_pushcfunction(l, f);
66 lua_setfield(l, index-1, name);
69 int GenericWrapperBase::object_meta_index_event(lua_State* l) {
71 /* should i check the object before the meta table? no... */
72 lua_getmetatable(l,-2);
74 //key, MT
75 lua_pushstring(l,".methods");
76 lua_rawget(l,-2);
77 lua_pushvalue(l,-3);
78 lua_gettable(l,-2);
80 //key, MT, .methods, method
81 if(!lua_isnil(l,-1)) {
82 lua_remove(l,-2);
83 lua_remove(l,-2);
84 lua_remove(l,-2);
85 return 1;
87 lua_pop(l,2);
89 //key, MT
90 lua_pushstring(l,".get");
91 lua_rawget(l,-2);
92 lua_pushvalue(l,-3);
93 lua_gettable(l,-2);
95 //key, MT, .get, getter
96 if(!lua_isnil(l,-1)) {
97 lua_remove(l,-2);
98 lua_remove(l,-2);
99 lua_remove(l,-2);
100 lua_pushvalue(l,-2);
101 lua_call(l, 1, 1);
102 return 1;
105 luaL_error(l, "Can't get unexisting method/property %s\n", lua_tostring(l,-4));
106 lua_pop(l,4);
107 return 0;
110 int GenericWrapperBase::object_meta_newindex_event(lua_State* l) {
111 StackCheck check(l, -2);
113 /* should i check the object before the meta table? no... */
114 lua_getmetatable(l,-3);
116 //key, value, MT
117 lua_pushstring(l,".set");
118 lua_rawget(l,-2);
119 lua_pushvalue(l,-4);
120 lua_gettable(l,-2);
122 //key, value, MT, .set, setter
123 if(!lua_isnil(l,-1)) {
124 lua_remove(l,-2);
125 lua_remove(l,-2);
126 lua_remove(l,-3);
127 lua_pushvalue(l,-3);
128 lua_pushvalue(l,-3);
129 lua_remove(l,-4);
130 lua_call(l, 2, 0);
131 return 0;
134 luaL_error(l, "Can't set unexisting property %s\n", lua_tostring(l,-5));
135 lua_pop(l, 5);
136 return 0;
140 } //end namespace LuaApi