forked from Jacopo/lightspark
/
swftypes.cpp
2080 lines (1844 loc) · 46.8 KB
/
swftypes.cpp
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
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/>.
**************************************************************************/
#define GL_GLEXT_PROTOTYPES
#include "swftypes.h"
#include "abc.h"
#include "tags.h"
#include "logger.h"
#include "actions.h"
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include "swf.h"
#include "geometry.h"
#include "class.h"
#include "exceptions.h"
using namespace std;
using namespace lightspark;
REGISTER_CLASS_NAME(ASObject);
extern TLSDATA SystemState* sys;
extern TLSDATA RenderThread* rt;
extern TLSDATA ParseThread* pt;
tiny_string ASObject::toString(bool debugMsg)
{
check();
if(debugMsg==false && hasPropertyByQName("toString",""))
{
objAndLevel obj_toString=getVariableByQName("toString","");
if(obj_toString.obj->getObjectType()==T_FUNCTION)
{
IFunction* f_toString=static_cast<IFunction*>(obj_toString.obj);
ASObject* ret=f_toString->call(this,NULL,0,obj_toString.level);
assert_and_throw(ret->getObjectType()==T_STRING);
return ret->toString();
}
}
if(getPrototype())
{
tiny_string ret;
ret+="[object ";
ret+=getPrototype()->class_name;
ret+="]";
return ret;
}
else
return "[object Object]";
}
bool ASObject::isLess(ASObject* r)
{
check();
if(hasPropertyByQName("valueOf",""))
{
if(r->hasPropertyByQName("valueOf","")==false)
throw RunTimeException("Missing valueof for second operand");
objAndLevel obj1=getVariableByQName("valueOf","");
objAndLevel obj2=r->getVariableByQName("valueOf","");
assert_and_throw(obj1.obj!=NULL && obj2.obj!=NULL);
assert_and_throw(obj1.obj->getObjectType()==T_FUNCTION && obj2.obj->getObjectType()==T_FUNCTION);
IFunction* f1=static_cast<IFunction*>(obj1.obj);
IFunction* f2=static_cast<IFunction*>(obj2.obj);
ASObject* ret1=f1->call(this,NULL,0,obj1.level);
ASObject* ret2=f2->call(r,NULL,0,obj2.level);
LOG(LOG_CALLS,"Overloaded isLess");
return ret1->isLess(ret2);
}
LOG(LOG_NOT_IMPLEMENTED,"Less than comparison between type "<<getObjectType()<< " and type " << r->getObjectType());
if(prototype)
LOG(LOG_NOT_IMPLEMENTED,"Type " << prototype->class_name);
throw RunTimeException("Not handled less comparison for objects");
return false;
}
bool ASObject::hasNext(unsigned int& index, bool& out)
{
assert_and_throw(implEnable);
return false;
}
bool ASObject::nextName(unsigned int index, ASObject*& out)
{
assert_and_throw(implEnable);
return false;
}
bool ASObject::nextValue(unsigned int index, ASObject*& out)
{
assert_and_throw(implEnable);
return false;
}
void ASObject::buildTraits(ASObject* o)
{
if(o->getActualPrototype()->class_name!="ASObject")
LOG(LOG_NOT_IMPLEMENTED,"Add buildTraits for class " << o->getActualPrototype()->class_name);
}
tiny_string multiname::qualifiedString() const
{
assert_and_throw(ns.size()==1);
assert_and_throw(name_type==NAME_STRING);
//TODO: what if the ns is empty
if(false && ns[0].name=="")
return name_s;
else
{
tiny_string ret=ns[0].name;
ret+="::";
ret+=name_s;
return ret;
}
}
bool ASObject::isEqual(ASObject* r)
{
check();
//if we are comparing the same object the answer is true
if(this==r)
return true;
if(r->getObjectType()==T_NULL || r->getObjectType()==T_UNDEFINED)
return false;
if(hasPropertyByQName("equals",""))
{
objAndLevel func_equals=getVariableByQName("equals","");
assert_and_throw(func_equals.obj!=NULL);
assert_and_throw(func_equals.obj->getObjectType()==T_FUNCTION);
IFunction* func=static_cast<IFunction*>(func_equals.obj);
ASObject* ret=func->call(this,&r,1,func_equals.level);
assert_and_throw(ret->getObjectType()==T_BOOLEAN);
LOG(LOG_CALLS,"Overloaded isEqual");
return Boolean_concrete(ret);
}
//We can try to call valueOf (maybe equals) and compare that
if(hasPropertyByQName("valueOf",""))
{
if(r->hasPropertyByQName("valueOf","")==false)
throw RunTimeException("Not handled less comparison for objects");
objAndLevel obj1=getVariableByQName("valueOf","");
objAndLevel obj2=r->getVariableByQName("valueOf","");
assert_and_throw(obj1.obj!=NULL && obj2.obj!=NULL);
assert_and_throw(obj1.obj->getObjectType()==T_FUNCTION && obj2.obj->getObjectType()==T_FUNCTION);
IFunction* f1=static_cast<IFunction*>(obj1.obj);
IFunction* f2=static_cast<IFunction*>(obj2.obj);
ASObject* ret1=f1->call(this,NULL,0,obj1.level);
ASObject* ret2=f2->call(r,NULL,0,obj2.level);
LOG(LOG_CALLS,"Overloaded isEqual");
return ret1->isEqual(ret2);
}
LOG(LOG_CALLS,"Equal comparison between type "<<getObjectType()<< " and type " << r->getObjectType());
if(prototype)
LOG(LOG_CALLS,"Type " << prototype->class_name);
return false;
}
unsigned int ASObject::toUInt()
{
return toInt();
}
int ASObject::toInt()
{
LOG(LOG_ERROR,"Cannot convert object of type " << getObjectType() << " to int");
throw RunTimeException("Cannot converto object to int");
return 0;
}
double ASObject::toNumber()
{
LOG(LOG_ERROR,"Cannot convert object of type " << getObjectType() << " to float");
throw RunTimeException("Cannot converto object to float");
return 0;
}
obj_var* variables_map::findObjVar(const tiny_string& n, const tiny_string& ns, int& level, bool create, bool searchPreviusLevels)
{
nameAndLevel name(n,level);
const var_iterator ret_begin=Variables.lower_bound(name);
//This actually look for the first different name, if we accept also previous levels
//Otherwise we are just doing equal_range
if(searchPreviusLevels)
name.level=0;
const var_iterator ret_end=Variables.upper_bound(name);
name.level=level;
var_iterator ret=ret_begin;
for(;ret!=ret_end;ret++)
{
if(ret->second.first==ns)
{
level=ret->first.level;
return &ret->second.second;
}
}
//Name not present, insert it if we have to create it
if(create)
{
var_iterator inserted=Variables.insert(ret_begin,make_pair(nameAndLevel(n,level), make_pair(ns, obj_var() ) ) );
return &inserted->second.second;
}
else
return NULL;
}
bool ASObject::hasPropertyByQName(const tiny_string& name, const tiny_string& ns)
{
check();
//We look in all the object's levels
int level=(prototype)?(prototype->max_level):0;
return (Variables.findObjVar(name, ns, level, false, true)!=NULL);
}
bool ASObject::hasPropertyByMultiname(const multiname& name)
{
check();
//We look in all the object's levels
int level=(prototype)?(prototype->max_level):0;
return (Variables.findObjVar(name, level, false, true)!=NULL);
}
void ASObject::setGetterByQName(const tiny_string& name, const tiny_string& ns, IFunction* o)
{
check();
assert_and_throw(!initialized);
//Getters are inserted with the current level of the prototype chain
int level=cur_level;
obj_var* obj=Variables.findObjVar(name,ns,level,true,false);
if(obj->getter!=NULL)
{
//This happens when interfaces are declared multiple times
assert_and_throw(o==obj->getter);
return;
}
obj->getter=o;
}
void ASObject::setSetterByQName(const tiny_string& name, const tiny_string& ns, IFunction* o)
{
check();
assert_and_throw(!initialized);
//Setters are inserted with the current level of the prototype chain
int level=cur_level;
obj_var* obj=Variables.findObjVar(name,ns,level,true,false);
if(obj->setter!=NULL)
{
//This happens when interfaces are declared multiple times
assert_and_throw(o==obj->setter);
return;
}
obj->setter=o;
}
void ASObject::deleteVariableByMultiname(const multiname& name)
{
assert_and_throw(ref_count>0);
//Find out if the variable is declared more than once
obj_var* obj=NULL;
int level;
unsigned int count=0;
//We search in every level
int max_level=(prototype)?prototype->max_level:0;
for(int i=max_level;i>=0;i--)
{
//We stick to the old iteration mode, as we need to count
obj=Variables.findObjVar(name,max_level,false,false);
if(obj)
{
count++;
level=i;
}
}
//if it's not present it's ok
if(count==0)
return;
assert_and_throw(count==1);
//Now dereference the values
//TODO: maybe we can look on the previous levels
obj=Variables.findObjVar(name,level,false,false);
if(obj->var)
obj->var->decRef();
if(obj->getter)
obj->getter->decRef();
if(obj->setter)
obj->setter->decRef();
//Now kill the variable
Variables.killObjVar(name,level);
}
//In all setter we first pass the value to the interface to see if special handling is possible
void ASObject::setVariableByMultiname_i(const multiname& name, intptr_t value)
{
check();
setVariableByMultiname(name,abstract_i(value));
}
obj_var* ASObject::findSettable(const multiname& name, int& level)
{
assert(level==cur_level);
obj_var* ret=NULL;
int max_level=cur_level;
for(int i=max_level;i>=0;i--)
{
//The variable i is automatically moved to the right level
ret=Variables.findObjVar(name,i,false,true);
if(ret)
{
//It seems valid for a class to redefine only the getter, so if we can't find
//something to get, just go to the previous level
if(ret->setter || ret->var)
{
level=i;
break;
}
}
}
return ret;
}
void ASObject::setVariableByMultiname(const multiname& name, ASObject* o, bool enableOverride)
{
check();
//It's always correct to use the current level for the object
//NOTE: we assume that [gs]etSuper and [sg]etProperty correctly manipulate the cur_level
int level=cur_level;
obj_var* obj=findSettable(name,level);
if(obj==NULL)
{
assert_and_throw(level==cur_level);
obj=Variables.findObjVar(name,level,true,false);
}
if(obj->setter)
{
//Call the setter
LOG(LOG_CALLS,"Calling the setter");
//Overriding function is automatically done by using cur_level
IFunction* setter=obj->setter;
if(enableOverride)
setter=setter->getOverride();
//One argument can be passed without creating an array
incRef();
ASObject* ret=setter->call(this,&o,1,level);
assert_and_throw(ret==NULL);
LOG(LOG_CALLS,"End of setter");
}
else
{
assert_and_throw(!obj->getter);
if(obj->var)
obj->var->decRef();
obj->var=o;
}
}
void ASObject::setVariableByQName(const tiny_string& name, const tiny_string& ns, ASObject* o, bool find_back, bool skip_impl)
{
obj_var* obj=NULL;
//It's always correct to use the current level for the object
//NOTE: we assume that [gs]etSuper and setProperty correctly manipulate the cur_level
int level=cur_level;
obj=Variables.findObjVar(name,ns,level,false,find_back);
if(obj==NULL)
{
//When the var is not found level should not be modified
assert_and_throw(cur_level==level);
obj=Variables.findObjVar(name,ns,level,true,false);
}
if(obj->setter)
{
//Call the setter
LOG(LOG_CALLS,"Calling the setter");
IFunction* setter=obj->setter->getOverride();
incRef();
//One argument can be passed without creating an array
ASObject* ret=setter->call(this,&o,1,level);
assert_and_throw(ret==NULL);
LOG(LOG_CALLS,"End of setter");
}
else
{
assert_and_throw(!obj->getter);
if(obj->var)
obj->var->decRef();
obj->var=o;
}
check();
}
void variables_map::killObjVar(const multiname& mname, int level)
{
nameAndLevel name("",level);
switch(mname.name_type)
{
case multiname::NAME_INT:
name.name=tiny_string(mname.name_i);
break;
case multiname::NAME_NUMBER:
name.name=tiny_string(mname.name_d);
break;
case multiname::NAME_STRING:
name.name=mname.name_s;
break;
default:
assert_and_throw("Unexpected name kind" && false);
}
const pair<var_iterator, var_iterator> ret=Variables.equal_range(name);
assert_and_throw(ret.first!=ret.second);
//Find the namespace
assert_and_throw(!mname.ns.empty());
for(unsigned int i=0;i<mname.ns.size();i++)
{
const tiny_string& ns=mname.ns[i].name;
var_iterator start=ret.first;
for(;start!=ret.second;start++)
{
if(start->second.first==ns)
{
Variables.erase(start);
return;
}
}
}
throw RunTimeException("Variable to kill not found");
}
obj_var* variables_map::findObjVar(const multiname& mname, int& level, bool create, bool searchPreviusLevels)
{
nameAndLevel name("",level);
switch(mname.name_type)
{
case multiname::NAME_INT:
name.name=tiny_string(mname.name_i);
break;
case multiname::NAME_NUMBER:
name.name=tiny_string(mname.name_d);
break;
case multiname::NAME_STRING:
name.name=mname.name_s;
break;
case multiname::NAME_OBJECT:
name.name=mname.name_o->toString();
break;
default:
assert_and_throw("Unexpected name kind" && false);
}
const var_iterator ret_begin=Variables.lower_bound(name);
//This actually look for the first different name, if we accept also previous levels
//Otherwise we are just doing equal_range
if(searchPreviusLevels)
name.level=0;
const var_iterator ret_end=Variables.upper_bound(name);
name.level=level;
var_iterator ret=ret_begin;
for(;ret!=ret_end;ret++)
{
//Check if one the namespace is already present
assert_and_throw(!mname.ns.empty());
//We can use binary search, as the namespace are ordered
if(binary_search(mname.ns.begin(),mname.ns.end(),ret->second.first))
{
level=ret->first.level;
return &ret->second.second;
}
}
//Name not present, insert it, if the multiname has a single ns and if we have to insert it
//TODO: HACK: this is needed if the property should be present but it's not
if(create)
{
if(mname.ns.size()>1)
{
//Hack, insert with empty name
//Here the object MUST exist
var_iterator inserted=Variables.insert(ret,make_pair(name, make_pair("", obj_var() ) ) );
return &inserted->second.second;
}
var_iterator inserted=Variables.insert(ret,make_pair(name, make_pair(mname.ns[0].name, obj_var() ) ) );
return &inserted->second.second;
}
else
return NULL;
}
ASFUNCTIONBODY(ASObject,_toString)
{
return Class<ASString>::getInstanceS(obj->toString());
}
ASFUNCTIONBODY(ASObject,hasOwnProperty)
{
assert_and_throw(argslen==1);
bool ret=obj->hasPropertyByQName(args[0]->toString(),"");
return abstract_b(ret);
}
ASFUNCTIONBODY(ASObject,_constructor)
{
return NULL;
}
/*ASFUNCTIONBODY(ASObject,_getPrototype)
{
if(prototype==NULL)
return new Undefined;
prototype->incRef();
return prototype;
}
ASFUNCTIONBODY(ASObject,_setPrototype)
{
if(prototype)
prototype->decRef();
prototype=args->at(0);
prototype->incRef();
return NULL;
}*/
void ASObject::initSlot(unsigned int n,const tiny_string& name, const tiny_string& ns)
{
//Should be correct to use the level on the prototype chain
assert_and_throw(!initialized);
Variables.initSlot(n,cur_level,name,ns);
}
ASObject* ASObject::getVariableByString(const std::string& name)
{
ASObject* ret=Variables.getVariableByString(name);
return ret;
}
//In all the getter function we first ask the interface, so that special handling (e.g. Array)
//can be done
intptr_t ASObject::getVariableByMultiname_i(const multiname& name)
{
check();
ASObject* ret=getVariableByMultiname(name).obj;
assert_and_throw(ret);
return ret->toInt();
}
obj_var* ASObject::findGettable(const multiname& name, int& level)
{
assert(level==cur_level);
obj_var* ret=NULL;
int max_level=cur_level;
for(int i=max_level;i>=0;i--)
{
//The variable i is automatically moved to the right level
ret=Variables.findObjVar(name,i,false,true);
if(ret)
{
//It seems valid for a class to redefine only the setter, so if we can't find
//something to get, just go to the previous level
if(ret->getter || ret->var)
{
level=i;
break;
}
}
}
return ret;
}
objAndLevel ASObject::getVariableByMultiname(const multiname& name, bool skip_impl, bool enableOverride)
{
check();
int level=cur_level;
obj_var* obj=findGettable(name,level);
if(obj!=NULL)
{
assert_and_throw(level!=-1);
if(obj->getter)
{
//Call the getter
if(prototype)
{
LOG(LOG_CALLS,"Calling the getter on type " << prototype->class_name);
}
else
{
LOG(LOG_CALLS,"Calling the getter");
}
IFunction* getter=obj->getter;
if(enableOverride)
getter=getter->getOverride();
incRef();
ASObject* ret=getter->call(this,NULL,0,level);
LOG(LOG_CALLS,"End of getter");
assert_and_throw(ret);
//The returned value is already owned by the caller
ret->fake_decRef();
//TODO: check
return objAndLevel(ret,level);
}
else
{
assert_and_throw(!obj->setter);
assert_and_throw(obj->var);
return objAndLevel(obj->var,level);
}
}
else
{
//Check if we should do lazy definition
if(name.name_s=="toString")
{
ASObject* ret=Class<IFunction>::getFunction(ASObject::_toString);
setVariableByQName("toString","",ret);
//Added at level 0, as Object is always the base
return objAndLevel(ret,0);
}
else if(name.name_s=="hasOwnProperty")
{
ASObject* ret=Class<IFunction>::getFunction(ASObject::hasOwnProperty);
setVariableByQName("hasOwnProperty","",ret);
//Added at level 0, as Object is always the base
return objAndLevel(ret,0);
}
else if(getObjectType()==T_FUNCTION && name.name_s=="call")
{
//Fake returning the function itself
return objAndLevel(this,0);
}
else if(getObjectType()==T_FUNCTION && name.name_s=="apply")
{
//Create on the fly a Function
//HACK: both call and apply should be included in the Function object
return objAndLevel(Class<IFunction>::getFunction(IFunction::apply),0);
}
//It has not been found yet, ask the prototype
if(prototype)
return prototype->getVariableByMultiname(name,skip_impl);
}
//If it has not been found
return objAndLevel(NULL,0);
}
objAndLevel ASObject::getVariableByQName(const tiny_string& name, const tiny_string& ns, bool skip_impl)
{
check();
obj_var* obj=NULL;
int level=cur_level;
obj=Variables.findObjVar(name,ns,level,false,true);
if(obj!=NULL)
{
if(obj->getter)
{
//Call the getter
LOG(LOG_CALLS,"Calling the getter");
IFunction* getter=obj->getter->getOverride();
incRef();
ASObject* ret=getter->call(this,NULL,0,level);
LOG(LOG_CALLS,"End of getter");
//The variable is already owned by the caller
ret->fake_decRef();
return objAndLevel(ret,level);
}
else
return objAndLevel(obj->var,level);
}
else if(prototype)
{
objAndLevel ret=prototype->getVariableByQName(name,ns);
if(ret.obj)
return ret;
}
return objAndLevel(NULL,0);
}
ASObject* variables_map::getVariableByString(const std::string& name)
{
//Slow linear lookup, should be avoided
var_iterator it=Variables.begin();
for(;it!=Variables.end();it++)
{
string cur(it->second.first.raw_buf());
if(!cur.empty())
cur+='.';
cur+=it->first.name.raw_buf();
if(cur==name)
{
if(it->second.second.getter)
throw UnsupportedException("Getters are not supported in getVariableByString");
return it->second.second.var;
}
}
return NULL;
}
std::ostream& lightspark::operator<<(std::ostream& s, const tiny_string& r)
{
s << r.buf;
return s;
}
std::ostream& lightspark::operator<<(std::ostream& s, const multiname& r)
{
for(unsigned int i=0;i<r.ns.size();i++)
{
string prefix;
switch(r.ns[i].kind)
{
case 0x08:
prefix="ns:";
break;
case 0x16:
prefix="pakns:";
break;
case 0x17:
prefix="pakintns:";
break;
case 0x18:
prefix="protns:";
break;
case 0x19:
prefix="explns:";
break;
case 0x1a:
prefix="staticprotns:";
break;
case 0x05:
prefix="privns:";
break;
}
s << '[' << prefix << r.ns[i].name << "] ";
}
if(r.name_type==multiname::NAME_INT)
s << r.name_i;
else if(r.name_type==multiname::NAME_NUMBER)
s << r.name_d;
else if(r.name_type==multiname::NAME_STRING)
s << r.name_s;
else
s << r.name_o; //We print the hexadecimal value
return s;
}
void ASObject::check() const
{
//Put here a bunch of safety check on the object
assert_and_throw(ref_count>0);
//Heavyweight stuff
#ifdef EXPENSIVE_DEBUG
variables_map::const_var_iterator it=Variables.Variables.begin();
for(;it!=Variables.Variables.end();it++)
{
variables_map::const_var_iterator next=it;
next++;
if(next==Variables.Variables.end())
break;
//No double definition of a single variable should exist
if(it->first.name==next->first.name && it->second.first==next->second.first)
{
if(it->second.second.var==NULL && next->second.second.var==NULL)
continue;
if(it->second.second.var==NULL || next->second.second.var==NULL)
{
cout << it->first.name << endl;
cout << it->second.second.var << ' ' << it->second.second.setter << ' ' << it->second.second.getter << endl;
cout << next->second.second.var << ' ' << next->second.second.setter << ' ' << next->second.second.getter << endl;
abort();
}
if(it->second.second.var->getObjectType()!=T_FUNCTION || next->second.second.var->getObjectType()!=T_FUNCTION)
{
cout << it->first.name << endl;
abort();
}
}
}
#endif
}
void variables_map::dumpVariables()
{
var_iterator it=Variables.begin();
for(;it!=Variables.end();it++)
LOG(LOG_NO_INFO,it->first.level << ": [" << it->second.first << "] "<< it->first.name << " " <<
it->second.second.var << ' ' << it->second.second.setter << ' ' << it->second.second.getter);
}
lightspark::RECT::RECT()
{
}
lightspark::RECT::RECT(int a, int b, int c, int d):Xmin(a),Xmax(b),Ymin(c),Ymax(d)
{
}
std::ostream& lightspark::operator<<(std::ostream& s, const RECT& r)
{
s << '{' << (int)r.Xmin << ',' << r.Xmax << ',' << r.Ymin << ',' << r.Ymax << '}';
return s;
}
ostream& lightspark::operator<<(ostream& s, const STRING& t)
{
for(unsigned int i=0;i<t.String.size();i++)
s << t.String[i];
return s;
}
std::ostream& operator<<(std::ostream& s, const RGBA& r)
{
s << "RGBA <" << (int)r.Red << ',' << (int)r.Green << ',' << (int)r.Blue << ',' << (int)r.Alpha << '>';
return s;
}
std::ostream& operator<<(std::ostream& s, const RGB& r)
{
s << "RGB <" << (int)r.Red << ',' << (int)r.Green << ',' << (int)r.Blue << '>';
return s;
}
void MATRIX::get4DMatrix(float matrix[16]) const
{
memset(matrix,0,sizeof(float)*16);
matrix[0]=ScaleX;
matrix[1]=RotateSkew0;
matrix[4]=RotateSkew1;
matrix[5]=ScaleY;
matrix[10]=1;
matrix[12]=TranslateX;
matrix[13]=TranslateY;
matrix[15]=1;
}
void MATRIX::multiply2D(number_t xin, number_t yin, number_t& xout, number_t& yout) const
{
xout=xin*ScaleX + yin*RotateSkew1 + TranslateX;
yout=xin*RotateSkew0 + yin*ScaleY + TranslateY;
}
void MATRIX::getTranslation(int& x, int& y) const
{
x=TranslateX;
y=TranslateY;
}
std::ostream& operator<<(std::ostream& s, const MATRIX& r)
{
s << "| " << r.ScaleX << ' ' << r.RotateSkew0 << " |" << std::endl;
s << "| " << r.RotateSkew1 << ' ' << r.ScaleY << " |" << std::endl;
s << "| " << (int)r.TranslateX << ' ' << (int)r.TranslateY << " |" << std::endl;
return s;
}
std::istream& lightspark::operator>>(std::istream& stream, STRING& v)
{
v.String.clear();
UI8 c;
do
{
stream >> c;
if(c==0)
break;
v.String.push_back(c);
}
while(c!=0);
return stream;
}
std::istream& lightspark::operator>>(std::istream& stream, RECT& v)
{
BitStream s(stream);
int nbits=UB(5,s);
v.Xmin=SB(nbits,s);
v.Xmax=SB(nbits,s);
v.Ymin=SB(nbits,s);
v.Ymax=SB(nbits,s);
return stream;
}
std::istream& lightspark::operator>>(std::istream& s, RGB& v)
{
s >> v.Red >> v.Green >> v.Blue;
return s;
}
std::istream& lightspark::operator>>(std::istream& s, RGBA& v)
{
s >> v.Red >> v.Green >> v.Blue >> v.Alpha;
return s;
}
void LINESTYLEARRAY::appendStyles(const LINESTYLEARRAY& r)
{
unsigned int count = LineStyleCount + r.LineStyleCount;
assert_and_throw(version!=-1);
assert_and_throw(r.version==version);
if(version<4)
LineStyles.insert(LineStyles.end(),r.LineStyles.begin(),r.LineStyles.end());
else
LineStyles2.insert(LineStyles2.end(),r.LineStyles2.begin(),r.LineStyles2.end());
LineStyleCount = count;
}
std::istream& lightspark::operator>>(std::istream& s, LINESTYLEARRAY& v)
{
assert_and_throw(v.version!=-1);
s >> v.LineStyleCount;
if(v.LineStyleCount==0xff)
LOG(LOG_ERROR,"Line array extended not supported");
if(v.version<4)
{
for(int i=0;i<v.LineStyleCount;i++)
{
LINESTYLE tmp;
tmp.version=v.version;
s >> tmp;
v.LineStyles.push_back(tmp);
}
}
else
{
for(int i=0;i<v.LineStyleCount;i++)
{
LINESTYLE2 tmp;
s >> tmp;
v.LineStyles2.push_back(tmp);
}
}
return s;
}