allow jumps to frames >256
[swftools.git] / lib / as3 / abc.c
index 623dc7c..edebba2 100644 (file)
@@ -40,43 +40,30 @@ int abc_RegisterPrivateNameSpace(abc_file_t*file, const char*name);
 /* TODO: switch to a datastructure with just values */
 #define NO_KEY ""
 
-static char* params_tostring(multiname_list_t*list)
+static void params_dump(FILE*fo, multiname_list_t*l, constant_list_t*o)
 {
-    multiname_list_t*l;
-    int n = list_length(list);
-    char**names = (char**)malloc(sizeof(char*)*n);
-    
-    l = list;
-    n = 0;
-    int size = 0;
-    while(l) {
-        names[n] = multiname_tostring(l->multiname);
-        size += strlen(names[n]) + 2;
-        n++;l=l->next;
-    }
+    int n = list_length(l);
+    int no = list_length(o);
+    int i = 0;
 
-    char* params = malloc(size+15);
-    params[0]='(';
-    params[1]=0;
-    l = list;
-    int s=0;
-    n = 0;
+    fprintf(fo, "(");
     while(l) {
-        if(s)
-            strcat(params, ", ");
-        strcat(params, names[n]);
-        free(names[n]);
-        l = l->next;
-        n++;
-        s=1;
+        char*s = multiname_tostring(l->multiname);
+        fprintf(fo, s);
+        free(s);
+        if(i>=n-no) {
+            s = constant_tostring(o->constant);
+            fprintf(fo, " = ");
+            fprintf(fo, s);
+            free(s);
+            o = o->next;
+        }
+
+        if(l->next)
+            fprintf(fo, ", ");
+        l = l->next;i++;
     }
-    free(names);
-    /*char num[20];
-    sprintf(num, "[%d params]", n);
-    strcat(params, num);*/
-    strcat(params, ")");
-    int t;
-    return params;
+    fprintf(fo, ")");
 }
 
 //#define DEBUG
@@ -356,12 +343,13 @@ static void dump_method(FILE*fo, const char*prefix,
         return_type = multiname_tostring(m->return_type);
     else
         return_type = strdup("void");
-    char*paramstr = params_tostring(m->parameters);
-    fprintf(fo, "%s%s%s %s %s=%s %s (%d params, %d optional)\n", prefix, attr, type, return_type, name, m->name, paramstr, 
-            list_length(m->parameters),
-            list_length(m->optional_parameters)
-            );
-    free(paramstr);paramstr=0;
+
+    fprintf(fo, "%s", prefix);
+    fprintf(fo, "%s %s ", attr, type);
+    fprintf(fo, "%s %s=%s", return_type, name, m->name);
+    params_dump(fo, m->parameters, m->optional_parameters);
+    fprintf(fo, "(%d params, %d optional)\n", list_length(m->parameters), list_length(m->optional_parameters));
+
     free(return_type);return_type=0;
 
     abc_method_body_t*c = m->body;
@@ -614,7 +602,7 @@ static void traits_dump(FILE*fo, const char*prefix, trait_list_t*traits, abc_fil
 void* swf_DumpABC(FILE*fo, void*code, char*prefix)
 {
     abc_file_t* file = (abc_file_t*)code;
-        
+
     if(file->name) {
         fprintf(fo, "%s#\n", prefix);
         fprintf(fo, "%s#name: %s\n", prefix, file->name);
@@ -704,7 +692,7 @@ void* swf_DumpABC(FILE*fo, void*code, char*prefix)
                 fprintf(fo, "%s//internal (non-class non-script) methods:\n", prefix);
             }
             char name[18];
-            sprintf(name, "%08x ", m);
+            sprintf(name, "%08x ", m->index);
             dump_method(fo, prefix, "", "internalmethod", name, m, file, methods_seen);
         }
     }
@@ -748,7 +736,7 @@ void* swf_ReadABC(TAG*tag)
        for(s=0;s<param_count;s++) {
            int type_index = swf_GetU30(tag);
             
-            /* type_index might be 0, which probably means "..." (varargs) */
+            /* type_index might be 0 ("*") */
             multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
             list_append(m->parameters, param);
         }
@@ -761,7 +749,9 @@ void* swf_ReadABC(TAG*tag)
 
        m->flags = swf_GetU8(tag);
        
-        DEBUG printf("method %d) %s %s flags=%02x\n", t, m->name, params_tostring(m->parameters), m->flags);
+        DEBUG printf("method %d) %s ", m->name);
+        DEBUG params_dump(stdout, m->parameters, m->optional_parameters);
+        DEBUG printf("flags=%02x\n", t, m->flags);
 
         if(m->flags&0x08) {
             m->optional_parameters = list_new();
@@ -772,6 +762,7 @@ void* swf_ReadABC(TAG*tag)
                 U8 vkind = swf_GetU8(tag); // specifies index type for "val"
                 constant_t*c = constant_fromindex(pool, vindex, vkind);
                 list_append(m->optional_parameters, c);
+
             }
         }
        if(m->flags&0x80) {
@@ -884,7 +875,7 @@ void* swf_ReadABC(TAG*tag)
        array_append(file->method_bodies, NO_KEY, c);
     }
     if(tag->len - tag->pos) {
-       fprintf(stderr, "%d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
+       fprintf(stderr, "ERROR: %d unparsed bytes remaining in ABC block\n", tag->len - tag->pos);
        return 0;
     }
 
@@ -934,10 +925,11 @@ void* swf_ReadABC(TAG*tag)
     return file;
 }
 
-void swf_WriteABC(TAG*abctag, void*code)
+static pool_t*writeABC(TAG*abctag, void*code, pool_t*pool)
 {
     abc_file_t*file = (abc_file_t*)code;
-    pool_t*pool = pool_new();
+    if(!pool) 
+        pool = pool_new();
 
     TAG*tmp = swf_InsertTag(0,0);
     TAG*tag = tmp;
@@ -1060,8 +1052,13 @@ void swf_WriteABC(TAG*abctag, void*code)
             swf_SetU30(tag, list_length(m->optional_parameters));
             constant_list_t*l = m->optional_parameters;
             while(l) {
-                swf_SetU30(tag, constant_get_index(pool, l->constant));
-                swf_SetU8(tag, l->constant->type);
+                int i = constant_get_index(pool, l->constant);
+                swf_SetU30(tag, i);
+                if(!i) {
+                    swf_SetU8(tag, CONSTANT_NULL);
+                } else {
+                    swf_SetU8(tag, l->constant->type);
+                }
                 l = l->next;
             }
         }
@@ -1138,7 +1135,6 @@ void swf_WriteABC(TAG*abctag, void*code)
        //swf_SetU30(tag, c->old.max_scope_depth);
 
        swf_SetU30(tag, c->stats->max_stack);
-
         int param_num = list_length(c->method->parameters)+1;
         if(c->method->flags&METHOD_NEED_REST)
             param_num++;
@@ -1192,11 +1188,22 @@ void swf_WriteABC(TAG*abctag, void*code)
     swf_SetBlock(tag, tmp->data, tmp->len);
 
     swf_DeleteTag(0, tmp);
+    return pool;
+}
+
+void swf_WriteABC(TAG*abctag, void*code)
+{
+    pool_t*pool = writeABC(abctag, code, 0);
+    pool_optimize(pool);
+    swf_ResetTag(abctag, abctag->id);
+    writeABC(abctag, code, pool);
     pool_destroy(pool);
 }
 
 void abc_file_free(abc_file_t*file)
 {
+    if(!file)
+        return;
     int t;
     if(file->metadata) {
         for(t=0;t<file->metadata->num;t++) {
@@ -1416,12 +1423,9 @@ void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
                 ActionTAG*oldaction = swf_ButtonGetAction(tag);
                 if(oldaction && oldaction->op == ACTION__GOTOFRAME) {
                     int framenr = GET16(oldaction->data);
-                    if(framenr>254) {
-                        fprintf(stderr, "Warning: Couldn't translate jump to frame %d to flash 9 actionscript\n", framenr);
-                    }
                     if(!events) {
                         __ findpropstrict(h,"[package]::gotoAndStop");
-                        __ pushbyte(h,framenr+1);
+                        __ pushuint(h,framenr+1);
                         __ callpropvoid(h,"[package]::gotoAndStop", 1);
                     } else {
                         char framename[80];