internal static LinkSystemObject createLinkObjectValue(Player player,Class cls)
 {
     if (cls.isLink_System)
     {
         ASBinCode.rtData.rtObjectBase rb; ASBinCode.rtData.rtObjectBase lr; string err;
         ASBinCode.rtti.Object         obj = createObject(player.swc,cls,null,out rb,out lr,out err);
         return((LinkSystemObject)obj);
     }
     else
     {
         throw new EngineException();
     }
 }
        private static void afterCreateInstanceDataCallBacker(BlockCallBackBase sender,object args)
        {
            InstanceCreator ic = (InstanceCreator)sender.args;

            ASBinCode.rtti.Object obj      = (ASBinCode.rtti.Object)sender.cacheObjects[0];
            RunTimeScope          objScope = (RunTimeScope)sender.cacheObjects[2];

            ASBinCode.rtData.rtObjectBase rtObject = (ASBinCode.rtData.rtObjectBase)sender.cacheObjects[1];

            ic.toNoticeFailed1 = sender;
            ic.exec_step2(obj,objScope,rtObject);
            ic.toNoticeFailed1 = null;
        }
        internal static ASBinCode.rtData.rtObjectBase createPureHostdOrLinkObject(Player player,Class cls)
        {
            if (cls.isLink_System)
            {
                ASBinCode.rtData.rtObjectBase rb; ASBinCode.rtData.rtObjectBase lr; string err;
                ASBinCode.rtti.Object         obj = createObject(player.swc,cls,null,out rb,out lr,out err);
                ASBinCode.rtData.rtObjectBase rtObj;
                if (lr != null)
                {
                    rtObj = lr;
                }
                else
                {
                    rtObj = new ASBinCode.rtData.rtObject(obj,null);
                }

                RunTimeScope scope = new RunTimeScope(
                    null
                    //player.genHeapFromCodeBlock(player.swc.blocks[cls.blockid])
                    ,cls.blockid,null,rtObj,
                    RunTimeScopeType.objectinstance);
                rtObj.objScope = scope;

                return(rtObj);
            }
            else if (cls.isUnmanaged)
            {
                ASBinCode.rtti.Object obj;
                if (cls.dynamic)
                {
                    obj = new HostedDynamicObject(cls);
                }
                else
                {
                    obj = new HostedObject(cls);
                }

                ASBinCode.rtData.rtObject rtObj = new ASBinCode.rtData.rtObject(obj,null);
                RunTimeScope scope = new RunTimeScope(
                    player.genHeapFromCodeBlock(player.swc.blocks[cls.blockid]),
                    cls.blockid,null,rtObj,RunTimeScopeType.objectinstance);
                rtObj.objScope = scope;


                return(rtObj);
            }
            else
            {
                throw new EngineException();
            }
        }
        public static bool init_static_class(Class cls,Player player,SourceToken token)
        {
            if (!player.static_instance.ContainsKey(cls.staticClass.classid))
            {
                int f = player.getRuntimeStackFlag();


                ASBinCode.RunTimeScope objScope;
                ASBinCode.rtti.Object  obj = makeObj(
                    player,token,
                    cls.staticClass,
                    null,null,out objScope).value;

                player.static_instance.Add(cls.staticClass.classid,
                                           new ASBinCode.rtData.rtObject(obj,objScope));

                if (cls.super != null)
                {
                    bool s = init_static_class(cls.super,player,token);

                    if (s)
                    {
                        ((DynamicObject)obj)._prototype_ = (DynamicObject)(player.static_instance[cls.super.staticClass.classid]).value;

                        bool result = player.step_toStackflag(f);

                        if (cls.classid != 2)
                        {
                            ((DynamicObject)((ASBinCode.rtData.rtObjectBase)obj.memberData[0].getValue()).value)["constructor"].directSet(player.static_instance[cls.staticClass.classid]);
                        }

                        return(result);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    bool result = player.step_toStackflag(f);

                    return(result);
                }
            }
            else
            {
                return(true);
            }
        }
        private static ASBinCode.rtData.rtObjectBase makeObj(
            Player player,
            SourceToken token,
            ASBinCode.rtti.Class cls,
            BlockCallBackBase callbacker,InstanceCreator creator,out ASBinCode.RunTimeScope objScope)
        {
            ASBinCode.rtData.rtObjectBase result; ASBinCode.rtData.rtObjectBase lr; string err;

            ASBinCode.rtti.Object obj = createObject(player.swc,cls,creator,out result,out lr,out err);

            if (obj == null)
            {
                objScope = null;
                if (callbacker != null)
                {
                    callbacker.noticeRunFailed();
                }
                player.throwWhenMakeObjFailed(new ASRunTimeException(err,player.stackTrace(0)));

                return(null);
            }

            if (result == null)
            {
                if (lr == null)
                {
                    result = new ASBinCode.rtData.rtObject(obj,null);
                }
                else
                {
                    result = lr;
                }
                if (cls.fields.Count > 0)
                {
                    obj.memberData = new ObjectMemberSlot[cls.fields.Count];
                    for (int i = 0; i < obj.memberData.Length; i++)
                    {
                        obj.memberData[i] = new ObjectMemberSlot(result,player.swc.FunctionClass.getRtType(),cls.fields[i].valueType,player.swc);

                        if (cls.fields[i].defaultValue == null)
                        {
                            obj.memberData[i].directSet(TypeConverter.getDefaultValue(cls.fields[i].valueType).getValue(null,null));
                        }

                        ((ObjectMemberSlot)obj.memberData[i]).isConstMember = cls.fields[i].isConst;
                    }
                }
                else
                {
                    obj.memberData = blankFields;
                }


                ASBinCode.CodeBlock codeblock = player.swc.blocks[cls.blockid];

                objScope = player.callBlock(codeblock,
                                            (ObjectMemberSlot[])obj.memberData,null,
                                            null
                                            ,token,callbacker
                                            ,
                                            result,RunTimeScopeType.objectinstance
                                            );

                result.objScope = objScope;

                if (objScope == null)
                {
                    if (callbacker != null)
                    {
                        callbacker.noticeRunFailed();
                    }

                    return(null);
                }

                //***把父类的初始化函数推到栈上去***
                var ss = cls.super;
                while (ss != null)
                {
                    var scope = player.callBlock(player.swc.blocks[ss.blockid],
                                                 (ObjectMemberSlot[])obj.memberData,
                                                 null,null,token,null,result,RunTimeScopeType.objectinstance
                                                 );

                    ss = ss.super;

                    if (scope == null)
                    {
                        if (callbacker != null)
                        {
                            callbacker.noticeRunFailed();
                        }

                        return(null);
                    }
                }
            }
            else
            {
                objScope = result.objScope;
                player.CallBlankBlock(callbacker);
            }

            if (callbacker != null)
            {
                //callbacker.obj = obj;
                //callbacker.rtObject = result;
                //callbacker.objScope = objScope;

                callbacker.cacheObjects[0] = obj;
                callbacker.cacheObjects[1] = result;
                callbacker.cacheObjects[2] = objScope;
            }

            return(result);
        }
        private static ASBinCode.rtti.Object createObject(CSWC swc,Class cls,InstanceCreator creator,
                                                          out ASBinCode.rtData.rtObjectBase rtObjectBase,
                                                          out ASBinCode.rtData.rtObjectBase linkrtobj,
                                                          out string errinfo
                                                          )
        {
            ASBinCode.rtti.Object obj = null;// = new ASBinCode.rtti.Object(cls);
            rtObjectBase = null; linkrtobj = null; errinfo = null;
            if (cls.isLink_System)
            {
                if (creator != null)
                {
                    StackSlot stackSlot = creator.objectStoreToSlot as StackSlot;
                    if (stackSlot != null)
                    {
                        rtObjectBase = stackSlot.getStackCacheObject(cls);
                        return(rtObjectBase.value);
                    }
                }


                var func = (NativeFunctionBase)swc.class_Creator[cls];

                string err; int no;
                ASBinCode.rtData.rtObjectBase rtObj =
                    func.execute(null,null,cls,out err,out no) as ASBinCode.rtData.rtObjectBase;
                linkrtobj = rtObj;
                if (rtObj == null)
                {
                    errinfo = cls.ToString() + " create linksystem object failed";
                    return(null);
                }
                else
                {
                    return(rtObj.value);
                }
            }
            else if (cls.isCrossExtend)
            {
                var scls = cls.super;
                while (!scls.isLink_System)
                {
                    scls = scls.super;
                }

                var cextend = scls.staticClass.linkObjCreator;
                var func    = swc.getNativeFunction(((ClassMethodGetter)cextend.bindField).functionId);

                if (func == null)
                {
                    errinfo = cls.ToString() + " create crossextend object failed, creator function not found";
                    return(null);
                }

                string err; int no;
                ASBinCode.rtData.rtObjectBase rtObj =
                    func.execute(null,null,cls,out err,out no) as ASBinCode.rtData.rtObjectBase;
                linkrtobj = rtObj;
                if (rtObj == null)
                {
                    errinfo = cls.ToString() + " create crossextend object failed";
                    return(null);
                }
                else
                {
                    LinkSystemObject lo = (LinkSystemObject)rtObj.value;
                    return(lo);
                }
            }
            else if (
                swc.DictionaryClass != null
                &&
                ClassMemberFinder.isInherits(cls,swc.DictionaryClass))
            {
                obj = new DictionaryObject(cls);
            }
            else if (cls.dynamic)
            {
                if (cls.isUnmanaged)
                {
                    obj = new HostedDynamicObject(cls);
                }
                else
                {
                    obj = new DynamicObject(cls);
                }
            }
            else if (cls.isUnmanaged)
            {
                obj = new HostedObject(cls);
            }
            else
            {
                obj = new ASBinCode.rtti.Object(cls);
            }

            return(obj);
        }
        private void exec_step2(
            ASBinCode.rtti.Object obj,
            RunTimeScope objScope,ASBinCode.rtData.rtObjectBase _object)
        {
            //***添加Object的动态对象****
            if (
                //(obj._class.classid == 0)
                obj is DynamicObject &&
                obj._class.staticClass != null)
            {
                DynamicObject dobj = (DynamicObject)obj;

                if (constructor == null)
                {
                    dobj.createproperty("constructor",new DynamicPropertySlot(_object,false,player.swc.FunctionClass.getRtType()));
                    dobj["constructor"].directSet(player.static_instance[obj._class.staticClass.classid]);
                    dobj._prototype_ = (DynamicObject)player.static_instance[_class.staticClass.classid].value;
                }
                else
                {
                    dobj._prototype_ = (DynamicObject)constructor.value;
                }
            }

            ClassMember ctor = obj._class.constructor;

            //if (obj._class.isCrossExtend)
            //{
            //	//***创建Adapter***
            //	var scls = obj._class.super;
            //	while (!scls.isLink_System)
            //	{
            //		scls = scls.super;
            //	}

            //	ctor = scls.crossExtendAdapterCreator;

            //	var nf = player.swc.getNativeFunction(((MethodGetterBase)ctor.bindField).functionId);
            //	if (!(nf is ICrossExtendAdapterCreator))
            //	{
            //		invokerFrame.throwAneException(token, "adapter不是ICrossExtendAdapterCreator");
            //		callbacker.noticeRunFailed();
            //		noticeWhenCreateAneFailed();
            //		invokerFrame.endStep();
            //		return;

            //	}

            //	constructorCaller.toCallFunc =  player.swc.functions[((MethodGetterBase)ctor.bindField).functionId];

            //}


            //调用构造函数
            if (ctor != null)
            {
                ASBinCode.rtData.rtFunction function =
                    (ASBinCode.rtData.rtFunction)((MethodGetterBase)ctor.bindField).getConstructor(objScope);


                HeapSlot _temp = tempSlot;
                constructorCaller.returnSlot = _temp;
                constructorCaller.SetFunction(function); function.Clear();


                //afterCallConstructor callbacker = new afterCallConstructor();
                var callbacker = player.blockCallBackPool.create();
                callbacker.args = this;
                //callbacker.obj = obj;
                //callbacker.objScope = objScope;
                //callbacker.rtObject = _object;
                callbacker.cacheObjects[0] = _object;
                callbacker.setWhenFailed(creatorFailed);
                callbacker.setCallBacker(afterCallConstructorCallbacker);


                constructorCaller.callbacker = callbacker;

                toNoticeFailed2 = callbacker;
                constructorCaller.call();
                toNoticeFailed2 = null;

                constructorCaller = null;
            }
            else
            {
                exec_step3(_object);
            }
        }
        public override RunTimeValueBase execute(RunTimeValueBase thisObj,SLOT[] argements,object stackframe,out string errormessage,out int errorno)
        {
            errormessage = null;
            errorno      = 0;

            var theObj = argements[0].getValue();

            if (theObj.rtType < RunTimeDataType.unknown)
            {
                return(ASBinCode.rtData.rtBoolean.False);
            }

            //***检查原型链***
            ASBinCode.rtti.Object obj = ((ASBinCode.rtData.rtObjectBase)theObj).value;

            ASBinCode.rtti.DynamicObject _proto = null;

            if (obj is ASBinCode.rtti.DynamicObject)
            {
                ASBinCode.rtti.DynamicObject dobj = (ASBinCode.rtti.DynamicObject)obj;
                _proto = dobj._prototype_;
            }
            else
            {
                _proto = (ASBinCode.rtti.DynamicObject)
                             ((StackFrame)stackframe).player.static_instance[obj._class.staticClass.classid].value;
            }

            var v2obj = ((ASBinCode.rtData.rtObjectBase)thisObj).value;

            bool found = false;

            while (_proto != null)
            {
                if (ReferenceEquals(_proto,v2obj))
                {
                    found = true;
                    break;
                }

                if (_proto._class.classid == ((StackFrame)stackframe).player.swc.FunctionClass.classid) //Function
                {
                    var o =
                        (ASBinCode.rtti.DynamicObject)
                            ((ASBinCode.rtData.rtObjectBase)_proto.memberData[1].getValue()).value;

                    _proto = o;
                }
                else if (_proto._class.classid == 1)
                {
                    _proto = null;
                }
                else
                {
                    break;
                }
            }

            if (found)
            {
                return(ASBinCode.rtData.rtBoolean.True);
            }
            else
            {
                return(ASBinCode.rtData.rtBoolean.False);
            }
        }
        private static IEnumerator <RunTimeValueBase> getForEach_IEnumerator(
            Player player,ASBinCode.rtti.Object obj,StackFrame frame,OpStep step,RunTimeScope scope,
            Dictionary <object,object> visited = null
            )
        {
            if (obj is ASBinCode.rtti.DynamicObject)
            {
                ASBinCode.rtti.DynamicObject dobj = (ASBinCode.rtti.DynamicObject)obj;
                {
                    var k = dobj.eachSlot();
                    while (k.MoveNext())
                    {
                        var c = k.Current;
                        DynamicPropertySlot ds = c as DynamicPropertySlot;
                        if (c != null)
                        {
                            yield return(ds.getValue()); //new rtString(ds._propname);
                        }
                    }
                }

                if (obj is DictionaryObject)
                {
                    DictionaryObject dictObj = (DictionaryObject)obj;
                    var k = dictObj.eachDictSlot();
                    while (k.MoveNext())
                    {
                        var            c  = k.Current;
                        DictionarySlot ds = c as DictionarySlot;
                        if (c != null)
                        {
                            yield return(ds.getValue()); //ds._key.key;
                        }
                    }
                }

                if (visited == null)
                {
                    visited = new Dictionary <object,object>();
                }
                //***再到原型链中查找
                if (dobj._prototype_ != null)
                {
                    var protoObj = dobj._prototype_;
                    //****_prototype_的类型,只可能是Function对象或Class对象
                    if (protoObj._class.classid == player.swc.FunctionClass.classid) //Function
                    {
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[1].getValue()).value;

                        if (visited.ContainsKey(dobj))
                        {
                            yield break;
                        }
                        visited.Add(dobj,null);

                        var res = getForEach_IEnumerator(player,dobj,frame,step,scope,visited);
                        while (res.MoveNext())
                        {
                            yield return(res.Current);
                        }
                    }
                    else if (protoObj._class.classid == 1) //搜索到根Object
                    {
                        //***根Object有继承自Class的prototype,再没有就没有了
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[0].getValue()).value;
                        {
                            var k = dobj.eachSlot();
                            while (k.MoveNext())
                            {
                                var c = k.Current;
                                DynamicPropertySlot ds = c as DynamicPropertySlot;
                                if (c != null)
                                {
                                    yield return(ds.getValue()); //new rtString(ds._propname);
                                }
                            }
                        }
                        yield break;
                    }
                    else if (protoObj._class.staticClass == null)
                    {
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[0].getValue()).value;
                        var res = getForEach_IEnumerator(player,dobj,frame,step,scope);
                        while (res.MoveNext())
                        {
                            yield return(res.Current);
                        }
                    }
                    else
                    {
                        frame.throwError((new error.InternalError(frame.player.swc,step.token,
                                                                  "遭遇了异常的_prototype_"
                                                                  )));
                        yield break;
                    }
                }
            }
            else if (obj is ASBinCode.rtti.Object)
            {
                //***处理.net IEnumerable***
                System.Collections.IEnumerator enumerator = null;
                if (obj is LinkSystemObject)
                {
                    var od = ((LinkSystemObject)obj).GetLinkData();
                    if (od is System.Collections.IEnumerable)
                    {
                        enumerator = ((System.Collections.IEnumerable)od).GetEnumerator();
                    }
                    else
                    {
                        enumerator = od as System.Collections.IEnumerator;
                    }
                }

                if (enumerator != null)
                {
                    var e = enumerator;
                    while (e.MoveNext())
                    {
                        int slotidx = frame.baseBottomSlotIndex;
                        if (slotidx >= Player.STACKSLOTLENGTH)
                        {
                            throw new ASRunTimeException("stack overflow",frame.player.stackTrace(0));
                        }
                        var tempslot = frame.stack[slotidx];
                        try
                        {
                            player.linktypemapper.storeLinkObject_ToSlot(e.Current,RunTimeDataType.rt_void,frame._tempSlot2,player.swc,player);
                        }
                        finally
                        {
                            tempslot.clear();
                        }

                        yield return(frame._tempSlot2.getValue());
                    }
                }
                else
                {
                    var dobj = ((ASBinCode.rtti.DynamicObject)
                                frame.player.static_instance[obj._class.staticClass.classid].value);

                    dobj = (ASBinCode.rtti.DynamicObject)((rtObjectBase)dobj.memberData[0].getValue()).value;
                    var res = getForEach_IEnumerator(player,dobj,frame,step,scope);
                    while (res.MoveNext())
                    {
                        yield return(res.Current);
                    }
                }
            }

            yield break;
        }
        private static IEnumerator <RunTimeValueBase> getForinIEnumerator(
            Player player,ASBinCode.rtti.Object obj,StackFrame frame,OpStep step,RunTimeScope scope,
            Dictionary <object,object> visited = null
            )
        {
            if (obj is ASBinCode.rtti.DynamicObject)
            {
                ASBinCode.rtti.DynamicObject dobj = (ASBinCode.rtti.DynamicObject)obj;
                {
                    var k = dobj.eachSlot();
                    while (k.MoveNext())
                    {
                        var c = k.Current;
                        DynamicPropertySlot ds = c as DynamicPropertySlot;
                        if (c != null)
                        {
                            yield return(new rtString(ds._propname));
                        }
                    }
                }

                if (obj is DictionaryObject)
                {
                    DictionaryObject dictObj = (DictionaryObject)obj;
                    var k = dictObj.eachDictSlot();
                    while (k.MoveNext())
                    {
                        var            c  = k.Current;
                        DictionarySlot ds = c as DictionarySlot;
                        if (c != null)
                        {
                            yield return(ds._key.key);
                        }
                    }
                }
                if (visited == null)
                {
                    visited = new Dictionary <object,object>();
                }
                //***再到原型链中查找
                if (dobj._prototype_ != null)
                {
                    var protoObj = dobj._prototype_;
                    //****_prototype_的类型,只可能是Function对象或Class对象
                    if (protoObj._class.classid == player.swc.FunctionClass.classid) //Function
                    {
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[1].getValue()).value;
                        if (visited.ContainsKey(dobj))
                        {
                            yield break;
                        }
                        visited.Add(dobj,null);

                        var res = getForinIEnumerator(player,dobj,frame,step,scope,visited);
                        while (res.MoveNext())
                        {
                            yield return(res.Current);
                        }
                    }
                    else if (protoObj._class.classid == 1) //搜索到根Object
                    {
                        //***根Object有继承自Class的prototype,再没有就没有了
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[0].getValue()).value;
                        {
                            var k = dobj.eachSlot();
                            while (k.MoveNext())
                            {
                                var c = k.Current;
                                DynamicPropertySlot ds = c as DynamicPropertySlot;
                                if (c != null)
                                {
                                    yield return(new rtString(ds._propname));
                                }
                            }
                        }
                        yield break;
                    }
                    else if (protoObj._class.staticClass == null)
                    {
                        dobj = (DynamicObject)((rtObjectBase)protoObj.memberData[0].getValue()).value;
                        var res = getForinIEnumerator(player,dobj,frame,step,scope);
                        while (res.MoveNext())
                        {
                            yield return(res.Current);
                        }
                    }
                    else
                    {
                        frame.throwError((new error.InternalError(frame.player.swc,step.token,
                                                                  "遭遇了异常的_prototype_"
                                                                  )));
                        yield break;
                    }
                }
            }
            else if (obj is ASBinCode.rtti.Object)
            {
                var dobj = ((ASBinCode.rtti.DynamicObject)
                            frame.player.static_instance[obj._class.staticClass.classid].value);

                dobj = (ASBinCode.rtti.DynamicObject)((rtObjectBase)dobj.memberData[0].getValue()).value;
                var res = getForinIEnumerator(player,dobj,frame,step,scope);
                while (res.MoveNext())
                {
                    yield return(res.Current);
                }
            }

            yield break;
        }
 public StackCacheObject(StackLinkObjectCache cache, ASBinCode.rtti.Object v, RunTimeScope s) : base(v, s)
 {
     this.cache = cache;
 }