1 // ----------------------------------------------------------------------------
2 // Overridden functions for the wxLua binding for wxLua
3 //
4 // Please keep these functions in the same order as the .i file and in the
5 // same order as the listing of the functions in that file.
6 // ----------------------------------------------------------------------------
7 
8 // ----------------------------------------------------------------------------
9 // Overrides for wxlua.i
10 // ----------------------------------------------------------------------------
11 
12 %override wxLua_function_CompileLuaScript
13 // %function int CompileLuaScript(const wxString& luaScript, const wxString& fileName)
14 static int LUACALL wxLua_function_CompileLuaScript(lua_State *L)
15 {
16     int returns;
17     // const wxString fileName
18     const wxString fileName = wxlua_getwxStringtype(L, 2);
19     // const wxString luaScript
20     const wxString luaScript = wxlua_getwxStringtype(L, 1);
21     wxString errMsg;
22     int line_num = -1;
23     wxLuaState wxlState2(true); // create a brand new empty lua state to compile in
24     returns = wxlState2.CompileString(luaScript, fileName, &errMsg, &line_num);
25     // push the result number
26     lua_pushnumber(L, returns);
27     wxlua_pushwxString(L, errMsg);
28     lua_pushnumber(L, line_num);
29     return 3;
30 }
31 %end
32 
33 %override wxLua_function_GetTrackedWindowInfo
34 // %function LuaTable GetTrackedWindowInfo(bool as_string = false)
35 static int LUACALL wxLua_function_GetTrackedWindowInfo(lua_State *L)
36 {
37     bool as_string = (0 != lua_toboolean(L, 1)); // ok if nil
38     if (as_string)
39         wxlua_pushwxString(L, wxlua_concatwxArrayString(wxluaW_gettrackedwindowinfo(L)));
40     else
41         wxlua_pushwxArrayStringtable(L, wxluaW_gettrackedwindowinfo(L));
42 
43     return 1;
44 }
45 %end
46 
47 %override wxLua_function_GetGCUserdataInfo
48 // %function LuaTable GetGCUserdataInfo(bool as_string = false)
49 static int LUACALL wxLua_function_GetGCUserdataInfo(lua_State *L)
50 {
51     bool as_string = (0 != lua_toboolean(L, 1)); // ok if nil
52     if (as_string)
53         wxlua_pushwxString(L, wxlua_concatwxArrayString(wxluaO_getgcobjectinfo(L)));
54     else
55         wxlua_pushwxArrayStringtable(L, wxluaO_getgcobjectinfo(L));
56 
57     return 1;
58 }
59 %end
60 
61 %override wxLua_function_GetTrackedObjectInfo
62 // %function LuaTable GetTrackedObjectInfo(bool as_string = false)
63 static int LUACALL wxLua_function_GetTrackedObjectInfo(lua_State *L)
64 {
65     bool as_string = (0 != lua_toboolean(L, 1)); // ok if nil
66     if (as_string)
67         wxlua_pushwxString(L, wxlua_concatwxArrayString(wxluaO_gettrackedweakobjectinfo(L)));
68     else
69         wxlua_pushwxArrayStringtable(L, wxluaO_gettrackedweakobjectinfo(L));
70 
71     return 1;
72 }
73 %end
74 
75 %override wxLua_function_GetTrackedEventCallbackInfo
76 // %function LuaTable GetTrackedEventCallbackInfo(bool as_string = false)
77 static int LUACALL wxLua_function_GetTrackedEventCallbackInfo(lua_State *L)
78 {
79     wxLuaState wxlState(L);
80     bool as_string = (0 != lua_toboolean(L, 1)); // ok if nil
81     if (as_string)
82         wxlua_pushwxString(L, wxlua_concatwxArrayString(wxlState.GetTrackedEventCallbackInfo()));
83     else
84         wxlua_pushwxArrayStringtable(L, wxlState.GetTrackedEventCallbackInfo());
85 
86     return 1;
87 }
88 %end
89 
90 %override wxLua_function_GetTrackedWinDestroyCallbackInfo
91 // %function LuaTable GetTrackedWinDestroyCallbackInfo(bool as_string = false)
92 static int LUACALL wxLua_function_GetTrackedWinDestroyCallbackInfo(lua_State *L)
93 {
94     wxLuaState wxlState(L);
95     bool as_string = (0 != lua_toboolean(L, 1)); // ok if nil
96     if (as_string)
97         wxlua_pushwxString(L, wxlua_concatwxArrayString(wxlState.GetTrackedWinDestroyCallbackInfo()));
98     else
99         wxlua_pushwxArrayStringtable(L, wxlState.GetTrackedWinDestroyCallbackInfo());
100 
101     return 1;
102 }
103 %end
104 
105 %override wxLua_function_isgcobject
106 // %function bool isgcobject(void* object)
107 static int LUACALL wxLua_function_isgcobject(lua_State *L)
108 {
109     bool ret = false;
110     if (wxlua_iswxuserdatatype(wxluaT_type(L, 1)))
111     {
112         void* obj_ptr = wxlua_touserdata(L, 1, false);
113         ret = wxluaO_isgcobject(L, obj_ptr);
114     }
115 
116     lua_pushboolean(L, ret);
117     return 1;
118 }
119 %end
120 
121 %override wxLua_function_istrackedobject
122 // %function bool istrackedobject(void* object)
123 static int LUACALL wxLua_function_istrackedobject(lua_State *L)
124 {
125     bool ret = false;
126     int wxl_type = wxluaT_type(L, 1);
127 
128     if (wxlua_iswxuserdatatype(wxl_type))
129     {
130         void* obj_ptr = wxlua_touserdata(L, 1, false);
131         ret = wxluaO_istrackedweakobject(L, obj_ptr, wxl_type, false);
132     }
133 
134     lua_pushboolean(L, ret);
135     return 1;
136 }
137 %end
138 
139 %override wxLua_function_isrefed
140 // %function bool isrefed(void* object)
141 static int LUACALL wxLua_function_isrefed(lua_State *L)
142 {
143     bool ret = wxluaR_isrefed(L, 1, &wxlua_lreg_refs_key) != LUA_NOREF;
144 
145     lua_pushboolean(L, ret);
146     return 1;
147 }
148 %end
149 
150 
151 %override wxLua_function_gcobject
152 // %function bool gcobject(void* object)
153 static int LUACALL wxLua_function_gcobject(lua_State *L)
154 {
155     bool ret = false;
156     if (!wxluaO_isgcobject(L, 1))
157     {
158         FIXME do we need to implement this function for anybody
159         ret = true;
160     }
161 
162     lua_pushboolean(L, ret);
163     return 1;
164 }
165 %end
166 
167 %override wxLua_function_ungcobject
168 // %function bool ungcobject(void* object)
169 static int LUACALL wxLua_function_ungcobject(lua_State *L)
170 {
171     bool ret = false;
172 
173     int l_type = lua_type(L, 1);
174 
175     if (!wxlua_iswxluatype(l_type, WXLUA_TUSERDATA))
176         wxlua_argerror(L, 1, wxT("a 'userdata'"));
177 
178     void* o = wxlua_touserdata(L, 1, false);
179 
180     if (wxluaO_isgcobject(L, o))
181     {
182         ret = wxluaO_undeletegcobject(L, o);
183     }
184 
185     lua_pushboolean(L, ret);
186     return 1;
187 }
188 %end
189 
190 
191 %override wxLua_function_type
192 // %function int type(int wxluaarg_tag)
193 static int LUACALL wxLua_function_type(lua_State *L)
194 {
195     int ltype = lua_type(L, 1);
196     const char* ltypename = lua_typename(L, ltype);
197 
198     int wxl_type = wxluaT_type(L, 1);
199     wxString wxltypeName = wxluaT_typename(L, wxl_type);
200 
201     // push the results
202     lua_pushstring(L, wx2lua(wxltypeName));
203     lua_pushnumber(L, wxl_type);
204 
205     lua_pushstring(L, ltypename);
206     lua_pushnumber(L, ltype);
207 
208     return 4;
209 }
210 %end
211 
212 %override wxLua_function_typename
213 // %function wxString typename(int wxluaarg_tag)
214 static int LUACALL wxLua_function_typename(lua_State *L)
215 {
216     // int wxluaarg_tag
217     int wxl_type = (int)wxlua_getnumbertype(L, 1);
218     // call wxlua_getwxluatypename
219     wxString returns = wxluaT_typename(L, wxl_type);
220     // push the result string
221     wxlua_pushwxString(L, returns);
222 
223     return 1;
224 }
225 %end
226 
227 // ===========================================================================
228 // ===========================================================================
229 
230 %override wxLua_function_GetBindings
231 
232 int LUACALL wxluabind_wxLuaBinding__index(lua_State* L);
233 
234 // %function LuaTable GetBindings()
wxLua_function_GetBindings(lua_State * L)235 static int LUACALL wxLua_function_GetBindings(lua_State *L)
236 {
237     lua_newtable(L); // the table that we return
238 
239     int idx = 1;
240 
241     wxLuaBindingArray& wxlbArray = wxLuaBinding::GetBindingArray();
242     size_t n, count = wxlbArray.GetCount();
243 
244     for (n = 0; n < count; n++, idx++)
245     {
246         // Push function to access the binding info
247         const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
248         *ptr = wxlbArray[n];
249             lua_newtable(L);
250             lua_pushstring(L, "__index");
251             lua_pushlightuserdata(L, wxlbArray[n]);                // push tag to recognize table call
252             lua_pushcclosure(L, wxluabind_wxLuaBinding__index, 1); // push func with tag as upvalue
253             lua_rawset(L, -3);
254 
255             //lua_pushstring(L, "__metatable");
256             //lua_pushstring(L, "Metatable is not accessible");
257             //lua_rawset(L, -3);
258 
259             lua_setmetatable(L, -2);
260 
261         lua_rawseti(L, -2, idx);
262     }
263 
264     return 1;
265 }
266 
267 //-----------------------------------------------------------------------------
268 // wxluabind_wxLuaBindCFunc__index
269 //-----------------------------------------------------------------------------
270 int LUACALL wxluabind_wxLuaBindClass__index(lua_State* L);
271 
wxluabind_wxLuaBindCFunc__index(lua_State * L)272 int LUACALL wxluabind_wxLuaBindCFunc__index(lua_State* L)
273 {
274     static const char* fields[] = { "lua_cfunc",
275                                     "method_type",
276                                     "minargs",
277                                     "maxargs",
278                                     "argtypes",
279                                     "class",
280                                     "class_name" };
281     static const size_t fields_count = sizeof(fields)/sizeof(fields[0]);
282 
283     void **ptr = (void **)lua_touserdata(L, 1);
284     wxLuaBindCFunc* wxlCFunc= (wxLuaBindCFunc*)*ptr;
285     wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1));
286 
287     int idx_type = lua_type(L, 2);
288 
289     if (idx_type == LUA_TSTRING)
290     {
291         const char* idx_str = lua_tostring(L, 2);
292 
293         if (strcmp(idx_str, "fields") == 0)
294         {
295             lua_newtable(L);
296             for (size_t i = 0; i < fields_count; ++i)
297             {
298                 lua_pushstring(L, fields[i]);
299                 lua_rawseti(L, -2, i + 1);
300             }
301             return 1;
302         }
303         else if (strcmp(idx_str, "lua_cfunc") == 0)
304         {
305             lua_pushcfunction(L, wxlCFunc->lua_cfunc);
306             return 1;
307         }
308         else if (strcmp(idx_str, "method_type") == 0)
309         {
310             lua_pushnumber(L, wxlCFunc->method_type);
311             return 1;
312         }
313         else if (strcmp(idx_str, "minargs") == 0)
314         {
315             lua_pushnumber(L, wxlCFunc->minargs);
316             return 1;
317         }
318         else if (strcmp(idx_str, "maxargs") == 0)
319         {
320             lua_pushnumber(L, wxlCFunc->maxargs);
321             return 1;
322         }
323         else if (strcmp(idx_str, "argtypes") == 0)
324         {
325             size_t idx, count = wxlCFunc->maxargs;
326             lua_createtable(L, count, 0);
327 
328             // check for terminating null in argtypes
329             for (idx = 0; (idx < count) && wxlCFunc->argtypes[idx]; ++idx)
330             {
331                 lua_pushnumber(L, *wxlCFunc->argtypes[idx]);
332                 lua_rawseti(L, -2, idx + 1);
333             }
334 
335             return 1;
336         }
337         else if (strcmp(idx_str, "class") == 0)
338         {
339             const wxLuaBindClass* c = wxlBinding->GetBindClass(wxlCFunc);
340             if (c != NULL)
341             {
342                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
343                 *ptr = c;
344                     lua_newtable(L);
345                     lua_pushstring(L, "__index");
346                     lua_pushlightuserdata(L, wxlBinding);
347                     lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
348                     lua_rawset(L, -3);
349                     lua_setmetatable(L, -2);
350 
351                 return 1;
352             }
353         }
354         else if (strcmp(idx_str, "class_name") == 0)
355         {
356             const wxLuaBindClass* c = wxlBinding->GetBindClass(wxlCFunc);
357             if (c != NULL)
358             {
359                 lua_pushstring(L, c->name);
360                 return 1;
361             }
362         }
363     }
364 
365     return 0;
366 }
367 
368 //-----------------------------------------------------------------------------
369 // wxluabind_wxLuaBindMethod__index
370 //-----------------------------------------------------------------------------
371 
wxluabind_wxLuaBindMethod__index(lua_State * L)372 int LUACALL wxluabind_wxLuaBindMethod__index(lua_State* L)
373 {
374     static const char* fields[] = { "name",
375                                     "method_type",
376                                     "wxluacfuncs",
377                                     "wxluacfuncs_n",
378                                     "basemethod",
379                                     "class",
380                                     "class_name" };
381     static const size_t fields_count = sizeof(fields)/sizeof(fields[0]);
382 
383     void **ptr = (void **)lua_touserdata(L, 1);
384     wxLuaBindMethod* wxlMethod = (wxLuaBindMethod*)*ptr;
385     wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1));
386 
387     int idx_type = lua_type(L, 2);
388 
389     if (idx_type == LUA_TSTRING)
390     {
391         const char* idx_str = lua_tostring(L, 2);
392 
393         if (strcmp(idx_str, "fields") == 0)
394         {
395             lua_newtable(L);
396             for (size_t i = 0; i < fields_count; ++i)
397             {
398                 lua_pushstring(L, fields[i]);
399                 lua_rawseti(L, -2, i + 1);
400             }
401             return 1;
402         }
403         else if (strcmp(idx_str, "name") == 0)
404         {
405             lua_pushstring(L, wxlMethod->name);
406             return 1;
407         }
408         else if (strcmp(idx_str, "method_type") == 0)
409         {
410             lua_pushnumber(L, wxlMethod->method_type);
411             return 1;
412         }
413         else if (strcmp(idx_str, "wxluacfuncs") == 0)
414         {
415             wxLuaBindCFunc* wxlCFunc = wxlMethod->wxluacfuncs;
416             size_t idx, count = wxlMethod->wxluacfuncs_n;
417             lua_createtable(L, count, 0);
418 
419             for (idx = 0; idx < count; ++idx, ++wxlCFunc)
420             {
421                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
422                 *ptr = wxlCFunc;
423                     lua_newtable(L);
424                     lua_pushstring(L, "__index");
425                     lua_pushlightuserdata(L, wxlBinding);
426                     lua_pushcclosure(L, wxluabind_wxLuaBindCFunc__index, 1); // push func with tag as upvalue
427                     lua_rawset(L, -3);
428                     lua_setmetatable(L, -2);
429 
430                 lua_rawseti(L, -2, idx + 1);
431             }
432 
433             return 1;
434         }
435         else if (strcmp(idx_str, "wxluacfuncs_n") == 0)
436         {
437             lua_pushnumber(L, wxlMethod->wxluacfuncs_n);
438             return 1;
439         }
440         else if (strcmp(idx_str, "basemethod") == 0)
441         {
442             if (wxlMethod->basemethod)
443             {
444                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
445                 *ptr = wxlMethod->basemethod;
446                     lua_newtable(L);
447                     lua_pushstring(L, "__index");
448                     lua_pushlightuserdata(L, wxlBinding);
449                     lua_pushcclosure(L, wxluabind_wxLuaBindMethod__index, 1); // push func with tag as upvalue
450                     lua_rawset(L, -3);
451                     lua_setmetatable(L, -2);
452 
453                 return 1;
454             }
455 
456             return 0;
457         }
458         else if (strcmp(idx_str, "class") == 0)
459         {
460             const wxLuaBindClass* c = wxlBinding->GetBindClass(wxlMethod);
461             if (c != NULL)
462             {
463                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
464                 *ptr = c;
465                     lua_newtable(L);
466                     lua_pushstring(L, "__index");
467                     lua_pushlightuserdata(L, wxlBinding);
468                     lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
469                     lua_rawset(L, -3);
470                     lua_setmetatable(L, -2);
471 
472                 return 1;
473             }
474         }
475         else if (strcmp(idx_str, "class_name") == 0)
476         {
477             const wxLuaBindClass* c = wxlBinding->GetBindClass(wxlMethod);
478             if (c != NULL)
479             {
480                 lua_pushstring(L, c->name);
481                 return 1;
482             }
483         }
484     }
485 
486     return 0;
487 }
488 
489 //-----------------------------------------------------------------------------
490 // wxluabind_wxLuaBindClass__index
491 //-----------------------------------------------------------------------------
492 
wxluabind_wxLuaBindClass__index(lua_State * L)493 int LUACALL wxluabind_wxLuaBindClass__index(lua_State* L)
494 {
495     static const char* fields[] = { "name",
496                                     "wxluamethods",
497                                     "wxluamethods_n",
498                                     "classInfo",
499                                     "wxluatype",
500                                     "baseclassNames",
501                                     "baseBindClasses",
502                                     "baseclass_wxluatypes",
503                                     "baseclass_vtable_offsets",
504                                     "enums",
505                                     "enums_n" };
506     static const size_t fields_count = sizeof(fields)/sizeof(fields[0]);
507 
508     void **ptr = (void **)lua_touserdata(L, 1);
509     wxLuaBindClass* wxlClass = (wxLuaBindClass*)*ptr;
510     wxLuaBinding *wxlBinding = (wxLuaBinding *)lua_touserdata(L, lua_upvalueindex(1));
511 
512     int idx_type = lua_type(L, 2);
513 
514     if (idx_type == LUA_TSTRING)
515     {
516         const char* idx_str = lua_tostring(L, 2);
517 
518         if (strcmp(idx_str, "fields") == 0)
519         {
520             lua_newtable(L);
521             for (size_t i = 0; i < fields_count; ++i)
522             {
523                 lua_pushstring(L, fields[i]);
524                 lua_rawseti(L, -2, i + 1);
525             }
526             return 1;
527         }
528         else if (strcmp(idx_str, "name") == 0)
529         {
530             lua_pushstring(L, wxlClass->name);
531             return 1;
532         }
533         else if (strcmp(idx_str, "wxluamethods") == 0)
534         {
535             size_t idx, count = wxlClass->wxluamethods_n;
536             lua_createtable(L, count, 0);
537             if (wxlClass->wxluamethods_n > 0)
538             {
539                 wxLuaBindMethod* wxlMethod = wxlClass->wxluamethods;
540 
541                 for (idx = 0; idx < count; ++idx, ++wxlMethod)
542                 {
543                     // Create table { wxLuaBindClass userdata }
544                     const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
545                     *ptr = wxlMethod;
546                         lua_newtable(L);
547                         lua_pushstring(L, "__index");
548                         lua_pushlightuserdata(L, wxlBinding);
549                         lua_pushcclosure(L, wxluabind_wxLuaBindMethod__index, 1); // push func with tag as upvalue
550                         lua_rawset(L, -3);
551                         lua_setmetatable(L, -2);
552 
553                     lua_rawseti(L, -2, idx + 1);
554                 }
555 
556                 lua_pushstring(L, "wxLuaBindClass"); // so we know where this came from
557                 lua_pushvalue(L, 1);
558                 lua_rawset(L, -3);
559             }
560 
561             return 1;
562         }
563         else if (strcmp(idx_str, "wxluamethods_n") == 0)
564         {
565             lua_pushnumber(L, wxlClass->wxluamethods_n);
566             return 1;
567         }
568         else if (strcmp(idx_str, "classInfo") == 0)
569         {
570             if (wxlClass->classInfo)
571             {
572                 const wxLuaBindClass* classInfoClass = wxluaT_getclass(L, "wxClassInfo");
573                 if (classInfoClass)
574                 {
575                     wxluaT_pushuserdatatype(L, wxlClass->classInfo, *classInfoClass->wxluatype);
576                     return 1;
577                 }
578             }
579 
580             return 0;
581         }
582         else if (strcmp(idx_str, "wxluatype") == 0)
583         {
584             lua_pushnumber(L, *wxlClass->wxluatype);
585             return 1;
586         }
587         else if (strcmp(idx_str, "baseclassNames") == 0)
588         {
589             lua_newtable(L);
590             if (wxlClass->baseclassNames)
591             {
592                 for (size_t i = 0; wxlClass->baseclassNames[i]; ++i)
593                 {
594                     lua_pushstring(L, wxlClass->baseclassNames[i]);
595                     lua_rawseti(L, -2, i + 1);
596                 }
597             }
598 
599             return 1;
600         }
601         else if (strcmp(idx_str, "baseBindClasses") == 0)
602         {
603             lua_newtable(L);
604             if (wxlClass->baseBindClasses)
605             {
606                 for (size_t i = 0; wxlClass->baseclassNames[i]; ++i) // use names to check for terminating NULL
607                 {
608                     if (wxlClass->baseBindClasses[i] == NULL) // may be NULL if not loaded
609                     {
610                         lua_pushnil(L);
611                     }
612                     else
613                     {
614                         const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
615                         *ptr = wxlClass->baseBindClasses[i];
616                             lua_newtable(L);
617                             lua_pushstring(L, "__index");
618                             lua_pushlightuserdata(L, wxlBinding);
619                             lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
620                             lua_rawset(L, -3);
621                             lua_setmetatable(L, -2);
622                     }
623 
624                     lua_rawseti(L, -2, i + 1);
625                 }
626             }
627 
628             return 1;
629         }
630         else if (strcmp(idx_str, "baseclass_wxluatypes") == 0)
631         {
632             lua_newtable(L);
633             if (wxlClass->baseclass_wxluatypes)
634             {
635                 size_t i = 0;
636                 while (wxlClass->baseclass_wxluatypes[i])
637                 {
638                     lua_pushnumber(L, *wxlClass->baseclass_wxluatypes[i]);
639                     lua_rawseti(L, -2, i + 1);
640                     ++i;
641                 }
642             }
643 
644             return 1;
645         }
646         else if (strcmp(idx_str, "baseclass_vtable_offsets") == 0)
647         {
648             lua_newtable(L);
649             if (wxlClass->baseclass_wxluatypes) // check this for NULL not baseclass_vtable_offsets
650             {
651                 size_t i = 0;
652                 while (wxlClass->baseclass_wxluatypes[i]) // see above
653                 {
654                     lua_pushnumber(L, wxlClass->baseclass_vtable_offsets[i]);
655                     lua_rawseti(L, -2, i + 1);
656                     ++i;
657                 }
658             }
659 
660             return 1;
661         }
662         else if (strcmp(idx_str, "enums") == 0)
663         {
664             size_t idx, count = wxlClass->enums_n;
665             lua_createtable(L, count, 0);
666             if (wxlClass->enums_n > 0)
667             {
668                 wxLuaBindNumber* wxlNumber = wxlClass->enums;
669 
670                 for (idx = 0; idx < count; ++idx, ++wxlNumber)
671                 {
672                     // Create table { name, value }
673                     lua_createtable(L, 0, 2);
674                     lua_pushstring(L, "name");
675                     lua_pushstring(L, wxlNumber->name);
676                     lua_rawset(L, -3);
677                     lua_pushstring(L, "value");
678                     lua_pushnumber(L, wxlNumber->value);
679                     lua_rawset(L, -3);
680 
681                     lua_rawseti(L, -2, idx + 1);
682                 }
683 
684                 //lua_pushstring(L, "wxLuaBindClass"); // so we know where this came from
685                 //lua_pushvalue(L, 1);
686                 //lua_rawset(L, -3);
687             }
688 
689             return 1;
690         }
691         else if (strcmp(idx_str, "enums_n") == 0)
692         {
693             lua_pushnumber(L, wxlClass->enums_n);
694             return 1;
695         }
696     }
697 
698     return 0;
699 }
700 
701 //-----------------------------------------------------------------------------
702 // wxluabind_wxLuaBinding__index
703 //-----------------------------------------------------------------------------
704 
wxluabind_wxLuaBinding__index(lua_State * L)705 int LUACALL wxluabind_wxLuaBinding__index(lua_State* L)
706 {
707     static const char* fields[] = { "GetBindingName",
708                                     "GetLuaNamespace",
709                                     "GetClassCount",
710                                     "GetFunctionCount",
711                                     "GetNumberCount",
712                                     "GetStringCount",
713                                     "GetEventCount",
714                                     "GetObjectCount",
715                                     "GetClassArray",
716                                     "GetFunctionArray",
717                                     "GetNumberArray",
718                                     "GetStringArray",
719                                     "GetEventArray",
720                                     "GetObjectArray" };
721     static const size_t fields_count = sizeof(fields)/sizeof(fields[0]);
722 
723     void **ptr = (void **)lua_touserdata(L, 1);
724     wxLuaBinding* wxlBinding = (wxLuaBinding*)*ptr;
725 
726     int idx_type = lua_type(L, 2);
727 
728     if (idx_type == LUA_TSTRING)
729     {
730         const char* idx_str = lua_tostring(L, 2);
731 
732         if (strcmp(idx_str, "fields") == 0)
733         {
734             lua_newtable(L);
735             for (size_t i = 0; i < fields_count; ++i)
736             {
737                 lua_pushstring(L, fields[i]);
738                 lua_rawseti(L, -2, i + 1);
739             }
740             return 1;
741         }
742         else if (strcmp(idx_str, "GetBindingName") == 0)
743         {
744             lua_pushstring(L, wx2lua(wxlBinding->GetBindingName()));
745             return 1;
746         }
747         else if (strcmp(idx_str, "GetLuaNamespace") == 0)
748         {
749             lua_pushstring(L, wx2lua(wxlBinding->GetLuaNamespace()));
750             return 1;
751         }
752         else if (strcmp(idx_str, "GetClassCount") == 0)
753         {
754             lua_pushnumber(L, wxlBinding->GetClassCount());
755             return 1;
756         }
757         else if (strcmp(idx_str, "GetFunctionCount") == 0)
758         {
759             lua_pushnumber(L, wxlBinding->GetFunctionCount());
760             return 1;
761         }
762         else if (strcmp(idx_str, "GetNumberCount") == 0)
763         {
764             lua_pushnumber(L, wxlBinding->GetNumberCount());
765             return 1;
766         }
767         else if (strcmp(idx_str, "GetStringCount") == 0)
768         {
769             lua_pushnumber(L, wxlBinding->GetStringCount());
770             return 1;
771         }
772         else if (strcmp(idx_str, "GetEventCount") == 0)
773         {
774             lua_pushnumber(L, wxlBinding->GetEventCount());
775             return 1;
776         }
777         else if (strcmp(idx_str, "GetObjectCount") == 0)
778         {
779             lua_pushnumber(L, wxlBinding->GetObjectCount());
780             return 1;
781         }
782         else if (strcmp(idx_str, "GetClassArray") == 0)
783         {
784             wxLuaBindClass* wxlClass = wxlBinding->GetClassArray();
785             size_t idx, count = wxlBinding->GetClassCount();
786             lua_createtable(L, count, 0);
787 
788             for (idx = 0; idx < count; ++idx, ++wxlClass)
789             {
790                 // Create table { wxLuaBindClass userdata }
791                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
792                 *ptr = wxlClass;
793                     lua_newtable(L);
794                     lua_pushstring(L, "__index");
795                     lua_pushlightuserdata(L, wxlBinding);
796                     lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
797                     lua_rawset(L, -3);
798                     lua_setmetatable(L, -2);
799 
800                 lua_rawseti(L, -2, idx + 1);
801             }
802 
803             return 1;
804         }
805         else if (strcmp(idx_str, "GetFunctionArray") == 0)
806         {
807             wxLuaBindMethod* wxlMethod = wxlBinding->GetFunctionArray();
808             size_t idx, count = wxlBinding->GetFunctionCount();
809             lua_createtable(L, count, 0);
810 
811             for (idx = 0; idx < count; ++idx, ++wxlMethod)
812             {
813                 // Create table { wxLuaBindClass userdata }
814                 const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
815                 *ptr = wxlMethod;
816                     lua_newtable(L);
817                     lua_pushstring(L, "__index");
818                     lua_pushlightuserdata(L, wxlBinding);
819                     lua_pushcclosure(L, wxluabind_wxLuaBindMethod__index, 1); // push func with tag as upvalue
820                     lua_rawset(L, -3);
821                     lua_setmetatable(L, -2);
822 
823                 lua_rawseti(L, -2, idx + 1);
824             }
825 
826             return 1;
827         }
828         else if (strcmp(idx_str, "GetNumberArray") == 0)
829         {
830             wxLuaBindNumber* wxlNumber = wxlBinding->GetNumberArray();
831             size_t idx, count = wxlBinding->GetNumberCount();
832             lua_createtable(L, count, 0);
833 
834             for (idx = 0; idx < count; ++idx, ++wxlNumber)
835             {
836                 // Create table { name, value }
837                 lua_createtable(L, 0, 2);
838                 lua_pushstring(L, "name");
839                 lua_pushstring(L, wxlNumber->name);
840                 lua_rawset(L, -3);
841                 lua_pushstring(L, "value");
842                 lua_pushnumber(L, wxlNumber->value);
843                 lua_rawset(L, -3);
844 
845                 lua_rawseti(L, -2, idx + 1);
846             }
847 
848             return 1;
849         }
850         else if (strcmp(idx_str, "GetStringArray") == 0)
851         {
852             wxLuaBindString* wxlString = wxlBinding->GetStringArray();
853             size_t idx, count = wxlBinding->GetStringCount();
854             lua_createtable(L, count, 0);
855 
856             for (idx = 0; idx < count; ++idx, ++wxlString)
857             {
858                 // Create table { name, value }
859                 lua_createtable(L, 0, 2);
860                 lua_pushstring(L, "name");
861                 lua_pushstring(L, wxlString->name);
862                 lua_rawset(L, -3);
863                 lua_pushstring(L, "value");
864                 if (wxlString->wxchar_string != NULL)
865                     lua_pushstring(L, wx2lua(wxlString->wxchar_string));
866                 else
867                     lua_pushstring(L, wxlString->c_string);
868                 lua_rawset(L, -3);
869 
870                 lua_rawseti(L, -2, idx + 1);
871             }
872 
873             return 1;
874         }
875         else if (strcmp(idx_str, "GetEventArray") == 0)
876         {
877             wxLuaBindEvent* wxlEvent = wxlBinding->GetEventArray();
878             size_t idx, count = wxlBinding->GetEventCount();
879             lua_createtable(L, count, 0);
880 
881             for (idx = 0; idx < count; ++idx, ++wxlEvent)
882             {
883                 // Create table { name, eventType, wxluatype }
884                 lua_createtable(L, 0, 3);
885                 lua_pushstring(L, "name");
886                 lua_pushstring(L, wxlEvent->name);
887                 lua_rawset(L, -3);
888                 lua_pushstring(L, "eventType");
889                 lua_pushnumber(L, *wxlEvent->eventType);
890                 lua_rawset(L, -3);
891                 lua_pushstring(L, "wxluatype");
892                 lua_pushnumber(L, *wxlEvent->wxluatype);
893                 lua_rawset(L, -3);
894 
895                 lua_pushstring(L, "wxLuaBindClass");
896                 const wxLuaBindClass* wxlClass = wxlBinding->GetBindClass(*wxlEvent->wxluatype);
897                 if (wxlClass == NULL)
898                 {
899                     lua_pushnil(L);
900                 }
901                 else
902                 {
903                     const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
904                     *ptr = wxlClass;
905                         lua_newtable(L);
906                         lua_pushstring(L, "__index");
907                         lua_pushlightuserdata(L, wxlBinding);
908                         lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
909                         lua_rawset(L, -3);
910                         lua_setmetatable(L, -2);
911                 }
912                 lua_rawset(L, -3);
913 
914                 lua_rawseti(L, -2, idx + 1);
915             }
916 
917             return 1;
918         }
919         else if (strcmp(idx_str, "GetObjectArray") == 0)
920         {
921             wxLuaBindObject* wxlObject = wxlBinding->GetObjectArray();
922             size_t idx, count = wxlBinding->GetObjectCount();
923             lua_createtable(L, count, 0);
924 
925             for (idx = 0; idx < count; ++idx, ++wxlObject)
926             {
927                 // Create table { name, object, wxluatype }
928                 lua_createtable(L, 0, 3);
929                 lua_pushstring(L, "name");
930                 lua_pushstring(L, wxlObject->name);
931                 lua_rawset(L, -3);
932 
933                 lua_pushstring(L, "object");
934                 if (wxlObject->objPtr != 0)
935                     wxluaT_pushuserdatatype(L, wxlObject->objPtr, *wxlObject->wxluatype, false);
936                 else
937                     wxluaT_pushuserdatatype(L, *wxlObject->pObjPtr, *wxlObject->wxluatype, false);
938                 lua_rawset(L, -3);
939 
940                 lua_pushstring(L, "wxluatype");
941                 lua_pushnumber(L, *wxlObject->wxluatype);
942                 lua_rawset(L, -3);
943 
944                 lua_pushstring(L, "wxLuaBindClass");
945                 const wxLuaBindClass* wxlClass = wxlBinding->GetBindClass(*wxlObject->wxluatype);
946                 if (wxlClass == NULL)
947                 {
948                     lua_pushnil(L);
949                 }
950                 else
951                 {
952                     const void **ptr = (const void **)lua_newuserdata(L, sizeof(void *));
953                     *ptr = wxlClass;
954                         lua_newtable(L);
955                         lua_pushstring(L, "__index");
956                         lua_pushlightuserdata(L, wxlBinding);
957                         lua_pushcclosure(L, wxluabind_wxLuaBindClass__index, 1); // push func with tag as upvalue
958                         lua_rawset(L, -3);
959                         lua_setmetatable(L, -2);
960                 }
961                 lua_rawset(L, -3);
962 
963                 lua_rawseti(L, -2, idx + 1);
964             }
965 
966             return 1;
967         }
968     }
969 
970     return 0;
971 }
972 
973 %end
974 
975 
976 // ===========================================================================
977 // ===========================================================================
978 
979 %override wxLua_wxLuaObject_constructor
980 // wxLuaObject(void *object)
981 static int LUACALL wxLua_wxLuaObject_constructor(lua_State *L)
982 {
983     wxLuaObject *returns;
984     // call constructor
985     returns = new wxLuaObject(L, 1);
986     // add to tracked memory list
987     wxluaO_addgcobject(L, returns, wxluatype_wxLuaObject);
988     // push the constructed class pointer
989     wxluaT_pushuserdatatype(L, returns, wxluatype_wxLuaObject);
990     // return the number of parameters
991     return 1;
992 }
993 %end
994 
995 %override wxLua_wxLuaObject_SetObject
996 // void SetObject(void *object)
997 static int LUACALL wxLua_wxLuaObject_SetObject(lua_State *L)
998 {
999     // get this
1000     wxLuaObject *self = (wxLuaObject *)wxluaT_getuserdatatype(L, 1, wxluatype_wxLuaObject);
1001     // call SetObject
1002     self->SetObject(L, 2);
1003     // return the number of parameters
1004     return 0;
1005 }
1006 %end
1007 
1008 %override wxLua_wxLuaObject_GetObject
1009 // void *GetObject() const
1010 static int LUACALL wxLua_wxLuaObject_GetObject(lua_State *L)
1011 {
1012     // get this
1013     wxLuaObject *self = (wxLuaObject *)wxluaT_getuserdatatype(L, 1, wxluatype_wxLuaObject);
1014     // call GetObject that push the item onto the stack, or nil
1015     if (self->GetObject(L))
1016         return 1;
1017 
1018     return 0;
1019 }
1020 %end
1021