~invernizzi/lightspark/readme-update

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
/**************************************************************************
    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 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 General Public License for more details.

    You should have received a copy of the GNU 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 "swftypes.h"
#include "frame.h"
#include "input.h"
#include "compat.h"
#include "exceptions.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 IInterface
{
protected:
	static void lookupAndLink(ASObject* o, const tiny_string& name, const tiny_string& interfaceNs);
};

class 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;
	}

public:
	Class_base* super;
	IFunction* constructor;
	//We need to know what is the context we are referring to
	ABCContext* context;
	tiny_string class_name;
	int class_index;
	int max_level;
	Class_base(const tiny_string& name):use_protected(false),super(NULL),constructor(NULL),context(NULL),class_name(name),class_index(-1),
		max_level(0) {type=T_CLASS;}
	~Class_base();
	virtual IInterface* 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)
	{
		abort();
		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);
};

class Class_object: public Class_base
{
private:
	Class_object():Class_base("Class"){};
	IInterface* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)
	{
		abort();
	}
	void buildInstanceTraits(ASObject* o) const
	{
		abort();
	}
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;
	IInterface* getInstance(bool construct, ASObject* const* args, const unsigned int argslen)
	{
		abort();
	}
	void buildInstanceTraits(ASObject* o) const
	{
		abort();
	}
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)
	{
		if(name.name_s=="borderSkin")
			std::cerr << "Looking for borderSkin on " << this << std::endl;
		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)
	{
		abort();
		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)
	{
		abort();
	}
	void setVariableByMultiname(const multiname& name, ASObject* o, bool enableOverride)
	{
		abort();
	}
	void setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o, bool find_back=true, bool skip_impl=false)
	{
		abort();
	}

	static Class_function* getClass();
};

class IFunction: public ASObject
{
public:
	ASFUNCTION(apply);
	IFunction();
	virtual ASObject* call(ASObject* obj, ASObject* const* args,int 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;
	}
protected:
	virtual IFunction* clone()=0;
	ASObject* closure_this;
	int closure_level;
	bool bound;
	IFunction* overriden_by;
};

class Function : public IFunction
{
public:
	typedef ASObject* (*as_function)(ASObject*, ASObject* const *, const unsigned int);
	Function(){}
	Function(as_function v):val(v){}
	ASObject* call(ASObject* obj, ASObject* const* args, int num_args, int level);
	IFunction* toFunction();
	bool isEqual(ASObject* r)
	{
		abort();
	}

private:
	as_function val;
	Function* clone()
	{
		return new Function(*this);
	}
};

class SyntheticFunction : public IFunction
{
friend class ABCVm;
friend void ASObject::handleConstruction(ASObject* const* args, unsigned int argslen, bool buildAndLink);
public:
	typedef ASObject* (*synt_function)(call_context* cc);
	SyntheticFunction(method_info* m);
	ASObject* call(ASObject* obj, ASObject* const* args,int 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(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);
	}

private:
	int hit_count;
	method_info* mi;
	synt_function val;
	SyntheticFunction* clone()
	{
		return new SyntheticFunction(*this);
	}
};

class Boolean: public ASObject
{
friend bool Boolean_concrete(ASObject* obj);
private:
	bool val;
public:
	Boolean(bool v):val(v){type=T_BOOLEAN;}
	int32_t toInt() const
	{
		return val;
	}
	bool isEqual(ASObject* r);
	tiny_string toString(bool debugMsg);
};

class Undefined : public ASObject
{
public:
	ASFUNCTION(call);
	Undefined();
	tiny_string toString(bool debugMsg);
	bool isEqual(ASObject* r);
	virtual ~Undefined(){}
};

class ASString: public IInterface
{
private:
	tiny_string toString() const;
public:
	std::string data;
	static void sinit(Class_base* c);
	static void buildTraits(ASObject* o);
	ASString();
	ASString(const std::string& s);
	ASString(const tiny_string& s);
	ASString(const char* s);
	ASFUNCTION(split);
	ASFUNCTION(_getLength);
	ASFUNCTION(replace);
	ASFUNCTION(concat);
	ASFUNCTION(slice);
	ASFUNCTION(substr);
	ASFUNCTION(indexOf);
	ASFUNCTION(charCodeAt);
	ASFUNCTION(toLowerCase);
	bool isEqual(bool& ret, ASObject* o);
	bool isLess(bool& ret, ASObject* o);
	bool toString(tiny_string& ret);
	bool toNumber(double& ret);
};

class Null: public ASObject
{
public:
	Null(){type=T_NULL;}
	tiny_string toString(bool debugMsg);
	bool isEqual(ASObject* r);
};

class ASQName: public IInterface
{
friend class ABCContext;
private:
	tiny_string uri;
	tiny_string local_name;
public:
	ASQName(){type=T_QNAME;}
	static void sinit(Class_base*);
	ASFUNCTION(_constructor);
};

class Namespace: public IInterface
{
friend class ASQName;
friend class ABCContext;
private:
	tiny_string uri;
public:
	Namespace(){type=T_NAMESPACE;}
	Namespace(const tiny_string& _uri):uri(_uri){type=T_NAMESPACE;}
	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 IInterface
{
friend class ABCVm;
protected:
	std::vector<data_slot> data;
	void outofbounds() const;
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);

	Array();
	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(_sort);
	ASFUNCTION(unshift);
	ASFUNCTION(filter);
	ASFUNCTION(indexOf);
	ASFUNCTION(_getLength);
	ASObject* at(unsigned int index) const
	{
		if(index<data.size())
		{
			assert(data[index].data);
			return data[index].data;
		}
		else
		{
			outofbounds();
			return NULL;
		}
	}
	void set(unsigned int index, ASObject* o)
	{
		if(index<data.size())
		{
			assert(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);
	}
	bool getVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject*& out);
	bool getVariableByMultiname(const multiname& name, ASObject*& out);
	bool getVariableByMultiname_i(const multiname& name, intptr_t& out);
	bool setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o);
	bool setVariableByMultiname(const multiname& name, ASObject* o);
	bool setVariableByMultiname_i(const multiname& name, intptr_t value);
	bool toString(tiny_string& ret);
	bool isEqual(bool& ret, ASObject* r);
	bool hasNext(unsigned int& index, bool& out);
	bool nextName(unsigned int index, ASObject*& out)
	{
		abort();
	}
	bool nextValue(unsigned int index, ASObject*& out);
	tiny_string toString() const;
};

class Integer : public ASObject
{
friend class Number;
friend class Array;
friend class ABCVm;
friend class ABCContext;
friend ASObject* abstract_i(intptr_t i);
private:
	int32_t val;
public:
	Integer(int32_t v=0):val(v){type=T_INTEGER;
		setVariableByQName("toString",AS3,new Function(Integer::_toString));
	}
	Integer(Manager* m):ASObject(m),val(0){type=T_INTEGER;
		setVariableByQName("toString",AS3,new Function(Integer::_toString));
	}
	ASFUNCTION(_toString);
	virtual ~Integer(){}
	tiny_string toString(bool debugMsg);
	int32_t toInt() const
	{
		return val;
	}
	double toNumber() const
	{
		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() const
	{
		return val;
	}
	uint32_t toUInt() const
	{
		return val;
	}
	double toNumber() const
	{
		return val;
	}
	bool isLess(ASObject* r);
	bool isEqual(ASObject* o);
};

class Number : public ASObject
{
friend ASObject* abstract_d(number_t i);
friend class ABCContext;
private:
	double val;
public:
	Number(double v):val(v){type=T_NUMBER;}
	Number(Manager* m):ASObject(m),val(0){type=T_NUMBER;}
	tiny_string toString(bool debugMsg);
	unsigned int toUInt() const
	{
		return (unsigned int)(val);
	}
	int32_t toInt() const
	{
		if(val<0)
			return int(val);
		else
		{
			uint32_t ret=val;
			return ret;
		}
	}
	double toNumber() const
	{
		return val;
	}
//	operator double() const {return val;}
	bool isLess(ASObject* o);
	bool isEqual(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 IInterface
{
private:
	int year;
	int month;
	int date;
	int hour;
	int minute;
	int second;
	int millisecond;
	int32_t toInt() const;
public:
	Date();
	static void sinit(Class_base*);
	static void buildTraits(ASObject* o);
	ASFUNCTION(_constructor);
	ASFUNCTION(getTimezoneOffset);
	ASFUNCTION(getTime);
	ASFUNCTION(getFullYear);
	ASFUNCTION(getHours);
	ASFUNCTION(getMinutes);
	ASFUNCTION(valueOf);
	bool toString(tiny_string& ret);
	tiny_string toString() const;
	bool toInt(int& ret);
};

//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 PlaceObject2Tag;

class DictionaryDefinable: public Definable
{
private:
	int dict_id;
	PlaceObject2Tag* p;
public:
	DictionaryDefinable(int d, PlaceObject2Tag* _p):dict_id(d),p(_p){}
	void define(ASObject* g);
};

class Math: public IInterface
{
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 IInterface
{
friend class ASString;
private:
	std::string re;
	bool global;
	bool ignoreCase;
	int lastIndex;
public:
	RegExp();
	static void sinit(Class_base* c);
	static void buildTraits(ASObject* o);
	ASFUNCTION(_constructor);
	ASFUNCTION(exec);
	ASFUNCTION(test);
	ASFUNCTION(_getGlobal);
};

};

#endif