added exception struct
authorkramm <kramm>
Mon, 24 Nov 2008 16:11:57 +0000 (16:11 +0000)
committerkramm <kramm>
Mon, 24 Nov 2008 16:11:57 +0000 (16:11 +0000)
lib/as3/abc.c
lib/as3/abc.h

index abdc258..d545a98 100644 (file)
@@ -322,14 +322,14 @@ static void dump_method(FILE*fo, const char*prefix, const char*type, const char*
         return;
     }
     
-    fprintf(fo, "%s[%d %d %d %d %d]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, c->exception_count);
+    fprintf(fo, "%s[%d %d %d %d %d]\n", prefix, c->max_stack, c->local_count, c->init_scope_depth, c->max_scope_depth, list_length(c->exceptions));
 
     char prefix2[80];
     sprintf(prefix2, "%s    ", prefix);
     if(c->traits)
         dump_traits(fo, prefix, c->traits, file);
     fprintf(fo, "%s{\n", prefix);
-    code_dump(c->code, file, prefix2, fo);
+    code_dump(c->code, c->exceptions, file, prefix2, fo);
     fprintf(fo, "%s}\n\n", prefix);
 }
 
@@ -634,8 +634,9 @@ void* swf_ReadABC(TAG*tag)
        int s;
        for(s=0;s<param_count;s++) {
            int type_index = swf_GetU30(tag);
-            multiname_t*param = multiname_clone(pool_lookup_multiname(pool, type_index));
+            
             /* type_index might be 0, which probably means "..." (varargs) */
+            multiname_t*param = type_index?multiname_clone(pool_lookup_multiname(pool, type_index)):0;
             list_append(m->parameters, param);
         }
 
@@ -735,18 +736,27 @@ void* swf_ReadABC(TAG*tag)
        m->body = c;
 
         int pos = tag->pos + code_length;
-        c->code = code_parse(tag, code_length, file, pool);
+        codelookup_t*codelookup = 0;
+        c->code = code_parse(tag, code_length, file, pool, &codelookup);
         tag->pos = pos;
 
        int exception_count = swf_GetU30(tag);
         int s;
+        c->exceptions = list_new();
         for(s=0;s<exception_count;s++) {
-            swf_GetU30(tag); //from
-            swf_GetU30(tag); //to
-            swf_GetU30(tag); //target
-            swf_GetU30(tag); //exc_type
-            swf_GetU30(tag); //var_name
+            exception_t*e = malloc(sizeof(exception_t));
+
+            e->from = code_atposition(codelookup, swf_GetU30(tag));
+            e->to = code_atposition(codelookup, swf_GetU30(tag));
+            e->target = code_atposition(codelookup, swf_GetU30(tag));
+
+            e->exc_type = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
+            e->var_name = multiname_clone(pool_lookup_multiname(pool, swf_GetU30(tag)));
+            //e->var_name = pool_lookup_string(pool, swf_GetU30(tag));
+            //if(e->var_name) e->var_name = strdup(e->var_name);
+            list_append(c->exceptions, e);
         }
+        codelookup_free(codelookup);
        c->traits = traits_parse(tag, pool, file);
 
        DEBUG printf("method_body %d) (method %d), %d bytes of code", t, methodnr, code_length);
@@ -948,7 +958,18 @@ void swf_WriteABC(TAG*abctag, void*code)
 
         code_write(tag, c->code, pool, file);
 
-       swf_SetU30(tag, c->exception_count);
+       swf_SetU30(tag, list_length(c->exceptions));
+        exception_list_t*l = c->exceptions;
+        while(l) {
+            // warning: assumes "pos" in each code_t is up-to-date
+            swf_SetU30(tag, l->exception->from->pos);
+            swf_SetU30(tag, l->exception->to->pos);
+            swf_SetU30(tag, l->exception->target->pos);
+            swf_SetU30(tag, pool_register_multiname(pool, l->exception->exc_type));
+            swf_SetU30(tag, pool_register_multiname(pool, l->exception->var_name));
+            l = l->next;
+        }
+
         traits_write(pool, tag, c->traits);
     }
 
@@ -972,9 +993,8 @@ void swf_WriteABC(TAG*abctag, void*code)
     pool_destroy(pool);
 }
 
-void swf_FreeABC(void*code)
+void abc_file_free(abc_file_t*file)
 {
-    abc_file_t*file= (abc_file_t*)code;
 
     int t;
     for(t=0;t<file->metadata->num;t++) {
@@ -1055,6 +1075,12 @@ void swf_FreeABC(void*code)
     free(file);
 }
 
+void swf_FreeABC(void*code)
+{
+    abc_file_t*file= (abc_file_t*)code;
+    abc_file_free(file);
+}
+
 void swf_AddButtonLinks(SWF*swf, char stop_each_frame, char events)
 {
     int num_frames = 0;
index a298973..cfa48b0 100644 (file)
@@ -32,6 +32,8 @@ DECLARE(abc_method);
 DECLARE(abc_method_body);
 DECLARE(abc_interface);
 DECLARE(abc_class);
+DECLARE(exception);
+DECLARE_LIST(exception);
 
 #include "code.h"
 #include "opcodes.h"
@@ -121,26 +123,32 @@ abc_method_body_t* abc_class_staticconstructor(abc_class_t*cls, char*returntype,
 abc_method_body_t* abc_class_constructor(abc_class_t*cls, char*returntype, int num_params, ...);
 abc_method_body_t* abc_class_method(abc_class_t*cls, char*returntype, char*name, int num_params, ...);
 
+struct _exception {
+    code_t*from;
+    code_t*to;
+    code_t*target;
+    multiname_t*exc_type;
+    multiname_t*var_name;
+};
+
 struct _abc_method_body {
     abc_file_t*pool;
     //abc_class_t*cls;
     abc_method_t*method;
-    abc_code_t*code;
+    code_t*code;
 
     int max_stack;
     int local_count;
     int init_scope_depth;
     int max_scope_depth;
 
-    int exception_count;
+    exception_list_t* exceptions;
+
     trait_list_t*traits;
     
     int index; // filled in during writing
 };
 
-typedef struct _abc_label {
-} abc_label_t;
-
 typedef struct _abc_script {
     abc_method_t*method;
     abc_file_t*pool;