summaryrefslogtreecommitdiff
path: root/apps/plugins
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2009-07-05 15:33:08 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2009-07-05 15:33:08 +0000
commitd755a5ac58b8401255eb3a2e7b00e902bc06ad1d (patch)
treedfcbe89f139dbe54ae1e9e37176c78ab6df68a0e /apps/plugins
parent9b1c774218e59fbcb7160709687686275ee04c13 (diff)
downloadrockbox-d755a5ac58b8401255eb3a2e7b00e902bc06ad1d.tar.gz
rockbox-d755a5ac58b8401255eb3a2e7b00e902bc06ad1d.zip
Lua: add script which wraps not-yet ported C functions to Lua
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@21657 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins')
-rw-r--r--apps/plugins/lua/lua.make12
-rw-r--r--apps/plugins/lua/rocklib.c363
-rwxr-xr-xapps/plugins/lua/rocklib_aux.pl291
3 files changed, 303 insertions, 363 deletions
diff --git a/apps/plugins/lua/lua.make b/apps/plugins/lua/lua.make
index 1232ddf043..7272118bcb 100644
--- a/apps/plugins/lua/lua.make
+++ b/apps/plugins/lua/lua.make
@@ -30,15 +30,21 @@ else
30 ROCKS += $(LUA_BUILDDIR)/lua.rock 30 ROCKS += $(LUA_BUILDDIR)/lua.rock
31endif 31endif
32 32
33$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua 33$(LUA_BUILDDIR)/lua.rock: $(LUA_OBJ) $(LUA_BUILDDIR)/actions.lua $(LUA_BUILDDIR)/buttons.lua $(LUA_BUILDDIR)/rocklib_aux.o
34 34
35$(LUA_BUILDDIR)/actions.lua: $(LUA_OBJ) 35$(LUA_BUILDDIR)/actions.lua:
36 $(call PRINTS,GEN $(@F))$(CC) $(INCLUDES) -E $(APPSDIR)/action.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua 36 $(call PRINTS,GEN $(@F))$(CC) $(INCLUDES) -E $(APPSDIR)/action.h | $(LUA_SRCDIR)/action_helper.pl > $(LUA_BUILDDIR)/actions.lua
37 37
38$(LUA_BUILDDIR)/buttons.lua: $(LUA_OBJ) 38$(LUA_BUILDDIR)/buttons.lua:
39 $(SILENT)$(CC) $(INCLUDES) -dM -E -include button-target.h - < /dev/null | $(LUA_SRCDIR)/button_helper.pl | $(HOSTCC) -fno-builtin $(INCLUDES) -x c -o $(LUA_BUILDDIR)/button_helper - 39 $(SILENT)$(CC) $(INCLUDES) -dM -E -include button-target.h - < /dev/null | $(LUA_SRCDIR)/button_helper.pl | $(HOSTCC) -fno-builtin $(INCLUDES) -x c -o $(LUA_BUILDDIR)/button_helper -
40 $(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/button_helper > $(LUA_BUILDDIR)/buttons.lua 40 $(call PRINTS,GEN $(@F))$(LUA_BUILDDIR)/button_helper > $(LUA_BUILDDIR)/buttons.lua
41 41
42$(LUA_BUILDDIR)/rocklib_aux.c: $(APPSDIR)/plugin.h
43 $(call PRINTS,GEN $(@F))$(CC) $(CFLAGS) $(INCLUDES) -E -include plugin.h - < /dev/null | $(LUA_SRCDIR)/rocklib_aux.pl $(LUA_SRCDIR) > $(LUA_BUILDDIR)/rocklib_aux.c
44
45$(LUA_BUILDDIR)/rocklib_aux.o: $(LUA_BUILDDIR)/rocklib_aux.c
46 $(call PRINTS,CC $(<F))$(CC) $(INCLUDES) -I $(LUA_SRCDIR) $(CFLAGS) -c $< -o $@
47
42$(LUA_BUILDDIR)/lua.refmap: $(LUA_OBJ) 48$(LUA_BUILDDIR)/lua.refmap: $(LUA_OBJ)
43 49
44$(LUA_OUTLDS): $(PLUGIN_LDS) $(LUA_BUILDDIR)/lua.refmap 50$(LUA_OUTLDS): $(PLUGIN_LDS) $(LUA_BUILDDIR)/lua.refmap
diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c
index 64c1a03d64..69e26fdee6 100644
--- a/apps/plugins/lua/rocklib.c
+++ b/apps/plugins/lua/rocklib.c
@@ -258,72 +258,6 @@ RB_WRAP(clear_viewport)
258 return 0; 258 return 0;
259} 259}
260 260
261RB_WRAP(splash)
262{
263 int ticks = luaL_checkint(L, 1);
264 const char *s = luaL_checkstring(L, 2);
265 rb->splash(ticks, s);
266 return 0;
267}
268
269RB_WRAP(lcd_update)
270{
271 (void)L;
272 rb->lcd_update();
273 return 0;
274}
275
276RB_WRAP(lcd_update_rect)
277{
278 int x = luaL_checkint(L, 1);
279 int y = luaL_checkint(L, 2);
280 int width = luaL_checkint(L, 3);
281 int height = luaL_checkint(L, 4);
282 rb->lcd_update_rect(x, y, width, height);
283 return 0;
284}
285
286RB_WRAP(lcd_clear_display)
287{
288 (void)L;
289 rb->lcd_clear_display();
290 return 0;
291}
292
293RB_WRAP(lcd_putsxy)
294{
295 int x = luaL_checkint(L, 1);
296 int y = luaL_checkint(L, 2);
297 const char* string = luaL_checkstring(L, 3);
298 rb->lcd_putsxy(x, y, string);
299 return 0;
300}
301
302RB_WRAP(lcd_puts)
303{
304 int x = luaL_checkint(L, 1);
305 int y = luaL_checkint(L, 2);
306 const char* string = luaL_checkstring(L, 3);
307 rb->lcd_puts(x, y, string);
308 return 0;
309}
310
311RB_WRAP(lcd_puts_scroll)
312{
313 int x = luaL_checkint(L, 1);
314 int y = luaL_checkint(L, 2);
315 const char* string = luaL_checkstring(L, 3);
316 rb->lcd_puts_scroll(x, y, string);
317 return 0;
318}
319
320RB_WRAP(lcd_stop_scroll)
321{
322 (void)L;
323 rb->lcd_stop_scroll();
324 return 0;
325}
326
327#ifdef HAVE_LCD_BITMAP 261#ifdef HAVE_LCD_BITMAP
328RB_WRAP(lcd_framebuffer) 262RB_WRAP(lcd_framebuffer)
329{ 263{
@@ -331,89 +265,6 @@ RB_WRAP(lcd_framebuffer)
331 return 1; 265 return 1;
332} 266}
333 267
334RB_WRAP(lcd_set_drawmode)
335{
336 int drawmode = luaL_checkint(L, 1);
337 rb->lcd_set_drawmode(drawmode);
338 return 0;
339}
340
341RB_WRAP(lcd_get_drawmode)
342{
343 int result = rb->lcd_get_drawmode();
344 lua_pushinteger(L, result);
345 return 1;
346}
347
348RB_WRAP(lcd_setfont)
349{
350 int font = luaL_checkint(L, 1);
351 rb->lcd_setfont(font);
352 return 0;
353}
354
355RB_WRAP(lcd_drawpixel)
356{
357 int x = luaL_checkint(L, 1);
358 int y = luaL_checkint(L, 2);
359
360 rb->lcd_drawpixel(x, y);
361 return 0;
362}
363
364RB_WRAP(lcd_drawline)
365{
366 int x1 = luaL_checkint(L, 1);
367 int y1 = luaL_checkint(L, 2);
368 int x2 = luaL_checkint(L, 3);
369 int y2 = luaL_checkint(L, 4);
370
371 rb->lcd_drawline(x1, y1, x2, y2);
372 return 0;
373}
374
375RB_WRAP(lcd_hline)
376{
377 int x1 = luaL_checkint(L, 1);
378 int x2 = luaL_checkint(L, 2);
379 int y = luaL_checkint(L, 3);
380
381 rb->lcd_hline(x1, x2, y);
382 return 0;
383}
384
385RB_WRAP(lcd_vline)
386{
387 int x = luaL_checkint(L, 1);
388 int y1 = luaL_checkint(L, 2);
389 int y2 = luaL_checkint(L, 3);
390
391 rb->lcd_vline(x, y1, y2);
392 return 0;
393}
394
395RB_WRAP(lcd_drawrect)
396{
397 int x = luaL_checkint(L, 1);
398 int y = luaL_checkint(L, 2);
399 int width = luaL_checkint(L, 3);
400 int height = luaL_checkint(L, 4);
401
402 rb->lcd_drawrect(x, y, width, height);
403 return 0;
404}
405
406RB_WRAP(lcd_fillrect)
407{
408 int x = luaL_checkint(L, 1);
409 int y = luaL_checkint(L, 2);
410 int width = luaL_checkint(L, 3);
411 int height = luaL_checkint(L, 4);
412
413 rb->lcd_fillrect(x, y, width, height);
414 return 0;
415}
416
417RB_WRAP(lcd_mono_bitmap_part) 268RB_WRAP(lcd_mono_bitmap_part)
418{ 269{
419 struct rocklua_image *src = rli_checktype(L, 1); 270 struct rocklua_image *src = rli_checktype(L, 1);
@@ -442,34 +293,6 @@ RB_WRAP(lcd_mono_bitmap)
442} 293}
443 294
444#if LCD_DEPTH > 1 295#if LCD_DEPTH > 1
445RB_WRAP(lcd_set_foreground)
446{
447 unsigned foreground = luaL_checkint(L, 1);
448 rb->lcd_set_foreground(foreground);
449 return 0;
450}
451
452RB_WRAP(lcd_get_foreground)
453{
454 unsigned result = rb->lcd_get_foreground();
455 lua_pushinteger(L, result);
456 return 1;
457}
458
459RB_WRAP(lcd_set_background)
460{
461 unsigned background = luaL_checkint(L, 1);
462 rb->lcd_set_background(background);
463 return 0;
464}
465
466RB_WRAP(lcd_get_background)
467{
468 unsigned result = rb->lcd_get_background();
469 lua_pushinteger(L, result);
470 return 1;
471}
472
473RB_WRAP(lcd_bitmap_part) 296RB_WRAP(lcd_bitmap_part)
474{ 297{
475 struct rocklua_image *src = rli_checktype(L, 1); 298 struct rocklua_image *src = rli_checktype(L, 1);
@@ -541,76 +364,12 @@ RB_WRAP(lcd_bitmap_transparent)
541 364
542#endif /* defined(LCD_BITMAP) */ 365#endif /* defined(LCD_BITMAP) */
543 366
544RB_WRAP(yield)
545{
546 (void)L;
547 rb->yield();
548 return 0;
549}
550
551RB_WRAP(sleep)
552{
553 int ticks = luaL_checkint(L, 1);
554 rb->sleep(ticks);
555 return 0;
556}
557
558RB_WRAP(current_tick) 367RB_WRAP(current_tick)
559{ 368{
560 lua_pushinteger(L, *rb->current_tick); 369 lua_pushinteger(L, *rb->current_tick);
561 return 1; 370 return 1;
562} 371}
563 372
564RB_WRAP(button_get)
565{
566 bool block = luaL_checkboolean(L, 1);
567 long result = rb->button_get(block);
568 lua_pushinteger(L, result);
569 return 1;
570}
571
572RB_WRAP(button_get_w_tmo)
573{
574 int ticks = luaL_checkint(L, 1);
575 long result = rb->button_get_w_tmo(ticks);
576 lua_pushinteger(L, result);
577 return 1;
578}
579
580RB_WRAP(button_status)
581{
582 int result = rb->button_status();
583 lua_pushinteger(L, result);
584 return 1;
585}
586
587#ifdef HAVE_BUTTON_DATA
588RB_WRAP(button_get_data)
589{
590 int result = rb->button_get_data();
591 lua_pushinteger(L, result);
592 return 1;
593}
594#endif
595
596#ifdef HAS_BUTTON_HOLD
597RB_WRAP(button_hold)
598{
599 bool result = rb->button_hold();
600 lua_pushboolean(L, result);
601 return 1;
602}
603#endif
604
605RB_WRAP(get_action)
606{
607 int context = luaL_checkint(L, 1);
608 int timeout = luaL_checkint(L, 2);
609 int result = rb->get_action(context, timeout);
610 lua_pushinteger(L, result);
611 return 1;
612}
613
614#ifdef HAVE_TOUCHSCREEN 373#ifdef HAVE_TOUCHSCREEN
615RB_WRAP(action_get_touchscreen_press) 374RB_WRAP(action_get_touchscreen_press)
616{ 375{
@@ -624,14 +383,6 @@ RB_WRAP(action_get_touchscreen_press)
624} 383}
625#endif 384#endif
626 385
627RB_WRAP(action_userabort)
628{
629 int timeout = luaL_checkint(L, 1);
630 bool result = rb->action_userabort(timeout);
631 lua_pushboolean(L, result);
632 return 1;
633}
634
635RB_WRAP(kbd_input) 386RB_WRAP(kbd_input)
636{ 387{
637 luaL_Buffer b; 388 luaL_Buffer b;
@@ -655,69 +406,6 @@ RB_WRAP(touchscreen_set_mode)
655} 406}
656#endif 407#endif
657 408
658RB_WRAP(backlight_on)
659{
660 (void)L;
661 rb->backlight_on();
662 return 0;
663}
664
665RB_WRAP(backlight_off)
666{
667 (void)L;
668 rb->backlight_off();
669 return 0;
670}
671
672RB_WRAP(backlight_set_timeout)
673{
674 int val = luaL_checkint(L, 1);
675 rb->backlight_set_timeout(val);
676 return 0;
677}
678
679#ifdef HAVE_BACKLIGHT_BRIGHTNESS
680RB_WRAP(backlight_set_brightness)
681{
682 int val = luaL_checkint(L, 1);
683 rb->backlight_set_brightness(val);
684 return 0;
685}
686#endif
687
688RB_WRAP(creat)
689{
690 const char* pathname = luaL_checkstring(L, 1);
691 int result = rb->creat(pathname);
692 lua_pushinteger(L, result);
693 return 1;
694}
695
696RB_WRAP(remove)
697{
698 const char* pathname = luaL_checkstring(L, 1);
699 int result = rb->remove(pathname);
700 lua_pushinteger(L, result);
701 return 1;
702}
703
704RB_WRAP(rename)
705{
706 const char* path = luaL_checkstring(L, 1);
707 const char* newname = luaL_checkstring(L, 2);
708 int result = rb->rename(path, newname);
709 lua_pushinteger(L, result);
710 return 1;
711}
712
713RB_WRAP(file_exists)
714{
715 const char* path = luaL_checkstring(L, 1);
716 bool result = rb->file_exists(path);
717 lua_pushboolean(L, result);
718 return 1;
719}
720
721RB_WRAP(font_getstringsize) 409RB_WRAP(font_getstringsize)
722{ 410{
723 const unsigned char* str = luaL_checkstring(L, 1); 411 const unsigned char* str = luaL_checkstring(L, 1);
@@ -796,32 +484,11 @@ RB_WRAP(current_path)
796static const luaL_Reg rocklib[] = 484static const luaL_Reg rocklib[] =
797{ 485{
798 /* Graphics */ 486 /* Graphics */
799 R(lcd_clear_display),
800 R(lcd_update),
801 R(lcd_update_rect),
802 R(lcd_puts),
803 R(lcd_putsxy),
804 R(lcd_puts_scroll),
805 R(lcd_stop_scroll),
806 R(splash),
807#ifdef HAVE_LCD_BITMAP 487#ifdef HAVE_LCD_BITMAP
808 R(lcd_framebuffer), 488 R(lcd_framebuffer),
809 R(lcd_set_drawmode),
810 R(lcd_get_drawmode),
811 R(lcd_setfont),
812 R(lcd_drawline),
813 R(lcd_drawpixel),
814 R(lcd_hline),
815 R(lcd_vline),
816 R(lcd_drawrect),
817 R(lcd_fillrect),
818 R(lcd_mono_bitmap_part), 489 R(lcd_mono_bitmap_part),
819 R(lcd_mono_bitmap), 490 R(lcd_mono_bitmap),
820#if LCD_DEPTH > 1 491#if LCD_DEPTH > 1
821 R(lcd_set_foreground),
822 R(lcd_get_foreground),
823 R(lcd_set_background),
824 R(lcd_get_background),
825 R(lcd_get_backdrop), 492 R(lcd_get_backdrop),
826 R(lcd_bitmap_part), 493 R(lcd_bitmap_part),
827 R(lcd_bitmap), 494 R(lcd_bitmap),
@@ -836,43 +503,16 @@ static const luaL_Reg rocklib[] =
836 R(lcd_rgbunpack), 503 R(lcd_rgbunpack),
837#endif 504#endif
838 505
839 /* File handling */
840 R(creat),
841 R(remove),
842 R(rename),
843 R(file_exists),
844
845 /* Kernel */ 506 /* Kernel */
846 R(sleep),
847 R(yield),
848 R(current_tick), 507 R(current_tick),
849 508
850 /* Buttons */ 509 /* Buttons */
851 R(button_get),
852 R(button_get_w_tmo),
853 R(button_status),
854#ifdef HAVE_BUTTON_DATA
855 R(button_get_data),
856#endif
857#ifdef HAS_BUTTON_HOLD
858 R(button_hold),
859#endif
860 R(get_action),
861 R(action_userabort),
862#ifdef HAVE_TOUCHSCREEN 510#ifdef HAVE_TOUCHSCREEN
863 R(action_get_touchscreen_press), 511 R(action_get_touchscreen_press),
864 R(touchscreen_set_mode), 512 R(touchscreen_set_mode),
865#endif 513#endif
866 R(kbd_input), 514 R(kbd_input),
867 515
868 /* Hardware */
869 R(backlight_on),
870 R(backlight_off),
871 R(backlight_set_timeout),
872#ifdef HAVE_BACKLIGHT_BRIGHTNESS
873 R(backlight_set_brightness),
874#endif
875
876 R(font_getstringsize), 516 R(font_getstringsize),
877 R(read_bmp_file), 517 R(read_bmp_file),
878 R(set_viewport), 518 R(set_viewport),
@@ -884,6 +524,8 @@ static const luaL_Reg rocklib[] =
884 {NULL, NULL} 524 {NULL, NULL}
885}; 525};
886#undef R 526#undef R
527extern const luaL_Reg rocklib_aux[];
528
887 529
888#define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x); 530#define RB_CONSTANT(x) lua_pushinteger(L, x); lua_setfield(L, -2, #x);
889/* 531/*
@@ -892,6 +534,7 @@ static const luaL_Reg rocklib[] =
892LUALIB_API int luaopen_rock(lua_State *L) 534LUALIB_API int luaopen_rock(lua_State *L)
893{ 535{
894 luaL_register(L, LUA_ROCKLIBNAME, rocklib); 536 luaL_register(L, LUA_ROCKLIBNAME, rocklib);
537 luaL_register(L, LUA_ROCKLIBNAME, rocklib_aux);
895 538
896 RB_CONSTANT(HZ); 539 RB_CONSTANT(HZ);
897 540
diff --git a/apps/plugins/lua/rocklib_aux.pl b/apps/plugins/lua/rocklib_aux.pl
new file mode 100755
index 0000000000..b5de1e4544
--- /dev/null
+++ b/apps/plugins/lua/rocklib_aux.pl
@@ -0,0 +1,291 @@
1#!/usr/bin/env perl
2
3sub trim
4{
5 my $text = $_[0];
6 $text =~ s/^\s+//;
7 $text =~ s/\s+$//;
8 return $text;
9}
10
11sub rand_string
12{
13 my @chars=('a'..'z');
14 my $ret;
15 foreach (1..5)
16 {
17 $ret .= $chars[rand @chars];
18 }
19 return $ret;
20}
21
22
23my @functions;
24my @ported_functions;
25my @forbidden_functions = ('^open$',
26 '^close$',
27 '^read$',
28 '^write$',
29 '^lseek$',
30 '^ftruncate$',
31 '^filesize$',
32 '^fdprintf$',
33 '^read_line$',
34 '^[a-z]+dir$',
35 '^__.+$',
36 '^.+_(un)?cached$',
37 '^audio_play$');
38
39my $rocklib = sprintf("%s/rocklib.c", $ARGV[0]);
40open ROCKLIB, "<$rocklib" or die("Couldn't open $rocklib: $!");
41while(<ROCKLIB>)
42{
43 if(/^RB_WRAP\(([^)]+)\)/)
44 {
45 push(@ported_functions, $1);
46 }
47}
48close ROCKLIB;
49
50# Parse plugin.h
51my $start = 0;
52while(<STDIN>)
53{
54 if(/struct plugin_api \{/)
55 {
56 $start = 1;
57 }
58 elsif($start && /\};/)
59 {
60 $start = 0;
61 }
62
63 if($start == 1)
64 {
65 my $line = $_;
66 while($line !~ /;/)
67 {
68 $line .= <STDIN>;
69 }
70
71 $line =~ s/(\n|\r)//g;
72
73 if($line =~ /([a-zA-Z *_]+)?\s?\(\*([^)]+)?\)\(([^)]+)\).*?;/)
74 {
75 $return_type = $1;
76 $name = $2;
77 $arguments = $3;
78
79 $return_type = trim($return_type);
80 $arguments =~ s/\s{2,}/ /g;
81
82 if( !grep($_ eq $name, @ported_functions) &&
83 !grep($name =~ $_, @forbidden_functions))
84 {
85 push(@functions, {'name' => $name, 'return' => $return_type, 'arg' => $arguments});
86 }
87 }
88 }
89}
90
91my $svnrev = '$Revision$';
92
93# Print the header
94print <<EOF
95/* Automatically generated of $svnrev from rocklib.c & plugin.h */
96
97#define lrocklib_c
98#define LUA_LIB
99
100#define _ROCKCONF_H_ /* We don't need strcmp() etc. wrappers */
101#include "lua.h"
102#include "lauxlib.h"
103#include "plugin.h"
104
105EOF
106;
107
108my %in_types = ('void' => \&in_void,
109 'int' => \&in_int,
110 'unsigned' => \&in_int,
111 'unsignedint' => \&in_int,
112 'signed' => \&in_int,
113 'signedint' => \&in_int,
114 'short' => \&in_int,
115 'unsignedshort' => \&in_int,
116 'signedshort' => \&in_int,
117 'long' => \&in_int,
118 'unsignedlong' => \&in_int,
119 'signedlong' => \&in_int,
120 'char' => \&in_int,
121 'unsignedchar' => \&in_int,
122 'signedchar' => \&in_int,
123 'char*' => \&in_string,
124 'signedchar*' => \&in_string,
125 'unsignedchar*' => \&in_string,
126 'bool' => \&in_bool,
127 '_Bool' => \&in_bool
128), %out_types = ('void' => \&out_void,
129 'int' => \&out_int,
130 'unsigned' => \&out_int,
131 'unsignedint' => \&out_int,
132 'signed' => \&out_int,
133 'signedint' => \&out_int,
134 'short' => \&out_int,
135 'unsignedshort' => \&out_int,
136 'signedshort' => \&out_int,
137 'long' => \&out_int,
138 'unsignedlong' => \&out_int,
139 'signedlong' => \&out_int,
140 'char' => \&out_int,
141 'unsignedchar' => \&out_int,
142 'signedchar' => \&out_int,
143 'char*' => \&out_string,
144 'signedchar*' => \&out_string,
145 'unsignedchar*' => \&out_string,
146 'bool' => \&out_bool,
147 '_Bool' => \&out_bool
148);
149
150sub in_void
151{
152 return "\t(void)L;\n";
153}
154
155sub in_int
156{
157 my ($name, $type, $pos) = @_;
158 return sprintf("\t%s %s = (%s) luaL_checkint(L, %d);\n", $type, $name, $type, $pos);
159}
160
161sub in_string
162{
163 my ($name, $type, $pos) = @_;
164 return sprintf("\t%s %s = (%s) luaL_checkstring(L, %d);\n", $type, $name, $type, $pos)
165}
166
167sub in_bool
168{
169 my ($name, $type, $pos) = @_;
170 return sprintf("\tbool %s = luaL_checkboolean(L, %d);\n", $name, $pos)
171}
172
173sub out_void
174{
175 my $name = $_[0];
176 return sprintf("\t%s;\n\treturn 0;\n", $name);
177}
178
179sub out_int
180{
181 my ($name, $type) = @_;
182 return sprintf("\t%s result = %s;\n\tlua_pushinteger(L, result);\n\treturn 1;\n", $type, $name);
183}
184
185sub out_string
186{
187 my ($name, $type) = @_;
188 return sprintf("\t%s result = %s;\n\tlua_pushstring(L, result);\n\treturn 1;\n", $type, $name);
189}
190
191sub out_bool
192{
193 my ($name, $type) = @_;
194 return sprintf("\tbool result = %s;\n\tlua_pushboolean(L, result);\n\treturn 1;\n", $name);
195}
196
197# Print the functions
198my @valid_functions;
199foreach my $function (@functions)
200{
201 my $valid = 1, @arguments = ();
202 # Check for supported arguments
203 foreach my $argument (split(/,/, @$function{'arg'}))
204 {
205 $argument = trim($argument);
206 if($argument !~ /\[.+\]/ && ($argument =~ /^(.+[\s*])([^[*\s]*)/
207 || $argument eq "void"))
208 {
209 my $literal_type, $type, $name;
210 if($argument eq "void")
211 {
212 $literal_type = "void", $type = "void", $name = "";
213 }
214 else
215 {
216 $literal_type = trim($1), $name = trim($2), $type = trim($1);
217 $type =~ s/(\s|const)//g;
218
219 if($name eq "")
220 {
221 $name = rand_string();
222 }
223 }
224
225 #printf "/* %s: %s|%s */\n", @$function{'name'}, $type, $name;
226 if(!defined $in_types{$type})
227 {
228 $valid = 0;
229 break;
230 }
231
232 push(@arguments, {'name' => $name,
233 'type' => $type,
234 'literal_type' => $literal_type
235 });
236 }
237 else
238 {
239 $valid = 0;
240 break;
241 }
242 }
243
244 # Check for supported return value
245 my $return = @$function{'return'};
246 $return =~ s/(\s|const)//g;
247 #printf "/* %s: %s [%d] */\n", @$function{'name'}, $return, $valid;
248 if(!defined $out_types{$return})
249 {
250 $valid = 0;
251 }
252
253 if($valid == 1)
254 {
255 # Print the header
256 printf "static int rock_%s(lua_State *L)\n".
257 "{\n",
258 @$function{'name'};
259
260 # Print the arguments
261 my $i = 1;
262 foreach my $argument (@arguments)
263 {
264 print $in_types{@$argument{'type'}}->(@$argument{'name'}, @$argument{'literal_type'}, $i++);
265 }
266
267 # Generate the arguments string
268 my $func_args = $arguments[0]{'name'};
269 for(my $i = 1; $i < $#arguments + 1; $i++)
270 {
271 $func_args .= ", ".$arguments[$i]{'name'};
272 }
273
274 # Print the function call
275 my $func = sprintf("rb->%s(%s)", @$function{'name'}, $func_args);
276
277 # Print the footer
278 print $out_types{$return}->($func, @$function{'return'});
279 print "}\n\n";
280
281 push(@valid_functions, $function);
282 }
283}
284
285# Print the C array
286print "const luaL_Reg rocklib_aux[] =\n{\n";
287foreach my $function (@valid_functions)
288{
289 printf "\t{\"%s\", rock_%s},\n", @$function{'name'}, @$function{'name'};
290}
291print "\t{NULL, NULL}\n};\n\n";