forked from Jacopo/lightspark
/
asobjects.h
752 lines (709 loc) · 18 KB
/
asobjects.h
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/**************************************************************************
Lightspark, a free flash player implementation
Copyright (C) 2009,2010 Alessandro Pignotti (a.pignotti@sssup.it)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#ifndef ASOBJECTS_H
#define ASOBJECTS_H
#include <vector>
#include <list>
#include <set>
#include "swftypes.h"
#include "frame.h"
#include "input.h"
#include "compat.h"
#include "exceptions.h"
#include "threading.h"
namespace lightspark
{
class SystemState;
};
extern TLSDATA lightspark::SystemState* sys;
namespace lightspark
{
const tiny_string AS3="http://adobe.com/AS3/2006/builtin";
class Event;
class method_info;
struct call_context;
class InterfaceClass: public ASObject
{
protected:
static void lookupAndLink(ASObject* o, const tiny_string& name, const tiny_string& interfaceNs);
};
class DLL_PUBLIC Class_base: public ASObject
{
friend class ABCVm;
friend class ABCContext;
private:
mutable std::vector<multiname> interfaces;
mutable std::vector<Class_base*> interfaces_added;
bool use_protected;
tiny_string protected_ns;
int _maxlevel()
{
return max_level;
}
void recursiveBuild(ASObject* target);
IFunction* constructor;
//Naive garbage collection until reference cycles are detected
Mutex referencedObjectsMutex;
std::set<ASObject*> referencedObjects;
public:
Class_base* super;
//We need to know what is the context we are referring to
ABCContext* context;
tiny_string class_name;
int class_index;
int max_level;
void handleConstruction(ASObject* target, ASObject* const* args, unsigned int argslen, bool buildAndLink);
void setConstructor(IFunction* c);
Class_base(const tiny_string& name):use_protected(false),constructor(NULL),referencedObjectsMutex("referencedObjects"),super(NULL),
context(NULL),class_name(name),class_index(-1),max_level(0) {type=T_CLASS;}
~Class_base();
virtual ASObject* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)=0;
objAndLevel getVariableByMultiname(const multiname& name, bool skip_impl, bool enableOverride=true)
{
objAndLevel ret=ASObject::getVariableByMultiname(name, skip_impl, enableOverride);
if(ret.obj==NULL && super)
ret=super->getVariableByMultiname(name, skip_impl, enableOverride);
return ret;
}
intptr_t getVariableByMultiname_i(const multiname& name)
{
throw UnsupportedException("Class_base::getVariableByMultiname_i");
return 0;
/* intptr_t ret=ASObject::getVariableByMultiname(name);
if(==NULL && super)
ret=super->getVariableByMultiname(name);
return ret;*/
}
objAndLevel getVariableByQName(const tiny_string& name, const tiny_string& ns, bool skip_impl=false)
{
objAndLevel ret=ASObject::getVariableByQName(name,ns,skip_impl);
if(ret.obj==NULL && super)
ret=super->getVariableByQName(name,ns,skip_impl);
return ret;
}
/* void setVariableByMultiname_i(const multiname& name, intptr_t value)
{
abort();
}
void setVariableByMultiname(const multiname& name, ASObject* o)
{
abort();
}
void setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o, bool find_back=true)
{
abort();
}*/
void addImplementedInterface(const multiname& i);
void addImplementedInterface(Class_base* i);
virtual void buildInstanceTraits(ASObject* o) const=0;
const std::vector<Class_base*>& getInterfaces() const;
void linkInterface(ASObject* obj) const;
bool isSubClass(const Class_base* cls) const;
tiny_string getQualifiedClassName() const;
tiny_string toString(bool debugMsg);
//DEPRECATED: naive garbage collector
void abandonObject(ASObject* ob);
void acquireObject(ASObject* ob);
void cleanUp();
};
class Class_object: public Class_base
{
private:
Class_object():Class_base("Class"){}
ASObject* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)
{
throw RunTimeException("Class_object::getInstance");
return NULL;
}
void buildInstanceTraits(ASObject* o) const
{
throw RunTimeException("Class_object::buildInstanceTraits");
}
public:
static Class_object* getClass();
};
//Adaptor from fuction to class, it does not seems to be a good idea to
//derive IFunction from Class_base, because it's too heavyweight
class Class_function: public Class_base
{
private:
IFunction* f;
ASObject* asprototype;
ASObject* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)
{
throw RunTimeException("Class_function::getInstance");
return NULL;
}
void buildInstanceTraits(ASObject* o) const
{
throw RunTimeException("Class_function::buildInstanceTraits");
}
public:
//Class_function is both used as the prototype for each function and as the Function classs object
Class_function():Class_base("Function"),f(NULL),asprototype(NULL){}
Class_function(IFunction* _f, ASObject* _p):Class_base("Function"),f(_f),asprototype(_p){}
tiny_string class_name;
objAndLevel getVariableByMultiname(const multiname& name, bool skip_impl=false, bool enableOverride=true)
{
objAndLevel ret=Class_base::getVariableByMultiname(name,skip_impl, enableOverride);
if(ret.obj==NULL && asprototype)
ret=asprototype->getVariableByMultiname(name,skip_impl, enableOverride);
return ret;
}
intptr_t getVariableByMultiname_i(const multiname& name)
{
throw UnsupportedException("Class_function::getVariableByMultiname_i");
return 0;
/* intptr_t ret=ASObject::getVariableByMultiname(name);
if(ret==NULL && super)
ret=super->getVariableByMultiname(name);
return ret;*/
}
objAndLevel getVariableByQName(const tiny_string& name, const tiny_string& ns, bool skip_impl=false)
{
objAndLevel ret=Class_base::getVariableByQName(name,ns,skip_impl);
if(ret.obj==NULL && asprototype)
ret=asprototype->getVariableByQName(name,ns,skip_impl);
return ret;
}
void setVariableByMultiname_i(const multiname& name, intptr_t value)
{
throw UnsupportedException("Class_function::setVariableByMultiname_i");
}
void setVariableByMultiname(const multiname& name, ASObject* o, bool enableOverride)
{
throw UnsupportedException("Class_function::setVariableByMultiname");
}
void setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o, bool find_back=true, bool skip_impl=false)
{
throw UnsupportedException("Class_function::setVariableByQName");
}
static Class_function* getClass();
};
class IFunction: public ASObject
{
CLASSBUILDABLE(IFunction);
protected:
IFunction();
virtual IFunction* clone()=0;
ASObject* closure_this;
int closure_level;
bool bound;
IFunction* overriden_by;
public:
ASFUNCTION(apply);
virtual ASObject* call(ASObject* obj, ASObject* const* args, uint32_t num_args, int level)=0;
IFunction* bind(ASObject* c, int level)
{
if(!bound)
{
//If binding with null we are not a class method
IFunction* ret;
incRef();
ret=this;
ret->bound=true;
ret->closure_this=c;
if(c)
c->incRef();
ret->closure_level=level;
//std::cout << "Binding " << ret << std::endl;
return ret;
}
else
{
incRef();
return this;
}
}
void override(IFunction* f)
{
overriden_by=f;
}
IFunction* getOverride()
{
//Get the last override
IFunction* cur=overriden_by;
if(cur==NULL)
return this;
else
return cur->getOverride();
}
bool isOverridden() const
{
return overriden_by!=NULL;
}
};
class Function : public IFunction
{
friend class Class<IFunction>;
public:
typedef ASObject* (*as_function)(ASObject*, ASObject* const *, const unsigned int);
private:
as_function val;
Function(){}
Function(as_function v):val(v){}
Function* clone()
{
return new Function(*this);
}
public:
ASObject* call(ASObject* obj, ASObject* const* args, uint32_t num_args, int level);
IFunction* toFunction();
bool isEqual(ASObject* r)
{
Function* f=dynamic_cast<Function*>(r);
if(f==NULL)
return false;
return val==f->val;
}
};
class SyntheticFunction : public IFunction
{
friend class ABCVm;
friend class Class<IFunction>;
public:
typedef ASObject* (*synt_function)(call_context* cc);
private:
int hit_count;
method_info* mi;
synt_function val;
SyntheticFunction(method_info* m);
SyntheticFunction* clone()
{
return new SyntheticFunction(*this);
}
public:
ASObject* call(ASObject* obj, ASObject* const* args, uint32_t num_args, int level);
IFunction* toFunction();
std::vector<ASObject*> func_scope;
bool isEqual(ASObject* r)
{
SyntheticFunction* sf=dynamic_cast<SyntheticFunction*>(r);
if(sf==NULL)
return false;
return mi==sf->mi;
}
void acquireScope(const std::vector<ASObject*>& scope)
{
assert_and_throw(func_scope.empty());
func_scope=scope;
for(unsigned int i=0;i<func_scope.size();i++)
func_scope[i]->incRef();
}
void addToScope(ASObject* s)
{
func_scope.push_back(s);
}
};
//Specialized class for IFunction
template<>
class Class<IFunction>: public Class_base
{
private:
Class<IFunction>():Class_base("Function"){}
ASObject* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)
{
throw UnsupportedException("Class<IFunction>::getInstance");
return NULL;
}
public:
static Class<IFunction>* getClass();
static Function* getFunction(Function::as_function v)
{
Class<IFunction>* c=Class<IFunction>::getClass();
Function* ret=new Function(v);
ret->setPrototype(c);
//c->handleConstruction(ret,NULL,0,true);
return ret;
}
static SyntheticFunction* getSyntheticFunction(method_info* m)
{
Class<IFunction>* c=Class<IFunction>::getClass();
SyntheticFunction* ret=new SyntheticFunction(m);
ret->setPrototype(c);
c->handleConstruction(ret,NULL,0,true);
return ret;
}
void buildInstanceTraits(ASObject* o) const
{
}
};
class Boolean: public ASObject
{
friend bool Boolean_concrete(ASObject* obj);
CLASSBUILDABLE(Boolean);
private:
bool val;
Boolean(){}
Boolean(bool v):val(v){type=T_BOOLEAN;}
public:
int32_t toInt()
{
return val;
}
bool isEqual(ASObject* r);
tiny_string toString(bool debugMsg);
static void buildTraits(ASObject* o){};
};
class Undefined : public ASObject
{
public:
ASFUNCTION(call);
Undefined();
tiny_string toString(bool debugMsg);
int toInt();
double toNumber();
bool isEqual(ASObject* r);
virtual ~Undefined(){}
};
class DLL_PUBLIC ASString: public ASObject
{
CLASSBUILDABLE(ASString);
private:
tiny_string toString_priv() const;
ASString();
ASString(const std::string& s);
ASString(const tiny_string& s);
ASString(const char* s);
public:
std::string data;
static void sinit(Class_base* c);
static void buildTraits(ASObject* o);
ASFUNCTION(split);
ASFUNCTION(_getLength);
ASFUNCTION(replace);
ASFUNCTION(concat);
ASFUNCTION(slice);
ASFUNCTION(substr);
ASFUNCTION(indexOf);
ASFUNCTION(charCodeAt);
ASFUNCTION(toLowerCase);
bool isEqual(ASObject* r);
bool isLess(ASObject* r);
tiny_string toString(bool debugMsg=false);
double toNumber();
int32_t toInt();
};
class Null: public ASObject
{
public:
Null(){type=T_NULL;}
tiny_string toString(bool debugMsg);
bool isEqual(ASObject* r);
};
class ASQName: public ASObject
{
friend class ABCContext;
CLASSBUILDABLE(ASQName);
private:
tiny_string uri;
tiny_string local_name;
ASQName(){type=T_QNAME;}
public:
static void sinit(Class_base*);
ASFUNCTION(_constructor);
};
class Namespace: public ASObject
{
friend class ASQName;
friend class ABCContext;
CLASSBUILDABLE(Namespace);
private:
tiny_string uri;
Namespace(){type=T_NAMESPACE;}
Namespace(const tiny_string& _uri):uri(_uri){type=T_NAMESPACE;}
public:
static void sinit(Class_base*);
static void buildTraits(ASObject* o);
ASFUNCTION(_constructor);
};
enum DATA_TYPE {DATA_OBJECT=0,DATA_INT};
struct data_slot
{
DATA_TYPE type;
union
{
ASObject* data;
int32_t data_i;
};
explicit data_slot(ASObject* o,DATA_TYPE t=DATA_OBJECT):type(t),data(o){}
data_slot():type(DATA_OBJECT),data(NULL){}
explicit data_slot(intptr_t i):type(DATA_INT),data_i(i){}
};
class Array: public ASObject
{
friend class ABCVm;
CLASSBUILDABLE(Array);
protected:
std::vector<data_slot> data;
void outofbounds() const;
Array();
public:
//These utility methods are also used by ByteArray
static bool isValidMultiname(const multiname& name, unsigned int& index);
static bool isValidQName(const tiny_string& name, const tiny_string& ns, unsigned int& index);
virtual ~Array();
static void sinit(Class_base*);
static void buildTraits(ASObject* o);
ASFUNCTION(_constructor);
ASFUNCTION(_push);
ASFUNCTION(_concat);
ASFUNCTION(_pop);
ASFUNCTION(join);
ASFUNCTION(shift);
ASFUNCTION(unshift);
ASFUNCTION(splice);
ASFUNCTION(_sort);
ASFUNCTION(sortOn);
ASFUNCTION(filter);
ASFUNCTION(indexOf);
ASFUNCTION(_getLength);
ASObject* at(unsigned int index) const
{
if(index<data.size())
{
assert_and_throw(data[index].data);
return data[index].data;
}
else
{
outofbounds();
return NULL;
}
}
void set(unsigned int index, ASObject* o)
{
if(index<data.size())
{
assert_and_throw(data[index].data==NULL);
data[index].data=o;
data[index].type=DATA_OBJECT;
}
else
outofbounds();
}
int size() const
{
return data.size();
}
void push(ASObject* o)
{
data.push_back(data_slot(o,DATA_OBJECT));
}
void resize(int n)
{
data.resize(n);
}
objAndLevel getVariableByQName(const tiny_string& name, const tiny_string& ns, bool skip_impl=false);
objAndLevel getVariableByMultiname(const multiname& name, bool skip_impl, bool enableOverride);
intptr_t getVariableByMultiname_i(const multiname& name);
void setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o, bool find_back=true, bool skip_impl=false);
void setVariableByMultiname(const multiname& name, ASObject* o, bool enableOverride=true);
void setVariableByMultiname_i(const multiname& name, intptr_t value);
tiny_string toString(bool debugMsg=false);
bool isEqual(ASObject* r);
bool hasNext(unsigned int& index, bool& out);
bool nextName(unsigned int index, ASObject*& out)
{
assert_and_throw(implEnable);
throw UnsupportedException("nextName not supported in Array");
}
bool nextValue(unsigned int index, ASObject*& out);
tiny_string toString_priv() const;
};
class Integer : public ASObject
{
friend class Number;
friend class Array;
friend class ABCVm;
friend class ABCContext;
friend ASObject* abstract_i(intptr_t i);
CLASSBUILDABLE(Integer);
private:
int32_t val;
Integer(int32_t v=0):val(v){type=T_INTEGER;}
Integer(Manager* m):ASObject(m),val(0){type=T_INTEGER;}
public:
static void buildTraits(ASObject* o){
o->setVariableByQName("toString",AS3,Class<IFunction>::getFunction(Integer::_toString));
};
ASFUNCTION(_toString);
tiny_string toString(bool debugMsg);
int32_t toInt()
{
return val;
}
double toNumber()
{
return val;
}
bool isLess(ASObject* r);
bool isEqual(ASObject* o);
};
class UInteger: public ASObject
{
private:
uint32_t val;
public:
UInteger(uint32_t v=0):val(v){type=T_UINTEGER;}
static void sinit(Class_base* c);
tiny_string toString(bool debugMsg);
int32_t toInt()
{
return val;
}
uint32_t toUInt()
{
return val;
}
double toNumber()
{
return val;
}
bool isLess(ASObject* r);
bool isEqual(ASObject* o);
};
class Number : public ASObject
{
friend ASObject* abstract_d(number_t i);
friend class ABCContext;
CLASSBUILDABLE(Number);
private:
double val;
Number(){}
Number(double v):val(v){type=T_NUMBER;}
Number(Manager* m):ASObject(m),val(0){type=T_NUMBER;}
public:
tiny_string toString(bool debugMsg);
unsigned int toUInt()
{
return (unsigned int)(val);
}
int32_t toInt()
{
if(val<0)
return int(val);
else
{
uint32_t ret=val;
return ret;
}
}
double toNumber()
{
return val;
}
bool isLess(ASObject* o);
bool isEqual(ASObject* o);
static void buildTraits(ASObject* o){};
};
class ASMovieClipLoader: public ASObject
{
public:
ASMovieClipLoader();
ASFUNCTION(addListener);
ASFUNCTION(constructor);
};
class ASXML: public ASObject
{
private:
char* xml_buf;
int xml_index;
static size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
public:
ASXML();
ASFUNCTION(constructor);
ASFUNCTION(load);
};
class Date: public ASObject
{
CLASSBUILDABLE(Date);
private:
int year;
int month;
int date;
int hour;
int minute;
int second;
int millisecond;
int32_t toInt();
Date();
public:
static void sinit(Class_base*);
static void buildTraits(ASObject* o);
ASFUNCTION(_constructor);
ASFUNCTION(getTimezoneOffset);
ASFUNCTION(getTime);
ASFUNCTION(getFullYear);
ASFUNCTION(getHours);
ASFUNCTION(getMinutes);
ASFUNCTION(valueOf);
tiny_string toString(bool debugMsg=false);
tiny_string toString_priv() const;
};
//Internal objects used to store traits declared in scripts and object placed, but not yet valid
class Definable : public ASObject
{
public:
Definable(){type=T_DEFINABLE;}
virtual void define(ASObject* g)=0;
};
class ScriptDefinable: public Definable
{
private:
IFunction* f;
public:
ScriptDefinable(IFunction* _f):f(_f){}
//The global object will be passed from the calling context
void define(ASObject* g){ f->call(g,NULL,0,0); }
};
class Math: public ASObject
{
public:
static void sinit(Class_base* c);
ASFUNCTION(atan2);
ASFUNCTION(abs);
ASFUNCTION(sin);
ASFUNCTION(cos);
ASFUNCTION(floor);
ASFUNCTION(ceil);
ASFUNCTION(round);
ASFUNCTION(sqrt);
ASFUNCTION(random);
ASFUNCTION(_max);
ASFUNCTION(_min);
ASFUNCTION(pow);
};
class RegExp: public ASObject
{
CLASSBUILDABLE(RegExp);
friend class ASString;
private:
std::string re;
bool global;
bool ignoreCase;
bool extended;
int lastIndex;
RegExp();
public:
static void sinit(Class_base* c);
static void buildTraits(ASObject* o);
ASFUNCTION(_constructor);
ASFUNCTION(exec);
ASFUNCTION(test);
ASFUNCTION(_getGlobal);
};
};
#endif