bool ASObject::hasPropertyByMultiname(const multiname& name, bool considerDynamic) { //We look in all the object's levels uint32_t validTraits=DECLARED_TRAIT; if(considerDynamic) validTraits|=DYNAMIC_TRAIT; if(Variables.findObjVar(name, NO_CREATE_TRAIT, validTraits)!=NULL) return true; if(classdef && classdef->Variables.findObjVar(name, NO_CREATE_TRAIT, BORROWED_TRAIT)!=NULL) return true; //Check prototype inheritance chain if(getClass()) { ASObject* proto = getClass()->prototype.getPtr(); while(proto) { if(proto->findGettable(name, false) != NULL) return true; proto = proto->getprop_prototype(); } } //Must not ask for non borrowed traits as static class member are not valid return false; }
_NR<ASObject> ASObject::getVariableByMultiname(const multiname& name, GET_VARIABLE_OPTION opt, Class_base* cls) { check(); assert(!cls || classdef->isSubClass(cls)); //Get from the current object without considering borrowed properties variable* obj=findGettable(name, false); if(!obj && cls) { //Look for borrowed traits before obj=cls->findGettable(name,true); } if(!obj && cls) { //Check prototype chain ASObject* proto = cls->prototype.getPtr(); while(proto) { obj = proto->findGettable(name, false); if(obj) { obj->var->incRef(); return _MNR(obj->var); } proto = proto->getprop_prototype(); } } if(!obj) return NullRef; if(obj->getter) { //Call the getter ASObject* target=this; if(target->classdef) { LOG(LOG_CALLS,_("Calling the getter on type ") << target->classdef->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")); // No incRef because ret is a new instance return _MNR(ret); } else { assert_and_throw(!obj->setter); assert_and_throw(obj->var); if(obj->var->getObjectType()==T_FUNCTION && obj->var->as<IFunction>()->isMethod()) { LOG(LOG_CALLS,"Attaching this " << this << " to function " << name); //the obj reference is acquired by the smart reference this->incRef(); IFunction* f=obj->var->as<IFunction>()->bind(_MR(this),-1); //No incref is needed, as the function is a new instance return _MNR(f); } obj->var->incRef(); return _MNR(obj->var); } }
void ASObject::setVariableByMultiname(const multiname& name, ASObject* o, Class_base* cls) { check(); assert(!cls || classdef->isSubClass(cls)); //NOTE: we assume that [gs]etSuper and [sg]etProperty correctly manipulate the cur_level (for getActualClass) bool has_getter=false; variable* obj=findSettable(name, false, &has_getter); if(!obj && cls) { //Look for borrowed traits before //It's valid to override only a getter, so keep //looking for a settable even if a super class sets //has_getter to true. obj=cls->findSettable(name,true,&has_getter); } if(!obj && cls) { //Look in prototype chain ASObject* proto = cls->prototype.getPtr(); while(proto) { variable* tmp = proto->findSettable(name, false, NULL /*prototypes never have getters/setters*/); if(tmp) { if (tmp->kind != DYNAMIC_TRAIT) // dynamic prototype properties can be overridden obj = tmp; break; } proto = proto->getprop_prototype(); } } if(!obj) { if(has_getter) { tiny_string err=tiny_string("Error #1074: Illegal write to read-only property ")+name.normalizedName(); if(cls) err+=tiny_string(" on type ")+cls->getQualifiedClassName(); throw Class<ReferenceError>::getInstanceS(err); } //Create a new dynamic variable obj=Variables.findObjVar(name,DYNAMIC_TRAIT,DYNAMIC_TRAIT); } 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=this; target->incRef(); _R<ASObject> ret= _MR( setter->call(target,&o,1) ); assert_and_throw(ret->is<Undefined>()); LOG(LOG_CALLS,_("End of setter")); } else { assert_and_throw(!obj->getter); obj->setVar(o); } }