summaryrefslogtreecommitdiff
path: root/tolua/src/lib/tolua_map.c
blob: c7324061b8eb3dadbccd928ab94b2b4569f9b823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* tolua: functions to map features
 ** Support code for Lua bindings.
 ** Written by Waldemar Celes
 ** TeCGraf/PUC-Rio
 ** Apr 2003
 ** $Id: tolua_map.c,v 1.10 2011/01/13 13:43:46 fabraham Exp $
 */

/* This code is free software; you can redistribute it and/or modify it.
 ** The software provided hereunder is on an "as is" basis, and
 ** the author has no obligation to provide maintenance, support, updates,
 ** enhancements, or modifications.
 */

#include "tolua.h"
#include "tolua_event.h"
#include "lauxlib.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


static char toluaname[128] = "tolua.";
static const char* TOLUANAME (const char* n)  
{
  sprintf(&toluaname[6],"%.120s",n);
  return toluaname;
}

/* Create metatable
 * Create and register new metatable
 */
void tolua_newmetatable (lua_State* L, const char* name)
{
  if (luaL_newmetatable(L,TOLUANAME(name)))
  {
    lua_pushvalue(L,-1);
    lua_pushstring(L,name);
    lua_rawset(L,LUA_REGISTRYINDEX);
  }

  /* set meta events */
  tolua_classevents(L);
  lua_pop(L,1);
}

/* Get metatable
 * Access already created metatable
 */
void tolua_getmetatable (lua_State* L, const char* name)
{
  luaL_getmetatable(L,TOLUANAME(name));
}

/* Map super classes
 * It sets 'name' as being also a 'base', mapping all super classes of 'base' in 'name'
 */
static void mapsuper (lua_State* L, const char* name, const char* base)
{
  /* push registry.super */
  lua_pushstring(L,"tolua_super");
  lua_rawget(L,LUA_REGISTRYINDEX);    /* stack: super */
  tolua_getmetatable(L,name);          /* stack: super mt */
  lua_rawget(L,-2);                   /* stack: super table */
  if (lua_isnil(L,-1))
  {
    /* create table */
    lua_pop(L,1);
    lua_newtable(L);                    /* stack: super table */
    tolua_getmetatable(L,name);          /* stack: super table mt */
    lua_pushvalue(L,-2);                /* stack: super table mt table */
    lua_rawset(L,-4);                   /* stack: super table */
  }

  /* set base as super class */
  lua_pushstring(L,base);
  lua_pushboolean(L,1);
  lua_rawset(L,-3);                    /* stack: super table */

  /* set all super class of base as super class of name */
  tolua_getmetatable(L,base);          /* stack: super table base_mt */
  lua_rawget(L,-3);                   /* stack: super table base_table */
  if (lua_istable(L,-1))
  {
    /* traverse base table */
    lua_pushnil(L);  /* first key */
    while (lua_next(L,-2) != 0)
    {
      /* stack: ... base_table key value */
      lua_pushvalue(L,-2);    /* stack: ... base_table key value key */
      lua_insert(L,-2);       /* stack: ... base_table key key value */
      lua_rawset(L,-5);       /* stack: ... base_table key */
    }
  }
  lua_pop(L,3);                       /* stack: <empty> */
}


/* Map inheritance
 * It sets 'name' as derived from 'base' by setting 'base' as metatable of 'name'
 */
static void mapinheritance (lua_State* L, const char* name, const char* base)
{
  /* set metatable inheritance */
  tolua_getmetatable(L,name);
  if (base && *base)
    tolua_getmetatable(L,base);
  else
    tolua_getmetatable(L,"tolua_commonclass");
  lua_setmetatable(L,-2);
  lua_pop(L,1);
}

/* Object type
 */
static int tolua_bnd_type (lua_State* L)
{
  tolua_typename(L,lua_gettop(L));
  return 1;
}

/* Take ownership
 */
int tolua_bnd_takeownership (lua_State* L)
{
  lua_CFunction func = 0;
  if (lua_isuserdata(L,1))
  {
    if (lua_getmetatable(L,1))        /* if metatable? */
    {
      void* u;
      lua_pushstring(L,".collector");
      lua_rawget(L,-2);
      func = lua_iscfunction(L,-1) ? lua_tocfunction(L,-1) : NULL; 
      lua_pop(L,2);
      u = *((void**)lua_touserdata(L,1));
      tolua_clone(L,u,func);
    }
  }
  lua_pushboolean(L,func!=0);
  return 1;
}

/* Release ownership
 */
static int tolua_bnd_releaseownership (lua_State* L)
{
  int done = 0;
  if (lua_isuserdata(L,1))
  {
    void* u = *((void**)lua_touserdata(L,1));
    lua_pushstring(L,"tolua_gc");
    lua_rawget(L,LUA_REGISTRYINDEX);
    lua_pushlightuserdata(L,u);
    lua_rawget(L,-2);
    lua_pushlightuserdata(L,u);
    lua_pushnil(L);
    lua_rawset(L,-4);
    done = 1;
  }
  lua_pushboolean(L,done!=0);
  return 1;
}

/* Type casting
 */
static int tolua_bnd_cast (lua_State* L)
{
  void* v = tolua_tousertype(L,1,NULL);
  const char* s = tolua_tostring(L,2,NULL);
  if (!v)
    lua_pushnil(L);
  else if (v && s) {
    tolua_getmetatable(L,s);             /* stack: ubox[u] super super[mt] flag mt */
    if (lua_isnil(L,-1)) {
      tolua_error(L,"Unknown 'type' for 'tolua.cast' function",NULL);
    }
    tolua_pushusertype(L,v,s);
  }
  else {
    tolua_error(L,"Invalid arguments for 'tolua.cast' function",NULL);
  }
  return 1;
}

/* Release
** Function to be called by a Lua code that uses a function to explicitly
** release a mapped object. This function is automatically called by all
** destructors bound by tolua and by all collected objects.
*/
static int tolua_bnd_release (lua_State* L)
{
  void* value = tolua_tousertype(L,1,NULL);
  if (value)
    tolua_release(L,value);
  return 1;
}

static void tolua_push_globals_table (lua_State* L)
{
  lua_pushvalue(L,LUA_REGISTRYINDEX); /* registry */
  lua_pushnumber(L,LUA_RIDX_GLOBALS); /* registry globalsindex */
  lua_rawget(L, -2);                  /* registry registry[globalsindex] */
  lua_remove(L, -2);                  /* registry[globalsindex] */
}

TOLUA_API void tolua_open (lua_State* L)
{
  int top = lua_gettop(L);
  lua_pushstring(L,"tolua_opened");
  lua_rawget(L,LUA_REGISTRYINDEX);
  if (!lua_isboolean(L,-1))
  {
    lua_pushstring(L,"tolua_opened"); lua_pushboolean(L,1); lua_rawset(L,LUA_REGISTRYINDEX);
    lua_pushstring(L,"tolua_ubox"); 
    lua_newtable(L); 
    lua_pushvalue(L, -1); /* metatable: for weak table */
    lua_pushstring(L, "__mode");
    lua_pushstring(L, "v");
    lua_rawset(L, -3);
    lua_setmetatable(L, -2);
    lua_rawset(L,LUA_REGISTRYINDEX);
    lua_pushstring(L,"tolua_peer"); 
    lua_newtable(L); 
    lua_pushvalue(L, -1); /* metatable: for weak table */
    lua_pushstring(L, "__mode");
    lua_pushstring(L, "k");
    lua_rawset(L, -3);
    lua_setmetatable(L, -2);
    lua_rawset(L,LUA_REGISTRYINDEX);
    lua_pushstring(L,"tolua_super"); lua_newtable(L); lua_rawset(L,LUA_REGISTRYINDEX);
    lua_pushstring(L,"tolua_gc"); lua_newtable(L); lua_rawset(L,LUA_REGISTRYINDEX);

    tolua_newmetatable(L,"tolua_commonclass");

    tolua_module(L,NULL,0);
    tolua_beginmodule(L,NULL);
    tolua_module(L,"tolua",0);
    tolua_beginmodule(L,"tolua");
    tolua_function(L,"type",tolua_bnd_type);
    tolua_function(L,"takeownership",tolua_bnd_takeownership);
    tolua_function(L,"releaseownership",tolua_bnd_releaseownership);
    tolua_function(L,"cast",tolua_bnd_cast);
    tolua_function(L,"release",tolua_bnd_release);
    tolua_endmodule(L);
    tolua_endmodule(L);
  }
  lua_settop(L,top);
}

/* Copy a C object
 */
TOLUA_API void* tolua_copy (lua_State* L, void* value, unsigned int size)
{
  void* clone = (void*)malloc(size);
  if (clone)
    memcpy(clone,value,size);
  else
    tolua_error(L,"insuficient memory",NULL);
  return clone;
}


/* Release userdata from tolua
 */
TOLUA_API void tolua_release (lua_State* L, void* value)
{
  void** p;
  lua_pushstring(L,"tolua_ubox");
  lua_rawget(L,LUA_REGISTRYINDEX); /* stack: ubox */
  lua_pushlightuserdata(L,value);  /* stack: ubox u */
  /* set userdata pointer to NULL: this pointer might be reused by C/C++ */
  lua_rawget(L,-2);                /* stack: ubox ud */
  p = (void**)lua_touserdata(L,-1);
  if (p) *p = NULL;
      /* fixed bug: thanks to Ulrik Sverdrup -- it was 1 instead of -1 */
  lua_pop(L,1);                    /* stack: ubox */
  /* remove value from ubox */
  lua_pushlightuserdata(L,value);  /* stack: ubox u */
  lua_pushnil(L);
  lua_rawset(L,-3);
  lua_pop(L,1);
}

/* Do clone
 */
TOLUA_API void* tolua_clone (lua_State* L, void* value, lua_CFunction func)
{
  lua_pushstring(L,"tolua_gc");
  lua_rawget(L,LUA_REGISTRYINDEX);
  lua_pushlightuserdata(L,value);
  lua_pushcfunction(L,func);
  lua_rawset(L,-3);
  lua_pop(L,1);
  return value;
}

/* Register a usertype
 * It creates the correspoding metatable in the registry, for both 'type' and 'const type'.
 * It maps 'const type' as being also a 'type'
 */
TOLUA_API void tolua_usertype (lua_State* L, const char* type)
{
  char ctype[128] = "const ";
  strncat(ctype,type,120);

  tolua_newmetatable(L,ctype);         /* create both metatables */
  tolua_newmetatable(L,type);

  mapsuper(L,type,ctype);             /* 'type' is also a 'const type' */
}


/* Begin module
 * It pushes the module (or class) table on the stack
 */
TOLUA_API void tolua_beginmodule (lua_State* L, const char* name)
{
  if (name)
  {
    lua_pushstring(L,name);
    lua_rawget(L,-2);
  }
  else
    tolua_push_globals_table(L);
}

/* End module
 * It pops the module (or class) from the stack
 */
TOLUA_API void tolua_endmodule (lua_State* L)
{
  lua_pop(L,1);
}

/* Map module
 * It creates a new module
 */
#if 1
TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
{
  if (name)
  {
    /* tolua module */
    lua_pushstring(L,name);
    lua_rawget(L,-2);
    if (!lua_istable(L,-1))  /* check if module already exists */
    {
      lua_pop(L,1);
      lua_newtable(L);
      lua_pushstring(L,name);
      lua_pushvalue(L,-2);
      lua_rawset(L,-4);       /* assing module into module */
    }
  }
  else
    tolua_push_globals_table(L);
  if (hasvar)
  {
    if (!tolua_ismodulemetatable(L))  /* check if it already has a module metatable */
    {
      /* create metatable to get/set C/C++ variable */
      lua_newtable(L);
      tolua_moduleevents(L);
      if (lua_getmetatable(L,-2))
        lua_setmetatable(L,-2);  /* set old metatable as metatable of metatable */
      lua_setmetatable(L,-2);
    }
  }
  lua_pop(L,1);               /* pop module */
}
#else
TOLUA_API void tolua_module (lua_State* L, const char* name, int hasvar)
{
  if (name)
  {
    /* tolua module */
    lua_pushstring(L,name);
    lua_newtable(L);
  }
  else
    tolua_push_globals_table(L);
  if (hasvar)
  {
    /* create metatable to get/set C/C++ variable */
    lua_newtable(L);
    tolua_moduleevents(L);
    if (lua_getmetatable(L,-2))
      lua_setmetatable(L,-2);  /* set old metatable as metatable of metatable */
    lua_setmetatable(L,-2);
  }
  if (name)
    lua_rawset(L,-3);       /* assing module into module */
  else
    lua_pop(L,1);           /* pop global table */
}
#endif

/* Map C class
 * It maps a C class, setting the appropriate inheritance and super classes.
 */
TOLUA_API void tolua_cclass (lua_State* L, const char* lname, const char* name, const char* base, lua_CFunction col)
{
  char cname[128] = "const ";
  char cbase[128] = "const ";
  strncat(cname,name,120);
  strncat(cbase,base,120);

  mapinheritance(L,name,base);
  mapinheritance(L,cname,name);

  mapsuper(L,cname,cbase);
  mapsuper(L,name,base);

  lua_pushstring(L,lname);
  tolua_getmetatable(L,name);
  lua_pushstring(L,".collector");
  lua_pushcfunction(L,col);
  lua_rawset(L,-3);              /* store collector function into metatable */
  lua_rawset(L,-3);              /* assign class metatable to module */
}

/* Map function
 * It assigns a function into the current module (or class)
 */
TOLUA_API void tolua_function (lua_State* L, const char* name, lua_CFunction func)
{
  lua_pushstring(L,name);
  lua_pushcfunction(L,func);
  lua_rawset(L,-3);
}

/* Map constant number
 * It assigns a constant number into the current module (or class)
 */
TOLUA_API void tolua_constant (lua_State* L, const char* name, double value)
{
  lua_pushstring(L,name);
  tolua_pushnumber(L,value);
  lua_rawset(L,-3);
}


/* Map variable
 * It assigns a variable into the current module (or class)
 */
TOLUA_API void tolua_variable (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
{
  /* get func */
  lua_pushstring(L,".get");
  lua_rawget(L,-2);
  if (!lua_istable(L,-1))
  {
    /* create .get table, leaving it at the top */
    lua_pop(L,1);
    lua_newtable(L);
    lua_pushstring(L,".get");
    lua_pushvalue(L,-2);
    lua_rawset(L,-4);
  }
  lua_pushstring(L,name);
  lua_pushcfunction(L,get);
  lua_rawset(L,-3);                  /* store variable */
  lua_pop(L,1);                      /* pop .get table */

  /* set func */
  if (set)
  {
    lua_pushstring(L,".set");
    lua_rawget(L,-2);
    if (!lua_istable(L,-1))
    {
      /* create .set table, leaving it at the top */
      lua_pop(L,1);
      lua_newtable(L);
      lua_pushstring(L,".set");
      lua_pushvalue(L,-2);
      lua_rawset(L,-4);
    }
    lua_pushstring(L,name);
    lua_pushcfunction(L,set);
    lua_rawset(L,-3);                  /* store variable */
    lua_pop(L,1);                      /* pop .set table */
  }
}

/* Access const array
 * It reports an error when trying to write into a const array
 */
static int const_array (lua_State* L)
{
  luaL_error(L,"value of const array cannot be changed");
  return 0;
}

/* Map an array
 * It assigns an array into the current module (or class)
 */
TOLUA_API void tolua_array (lua_State* L, const char* name, lua_CFunction get, lua_CFunction set)
{
  lua_pushstring(L,".get");
  lua_rawget(L,-2);
  if (!lua_istable(L,-1))
  {
    /* create .get table, leaving it at the top */
    lua_pop(L,1);
    lua_newtable(L);
    lua_pushstring(L,".get");
    lua_pushvalue(L,-2);
    lua_rawset(L,-4);
  }
  lua_pushstring(L,name);

  lua_newtable(L);           /* create array metatable */
  lua_pushvalue(L,-1);
  lua_setmetatable(L,-2);    /* set the own table as metatable (for modules) */
  lua_pushstring(L,"__index");
  lua_pushcfunction(L,get);
  lua_rawset(L,-3);
  lua_pushstring(L,"__newindex");
  lua_pushcfunction(L,set?set:const_array);
  lua_rawset(L,-3);

  lua_rawset(L,-3);                  /* store variable */
  lua_pop(L,1);                      /* pop .get table */
}