lua bindings: add ctor/dtor.
[apps/madmutt.git] / lib-lua / luapkg2c.pl
index 72d84c7..9ab8789 100755 (executable)
@@ -83,7 +83,7 @@ sub parse_type($$) {
 
     while (stream_getline($src)) {
         last if (/^\s*\}\s*;\s*/);
-        if (/^\s*\.(push|kind|ctype|check)\s*=\s*(.*?)\s*;\s*$/) {
+        if (/^\s*\.(push|kind|ctype|dtor|ctor|check)\s*=\s*(.*?)\s*;\s*$/) {
             $t{$1} = $2;
             if ($1 eq "kind") {
                 if ($2 =~ /^'([bis])'$/) {
@@ -97,8 +97,13 @@ sub parse_type($$) {
         }
     }
 
-    for my $i ('kind', 'ctype') {
-        fatal($src, "incomplete type $type: missing .$i") unless defined $t{$i};
+    for ('kind', 'ctype') {
+        fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_};
+    }
+    if ($t{kind} eq 's') {
+        for ('dtor', 'ctor') {
+            fatal($src, "incomplete type $type: missing .$_") unless defined $t{$_};
+        }
     }
     unless (defined $t{check}) {
         if ($t{kind} eq 'b') {
@@ -286,9 +291,12 @@ EOF
         next if $t{const};
 
         put_line($p, 0);
-        print "        $var = $p->{init};\n"               if ($t{type}->{kind} eq 'b');
-        print "        $var = $p->{init};\n"               if ($t{type}->{kind} eq 'i');
-        print "        m_strreplace(&$var, $p->{init});\n" if ($t{type}->{kind} eq 's');
+        if ($t{type}->{dtor}) {
+            my $dtor = $t{type}->{dtor};
+            $dtor =~ s/\$\$/\&$var/;
+            print "    $dtor;\n";
+        }
+        print "    $var = $p->{init};\n"
     }
     print "};\n";
 };
@@ -300,7 +308,11 @@ sub dump_struct_short($) {
     foreach (@{$pkg->{props}}) {
         my $p = $pkg->{members}{$_};
         my %t = find_type($p, $p->{type});
-        printf "    $t{type}->{ctypefmt};\n", $p->{name};
+        if ($t{const}) {
+            printf "    const $t{type}->{ctypefmt};\n", $p->{name};
+        } else {
+            printf "    $t{type}->{ctypefmt};\n", $p->{name};
+        }
     }
     put_line($pkg, 0);
     print <<EOF;
@@ -394,11 +406,18 @@ EOF
 
         put_line($p, 0);
         print  "      case LTK_$tok: \n";
-        print "        $var = $check"               if ($t{type}->{kind} eq 'b');
-        print "        $var = $check"               if ($t{type}->{kind} eq 'i');
-        print "        m_strreplace(&$var, $check)" if ($t{type}->{kind} eq 's');
-
-        print  ";\n        return 1;\n";
+        if ($t{type}->{dtor}) {
+            my $dtor = $t{type}->{dtor};
+            $dtor =~ s/\$\$/\&$var/;
+            print "        $dtor;\n";
+        }
+        if ($t{type}->{ctor}) {
+            my $ctor = $t{type}->{ctor};
+            $ctor =~ s/\$\$/$check/;
+            $check = $ctor;
+        }
+        print "        $var = $check;\n";
+        print "        return 1;\n";
     }
 
 print <<EOF;