// ECMA-262 section 15.6.4.3 private static void valueOf(ref mdr.CallFrame callFrame) { Debug.WriteLine("Calling JSBoolean.valueOf"); if (callFrame.This.ValueType != mdr.ValueTypes.Boolean) throw new Exception("Boolean.prototype.valueOf is not generic"); callFrame.Return.Set(callFrame.This.ToBoolean()); }
public static void CreateFunction(ref mdr.CallFrame callFrame, int funcDefIndex, mdr.DObject context, ref Stack stack) { //Debug.WriteLine("calling Exec.CreateFunction"); var funcDef = ((JSFunctionMetadata)callFrame.Function.Metadata).SubFunctions[funcDefIndex]; var func = new mdr.DFunction(funcDef, context); stack.Items[stack.Sp++].Set(func); ; }
public virtual mdr.DObject Construct(mdr.DFunction func) { if (_construct != null) return _construct(func); else return base.Construct(func); }
internal static void EvalString(string inputString, ref mdr.DValue result, mdr.DFunction callerFunction = null, mdr.DObject callerContext = null, mdr.DObject thisArg = null) { var funcMetadata = JSParser.ParseScript(inputString).Expression.Metadata; var func = new mdr.DFunction(funcMetadata, null); var tempCallFrame = new mdr.CallFrame(); bool isDirectEval = callerContext != null; if (isDirectEval) { //function will behave as if it was the caller Debug.Assert(thisArg != null && callerFunction != null && callerContext != null, "Invalid situation! Direct eval call must have thisArg, callerFunction, callerContext set"); funcMetadata.Scope.IsProgram = false; funcMetadata.Scope.IsEvalFunction = true; funcMetadata.ParentFunction = (JSFunctionMetadata)callerFunction.Metadata; tempCallFrame.CallerContext = callerContext; tempCallFrame.This = thisArg; } else { //This will behave just like a program code tempCallFrame.CallerContext = mdr.Runtime.Instance.GlobalContext; tempCallFrame.This = (mdr.Runtime.Instance.GlobalContext); } //TODO: find a way to assign a name to this //funcMetadata.Name += "_eval"; //After we know the ParentFunction tempCallFrame.Function = func; tempCallFrame.Signature = mdr.DFunctionSignature.EmptySignature; func.Call(ref tempCallFrame); result.Set(ref tempCallFrame.Return); }
public static bool CallProperty(mdr.DObject input, string propName, out mdr.DValue output) { if (input != null) { var propDesc = input.GetPropertyDescriptor(propName); var prop = new mdr.DValue(); propDesc.Get(input, ref prop); mdr.DFunction func = null; if (prop.ValueType == mdr.ValueTypes.Function) { func = prop.AsDFunction(); //if (toString != null) //{ mdr.CallFrame callFrame = new mdr.CallFrame(); callFrame.This = (input); callFrame.Function = func; func.Call(ref callFrame); if (ValueTypesHelper.IsPrimitive(callFrame.Return.ValueType)) { output = callFrame.Return; return true; } } } output = new mdr.DValue(); output.SetUndefined(); return false; }
public JSTypedArrayBase(mdr.DObject prototype, string arrayname, int typesize) : base(prototype, arrayname) { TypeSize = typesize; TargetPrototype.DefineOwnProperty("length", new mdr.DProperty() { TargetValueType = mdr.ValueTypes.Int32, OnGetDValue = (mdr.DObject This, ref mdr.DValue v) => { v.Set((This as DTypedArray).ByteLength / TypeSize); }, OnSetDValue = (mdr.DObject This, ref mdr.DValue v) => { /* do nothing */ }, OnSetInt = (mdr.DObject This, int v) => { /* do nothing */ }, }); TargetPrototype.DefineOwnProperty("byteLength", new mdr.DProperty() { TargetValueType = mdr.ValueTypes.Int32, OnGetDValue = (mdr.DObject This, ref mdr.DValue v) => { v.Set((This as DTypedArray).ByteLength); }, OnSetDValue = (mdr.DObject This, ref mdr.DValue v) => { /* do nothing */ }, OnSetInt = (mdr.DObject This, int v) => { /* do nothing */ }, }); // Constants this.DefineOwnProperty("BYTES_PER_ELEMENT", TypeSize, mdr.PropertyDescriptor.Attributes.NotConfigurable | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.Data); TargetPrototype.DefineOwnProperty("BYTES_PER_ELEMENT", TypeSize, mdr.PropertyDescriptor.Attributes.NotConfigurable | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.Data); }
internal static void Init(mdr.DObject obj) { obj.SetField("global", obj); //obj.SetField("null", mdr.Runtime.Instance.DefaultDNull); obj.DefineOwnProperty("undefined", mdr.Runtime.Instance.DefaultDUndefined, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable); obj.DefineOwnProperty("NaN", double.NaN, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable); obj.DefineOwnProperty("Infinity", double.PositiveInfinity, mdr.PropertyDescriptor.Attributes.Data | mdr.PropertyDescriptor.Attributes.NotWritable | mdr.PropertyDescriptor.Attributes.NotEnumerable | mdr.PropertyDescriptor.Attributes.NotConfigurable); obj.SetField("Object", new JSObject()); obj.SetField("Function", new JSFunction()); obj.SetField("Array", new JSArray()); obj.SetField("ArrayBuffer", new JSArrayBuffer()); obj.SetField("Int8Array", new JSInt8Array()); obj.SetField("Uint8Array", new JSUint8Array()); obj.SetField("Int16Array", new JSInt16Array()); obj.SetField("Uint16Array", new JSUint16Array()); obj.SetField("Int32Array", new JSInt32Array()); obj.SetField("Uint32Array", new JSUint32Array()); obj.SetField("Float32Array", new JSFloat32Array()); obj.SetField("Float64Array", new JSFloat64Array()); obj.SetField("Math", new JSMath()); obj.SetField("String", new JSString()); obj.SetField("Number", new JSNumber()); obj.SetField("Date", new JSDate()); obj.SetField("Boolean", new JSBoolean()); obj.SetField("Error", new JSError()); obj.SetField("RegExp", new JSRegExp()); obj.SetField("eval", BuiltinEval); AddStandardMethods(obj); AddExtendedMethods(obj); }
public JSBuiltinFunctionImp(mdr.DFuncImpInstance.JittedMethod m, Constructor c) : base(null, null) //base(m.Method.Name, null, null) { Name = m.Method.Name; _method = m; _construct = c; }
// i0 ? i1 : i2 public static void Run(ref mdr.DValue i0, ref mdr.DValue i1, ref mdr.DValue i2, ref mdr.DValue result) { if (Operations.Convert.ToBoolean.Run(ref i0)) result.Set(ref i1); else result.Set(ref i2); }
public static mdr.DArray CreateArgumentsObject(ref mdr.CallFrame callFrame, mdr.DObject context) { var metadata = (JSFunctionMetadata)callFrame.Function.Metadata; Debug.Assert(metadata.Scope.HasArgumentsSymbol, "Invalid situation, created arguments for the wrong scope!"); mdr.DArray arguments = null; if (metadata.Scope.IsEvalFunction) { //Read from context var tmp = new mdr.DValue(); context.GetField(JSFunctionArguments.Name, ref tmp); arguments = tmp.AsDArray(); } else { arguments = CreateArgumentsObject(ref callFrame); var parameters = metadata.FunctionIR.Parameters; Debug.Assert(arguments.Length >= parameters.Count, "arguments array is not large enough to hold all arguments."); for (var i = parameters.Count - 1; i >= 0; --i) { var symbol = parameters[i].Symbol; var paramIndex = symbol.ParameterIndex; Debug.Assert(paramIndex == i, "Invalid situation!, Parameter indexes don't match!"); if (symbol.SymbolType == JSSymbol.SymbolTypes.ClosedOnLocal) { var pd = context.AddOwnPropertyDescriptorByFieldId(symbol.FieldId, mdr.PropertyDescriptor.Attributes.Accessor | mdr.PropertyDescriptor.Attributes.NotConfigurable); context.Fields[pd.Index].Set(new ArgumentAccessor(arguments, paramIndex)); } } if (metadata.Scope.HasEval) context.SetField(JSFunctionArguments.Name, arguments); } return arguments; }
public static string GetEventHandlerAttr(mdr.DObject obj, string name) { var ehp = obj.GetPropertyDescriptor(name).GetProperty() as EventHandlerProperty; if (ehp == null) throw new Exception("Invalid Event " + name); return GetEventHandlerAttr(obj, ehp.EventType, name); }
public static mdr.DObject CheckUndefined(mdr.DObject obj) { // In case of calling "item" method on lists, a returned null should be converted to undefined. if (obj == null) return mdr.Runtime.Instance.DefaultDUndefined; else return obj; }
protected DTypedArray(mdr.DObject prototype, DArrayBuffer array, int byteoffset, int bytelength, int typesize) : base(prototype) { ByteOffset_ = byteoffset; TypeSize_ = typesize; ByteLength_ = bytelength; Elements_ = array.Elements_; }
public void Add(mdr.DFunction f, bool useCapture) { if (_listeners == null) _listeners = new List<mdr.DFunction>(); _listeners.Add(f); if (useCapture) _useCaptures.Add(f); }
public DArrayBuffer(mdr.DObject prototype, int bytesize) : base(prototype) { ByteLength_ = Math.Min(bytesize, MaxElementsCount); Elements_ = new byte[ByteLength_]; for (int i = 0; i < ByteLength_; ++i) Elements_[i] = 0x00; }
public static void CreateArray(ref mdr.CallFrame callFrame, int resultIndex, int valuesCount) { var values = callFrame.Values; var array = new mdr.DArray(valuesCount); for (var i = valuesCount - 1; i >= 0; --i) array.Elements[i] = values[resultIndex + i]; values[resultIndex].Set(array); }
protected DTypedArray(mdr.DObject prototype, int bytelength, int typesize) : base(prototype) { ByteLength_ = Math.Min(bytelength, MaxElementsCount); TypeSize_ = typesize; Elements_ = new byte[ByteLength_]; for (int i = 0; i < ByteLength_; ++i) Elements_[i] = 0x00; }
public PositionListener(mdr.DFunction handler, mdr.DFunction errorHandler, PositionListenerOptions options) { this._handler = handler; // TODO: Unused. /*this._errorHandler = errorHandler; this._options = options;*/ _callFrame = new mdr.CallFrame(); _callFrame.Function = _handler; }
/// <summary> /// Parse a number. Intended to be used at runtime for string-to-number conversions. /// </summary> public static void ParseNumber(string number, ref mdr.DValue dValue) { var parser = new JavaScriptParser.SequentialParser(number); var numericLiteral = parser.ParseNumber(); // Convert to a DValue and return via the ref parameter. Debug.Assert(numericLiteral is IR.PrimitiveLiteral, "ParseNumber() should yield a PrimitiveLiteral."); (numericLiteral as IR.PrimitiveLiteral).SetAsDValue(ref dValue); }
static partial void CustomFillPrototype(mdr.DObject prototype) { var window = mwr.HTMLRuntime.Instance.GlobalContext; prototype.DefineOwnProperty("onblur", new mdr.DForwardingProperty(window, "onblur")); prototype.DefineOwnProperty("onerror", new mdr.DForwardingProperty(window, "onerror")); prototype.DefineOwnProperty("onfocus", new mdr.DForwardingProperty(window, "onfocus")); prototype.DefineOwnProperty("onload", new mdr.DForwardingProperty(window, "onload")); prototype.DefineOwnProperty("onscroll", new mdr.DForwardingProperty(window, "onscroll")); }
public static mdr.DObject CreateConstantContext(ref mdr.CallFrame callFrame) { //return CreateFunctionContext(ref callFrame); //TODO: the following optimization requires proper support in the code generation as well! For now, we don't do much! //this function will not change its context, so we can just reuse the outer context Debug.Assert(((JSFunctionMetadata)callFrame.Function.Metadata).Scope.IsConstContext, "Function {0} will need its own context", ((JSFunctionMetadata)callFrame.Function.Metadata).Declaration); var context = callFrame.Function.OuterContext; return context; }
public static void CreateObject(ref mdr.CallFrame callFrame, int resultIndex, int fieldsCount) { //Here we assume we have (fieldId, value) pairs on the stack starting at resultIndex var values = callFrame.Values; var obj = new mdr.DObject(); var lastSP = resultIndex + fieldsCount * 2; for (var sp = resultIndex; sp < lastSP; sp += 2) obj.SetFieldByFieldId(values[sp].AsInt32(), ref values[sp + 1]); values[resultIndex].Set(obj); }
public static void Execute(ref mdr.CallFrame callFrame, int fromIndex, int toIndex) { var ics = callFrame.Function.Metadata.InlineCache; var i = fromIndex; while (i <= toIndex) { Debug.WriteLine("Running {0}:{1}", i, ics[i].Method.Name); i = ics[i](ref callFrame, i); } }
public static mdr.DObject CreateProgramContext(ref mdr.CallFrame callFrame) { //this function should add everything to global context //since this is called once, it will also add all the symbols through a not so efficient loop var metadata = (JSFunctionMetadata)callFrame.Function.Metadata; Debug.Assert(metadata.Scope.IsProgram, "Function {0} is not a program", metadata.Declaration); var context = JSRuntime.Instance.GlobalContext; AddSymbolsToContext(metadata.Scope.Symbols, context); return context; }
public static mdr.DObject CreateEvalContext(ref mdr.CallFrame callFrame) { //this function should add everything to its parent context //since we don't cache the generated code, it will also add all the symbols through a not so efficient loop var metadata = (JSFunctionMetadata)callFrame.Function.Metadata; Debug.Assert(metadata.Scope.IsEvalFunction, "Function {0} is not an eval", metadata.Declaration); Debug.Assert(callFrame.CallerContext != null, "Eval function {0} needs CallerContext in its call frame", metadata.Declaration); var context = callFrame.CallerContext; AddSymbolsToContext(metadata.Scope.Symbols, context); return context; }
static partial void CustomFillPrototype(mdr.DObject prototype) { Debug.WriteLine("++$> adding geolocation to Navigator props"); prototype.DefineOwnProperty("geolocation", new mdr.DProperty() { TargetValueType = mdr.ValueTypes.String, OnGetDValue = (mdr.DObject This, ref mdr.DValue v) => { v.Set((This as Navigator).Geolocation); }, }, mdr.PropertyDescriptor.Attributes.NotWritable); }
static string FormatArgs(ref mdr.CallFrame callFrame) { var formatString = callFrame.Arg0.AsString(); StringBuilder str = new StringBuilder(); int escapeIdx = 0, nextEscapeIdx = 0; var nextArg = 1; while ((nextEscapeIdx = formatString.IndexOfAny(FormatChars, escapeIdx)) != -1) { // Append non-escape characters str.Append(formatString.Substring(escapeIdx, nextEscapeIdx - escapeIdx)); // If we're at the last character in the string, just break out of the loop if (nextEscapeIdx == formatString.Length - 1) { escapeIdx = nextEscapeIdx; break; } // Interpret escape character if (formatString[nextEscapeIdx] == '\\') { if (formatString[nextEscapeIdx + 1] == '%') str.Append('%'); else str.AppendFormat("\\{0}", formatString[nextEscapeIdx + 1]); } else if (formatString[nextEscapeIdx] == '%') { var arg = callFrame.Arg(nextArg++); if (formatString[nextEscapeIdx + 1] == 's') str.Append(mjr.Operations.Convert.ToString.Run(ref arg)); else if (formatString[nextEscapeIdx + 1] == 'd') str.Append(mjr.Operations.Convert.ToInt64.Run(ref arg)); else if (formatString[nextEscapeIdx + 1] == 'i') str.Append(mjr.Operations.Convert.ToInt64.Run(ref arg)); else if (formatString[nextEscapeIdx + 1] == 'f') str.Append(mjr.Operations.Convert.ToDouble.Run(ref arg)); else str.AppendFormat("[%{0}]", formatString[nextEscapeIdx + 1]); } // Advance past the escaped character escapeIdx = nextEscapeIdx + 2; } // Append any remaining characters str.Append(formatString.Substring(escapeIdx)); // Return the finished formatted string return str.ToString(); }
static void setAttribute (ref mdr.CallFrame callFrame) { var elem = (Element) callFrame.This; var name = callFrame.Arg0.AsString (); var val = callFrame.Arg1.AsString (); var ehp = callFrame.This.GetPropertyDescriptor(name).GetProperty() as EventHandlerProperty; if (ehp != null) { SetEventHandlerAttr(callFrame.This, ehp.EventType, name, val); return; } elem.SetContentAttribute(name, val); }
public static void SetEventHandlerAttr(mdr.DObject obj, string name, string script) { #if ENABLE_RR if (RecordReplayManager.Instance != null && RecordReplayManager.Instance.RecordEnabled) { RecordReplayManager.Instance.Record("Element", null, "SetEventHandlerAttr", false, obj, name, script); } #endif var ehp = obj.GetPropertyDescriptor(name).GetProperty() as EventHandlerProperty; if (ehp == null) throw new Exception("Invalid Event " + name); SetEventHandlerAttr(obj, ehp.EventType, name, script); }
// all event handler attribute accessors should eventually call these public static string GetEventHandlerAttr(mdr.DObject obj, EventTypes type, string name) { #if ENABLE_RR if (RecordReplayManager.Instance != null && RecordReplayManager.Instance.RecordEnabled) { mwr.RecordReplayManager.Instance.Record("Element", null, "GetEventHandlerAttr", false, obj, type, name); } #endif var targetElement = obj.FirstInPrototypeChainAs<HTMLElement>(); var s = targetElement.PrimGetEventHandlerAttr(type); Debug.WriteLine("GetEventHandlerAttr({0}) = '{1}'", name, s); return s; }