1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* initcode.c
Routines for handling/compiling Flash2 AVM2 ABC Actionscript
Extension module for the rfxswf library.
Part of the swftools package.
Copyright (c) 2008,2009 Matthias Kramm <kramm@quiss.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <assert.h>
#include "../q.h"
#include "abc.h"
#include "code.h"
#include "common.h"
#include "registry.h"
#include "initcode.h"
int compare_parsedclass(const void *_v1, const void *_v2)
{
parsedclass_t*p1 = *(parsedclass_t**)_v1;
parsedclass_t*p2 = *(parsedclass_t**)_v2;
if((p1->cls->flags^p2->cls->flags)&FLAG_INTERFACE) {
return (int)(p2->cls->flags&FLAG_INTERFACE) - (int)(p1->cls->flags&FLAG_INTERFACE);
}
classinfo_t*c2 = dict_lookup(&p1->parents, p2);
classinfo_t*c1 = dict_lookup(&p2->parents, p1);
assert(!c1 || !c2); // otherwise we would have a loop
assert(!c1 || c1==p1->cls);
assert(!c2 || c2==p2->cls);
if(c1) {
return -1;
}
if(c2) {
return 1;
}
c2 = dict_lookup(&p1->usedclasses_deep, p2);
c1 = dict_lookup(&p2->usedclasses_deep, p1);
assert(!c1 || !c2);
assert(!c1 || c1==p1->cls);
assert(!c2 || c2==p2->cls);
if(c1) {
return -1;
}
if(c2) {
return 1;
}
return 0;
}
static void add_parent(parsedclass_t*p, classinfo_t*c, dict_t*s2p, char soft)
{
dict_t*parents = soft?(&p->usedclasses_deep):(&p->parents);
int t;
if(dict_contains(parents, p)) {
if(soft) {
as3_warning("circular reference: class %s references self (through static code)", p->cls->name);
return;
} else {
syntaxerror("circular reference: class %s references self", p->cls->name);
}
}
if(c) {
parsedclass_t*n = dict_lookup(s2p, c);
if(n && !dict_contains(parents, n)) {
assert(n->cls == c);
dict_put(parents, n, c);
}
} else {
c = p->cls;
}
if(soft && dict_contains(s2p, c)) {
parsedclass_t*pp = dict_lookup(s2p, c);
DICT_ITERATE_KEY(&pp->usedclasses, classinfo_t*, cc) {
add_parent(p, cc, s2p, soft);
}
}
if(c->superclass) {
add_parent(p, c->superclass, s2p, soft);
}
for(t=0;c->interfaces[t];t++) {
add_parent(p, c->interfaces[t], s2p, soft);
}
}
parsedclass_t* parsedclass_new(classinfo_t*cls, abc_class_t*abc)
{
NEW(parsedclass_t,p);
p->cls = cls;
p->abc = abc;
dict_init2(&p->parents, &ptr_type, 1);
dict_init2(&p->usedclasses, &ptr_type, 1);
dict_init2(&p->usedclasses_deep, &ptr_type, 1);
return p;
}
/* sort classes so that
(a) interfaces appear before classes
(b) base classes always appear before their subclasses
(c) classes appear after the classes they use in static code
*/
parsedclass_t** initcode_sort_classlist(parsedclass_list_t*classes)
{
dict_t* s2p = dict_new2(&ptr_type);
/* create hash tables */
int count = 0;
parsedclass_list_t*l;
for(l=classes;l;l=l->next) {
dict_put(s2p, l->parsedclass->cls, l->parsedclass);
count++;
}
for(l=classes;l;l=l->next) {
add_parent(l->parsedclass, 0, s2p, 0);
DICT_ITERATE_KEY(&l->parsedclass->usedclasses, classinfo_t*, c) {
add_parent(l->parsedclass, c, s2p, 1);
}
}
parsedclass_t**list = malloc(sizeof(parsedclass_t*)*count);
/* build an array for each class */
int i = 0;
for(l=classes;l;l=l->next) {
list[i++] = l->parsedclass;
}
/* sort and flatten.
We unfortunately need to do insertion sort O(n^2) as
our dependencies are only partially ordered */
int j;
for(i=0;i<count;i++) {
for(j=i+1;j<count;j++) {
int r = compare_parsedclass(list+i,list+j);
if(r>0) {
parsedclass_t*p1 = list[i];
parsedclass_t*p2 = list[j];
list[i] = p2;
list[j] = p1;
}
}
}
parsedclass_t**list2 = malloc(sizeof(parsedclass_t*)*(count+1));
for(i=0;i<count;i++) {
list2[i] = (parsedclass_t*)list[i];
#ifdef DEBUG
parsedclass_t*p = list[i];
printf("%s\n", p->cls->name);
if(p->cls->superclass)
printf(" extends %s\n", p->cls->superclass->name);
int t;
for(t=0;p->cls->interfaces[t];t++)
printf(" interface %s\n", p->cls->interfaces[t]->name);
DICT_ITERATE_KEY(&p->usedclasses, classinfo_t*, c) {
printf(" uses %s\n", c->name);
}
DICT_ITERATE_KEY(&p->parents, parsedclass_t*, pp) {
printf(" depends on (deep) %s\n", pp->cls->name);
}
DICT_ITERATE_KEY(&p->usedclasses_deep, parsedclass_t*, px) {
printf(" uses (deep) %s\n", px->cls->name);
}
printf("\n");
#endif
}
list2[count]=0;
free(list);
dict_destroy(s2p);
return list2;
}
void parsedclass_add_dependency(parsedclass_t*p, classinfo_t*c)
{
if(!dict_contains(&p->usedclasses, c)) {
dict_put(&p->usedclasses, c, c);
}
}
void initcode_add_classlist(abc_script_t*init, parsedclass_list_t*_classes)
{
code_t*c = 0;
c = abc_getlocal_0(c);
c = abc_pushscope(c);
parsedclass_t**classes = initcode_sort_classlist(_classes);
int t;
for(t=0;classes[t];t++) {
abc_class_t*abc = classes[t]->abc;
classinfo_t*cls = classes[t]->cls;
array_append(init->file->classes, "", abc);
/* write the construction code for this class to the global init
function */
MULTINAME(classname2,cls);
trait_t*trait = abc_initscript_addClassTrait(init, &classname2, abc);
c = abc_getglobalscope(c);
classinfo_t*s = cls->superclass;
int count=0;
while(s) {
//TODO: take a look at the current scope stack, maybe
// we can re-use something
s = s->superclass;
if(!s)
break;
multiname_t*s2 = sig2mname(s);
c = abc_getlex2(c, s2);
multiname_destroy(s2);
c = abc_pushscope(c); count++;
c = c->prev->prev; // invert
}
/* continue appending after last op end */
while(c && c->next) c = c->next;
multiname_t*extends2 = sig2mname(cls->superclass);
/* TODO: if this is one of *our* classes, we can also
do a getglobalscope/getslot <nr> (which references
the init function's slots) */
if(extends2) {
c = abc_getlex2(c, extends2);
c = abc_dup(c);
/* notice: we get a Verify Error #1107 if the top elemnt on the scope
stack is not the superclass */
c = abc_pushscope(c);count++;
} else {
c = abc_pushnull(c);
/* notice: we get a verify error #1107 if the top element on the scope
stack is not the global object */
c = abc_getlocal_0(c);
c = abc_pushscope(c);count++;
}
c = abc_newclass(c,abc);
while(count--) {
c = abc_popscope(c);
}
c = abc_setslot(c, trait->slot_id);
multiname_destroy(extends2);
}
c = abc_returnvoid(c);
free(classes);
init->method->body->code = c;
}
|