2 #include "pcheader.hpp"
6 #include "userdata.hpp"
7 #include "funcproto.hpp"
14 VXWeakRefObj
*VXRefCountedObj::GetWeakRef(VXOType type
)
18 vox_new(_weakref
,VXWeakRefObj
);
19 _weakref
->_obj
._type
= type
;
20 _weakref
->_obj
._unVal
.pRefCounted
= this;
25 VXRefCountedObj::~VXRefCountedObj()
29 _weakref
->_obj
._type
= VX_OT_NULL
;
30 _weakref
->_obj
._unVal
.pRefCounted
= NULL
;
34 void VXWeakRefObj::Release()
36 if(ISREFCOUNTED(_obj
._type
))
38 _obj
._unVal
.pRefCounted
->_weakref
= NULL
;
40 vox_delete(this,VXWeakRefObj
);
43 bool VXDelegableObj::GetMetaMethod(VXState
*v
,VXMetaMethod mm
,VXObject
&res
)
47 return _delegate
->Get((*_ss(v
)->_metamethods
)[mm
],res
);
52 bool VXDelegableObj::SetDelegate(VXTableObj
*mt
)
54 VXTableObj
*temp
= mt
;
55 if(temp
== this) return false;
59 if (temp
->_delegate
== this) return false;
60 temp
= temp
->_delegate
;
62 if (mt
) __ObjAddRef(mt
);
63 __ObjRelease(_delegate
);
68 bool VXGeneratorObj::Yield(VXState
*v
,VXInteger target
)
70 if(_state
==eSuspended
)
72 v
->ThrowError("internal vm error, yielding dead generator");
77 v
->ThrowError("internal vm error, yielding a dead generator");
80 VXInteger size
= ((v
->_top
) - (v
->_stackbase
));
82 VXRawObj _this
= v
->_stack
[v
->_stackbase
];
83 _stack
.values()[0] = (
84 ISREFCOUNTED(type(_this
)) ?
85 VXObject(_refcounted(_this
)->GetWeakRef(type(_this
))) :
87 for(VXInteger n
=1; n
<target
; n
++)
89 _stack
.values()[n
] = v
->_stack
[v
->_stackbase
+n
];
91 for(VXInteger j
=0; j
< size
; j
++)
93 v
->_stack
[v
->_stackbase
+j
].Null();
98 for(VXInteger i
=0;i
<_ci
._etraps
;i
++)
100 _etraps
.push_back(v
->_etraps
.top());
101 v
->_etraps
.pop_back();
107 bool VXGeneratorObj::Resume(VXState
*v
,VXObject
&dest
)
111 v
->ThrowError("resuming dead generator");
116 v
->ThrowError("resuming active generator");
119 VXInteger size
= _stack
.size();
120 VXInteger target
= &dest
- &(v
->_stack
.values()[v
->_stackbase
]);
121 vox_assert(target
>=0 && target
<=255);
122 if(!v
->EnterFrame(v
->_top
, v
->_top
+ size
, false))
126 v
->ci
->_generator
= this;
127 v
->ci
->_target
= (VXInt32
)target
;
128 v
->ci
->_closure
= _ci
._closure
;
129 v
->ci
->_ip
= _ci
._ip
;
130 v
->ci
->_literals
= _ci
._literals
;
131 v
->ci
->_ncalls
= _ci
._ncalls
;
132 v
->ci
->_etraps
= _ci
._etraps
;
133 v
->ci
->_root
= _ci
._root
;
134 for(VXInteger i
=0;i
<_ci
._etraps
;i
++)
136 v
->_etraps
.push_back(_etraps
.top());
139 VXRawObj _this
= _stack
.values()[0];
140 v
->_stack
[v
->_stackbase
] = type(_this
) == VX_OT_WEAKREF
? _weakref(_this
)->_obj
: _this
;
142 for(VXInteger n
= 1; n
<size
; n
++)
144 v
->_stack
[v
->_stackbase
+n
] = _stack
.values()[n
];
145 _stack
.values()[n
].Null();
151 v
->CallDebugHook('c');
157 void VXInstanceObj::Mark(VXCollectable
**chain
)
161 VXUnsignedInteger nvalues
= _class
->_defaultvalues
.size();
162 for(VXUnsignedInteger i
=0; i
< nvalues
; i
++)
164 VXSharedState::MarkObject(_values
[i
], chain
);
169 void VXGeneratorObj::Mark(VXCollectable
**chain
)
172 for(VXUnsignedInteger i
= 0; i
< _stack
.size(); i
++)
174 VXSharedState::MarkObject(_stack
[i
], chain
);
176 VXSharedState::MarkObject(_closure
, chain
);
182 void VXOuterObj::Mark(VXCollectable
**chain
)
185 /* If the valptr points to a closed value, that value is alive */
186 if(_valptr
== &_value
)
188 VXSharedState::MarkObject(_value
, chain
);
193 void VXUserDataObj::Mark(VXCollectable
**chain
)
198 _delegate
->Mark(chain
);
203 void VXCollectable::UnMark()
210 void VXObject::Null()
212 __Release(_type
,_unVal
);
214 _unVal
.raw
= (VXRawValue
)NULL
;
217 bool VXObject::IsNull() const
219 return (_unVal
.raw
== (VXRawValue
)NULL
);
222 bool VXObject::IsNumeric() const
224 return (Type() & VXOBJECT_NUMERIC
);
229 * Object instance access methods
232 VXInteger
VXObject::Integer()
234 return _unVal
.nInteger
;
237 VXInteger
VXObject::Integer() const
239 return _unVal
.nInteger
;
242 VXFloat
VXObject::Float()
244 return _unVal
.fFloat
;
247 VXFloat
VXObject::Float() const
249 return _unVal
.fFloat
;
252 VXStringObj
* VXObject::String()
254 return _unVal
.pString
;
258 VXStringObj
* VXObject::String() const
260 return _unVal
.pString
;
263 void VXObject::String(const char** dest
, VXInteger
* len_dest
) const
265 VXStringObj
* ob
= this->String();
269 (*len_dest
) = ob
->_len
;
273 VXTableObj
* VXObject::Table()
275 return _unVal
.pTable
;
278 VXTableObj
* VXObject::Table() const
280 return _unVal
.pTable
;
283 VXArrayObj
* VXObject::Array()
285 return _unVal
.pArray
;
288 VXArrayObj
* VXObject::Array() const
290 return _unVal
.pArray
;
292 VXForeignClosureObj
* VXObject::Closure()
294 return _unVal
.pClosure
;
297 VXGeneratorObj
* VXObject::Generator()
299 return _unVal
.pGenerator
;
302 VXNativeClosureObj
* VXObject::NativeClosure()
304 return _unVal
.pNativeClosure
;
307 VXUserDataObj
* VXObject::UserData()
309 return _unVal
.pUserData
;
312 VXUserPointer
VXObject::UserPointer()
314 return _unVal
.pUserPointer
;
317 VXState
* VXObject::Thread()
319 return _unVal
.pThread
;
322 VXFuncProtoObj
* VXObject::FuncProto()
324 return _unVal
.pFunctionProto
;
327 VXClassObj
* VXObject::Class()
329 return _unVal
.pClass
;
332 VXInstanceObj
* VXObject::Instance()
334 return _unVal
.pInstance
;
337 VXDelegableObj
* VXObject::Delegable()
339 return (VXDelegableObj
*)(_unVal
.pDelegable
);
342 VXWeakRefObj
* VXObject::WeakRef()
344 return _unVal
.pWeakRef
;
347 VXOuterObj
* VXObject::Outer()
349 return _unVal
.pOuter
;
352 VXRefCountedObj
* VXObject::RefCounted()
354 return _unVal
.pRefCounted
;
357 VXRefCountedObj
* VXObject::RefCounted() const
359 return _unVal
.pRefCounted
;
362 VXRawValue
VXObject::Raw()
368 template<> VXFloat
VXObject::As
<VXFloat
>()
373 template<> VXInteger
VXObject::As
<VXInteger
>()
382 VXOType
VXObject::Type() const
387 const char* VXObject::TypeString() const
389 return IdType2Name(this->Type());
392 bool VXObject::Is(VXOType tp
) const
397 bool VXObject::IsTable() const
399 return Is(VX_OT_TABLE
);
402 bool VXObject::IsArray() const
404 return Is(VX_OT_ARRAY
);
407 bool VXObject::IsFunction() const
409 return Is(VX_OT_FUNCPROTO
);
411 bool VXObject::IsClosure() const
413 return Is(VX_OT_CLOSURE
);
416 bool VXObject::IsNativeClosure() const
418 return Is(VX_OT_NATIVECLOSURE
);
421 bool VXObject::IsGenerator() const
423 return Is(VX_OT_GENERATOR
);
426 bool VXObject::IsString() const
428 return Is(VX_OT_STRING
);
431 bool VXObject::IsInteger() const
433 return Is(VX_OT_INTEGER
);
436 bool VXObject::IsFloat() const
438 return Is(VX_OT_FLOAT
);
441 bool VXObject::IsUserPointer() const
443 return Is(VX_OT_USERPOINTER
);
446 bool VXObject::IsUserData() const
448 return Is(VX_OT_USERDATA
);
451 bool VXObject::IsThread() const
453 return Is(VX_OT_THREAD
);
456 bool VXObject::IsClass() const
458 return Is(VX_OT_CLASS
);
461 bool VXObject::IsInstance() const
463 return Is(VX_OT_INSTANCE
);
466 bool VXObject::IsBool() const
468 return Is(VX_OT_BOOL
);
471 bool VXObject::IsWeakRef() const
473 return Is(VX_OT_WEAKREF
);
480 VXObject::~VXObject()
482 __Release(_type
,_unVal
);
487 VX_OBJECT_RAWINIT(this)
489 _unVal
.pUserPointer
=NULL
;
491 VXObject::VXObject(const VXObject
&o
)
495 VX_ADDREF(_type
,_unVal
);
498 VXObject::VXObject(const VXRawObj
&o
)
502 VX_ADDREF(_type
,_unVal
);
506 VXObject::VXObject(bool bBool
)
508 VX_OBJECT_RAWINIT(this)
510 _unVal
.nInteger
= bBool
?1:0;
513 VXObject::VXObject (VXTableObj
* x
)
515 VX_OBJECT_RAWINIT(this)
518 vox_assert (_unVal
.pTable
);
519 _unVal
.pRefCounted
->_uiRef
++;
522 VXObject::VXObject (VXClassObj
* x
)
524 VX_OBJECT_RAWINIT(this)
527 vox_assert (_unVal
.pTable
);
528 _unVal
.pRefCounted
->_uiRef
++;
531 VXObject
& VXObject::operator= (VXClassObj
* x
)
534 VXRawObj::Value unOldVal
;
538 VX_REFOBJECT_INIT (this)
540 _unVal
.pRefCounted
->_uiRef
++;
541 __Release (tOldType
, unOldVal
);
545 VXObject::VXObject (VXInstanceObj
* x
)
547 VX_OBJECT_RAWINIT(this)
548 _type
= VX_OT_INSTANCE
;
549 _unVal
.pInstance
= x
;
550 vox_assert (_unVal
.pTable
);
551 _unVal
.pRefCounted
->_uiRef
++;
554 VXObject::VXObject(VXArrayObj
* x
)
556 VX_OBJECT_RAWINIT(this)
559 vox_assert(_unVal
.pTable
);
560 _unVal
.pRefCounted
->_uiRef
++;
565 VXObject::VXObject (VXForeignClosureObj
* x
)
567 VX_OBJECT_RAWINIT(this)
568 _type
= VX_OT_CLOSURE
;
570 vox_assert (_unVal
.pTable
);
571 _unVal
.pRefCounted
->_uiRef
++;
576 VXObject::VXObject (VXNativeClosureObj
* x
)
578 VX_OBJECT_RAWINIT(this)
579 _type
= VX_OT_NATIVECLOSURE
;
580 _unVal
.pNativeClosure
= x
;
581 vox_assert (_unVal
.pTable
);
582 _unVal
.pRefCounted
->_uiRef
++;
587 VXObject::VXObject (VXOuterObj
* x
)
589 VX_OBJECT_RAWINIT(this)
592 vox_assert (_unVal
.pTable
);
593 _unVal
.pRefCounted
->_uiRef
++;
597 VXObject::VXObject (VXGeneratorObj
* x
)
599 VX_OBJECT_RAWINIT(this)
600 _type
= VX_OT_GENERATOR
;
601 _unVal
.pGenerator
= x
;
602 vox_assert (_unVal
.pTable
);
603 _unVal
.pRefCounted
->_uiRef
++;
606 VXObject::VXObject (VXStringObj
* x
)
608 VX_OBJECT_RAWINIT(this)
609 _type
= VX_OT_STRING
;
611 vox_assert (_unVal
.pTable
);
612 _unVal
.pRefCounted
->_uiRef
++;
615 VXObject::VXObject (VXUserDataObj
* x
)
617 VX_OBJECT_RAWINIT(this)
618 _type
= VX_OT_USERDATA
;
619 _unVal
.pUserData
= x
;
620 vox_assert (_unVal
.pTable
);
621 _unVal
.pRefCounted
->_uiRef
++;
625 VXObject::VXObject (VXWeakRefObj
* x
)
627 VX_OBJECT_RAWINIT(this)
628 _type
= VX_OT_WEAKREF
;
630 vox_assert (_unVal
.pTable
);
631 _unVal
.pRefCounted
->_uiRef
++;
635 VXObject::VXObject (VXState
* x
)
637 VX_OBJECT_RAWINIT(this)
638 _type
= VX_OT_THREAD
;
640 vox_assert (_unVal
.pTable
);
641 _unVal
.pRefCounted
->_uiRef
++;
645 VXObject::VXObject (VXFuncProtoObj
* x
)
647 VX_OBJECT_RAWINIT(this)
648 _unVal
.pFunctionProto
= x
;
649 _type
= VX_OT_FUNCPROTO
;
650 vox_assert (_unVal
.pTable
);
651 _unVal
.pRefCounted
->_uiRef
++;
654 VXObject::VXObject(VXInteger x
)
656 VX_OBJECT_RAWINIT(this)
657 _type
= VX_OT_INTEGER
;
661 VXObject::VXObject (VXFloat x
)
663 VX_OBJECT_RAWINIT(this)
668 VXObject::VXObject (VXUserPointer x
)
670 VX_OBJECT_RAWINIT(this)
671 _type
= VX_OT_USERPOINTER
;
672 _unVal
.pUserPointer
= x
;
676 * operator=() methods
679 VXObject
& VXObject::operator= (VXGeneratorObj
* x
)
682 VXRawObj::Value unOldVal
;
685 _type
= VX_OT_GENERATOR
;
686 VX_REFOBJECT_INIT (this)
687 _unVal
.pGenerator
= x
;
688 _unVal
.pRefCounted
->_uiRef
++;
689 __Release (tOldType
, unOldVal
);
693 VXObject
& VXObject::operator= (VXOuterObj
* x
)
696 VXRawObj::Value unOldVal
;
700 VX_REFOBJECT_INIT (this)
702 _unVal
.pRefCounted
->_uiRef
++;
703 __Release (tOldType
, unOldVal
);
708 VXObject
& VXObject::operator= (VXNativeClosureObj
* x
)
711 VXRawObj::Value unOldVal
;
714 _type
= VX_OT_NATIVECLOSURE
;
715 VX_REFOBJECT_INIT (this)
716 _unVal
.pNativeClosure
= x
;
717 _unVal
.pRefCounted
->_uiRef
++;
718 __Release (tOldType
, unOldVal
);
722 VXObject
& VXObject::operator= (VXForeignClosureObj
* x
)
725 VXRawObj::Value unOldVal
;
728 _type
= VX_OT_CLOSURE
;
729 VX_REFOBJECT_INIT (this)
731 _unVal
.pRefCounted
->_uiRef
++;
732 __Release (tOldType
, unOldVal
);
736 VXObject
& VXObject::operator= (VXArrayObj
* x
)
739 VXRawObj::Value unOldVal
;
743 VX_REFOBJECT_INIT(this)
745 _unVal
.pRefCounted
->_uiRef
++;
746 __Release (tOldType
, unOldVal
);
750 VXObject
& VXObject::operator= (VXInstanceObj
* x
)
753 VXRawObj::Value unOldVal
;
756 _type
= VX_OT_INSTANCE
;
757 VX_REFOBJECT_INIT (this)
758 _unVal
.pInstance
= x
;
759 _unVal
.pRefCounted
->_uiRef
++;
760 __Release (tOldType
, unOldVal
);
764 VXObject
& VXObject::operator= (VXTableObj
* x
)
767 VXRawObj::Value unOldVal
;
771 VX_REFOBJECT_INIT(this)
773 _unVal
.pRefCounted
->_uiRef
++;
774 __Release (tOldType
, unOldVal
);
778 VXObject
& VXObject::operator=(const VXObject
& obj
)
781 VXRawObj::Value unOldVal
;
786 VX_ADDREF(_type
,_unVal
);
787 __Release(tOldType
,unOldVal
);
791 VXObject
& VXObject::operator=(const VXRawObj
& obj
)
794 VXRawObj::Value unOldVal
;
799 VX_ADDREF(_type
,_unVal
);
800 __Release(tOldType
,unOldVal
);
804 VXObject
& VXObject::operator=(bool b
)
806 __Release(_type
,_unVal
);
807 VX_OBJECT_RAWINIT(this)
809 _unVal
.nInteger
= b
?1:0;
813 VXObject
& VXObject::operator= (VXStringObj
* x
)
816 VXRawObj::Value unOldVal
;
819 _type
= VX_OT_STRING
;
820 VX_REFOBJECT_INIT (this)
822 _unVal
.pRefCounted
->_uiRef
++;
823 __Release (tOldType
, unOldVal
);
827 VXObject
& VXObject::operator= (VXUserDataObj
* x
)
830 VXRawObj::Value unOldVal
;
833 _type
= VX_OT_USERDATA
;
834 VX_REFOBJECT_INIT (this)
835 _unVal
.pUserData
= x
;
836 _unVal
.pRefCounted
->_uiRef
++;
837 __Release (tOldType
, unOldVal
);
841 VXObject
& VXObject::operator= (VXWeakRefObj
* x
)
844 VXRawObj::Value unOldVal
;
847 _type
= VX_OT_WEAKREF
;
848 VX_REFOBJECT_INIT (this)
850 _unVal
.pRefCounted
->_uiRef
++;
851 __Release (tOldType
, unOldVal
);
855 VXObject
& VXObject::operator= (VXState
* x
)
858 VXRawObj::Value unOldVal
;
861 _type
= VX_OT_THREAD
;
862 VX_REFOBJECT_INIT (this)
864 _unVal
.pRefCounted
->_uiRef
++;
865 __Release (tOldType
, unOldVal
);
869 VXObject
& VXObject::operator= (VXFuncProtoObj
* x
)
872 VXRawObj::Value unOldVal
;
875 _type
= VX_OT_FUNCPROTO
;
876 VX_REFOBJECT_INIT (this)
877 _unVal
.pFunctionProto
= x
;
878 _unVal
.pRefCounted
->_uiRef
++;
879 __Release (tOldType
, unOldVal
);
884 VXObject
& VXObject::operator= (VXFloat x
)
886 __Release (_type
, _unVal
);
888 VX_OBJECT_RAWINIT(this)
893 VXObject
& VXObject::operator= (VXInteger x
)
895 __Release (_type
, _unVal
);
896 _type
= VX_OT_INTEGER
;
897 VX_OBJECT_RAWINIT(this)
902 VXObject
& VXObject::operator= (VXUserPointer x
)
904 __Release (_type
, _unVal
);
905 _type
= VX_OT_USERPOINTER
;
906 VX_OBJECT_RAWINIT(this)
907 _unVal
.pUserPointer
= x
;