new function dumpclasses()
[swftools.git] / lib / as3 / registry.c
index 06ae5c5..c65bcf5 100644 (file)
@@ -81,7 +81,7 @@ type_t memberinfo_type = {
 // ------------------------- constructors --------------------------------
 
 #define AVERAGE_NUMBER_OF_MEMBERS 8
-classinfo_t* classinfo_register(int access, char*package, char*name, int num_interfaces)
+classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
 {
     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
     c->interfaces[0] = 0;
@@ -93,27 +93,6 @@ classinfo_t* classinfo_register(int access, char*package, char*name, int num_int
     return c;
 }
 
-/* function and class pointers get their own type class */
-static dict_t* functionobjects = 0;
-classinfo_t* registry_getfunctionclass(memberinfo_t*f) {
-    if(!functionobjects) {
-        functionobjects = dict_new2(&ptr_type);
-    } else {
-        classinfo_t*c = dict_lookup(functionobjects, f);
-        if(c)
-            return c;
-    }
-
-    NEW(classinfo_t,c);
-    c->access = ACCESS_PUBLIC;
-    c->package = "";
-    c->name = "Function";
-    dict_init(&c->members,1);
-    dict_put(&c->members, "call", f);
-
-    dict_put(functionobjects, f, c);
-    return c;
-}
 static dict_t* classobjects = 0;
 classinfo_t* registry_getclassclass(classinfo_t*a) {
     if(!classobjects) {
@@ -128,14 +107,8 @@ classinfo_t* registry_getclassclass(classinfo_t*a) {
     c->access = ACCESS_PUBLIC;
     c->package = "";
     c->name = "Class";
-    
-    NEW(memberinfo_t,m);
-    m->kind = MEMBER_SLOT;
-    m->name = "prototype";
-    m->type = a;
-
     dict_init(&c->members,1);
-    dict_put(&c->members, "prototype", m);
+    c->cls = a;
 
     dict_put(classobjects, a, c);
     return c;
@@ -146,9 +119,24 @@ memberinfo_t* memberinfo_register(classinfo_t*cls, const char*name, U8 kind)
     NEW(memberinfo_t,m);
     m->kind = kind;
     m->name = strdup(name);
+    m->parent = cls;
     dict_put(&cls->members, name, m);
     return m;
 }
+memberinfo_t* memberinfo_register_global(U8 access, const char*package, const char*name, U8 kind)
+{
+    NEW(memberinfo_t, m);
+    m->kind = kind;
+    m->flags = FLAG_STATIC;
+    m->name = name;
+    m->parent = 0;
+
+    classinfo_t*c = classinfo_register(access, package, name, 0);
+    c->function = m;
+    c->flags |= FLAG_METHOD;
+    return m;
+}
+
 
 // --------------- builtin classes (from builtin.c) ----------------------
 
@@ -162,6 +150,7 @@ classinfo_t* registry_safefindclass(const char*package, const char*name)
     assert(c);
     return c;
 }
+
 classinfo_t* registry_findclass(const char*package, const char*name)
 {
     assert(classes);
@@ -173,9 +162,46 @@ classinfo_t* registry_findclass(const char*package, const char*name)
         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
     return c;
 }
-memberinfo_t* registry_findmember(classinfo_t*cls, const char*name)
+void registry_dumpclasses()
+{
+    int t;
+    for(t=0;t<classes->hashsize;t++) {
+        dictentry_t*e = classes->slots[t];
+        while(e) {
+            dictentry_t*next = e->next;
+            classinfo_t*i = (classinfo_t*)e->key;
+            printf("%s.%s\n", i->package, i->name);
+            e = e->next;
+        }
+    }
+}
+
+memberinfo_t* registry_findmember(classinfo_t*cls, const char*name, char recursive)
 {
-    return (memberinfo_t*)dict_lookup(&cls->members, name);
+    if(!recursive) {
+        return (memberinfo_t*)dict_lookup(&cls->members, name);
+    }
+    /* look at classes directly extended by this class */
+    memberinfo_t*m = 0;
+    classinfo_t*s = cls;
+    while(s) {
+        m = (memberinfo_t*)dict_lookup(&s->members, name);
+        if(m) return m;
+        s = s->superclass;
+    }
+    /* look at interfaces, and parent interfaces */
+    int t=0;
+    while(cls->interfaces[t]) {
+        classinfo_t*s = cls->interfaces[t];
+        while(s) {
+            m = (memberinfo_t*)dict_lookup(&s->members, name);
+            if(m) return m;
+            s = s->superclass;
+        }
+        t++;
+    }
+    return 0;
+
 }
 void registry_fill_multiname(multiname_t*m, namespace_t*n, classinfo_t*c)
 {
@@ -191,10 +217,47 @@ multiname_t* classinfo_to_multiname(classinfo_t*cls)
     if(!cls)
         return 0;
     multiname_t*m=0;
-    namespace_t*ns = namespace_new(cls->access, cls->package);
-    return multiname_new(ns,cls->name);
+    namespace_t ns = {cls->access, (char*)cls->package};
+    return multiname_new(&ns,cls->name);
 }
 
+// ----------------------- memberinfo methods ------------------------------
+
+/* function and class pointers get their own type class */
+static dict_t* functionobjects = 0;
+classinfo_t* memberinfo_asclass(memberinfo_t*f) {
+    if(!functionobjects) {
+        functionobjects = dict_new2(&ptr_type);
+    } else {
+        classinfo_t*c = dict_lookup(functionobjects, f);
+        if(c)
+            return c;
+    }
+
+    NEW(classinfo_t,c);
+    c->access = ACCESS_PUBLIC;
+    c->package = "";
+    c->name = "Function";
+    
+    dict_init(&c->members,1);
+    c->function = f;
+
+    dict_put(functionobjects, f, c);
+    return c;
+}
+
+classinfo_t* memberinfo_gettype(memberinfo_t*f)
+{
+    if(f) {
+       if(f->kind == MEMBER_METHOD) {
+           return memberinfo_asclass(f);
+       } else {
+           return f->type;
+       }
+    } else {
+       return registry_getanytype();
+    }
+}
 // ----------------------- builtin types ------------------------------
 classinfo_t* registry_getanytype() {return 0;}
 
@@ -217,6 +280,11 @@ classinfo_t* registry_getstringclass() {
     if(!c) c = registry_safefindclass("", "String");
     return c;
 }
+classinfo_t* registry_getarrayclass() {
+    static classinfo_t*c = 0;
+    if(!c) c = registry_safefindclass("", "Array");
+    return c;
+}
 classinfo_t* registry_getintclass() {
     static classinfo_t*c = 0;
     if(!c) c = registry_safefindclass("", "int");
@@ -237,6 +305,11 @@ classinfo_t* registry_getnumberclass() {
     if(!c) c = registry_safefindclass("", "Number");
     return c;
 }
+classinfo_t* registry_getregexpclass() {
+    static classinfo_t*c = 0;
+    if(!c) c = registry_safefindclass("", "RegExp");
+    return c;
+}
 classinfo_t* registry_getMovieClip() {
     static classinfo_t*c = 0;
     if(!c) c = registry_safefindclass("flash.display", "MovieClip");
@@ -251,3 +324,23 @@ classinfo_t* registry_getnullclass() {
     return &nullclass;
 }
 
+// ---------------------------------------------------------------------
+namespace_t flags2namespace(int flags, char*package)
+{
+    namespace_t ns;
+    ns.name = package;
+    if(flags&FLAG_PUBLIC)  {
+        ns.access = ACCESS_PACKAGE;
+    } else if(flags&FLAG_PRIVATE) {
+        ns.access = ACCESS_PRIVATE;
+    } else if(flags&FLAG_PROTECTED) {
+        ns.access = ACCESS_PROTECTED;
+    } else if(flags&FLAG_NAMESPACE_ADOBE) {
+        ns.access = ACCESS_NAMESPACE;
+        assert(!package || !package[0]);
+        ns.name = "http://adobe.com/AS3/2006/builtin";
+    } else {
+        ns.access = ACCESS_PACKAGEINTERNAL;
+    }
+    return ns;
+}