forked from lightspark/lightspark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasobject.cpp
938 lines (815 loc) · 22.8 KB
/
asobject.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
/**************************************************************************
Lightspark, a free flash player implementation
Copyright (C) 2009,2010 Alessandro Pignotti ([email protected])
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/>.
**************************************************************************/
#include "asobject.h"
#include "scripting/class.h"
#include "scripting/toplevel.h"
#include <algorithm>
#include "compat.h"
using namespace lightspark;
using namespace std;
REGISTER_CLASS_NAME2(ASObject,"Object","");
tiny_string ASObject::toStringImpl() const
{
tiny_string ret;
if(getPrototype())
{
ret+="[object ";
ret+=getPrototype()->class_name.name;
ret+="]";
return ret;
}
else
ret="[object Object]";
return ret;
}
tiny_string ASObject::toString(bool debugMsg)
{
check();
multiname toStringName;
toStringName.name_type=multiname::NAME_STRING;
toStringName.name_s="toString";
toStringName.ns.push_back(nsNameAndKind("",PACKAGE_NAMESPACE));
if(debugMsg==false && hasPropertyByMultiname(toStringName))
{
ASObject* obj_toString=getVariableByMultiname(toStringName);
if(obj_toString->getObjectType()==T_FUNCTION)
{
IFunction* f_toString=static_cast<IFunction*>(obj_toString);
ASObject* ret=f_toString->call(this,NULL,0);
assert_and_throw(ret->getObjectType()==T_STRING);
tiny_string retS=ret->toString();
ret->decRef();
return retS;
}
}
return toStringImpl();
}
TRISTATE ASObject::isLess(ASObject* r)
{
check();
multiname valueOfName;
valueOfName.name_type=multiname::NAME_STRING;
valueOfName.name_s="valueOf";
valueOfName.ns.push_back(nsNameAndKind("",NAMESPACE));
if(hasPropertyByMultiname(valueOfName))
{
if(r->hasPropertyByMultiname(valueOfName)==false)
throw RunTimeException("Missing valueof for second operand");
ASObject* obj1=getVariableByMultiname(valueOfName);
ASObject* obj2=r->getVariableByMultiname(valueOfName);
assert_and_throw(obj1!=NULL && obj2!=NULL);
assert_and_throw(obj1->getObjectType()==T_FUNCTION && obj2->getObjectType()==T_FUNCTION);
IFunction* f1=static_cast<IFunction*>(obj1);
IFunction* f2=static_cast<IFunction*>(obj2);
incRef();
ASObject* ret1=f1->call(this,NULL,0);
r->incRef();
ASObject* ret2=f2->call(r,NULL,0);
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 TFALSE;
}
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::sinit(Class_base* c)
{
}
void ASObject::buildTraits(ASObject* o)
{
if(o->getActualPrototype()->class_name.name!="Object")
LOG(LOG_NOT_IMPLEMENTED,_("Add buildTraits for class ") << o->getActualPrototype()->class_name);
}
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;
multiname equalsName;
equalsName.name_type=multiname::NAME_STRING;
equalsName.name_s="equals";
equalsName.ns.push_back(nsNameAndKind("",NAMESPACE));
if(hasPropertyByMultiname(equalsName))
{
ASObject* func_equals=getVariableByMultiname(equalsName);
assert_and_throw(func_equals!=NULL);
assert_and_throw(func_equals->getObjectType()==T_FUNCTION);
IFunction* func=static_cast<IFunction*>(func_equals);
incRef();
r->incRef();
ASObject* ret=func->call(this,&r,1);
assert_and_throw(ret->getObjectType()==T_BOOLEAN);
LOG(LOG_CALLS,_("Overloaded isEqual"));
return Boolean_concrete(ret);
}
//We can try to call valueOf and compare that
multiname valueOfName;
valueOfName.name_type=multiname::NAME_STRING;
valueOfName.name_s="valueOf";
valueOfName.ns.push_back(nsNameAndKind("",NAMESPACE));
if(hasPropertyByMultiname(valueOfName))
{
if(r->hasPropertyByMultiname(valueOfName)==false)
throw RunTimeException("Not handled less comparison for objects");
ASObject* obj1=getVariableByMultiname(valueOfName);
ASObject* obj2=r->getVariableByMultiname(valueOfName);
assert_and_throw(obj1!=NULL && obj2!=NULL);
assert_and_throw(obj1->getObjectType()==T_FUNCTION && obj2->getObjectType()==T_FUNCTION);
IFunction* f1=static_cast<IFunction*>(obj1);
IFunction* f2=static_cast<IFunction*>(obj2);
incRef();
ASObject* ret1=f1->call(this,NULL,0);
r->incRef();
ASObject* ret2=f2->call(r,NULL,0);
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 nsNameAndKind& ns, bool create, bool borrowedMode)
{
const var_iterator ret_begin=Variables.lower_bound(n);
//This actually look for the first different name, if we accept also previous levels
//Otherwise we are just doing equal_range
const var_iterator ret_end=Variables.upper_bound(n);
var_iterator ret=ret_begin;
for(;ret!=ret_end;++ret)
{
if(ret->second.kind==BORROWED_TRAIT && !borrowedMode)
continue;
else if(ret->second.kind==OWNED_TRAIT && borrowedMode)
continue;
if(ret->second.ns==ns)
return &ret->second.var;
}
//Name not present, insert it if we have to create it
if(create)
{
//borrowedMode is used to create borrowed traits
var_iterator inserted=Variables.insert(ret_begin,make_pair(n, variable(ns, (borrowedMode)?BORROWED_TRAIT:OWNED_TRAIT) ) );
return &inserted->second.var;
}
else
return NULL;
}
bool ASObject::hasPropertyByMultiname(const multiname& name)
{
check();
//We look in all the object's levels
bool ret=(Variables.findObjVar(name, false, false)!=NULL);
if(!ret) //Ask the prototype chain for borrowed traits
{
Class_base* cur=prototype;
while(cur)
{
ret=(cur->Variables.findObjVar(name, false, true)!=NULL);
if(ret)
break;
cur=cur->super;
}
}
//Must not ask for non borrowed traits as static class member are not valid
return ret;
}
void ASObject::setMethodByQName(const tiny_string& name, const nsNameAndKind& ns, IFunction* o, bool isBorrowed)
{
check();
#ifndef NDEBUG
assert(!initialized);
#endif
//assert(getObjectType()==T_CLASS);
obj_var* obj=Variables.findObjVar(name,ns,true,isBorrowed);
if(obj->var!=NULL)
{
//This happens when interfaces are declared multiple times
assert_and_throw(o==obj->var);
return;
}
obj->var=o;
}
void ASObject::setMethodByQName(const tiny_string& name, const tiny_string& ns, IFunction* o, bool isBorrowed)
{
setMethodByQName(name, nsNameAndKind(ns, NAMESPACE), o, isBorrowed);
}
void ASObject::setGetterByQName(const tiny_string& name, const nsNameAndKind& ns, IFunction* o, bool isBorrowed)
{
check();
#ifndef NDEBUG
assert(!initialized);
#endif
assert(getObjectType()==T_CLASS);
obj_var* obj=Variables.findObjVar(name,ns,true,isBorrowed);
if(obj->getter!=NULL)
{
//This happens when interfaces are declared multiple times
assert_and_throw(o==obj->getter);
return;
}
obj->getter=o;
}
void ASObject::setGetterByQName(const tiny_string& name, const tiny_string& ns, IFunction* o, bool isBorrowed)
{
setGetterByQName(name, nsNameAndKind(ns, NAMESPACE), o, isBorrowed);
}
void ASObject::setSetterByQName(const tiny_string& name, const nsNameAndKind& ns, IFunction* o, bool isBorrowed)
{
check();
#ifndef NDEBUG
assert_and_throw(!initialized);
#endif
assert(getObjectType()==T_CLASS);
obj_var* obj=Variables.findObjVar(name,ns,true,isBorrowed);
if(obj->setter!=NULL)
{
//This happens when interfaces are declared multiple times
assert_and_throw(o==obj->setter);
return;
}
obj->setter=o;
}
void ASObject::setSetterByQName(const tiny_string& name, const tiny_string& ns, IFunction* o, bool isBorrowed)
{
setSetterByQName(name, nsNameAndKind(ns, NAMESPACE), o, isBorrowed);
}
void ASObject::deleteVariableByMultiname(const multiname& name)
{
assert_and_throw(ref_count>0);
//Do not ask for borrowed traits in the protoype chain as those are undeletable
obj_var* obj=Variables.findObjVar(name,false,false);
if(obj==NULL)
return;
//Now dereference the values
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);
}
//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, bool borrowedMode)
{
obj_var* ret=Variables.findObjVar(name,false,borrowedMode);
if(ret)
{
//It seems valid for a class to redefine only the getter, so if we can't find
//something to get, it's ok
if(!(ret->setter || ret->var))
ret=NULL;
}
return ret;
}
void ASObject::setVariableByMultiname(const multiname& name, ASObject* o, ASObject* base)
{
check();
//NOTE: we assume that [gs]etSuper and [sg]etProperty correctly manipulate the cur_level (for getActualPrototype)
obj_var* obj=findSettable(name, false);
if(obj==NULL && prototype)
{
//Look for borrowed traits before
Class_base* cur=getActualPrototype();
while(cur)
{
//TODO: should be only findSetter
obj=cur->findSettable(name,true);
if(obj)
break;
cur=cur->super;
}
if(obj==NULL)
{
cur=getActualPrototype();
while(cur)
{
//TODO: should be only findSetter
obj=cur->findSettable(name, false);
if(obj)
break;
cur=cur->super;
}
}
}
if(obj==NULL)
obj=Variables.findObjVar(name,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;
//One argument can be passed without creating an array
ASObject* target=(base)?base:this;
target->incRef();
ASObject* ret=setter->call(target,&o,1);
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)
{
//TODO: what about BORROWED traits
const nsNameAndKind tmpns(ns, NAMESPACE);
//NOTE: we assume that [gs]etSuper and setProperty correctly manipulate the cur_level
obj_var* obj=Variables.findObjVar(name,tmpns,false,false);
if(obj==NULL)
obj=Variables.findObjVar(name,tmpns,true,false);
if(obj->setter)
{
//Call the setter
LOG(LOG_CALLS,_("Calling the setter"));
IFunction* setter=obj->setter;
incRef();
//One argument can be passed without creating an array
ASObject* ret=setter->call(this,&o,1);
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)
{
tiny_string name;
switch(mname.name_type)
{
case multiname::NAME_INT:
name=tiny_string(mname.name_i);
break;
case multiname::NAME_NUMBER:
name=tiny_string(mname.name_d);
break;
case multiname::NAME_STRING:
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 nsNameAndKind& ns=mname.ns[i];
var_iterator start=ret.first;
for(;start!=ret.second;++start)
{
if(start->second.ns==ns)
{
Variables.erase(start);
return;
}
}
}
throw RunTimeException("Variable to kill not found");
}
obj_var* variables_map::findObjVar(const multiname& mname, bool create, bool borrowedMode)
{
tiny_string name;
switch(mname.name_type)
{
case multiname::NAME_INT:
name=tiny_string(mname.name_i);
break;
case multiname::NAME_NUMBER:
name=tiny_string(mname.name_d);
break;
case multiname::NAME_STRING:
name=mname.name_s;
break;
case multiname::NAME_OBJECT:
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
const var_iterator ret_end=Variables.upper_bound(name);
assert_and_throw(!mname.ns.empty());
var_iterator ret=ret_begin;
for(;ret!=ret_end;++ret)
{
if(ret->second.kind==BORROWED_TRAIT && !borrowedMode)
continue;
else if(ret->second.kind==OWNED_TRAIT && borrowedMode)
continue;
//Check if one the namespace is already present
//We can use binary search, as the namespace are ordered
if(binary_search(mname.ns.begin(),mname.ns.end(),ret->second.ns))
return &ret->second.var;
}
//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,
variable(nsNameAndKind("",NAMESPACE), (borrowedMode)?BORROWED_TRAIT:OWNED_TRAIT)));
return &inserted->second.var;
}
var_iterator inserted=Variables.insert(ret,make_pair(name, variable(mname.ns[0], (borrowedMode)?BORROWED_TRAIT:OWNED_TRAIT)));
return &inserted->second.var;
}
else
return NULL;
}
ASFUNCTIONBODY(ASObject,generator)
{
//By default we assume it's a passtrough cast
assert_and_throw(argslen==1);
LOG(LOG_CALLS,_("Passthrough of ") << args[0]);
args[0]->incRef();
return args[0];
}
ASFUNCTIONBODY(ASObject,_toString)
{
return Class<ASString>::getInstanceS(obj->toStringImpl());
}
ASFUNCTIONBODY(ASObject,hasOwnProperty)
{
assert_and_throw(argslen==1);
multiname name;
name.name_type=multiname::NAME_STRING;
name.name_s=args[0]->toString();
name.ns.push_back(nsNameAndKind("",NAMESPACE));
bool ret=obj->hasPropertyByMultiname(name);
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 multiname& name)
{
//Should be correct to use the level on the prototype chain
#ifndef NDEBUG
assert(!initialized);
#endif
Variables.initSlot(n,name.name_s,name.ns[0]);
}
//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);
assert_and_throw(ret);
return ret->toInt();
}
obj_var* ASObject::findGettable(const multiname& name)
{
obj_var* ret=Variables.findObjVar(name,false,false);
if(ret)
{
//It seems valid for a class to redefine only the setter, so if we can't find
//something to get, it's ok
if(!(ret->getter || ret->var))
ret=NULL;
}
return ret;
}
ASObject* ASObject::getVariableByMultiname(const multiname& name, bool skip_impl, ASObject* base)
{
check();
obj_var* obj=findGettable(name);
if(obj!=NULL)
{
if(obj->getter)
{
//Call the getter
ASObject* target=(base)?base:this;
if(target->prototype)
{
LOG(LOG_CALLS,_("Calling the getter on type ") << target->prototype->class_name);
}
else
{
LOG(LOG_CALLS,_("Calling the getter"));
}
IFunction* getter=obj->getter;
target->incRef();
ASObject* ret=getter->call(target,NULL,0);
LOG(LOG_CALLS,_("End of getter"));
assert_and_throw(ret);
//The returned value is already owned by the caller
ret->fake_decRef();
return ret;
}
else
{
assert_and_throw(!obj->setter);
assert_and_throw(obj->var);
return obj->var;
}
}
else if(prototype && getActualPrototype())
{
//First of all see if the prototype chain contains some borrowed properties
ASObject* ret=getActualPrototype()->getBorrowedVariableByMultiname(name,skip_impl,this);
//If it has not been found yet, ask the prototype
if(!ret)
ret=getActualPrototype()->getVariableByMultiname(name,skip_impl,this);
return ret;
}
//If it has not been found
return NULL;
}
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==next->first && it->second.ns==next->second.ns)
{
if(it->second.var.var==NULL && next->second.var.var==NULL)
continue;
if(it->second.var.var==NULL || next->second.var.var==NULL)
{
cout << it->first << endl;
cout << it->second.var.var << ' ' << it->second.var.setter << ' ' << it->second.var.getter << endl;
cout << next->second.var.var << ' ' << next->second.var.setter << ' ' << next->second.var.getter << endl;
abort();
}
if(it->second.var.var->getObjectType()!=T_FUNCTION || next->second.var.var->getObjectType()!=T_FUNCTION)
{
cout << it->first << endl;
abort();
}
}
}
#endif
}
void variables_map::dumpVariables()
{
var_iterator it=Variables.begin();
for(;it!=Variables.end();++it)
LOG(LOG_NO_INFO, ((it->second.kind==OWNED_TRAIT)?"O: ":"B: ") << '[' << it->second.ns.name << "] "<< it->first << ' ' <<
it->second.var.var << ' ' << it->second.var.setter << ' ' << it->second.var.getter);
}
variables_map::~variables_map()
{
destroyContents();
}
void variables_map::destroyContents()
{
if(sys->finalizingDestruction) //Objects are being destroyed by the relative classes
return;
var_iterator it=Variables.begin();
for(;it!=Variables.end();++it)
{
if(it->second.var.var)
it->second.var.var->decRef();
if(it->second.var.setter)
it->second.var.setter->decRef();
if(it->second.var.getter)
it->second.var.getter->decRef();
}
Variables.clear();
}
ASObject::ASObject(Manager* m):type(T_OBJECT),ref_count(1),manager(m),cur_level(0),prototype(NULL),implEnable(true)
{
#ifndef NDEBUG
//Stuff only used in debugging
initialized=false;
#endif
}
ASObject::ASObject(const ASObject& o):type(o.type),ref_count(1),manager(NULL),cur_level(0),prototype(o.prototype),implEnable(true)
{
if(prototype)
{
prototype->incRef();
cur_level=prototype->max_level;
}
#ifndef NDEBUG
//Stuff only used in debugging
initialized=false;
#endif
assert_and_throw(o.Variables.size()==0);
}
void ASObject::setPrototype(Class_base* c)
{
if(prototype)
{
prototype->abandonObject(this);
prototype->decRef();
}
prototype=c;
if(prototype)
{
prototype->acquireObject(this);
prototype->incRef();
}
}
ASObject::~ASObject()
{
if(prototype && !sys->finalizingDestruction)
{
prototype->decRef();
prototype->abandonObject(this);
}
}
int ASObject::_maxlevel()
{
return (prototype)?(prototype->max_level):0;
}
void ASObject::resetLevel()
{
cur_level=_maxlevel();
}
Class_base* ASObject::getActualPrototype() const
{
Class_base* ret=prototype;
if(ret==NULL)
{
assert(type==T_CLASS);
return NULL;
}
for(int i=prototype->max_level;i>cur_level;i--)
ret=ret->super;
assert(ret);
assert(ret->max_level==cur_level);
return ret;
}
void variables_map::initSlot(unsigned int n, const tiny_string& name, const nsNameAndKind& ns)
{
if(n>slots_vars.size())
slots_vars.resize(n,Variables.end());
pair<var_iterator, var_iterator> ret=Variables.equal_range(name);
if(ret.first!=ret.second)
{
//Check if this namespace is already present
var_iterator start=ret.first;
for(;start!=ret.second;++start)
{
if(start->second.ns==ns)
{
slots_vars[n-1]=start;
return;
}
}
}
//Name not present, no good
throw RunTimeException("initSlot on missing variable");
}
void variables_map::setSlot(unsigned int n,ASObject* o)
{
if(n-1<slots_vars.size())
{
assert_and_throw(slots_vars[n-1]!=Variables.end());
if(slots_vars[n-1]->second.var.setter)
throw UnsupportedException("setSlot has setters");
slots_vars[n-1]->second.var.var->decRef();
slots_vars[n-1]->second.var.var=o;
}
else
throw RunTimeException("setSlot out of bounds");
}
obj_var* variables_map::getValueAt(unsigned int index)
{
//TODO: CHECK behaviour on overridden methods
if(index<Variables.size())
{
var_iterator it=Variables.begin();
for(unsigned int i=0;i<index;i++)
++it;
return &it->second.var;
}
else
throw RunTimeException("getValueAt out of bounds");
}
ASObject* ASObject::getValueAt(int index)
{
obj_var* obj=Variables.getValueAt(index);
assert_and_throw(obj);
ASObject* ret;
if(obj->getter)
{
//Call the getter
LOG(LOG_CALLS,_("Calling the getter"));
IFunction* getter=obj->getter;
incRef();
ret=getter->call(this,NULL,0);
ret->fake_decRef();
LOG(LOG_CALLS,_("End of getter"));
}
else
ret=obj->var;
return ret;
}
tiny_string variables_map::getNameAt(unsigned int index)
{
//TODO: CHECK behaviour on overridden methods
if(index<Variables.size())
{
var_iterator it=Variables.begin();
for(unsigned int i=0;i<index;i++)
++it;
return it->first;
}
else
throw RunTimeException("getNameAt out of bounds");
}
unsigned int ASObject::numVariables()
{
return Variables.size();
}