public virtual void Construct(ref CallFrame callFrame) { DValue proto = new DValue(); PrototypePropertyDescriptor.Get(this, ref proto); var protoObj = proto.AsDObject(); if (_prototypeMapMetadata == null || _prototypeMapMetadata.Prototype != protoObj) { _prototypeMapMetadata = Runtime.Instance.GetMapMetadataOfPrototype(protoObj); } callFrame.Function = this; if (Metadata != null) { callFrame.This = (new DObject(Metadata.TypicalConstructedFieldsLength, _prototypeMapMetadata.Root)); } else { callFrame.This = (new DObject(0, _prototypeMapMetadata.Root)); } JittedCode(ref callFrame); if (Metadata != null && Metadata.TypicalConstructedFieldsLength < callFrame.This.Fields.Length) { Metadata.TypicalConstructedFieldsLength = callFrame.This.Fields.Length; } if (ValueTypesHelper.IsObject(callFrame.Return.ValueType)) { callFrame.This = callFrame.Return.AsDObject(); } }
///// <summary> ///// List of all DTypes whose prorotype is using this DType! ///// </summary> //LinkedList<WeakReference> _subTypes; ///// <summary> ///// Number of actual fields (as opposed to inherited fields that might be added) ///// if (DObject.DType==this) DObject.Fields.Length >= this.OwnFiledsCount; ///// </summary> //public int OwnFieldsCount { get; private set; } internal PropertyMap(PropertyMapMetadata metadata, PropertyMap parent, PropertyDescriptor property, PropertyDescriptor overriddenProperty) { Metadata = metadata; Parent = parent; Property = property; OverriddenPropery = overriddenProperty; UniqueId = _uniqueIdCounter++; Debug.Assert(Metadata != null, "PropertyMap.Metadata cannot be null"); Debug.Assert(Property != null, "PropertyMap.Property cannot be null"); }
protected Runtime(RuntimeConfiguration configuration) { _inheritPropertyObjectCacheMaxIndex = 0; //We need to first set the Instance since the following constructors may use it! Runtime.Instance = this; Configuration = configuration; configuration.ParseArgs();//Do this now before anyone tries to read any configuration value. if (configuration.EnableTimers) { Timers = new m.Util.Timers(); _timer = StartSimpleTimer(true, "MCJS"); } if (configuration.EnableCounters) { Counters = new m.Util.Counters(); } EmptyPropertyMapMetadata = new PropertyMapMetadata(null); ///We can initialize commong field Ids to save lookups later ValueOfFieldId = GetFieldId("valueOf"); ToStringFieldId = GetFieldId("toString"); PrototypeFieldId = GetFieldId("prototype"); LengthFieldId = GetFieldId("length"); ///In each instance of Runtime we need to first reset prototypes in case a program has changed them //DUndefinedPrototype = new DObject(root.Root); DObjectPrototype = new DObject(EmptyPropertyMapMetadata.Root); DObjectMap = GetRootMapOfPrototype(DObjectPrototype); DFunctionPrototype = new DObject(DObjectMap); DFunctionMap = GetRootMapOfPrototype(DFunctionPrototype); DNumberPrototype = new DObject(DObjectMap); DNumberMap = GetRootMapOfPrototype(DNumberPrototype); DStringPrototype = new DObject(DObjectMap); DStringMap = GetRootMapOfPrototype(DStringPrototype); DBooleanPrototype = new DObject(DObjectMap); DBooleanMap = GetRootMapOfPrototype(DBooleanPrototype); DArrayPrototype = new DObject(DObjectMap); DArrayMap = GetRootMapOfPrototype(DArrayPrototype); DRegExpPrototype = new DObject(DObjectMap); DRegExpMap = GetRootMapOfPrototype(DRegExpPrototype); //Now need to recreate default values based on fresh prototypes. DefaultDUndefined = new DUndefined(); DefaultDNull = new DNull(); //DefaultDObject = new DObject(); //DefaultDDouble = new DDouble(default(double)); //DefaultDString = new DString(default(string)); //DefaultDInt = new DInt(default(int)); //DefaultDBoolean = new DBoolean(default(bool)); //DefaultDFunction = new DFunction(null); //DefaultDArray = new DArray(); //DefaultDProperty = new DProperty(); ArrayItemAccessor = new PropertyDescriptor(null) { Getter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { value = (obj as DArray).Elements[pd.Index]; /*if (mdr.Runtime.Instance.Configuration.ProfileStats) * { * mdr.Runtime.Instance.Counters.GetCounter("ArrayItemAccessor").Count++; * }*/ }, Setter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { (obj as DArray).Elements[pd.Index] = value; }, }; StringItemAccessor = new PropertyDescriptor(null) { Getter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { var strObj = obj.FirstInPrototypeChainAs <DString>(); value.Set(strObj.PrimitiveValue.AsString()[pd.Index]); }, Setter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { var strObj = obj.FirstInPrototypeChainAs <DString>(); var chars = strObj.PrimitiveValue.AsString().ToCharArray(); chars[pd.Index] = value.AsChar(); strObj.PrimitiveValue.Set(new String(chars)); }, }; var lengthFieldName = GetFieldName(LengthFieldId); ProtoInitializer.InitDArrayPrototype(DArrayPrototype, lengthFieldName); ProtoInitializer.InitDStringPrototype(DStringPrototype, lengthFieldName); var protoFieldName = GetFieldName(PrototypeFieldId); ProtoInitializer.InitDFunctionPrototype(DFunctionPrototype, protoFieldName, PrototypeFieldId); }
public PropertyMapMetadata GetMapMetadataOfPrototype(DObject prototype, bool addIfMissing = true) { Debug.Assert(prototype != null, "cannot lookup type information for null prototype"); WeakReference emptyNode = null; //sice we use weakref, we might want to reuse emty elements of the list #if !SEARCH_CHILDREN_LIST //TODO: we can explore the effect of the following by commening the next few lines if (prototype.SubMapsMetadata != null) return prototype.SubMapsMetadata; var iter = _children.GetEnumerator(); while (iter.MoveNext()) { var childRef = iter.Current; if (!childRef.IsAlive) { emptyNode = childRef; break; } } #else var iter = _children.GetEnumerator(); while (iter.MoveNext()) { var childRef = iter.Current; if (childRef.IsAlive) { var child = childRef.Target as PropertyMapMetadata; if (child.Prototype == prototype) return child; } else if (emptyNode == null) emptyNode = childRef; } #endif ///Ok, we did not find any, let's add one to the list if (!addIfMissing) return null; var newRoot = new PropertyMapMetadata(prototype); if (emptyNode == null) { emptyNode = new WeakReference(newRoot); _children.AddLast(emptyNode); } else emptyNode.Target = newRoot; prototype.SubMapsMetadata = newRoot; return newRoot; }
public virtual void Construct(ref CallFrame callFrame) { DValue proto = new DValue(); PrototypePropertyDescriptor.Get(this, ref proto); var protoObj = proto.AsDObject(); if (_prototypeMapMetadata == null || _prototypeMapMetadata.Prototype != protoObj) _prototypeMapMetadata = Runtime.Instance.GetMapMetadataOfPrototype(protoObj); callFrame.Function = this; if (Metadata != null) { callFrame.This = (new DObject(Metadata.TypicalConstructedFieldsLength, _prototypeMapMetadata.Root)); } else { callFrame.This = (new DObject(0, _prototypeMapMetadata.Root)); } JittedCode(ref callFrame); if (Metadata != null && Metadata.TypicalConstructedFieldsLength < callFrame.This.Fields.Length) Metadata.TypicalConstructedFieldsLength = callFrame.This.Fields.Length; if (ValueTypesHelper.IsObject(callFrame.Return.ValueType)) callFrame.This = callFrame.Return.AsDObject(); }
public PropertyMapMetadata GetMapMetadataOfPrototype(DObject prototype, bool addIfMissing = true) { Debug.Assert(prototype != null, "cannot lookup type information for null prototype"); WeakReference emptyNode = null; //sice we use weakref, we might want to reuse emty elements of the list #if !SEARCH_CHILDREN_LIST //TODO: we can explore the effect of the following by commening the next few lines if (prototype.SubMapsMetadata != null) { return(prototype.SubMapsMetadata); } var iter = _children.GetEnumerator(); while (iter.MoveNext()) { var childRef = iter.Current; if (!childRef.IsAlive) { emptyNode = childRef; break; } } #else var iter = _children.GetEnumerator(); while (iter.MoveNext()) { var childRef = iter.Current; if (childRef.IsAlive) { var child = childRef.Target as PropertyMapMetadata; if (child.Prototype == prototype) { return(child); } } else if (emptyNode == null) { emptyNode = childRef; } } #endif ///Ok, we did not find any, let's add one to the list if (!addIfMissing) { return(null); } var newRoot = new PropertyMapMetadata(prototype); if (emptyNode == null) { emptyNode = new WeakReference(newRoot); _children.AddLast(emptyNode); } else { emptyNode.Target = newRoot; } prototype.SubMapsMetadata = newRoot; return(newRoot); }
protected Runtime(RuntimeConfiguration configuration) { _inheritPropertyObjectCacheMaxIndex = 0; //We need to first set the Instance since the following constructors may use it! Runtime.Instance = this; Configuration = configuration; configuration.ParseArgs();//Do this now before anyone tries to read any configuration value. if (configuration.EnableTimers) { Timers = new m.Util.Timers(); _timer = StartSimpleTimer(true, "MCJS"); } if (configuration.EnableCounters) Counters = new m.Util.Counters(); EmptyPropertyMapMetadata = new PropertyMapMetadata(null); ///We can initialize commong field Ids to save lookups later ValueOfFieldId = GetFieldId("valueOf"); ToStringFieldId = GetFieldId("toString"); PrototypeFieldId = GetFieldId("prototype"); LengthFieldId = GetFieldId("length"); ///In each instance of Runtime we need to first reset prototypes in case a program has changed them //DUndefinedPrototype = new DObject(root.Root); DObjectPrototype = new DObject(EmptyPropertyMapMetadata.Root); DObjectMap = GetRootMapOfPrototype(DObjectPrototype); DFunctionPrototype = new DObject(DObjectMap); DFunctionMap = GetRootMapOfPrototype(DFunctionPrototype); DNumberPrototype = new DObject(DObjectMap); DNumberMap = GetRootMapOfPrototype(DNumberPrototype); DStringPrototype = new DObject(DObjectMap); DStringMap = GetRootMapOfPrototype(DStringPrototype); DBooleanPrototype = new DObject(DObjectMap); DBooleanMap = GetRootMapOfPrototype(DBooleanPrototype); DArrayPrototype = new DObject(DObjectMap); DArrayMap = GetRootMapOfPrototype(DArrayPrototype); DRegExpPrototype = new DObject(DObjectMap); DRegExpMap = GetRootMapOfPrototype(DRegExpPrototype); //Now need to recreate default values based on fresh prototypes. DefaultDUndefined = new DUndefined(); DefaultDNull = new DNull(); //DefaultDObject = new DObject(); //DefaultDDouble = new DDouble(default(double)); //DefaultDString = new DString(default(string)); //DefaultDInt = new DInt(default(int)); //DefaultDBoolean = new DBoolean(default(bool)); //DefaultDFunction = new DFunction(null); //DefaultDArray = new DArray(); //DefaultDProperty = new DProperty(); ArrayItemAccessor = new PropertyDescriptor(null) { Getter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { value = (obj as DArray).Elements[pd.Index]; /*if (mdr.Runtime.Instance.Configuration.ProfileStats) { mdr.Runtime.Instance.Counters.GetCounter("ArrayItemAccessor").Count++; }*/ }, Setter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { (obj as DArray).Elements[pd.Index] = value; }, }; StringItemAccessor = new PropertyDescriptor(null) { Getter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { var strObj = obj.FirstInPrototypeChainAs<DString>(); value.Set(strObj.PrimitiveValue.AsString()[pd.Index]); }, Setter = (mdr.PropertyDescriptor pd, mdr.DObject obj, ref mdr.DValue value) => { var strObj = obj.FirstInPrototypeChainAs<DString>(); var chars = strObj.PrimitiveValue.AsString().ToCharArray(); chars[pd.Index] = value.AsChar(); strObj.PrimitiveValue.Set(new String(chars)); }, }; var lengthFieldName = GetFieldName(LengthFieldId); ProtoInitializer.InitDArrayPrototype(DArrayPrototype, lengthFieldName); ProtoInitializer.InitDStringPrototype(DStringPrototype, lengthFieldName); var protoFieldName = GetFieldName(PrototypeFieldId); ProtoInitializer.InitDFunctionPrototype(DFunctionPrototype, protoFieldName, PrototypeFieldId); }