make Alias be a module as well.
[apps/madmutt.git] / lib-lua / luapkg2c.pl
index ac46db6..b7d1ba3 100755 (executable)
@@ -323,14 +323,74 @@ extern struct luaM_$pkg->{name}_t $pkg->{cname};
 EOF
 }
 
+sub dump_struct_static($) {
+    my ($pkg) = @_;
+
+    print "static struct {\n";
+    foreach (@{$pkg->{props}}) {
+        my $p = $pkg->{members}{$_};
+        my %t = find_type($p, $p->{type});
+        if ($t{const}) {
+            printf "    const $t{type}->{ctypefmt};\n", $p->{name};
+        } else {
+            printf "    $t{type}->{ctypefmt};\n", $p->{name};
+        }
+    }
+    put_line($pkg, 0);
+    print "} $pkg->{cname} = {\n";
+
+    foreach (@{$pkg->{props}}) {
+        my $p = $pkg->{members}{$_};
+        my %t = find_type($p, $p->{type});
+
+        if ($t{const}) {
+            put_line($p, 0);
+            print "    $p->{init},\n";
+        } else {
+            print "    0,\n"    if ($t{type}->{kind} eq 'b');
+            print "    0,\n"    if ($t{type}->{kind} eq 'i');
+            print "    NULL,\n" if ($t{type}->{kind} eq 's');
+        }
+    }
+
+    print <<EOF;
+};
+
+static void $pkg->{cname}_init(void)
+{
+EOF
+
+    foreach (@{$pkg->{props}}) {
+        my $p   = $pkg->{members}{$_};
+        my %t   = find_type($p, $p->{type});
+        my $var = $pkg->{cname}.".".$p->{name};
+
+        next if $t{const};
+
+        put_line($p, 0);
+        if ($t{type}->{dtor}) {
+            my $dtor = $t{type}->{dtor};
+            $dtor =~ s/\$L/L/;
+            $dtor =~ s/\$\$/\&$var/;
+            print "    $dtor;\n";
+        }
+        print "    $var = $p->{init};\n"
+    }
+    print "};\n";
+};
+
 #}}}
 
 #{{{ dump package
 
-sub dump_package_full($) {
-    my $pkg = shift;
+sub dump_package_full($$) {
+    my ($pkg, $static) = @_;
 
-    dump_struct_full($pkg);
+    if ($static) {
+        dump_struct_static($pkg);
+    } else {
+        dump_struct_full($pkg);
+    }
     map { print "\n"; dump_fun($pkg->{name}, $pkg->{members}{$_}); } @{$pkg->{meths}};
     print <<EOF;
 
@@ -496,8 +556,11 @@ sub do_c($) {
         if (/^\s*\@type\s+([a-zA-Z]\w*)\s*=\s*{\s*$/) {
             parse_type(\%src, $1);
             $resync = 1;
+        } elsif (/^\s*static\s+\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
+            dump_package_full(parse_package(\%src, $1), 1);
+            $resync = 1;
         } elsif (/^\s*\@package\s+([a-zA-Z]\w*)\s+{\s*$/) {
-            dump_package_full(parse_package(\%src, $1));
+            dump_package_full(parse_package(\%src, $1), 0);
             $resync = 1;
         } elsif (/^\s*(\@\w*)/) {
             fatal(\%src, "syntax error: unknown directive `$1'");