protected internal override void FillConstructorProperties(IdFunctionObject ctor) { AddIdFunctionProperty(ctor, DATE_TAG, ConstructorId_now, "now", 0); AddIdFunctionProperty(ctor, DATE_TAG, ConstructorId_parse, "parse", 1); AddIdFunctionProperty(ctor, DATE_TAG, ConstructorId_UTC, "UTC", 1); base.FillConstructorProperties(ctor); }
public static bool IsContinuationConstructor(IdFunctionObject f) { if (f.HasTag(FTAG) && f.MethodId() == Id_constructor) { return true; } return false; }
protected internal override void FillConstructorProperties(IdFunctionObject ctor) { int attr = ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY; ctor.DefineProperty("NaN", ScriptRuntime.NaNobj, attr); ctor.DefineProperty("POSITIVE_INFINITY", ScriptRuntime.WrapNumber(double.PositiveInfinity), attr); ctor.DefineProperty("NEGATIVE_INFINITY", ScriptRuntime.WrapNumber(double.NegativeInfinity), attr); ctor.DefineProperty("MAX_VALUE", ScriptRuntime.WrapNumber(double.MaxValue), attr); ctor.DefineProperty("MIN_VALUE", ScriptRuntime.WrapNumber(double.MinValue), attr); base.FillConstructorProperties(ctor); }
internal static void Init(Scriptable scope, bool @sealed) { Rhino.NativeWith obj = new Rhino.NativeWith(); obj.SetParentScope(scope); obj.SetPrototype(ScriptableObject.GetObjectPrototype(scope)); IdFunctionObject ctor = new IdFunctionObject(obj, FTAG, Id_constructor, "With", 0, scope); ctor.MarkAsConstructor(obj); if (@sealed) { ctor.SealObject(); } ctor.ExportAsScopeProperty(); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(FTAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); switch (id) { case Id_constructor: { throw Context.ReportRuntimeError("Direct call is not supported"); } } throw new ArgumentException(id.ToString()); }
public static void Init(Context cx, Scriptable scope, bool @sealed) { ClassLoader loader = cx.GetApplicationClassLoader(); Rhino.NativeJavaTopPackage top = new Rhino.NativeJavaTopPackage(loader); top.SetPrototype(GetObjectPrototype(scope)); top.SetParentScope(scope); for (int i = 0; i != commonPackages.Length; i++) { NativeJavaPackage parent = top; for (int j = 0; j != commonPackages[i].Length; j++) { parent = parent.ForcePackage(commonPackages[i][j], scope); } } // getClass implementation IdFunctionObject getClass = new IdFunctionObject(top, FTAG, Id_getClass, "getClass", 1, scope); // We want to get a real alias, and not a distinct JavaPackage // with the same packageName, so that we share classes and top // that are underneath. string[] topNames = ScriptRuntime.GetTopPackageNames(); NativeJavaPackage[] topPackages = new NativeJavaPackage[topNames.Length]; for (int i_1 = 0; i_1 < topNames.Length; i_1++) { topPackages[i_1] = (NativeJavaPackage)top.Get(topNames[i_1], top); } // It's safe to downcast here since initStandardObjects takes // a ScriptableObject. ScriptableObject global = (ScriptableObject)scope; if (@sealed) { getClass.SealObject(); } getClass.ExportAsScopeProperty(); global.DefineProperty("Packages", top, ScriptableObject.DONTENUM); for (int i_2 = 0; i_2 < topNames.Length; i_2++) { global.DefineProperty(topNames[i_2], topPackages[i_2], ScriptableObject.DONTENUM); } }
internal static NativeError Make(Context cx, Scriptable scope, IdFunctionObject ctorObj, object[] args) { Scriptable proto = (Scriptable)(ctorObj.Get("prototype", ctorObj)); NativeError obj = new NativeError(); obj.SetPrototype(proto); obj.SetParentScope(scope); int arglen = args.Length; if (arglen >= 1) { ScriptableObject.PutProperty(obj, "message", ScriptRuntime.ToString(args[0])); if (arglen >= 2) { ScriptableObject.PutProperty(obj, "fileName", args[1]); if (arglen >= 3) { int line = ScriptRuntime.ToInt32(args[2]); ScriptableObject.PutProperty(obj, "lineNumber", Sharpen.Extensions.ValueOf(line)); } } } return obj; }
protected internal override void FillConstructorProperties(IdFunctionObject ctor) { AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_fromCharCode, "fromCharCode", 1); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_charAt, "charAt", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_charCodeAt, "charCodeAt", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_indexOf, "indexOf", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_lastIndexOf, "lastIndexOf", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_split, "split", 3); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_substring, "substring", 3); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_toLowerCase, "toLowerCase", 1); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_toUpperCase, "toUpperCase", 1); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_substr, "substr", 3); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_concat, "concat", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_slice, "slice", 3); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_equalsIgnoreCase, "equalsIgnoreCase", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_match, "match", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_search, "search", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_replace, "replace", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_localeCompare, "localeCompare", 2); AddIdFunctionProperty(ctor, STRING_TAG, ConstructorId_toLocaleLowerCase, "toLocaleLowerCase", 1); base.FillConstructorProperties(ctor); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(JSON_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int methodId = f.MethodId(); switch (methodId) { case Id_toSource: { return "JSON"; } case Id_parse: { string jtext = ScriptRuntime.ToString(args, 0); object reviver = null; if (args.Length > 1) { reviver = args[1]; } if (reviver is Callable) { return Parse(cx, scope, jtext, (Callable)reviver); } else { return Parse(cx, scope, jtext); } goto case Id_stringify; } case Id_stringify: { object value = null; object replacer = null; object space = null; switch (args.Length) { case 3: default: { space = args[2]; goto case 2; } case 2: { replacer = args[1]; goto case 1; } case 1: { value = args[0]; goto case 0; } case 0: { break; } } return Stringify(cx, scope, value, replacer, space); } default: { throw new InvalidOperationException(methodId.ToString()); } } }
internal void InitValue(int id, string name, object value, int attributes) { if (!(1 <= id && id <= maxId)) { throw new ArgumentException(); } if (name == null) { throw new ArgumentException(); } if (value == ScriptableConstants.NOT_FOUND) { throw new ArgumentException(); } ScriptableObject.CheckValidAttributes(attributes); if (obj.FindPrototypeId(name) != id) { throw new ArgumentException(name); } if (id == constructorId) { if (!(value is IdFunctionObject)) { throw new ArgumentException("consructor should be initialized with IdFunctionObject"); } constructor = (IdFunctionObject)value; constructorAttrs = (short)attributes; return; } InitSlot(id, name, value, attributes); }
private IdFunctionObject NewIdFunction(object tag, int id, string name, int arity, Scriptable scope) { IdFunctionObject f = new IdFunctionObject(this, tag, id, name, arity, scope); if (IsSealed()) { f.SealObject(); } return f; }
protected internal virtual void FillConstructorProperties(IdFunctionObject ctor) { }
/// <summary> /// Utility method to construct type error to indicate incompatible call /// when converting script thisObj to a particular type is not possible. /// </summary> /// <remarks> /// Utility method to construct type error to indicate incompatible call /// when converting script thisObj to a particular type is not possible. /// Possible usage would be to have a private function like realThis: /// <pre> /// private static NativeSomething realThis(Scriptable thisObj, /// IdFunctionObject f) /// { /// if (!(thisObj instanceof NativeSomething)) /// throw incompatibleCallError(f); /// return (NativeSomething)thisObj; /// } /// </pre> /// Note that although such function can be implemented universally via /// java.lang.Class.isInstance(), it would be much more slower. /// </remarks> /// <param name="f"> /// function that is attempting to convert 'this' /// object. /// </param> /// <returns> /// Scriptable object suitable for a check by the instanceof /// operator. /// </returns> /// <exception cref="System.Exception">if no more instanceof target can be found</exception> protected internal static EcmaError IncompatibleCallError(IdFunctionObject f) { throw ScriptRuntime.TypeError1("msg.incompat.call", f.GetFunctionName()); }
public virtual object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (f.HasTag(FTAG)) { if (f.MethodId() == Id_getClass) { return Js_getClass(cx, scope, args); } } throw f.Unknown(); }
public void InitPrototypeConstructor(IdFunctionObject f) { int id = prototypeValues.constructorId; if (id == 0) { throw new InvalidOperationException(); } if (f.MethodId() != id) { throw new ArgumentException(); } if (IsSealed()) { f.SealObject(); } prototypeValues.InitValue(id, "constructor", f, DONTENUM); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(DATE_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); switch (id) { case ConstructorId_now: { return ScriptRuntime.WrapNumber(Now()); } case ConstructorId_parse: { string dataStr = ScriptRuntime.ToString(args, 0); return ScriptRuntime.WrapNumber(Date_parseString(dataStr)); } case ConstructorId_UTC: { return ScriptRuntime.WrapNumber(JsStaticFunction_UTC(args)); } case Id_constructor: { // if called as a function, just return a string // representing the current time. if (thisObj != null) { return Date_format(Now(), Id_toString); } return JsConstructor(args); } case Id_toJSON: { if (thisObj is Rhino.NativeDate) { return ((Rhino.NativeDate)thisObj).ToISOString(); } string toISOString = "toISOString"; Scriptable o = ScriptRuntime.ToObject(cx, scope, thisObj); object tv = ScriptRuntime.ToPrimitive(o, ScriptRuntime.NumberClass); if (tv is Number) { double d = System.Convert.ToDouble(((Number)tv)); if (d != d || System.Double.IsInfinity(d)) { return null; } } object toISO = o.Get(toISOString, o); if (toISO == ScriptableConstants.NOT_FOUND) { throw ScriptRuntime.TypeError2("msg.function.not.found.in", toISOString, ScriptRuntime.ToString(o)); } if (!(toISO is Callable)) { throw ScriptRuntime.TypeError3("msg.isnt.function.in", toISOString, ScriptRuntime.ToString(o), ScriptRuntime.ToString(toISO)); } object result = ((Callable)toISO).Call(cx, scope, o, ScriptRuntime.emptyArgs); if (!ScriptRuntime.IsPrimitive(result)) { throw ScriptRuntime.TypeError1("msg.toisostring.must.return.primitive", ScriptRuntime.ToString(result)); } return result; } } // The rest of Date.prototype methods require thisObj to be Date if (!(thisObj is Rhino.NativeDate)) { throw IncompatibleCallError(f); } Rhino.NativeDate realThis = (Rhino.NativeDate)thisObj; double t = realThis.date; switch (id) { case Id_toString: case Id_toTimeString: case Id_toDateString: { if (t == t) { return Date_format(t, id); } return js_NaN_date_str; } case Id_toLocaleString: case Id_toLocaleTimeString: case Id_toLocaleDateString: { if (t == t) { return ToLocale_helper(t, id); } return js_NaN_date_str; } case Id_toUTCString: { if (t == t) { return Js_toUTCString(t); } return js_NaN_date_str; } case Id_toSource: { return "(new Date(" + ScriptRuntime.ToString(t) + "))"; } case Id_valueOf: case Id_getTime: { return ScriptRuntime.WrapNumber(t); } case Id_getYear: case Id_getFullYear: case Id_getUTCFullYear: { if (t == t) { if (id != Id_getUTCFullYear) { t = LocalTime(t); } t = YearFromTime(t); if (id == Id_getYear) { if (cx.HasFeature(Context.FEATURE_NON_ECMA_GET_YEAR)) { if (1900 <= t && t < 2000) { t -= 1900; } } else { t -= 1900; } } } return ScriptRuntime.WrapNumber(t); } case Id_getMonth: case Id_getUTCMonth: { if (t == t) { if (id == Id_getMonth) { t = LocalTime(t); } t = MonthFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getDate: case Id_getUTCDate: { if (t == t) { if (id == Id_getDate) { t = LocalTime(t); } t = DateFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getDay: case Id_getUTCDay: { if (t == t) { if (id == Id_getDay) { t = LocalTime(t); } t = WeekDay(t); } return ScriptRuntime.WrapNumber(t); } case Id_getHours: case Id_getUTCHours: { if (t == t) { if (id == Id_getHours) { t = LocalTime(t); } t = HourFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getMinutes: case Id_getUTCMinutes: { if (t == t) { if (id == Id_getMinutes) { t = LocalTime(t); } t = MinFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getSeconds: case Id_getUTCSeconds: { if (t == t) { if (id == Id_getSeconds) { t = LocalTime(t); } t = SecFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getMilliseconds: case Id_getUTCMilliseconds: { if (t == t) { if (id == Id_getMilliseconds) { t = LocalTime(t); } t = MsFromTime(t); } return ScriptRuntime.WrapNumber(t); } case Id_getTimezoneOffset: { if (t == t) { t = (t - LocalTime(t)) / msPerMinute; } return ScriptRuntime.WrapNumber(t); } case Id_setTime: { t = TimeClip(ScriptRuntime.ToNumber(args, 0)); realThis.date = t; return ScriptRuntime.WrapNumber(t); } case Id_setMilliseconds: case Id_setUTCMilliseconds: case Id_setSeconds: case Id_setUTCSeconds: case Id_setMinutes: case Id_setUTCMinutes: case Id_setHours: case Id_setUTCHours: { t = MakeTime(t, args, id); realThis.date = t; return ScriptRuntime.WrapNumber(t); } case Id_setDate: case Id_setUTCDate: case Id_setMonth: case Id_setUTCMonth: case Id_setFullYear: case Id_setUTCFullYear: { t = MakeDate(t, args, id); realThis.date = t; return ScriptRuntime.WrapNumber(t); } case Id_setYear: { double year = ScriptRuntime.ToNumber(args, 0); if (year != year || System.Double.IsInfinity(year)) { t = ScriptRuntime.NaN; } else { if (t != t) { t = 0; } else { t = LocalTime(t); } if (year >= 0 && year <= 99) { year += 1900; } double day = MakeDay(year, MonthFromTime(t), DateFromTime(t)); t = MakeDate(day, TimeWithinDay(t)); t = InternalUTC(t); t = TimeClip(t); } realThis.date = t; return ScriptRuntime.WrapNumber(t); } case Id_toISOString: { return realThis.ToISOString(); } default: { throw new ArgumentException(id.ToString()); } } }
public virtual object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (f.HasTag(FTAG)) { if (f.MethodId() == Id_constructor) { throw Context.ReportRuntimeError1("msg.cant.call.indirect", "With"); } } throw f.Unknown(); }
private static Interpreter.CallFrame InitFrameForApplyOrCall(Context cx, Interpreter.CallFrame frame, int indexReg, object[] stack, double[] sDbl, int stackTop, int op, Scriptable calleeScope, IdFunctionObject ifun, InterpretedFunction iApplyCallable) { Scriptable applyThis; if (indexReg != 0) { object obj = stack[stackTop + 2]; if (obj == UniqueTag.DOUBLE_MARK) { obj = ScriptRuntime.WrapNumber(sDbl[stackTop + 2]); } applyThis = ScriptRuntime.ToObjectOrNull(cx, obj); } else { applyThis = null; } if (applyThis == null) { // This covers the case of args[0] == (null|undefined) as well. applyThis = ScriptRuntime.GetTopCallScope(cx); } if (op == Icode_TAIL_CALL) { ExitFrame(cx, frame, null); frame = frame.parentFrame; } else { frame.savedStackTop = stackTop; frame.savedCallOp = op; } Interpreter.CallFrame calleeFrame = new Interpreter.CallFrame(); if (BaseFunction.IsApply(ifun)) { object[] callArgs = indexReg < 2 ? ScriptRuntime.emptyArgs : ScriptRuntime.GetApplyArguments(cx, stack[stackTop + 3]); InitFrame(cx, calleeScope, applyThis, callArgs, null, 0, callArgs.Length, iApplyCallable, frame, calleeFrame); } else { // Shift args left for (int i = 1; i < indexReg; ++i) { stack[stackTop + 1 + i] = stack[stackTop + 2 + i]; sDbl[stackTop + 1 + i] = sDbl[stackTop + 2 + i]; } int argCount = indexReg < 2 ? 0 : indexReg - 1; InitFrame(cx, calleeScope, applyThis, stack, sDbl, stackTop + 2, argCount, iApplyCallable, frame, calleeFrame); } frame = calleeFrame; return frame; }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(ERROR_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); switch (id) { case Id_constructor: { return Make(cx, scope, f, args); } case Id_toString: { return Js_toString(thisObj); } case Id_toSource: { return Js_toSource(cx, scope, thisObj); } } throw new ArgumentException(id.ToString()); }
private Rhino.Xmlimpl.Namespace RealThis(Scriptable thisObj, IdFunctionObject f) { if (!(thisObj is Rhino.Xmlimpl.Namespace)) { throw IncompatibleCallError(f); } return (Rhino.Xmlimpl.Namespace)thisObj; }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(NAMESPACE_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); switch (id) { case Id_constructor: { return JsConstructor(cx, (thisObj == null), args); } case Id_toString: { return RealThis(thisObj, f).ToString(); } case Id_toSource: { return RealThis(thisObj, f).Js_toSource(); } } throw new ArgumentException(id.ToString()); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(CALL_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); if (id == Id_constructor) { if (thisObj != null) { throw Context.ReportRuntimeError1("msg.only.from.new", "Call"); } ScriptRuntime.CheckDeprecated(cx, "Call"); Rhino.NativeCall result = new Rhino.NativeCall(); result.SetPrototype(GetObjectPrototype(scope)); return result; } throw new ArgumentException(id.ToString()); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(BOOLEAN_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); if (id == Id_constructor) { bool b; if (args.Length == 0) { b = false; } else { b = args[0] is ScriptableObject && ((ScriptableObject)args[0]).AvoidObjectDetection() ? true : ScriptRuntime.ToBoolean(args[0]); } if (thisObj == null) { // new Boolean(val) creates a new boolean object. return new Rhino.NativeBoolean(b); } // Boolean(val) converts val to a boolean. return ScriptRuntime.WrapBoolean(b); } // The rest of Boolean.prototype methods require thisObj to be Boolean if (!(thisObj is Rhino.NativeBoolean)) { throw IncompatibleCallError(f); } bool value = ((Rhino.NativeBoolean)thisObj).booleanValue; switch (id) { case Id_toString: { return value ? "true" : "false"; } case Id_toSource: { return value ? "(new Boolean(true))" : "(new Boolean(false))"; } case Id_valueOf: { return ScriptRuntime.WrapBoolean(value); } } throw new ArgumentException(id.ToString()); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(GENERATOR_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); if (!(thisObj is NativeGenerator)) { throw IncompatibleCallError(f); } NativeGenerator generator = (NativeGenerator)thisObj; switch (id) { case Id_close: { // need to run any pending finally clauses return generator.Resume(cx, scope, GENERATOR_CLOSE, new NativeGenerator.GeneratorClosedException()); } case Id_next: { // arguments to next() are ignored generator.firstTime = false; return generator.Resume(cx, scope, GENERATOR_SEND, Undefined.instance); } case Id_send: { object arg = args.Length > 0 ? args[0] : Undefined.instance; if (generator.firstTime && !arg.Equals(Undefined.instance)) { throw ScriptRuntime.TypeError0("msg.send.newborn"); } return generator.Resume(cx, scope, GENERATOR_SEND, arg); } case Id_throw: { return generator.Resume(cx, scope, GENERATOR_THROW, args.Length > 0 ? args[0] : Undefined.instance); } case Id___iterator__: { return thisObj; } default: { throw new ArgumentException(id.ToString()); } } }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(MATH_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } double x; int methodId = f.MethodId(); switch (methodId) { case Id_toSource: { return "Math"; } case Id_abs: { x = ScriptRuntime.ToNumber(args, 0); // abs(-0.0) should be 0.0, but -0.0 < 0.0 == false x = (x == 0.0) ? 0.0 : (x < 0.0) ? -x : x; break; } case Id_acos: case Id_asin: { x = ScriptRuntime.ToNumber(args, 0); if (x == x && -1.0 <= x && x <= 1.0) { x = (methodId == Id_acos) ? Math.Acos(x) : Math.Asin(x); } else { x = double.NaN; } break; } case Id_atan: { x = ScriptRuntime.ToNumber(args, 0); x = Math.Atan(x); break; } case Id_atan2: { x = ScriptRuntime.ToNumber(args, 0); x = Math.Atan2(x, ScriptRuntime.ToNumber(args, 1)); break; } case Id_ceil: { x = ScriptRuntime.ToNumber(args, 0); x = System.Math.Ceiling(x); break; } case Id_cos: { x = ScriptRuntime.ToNumber(args, 0); x = (x == double.PositiveInfinity || x == double.NegativeInfinity) ? double.NaN : Math.Cos(x); break; } case Id_exp: { x = ScriptRuntime.ToNumber(args, 0); x = (x == double.PositiveInfinity) ? x : (x == double.NegativeInfinity) ? 0.0 : Math.Exp(x); break; } case Id_floor: { x = ScriptRuntime.ToNumber(args, 0); x = Math.Floor(x); break; } case Id_log: { x = ScriptRuntime.ToNumber(args, 0); // Java's log(<0) = -Infinity; we need NaN x = (x < 0) ? double.NaN : Math.Log(x); break; } case Id_max: case Id_min: { x = (methodId == Id_max) ? double.NegativeInfinity : double.PositiveInfinity; for (int i = 0; i != args.Length; ++i) { double d = ScriptRuntime.ToNumber(args[i]); if (d != d) { x = d; // NaN break; } if (methodId == Id_max) { // if (x < d) x = d; does not work due to -0.0 >= +0.0 x = Math.Max(x, d); } else { x = Math.Min(x, d); } } break; } case Id_pow: { x = ScriptRuntime.ToNumber(args, 0); x = Js_pow(x, ScriptRuntime.ToNumber(args, 1)); break; } case Id_random: { x = Math.Random(); break; } case Id_round: { x = ScriptRuntime.ToNumber(args, 0); if (x == x && x != double.PositiveInfinity && x != double.NegativeInfinity) { // Round only finite x long l = Math.Round(x); if (l != 0) { x = l; } else { // We must propagate the sign of d into the result if (x < 0.0) { x = ScriptRuntime.negativeZero; } else { if (x != 0.0) { x = 0.0; } } } } break; } case Id_sin: { x = ScriptRuntime.ToNumber(args, 0); x = (x == double.PositiveInfinity || x == double.NegativeInfinity) ? double.NaN : Math.Sin(x); break; } case Id_sqrt: { x = ScriptRuntime.ToNumber(args, 0); x = Math.Sqrt(x); break; } case Id_tan: { x = ScriptRuntime.ToNumber(args, 0); x = Math.Tan(x); break; } default: { throw new InvalidOperationException(methodId.ToString()); } } return ScriptRuntime.WrapNumber(x); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(XMLOBJECT_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); if (id == Id_constructor) { return JsConstructor(cx, thisObj == null, args); } // All (XML|XMLList).prototype methods require thisObj to be XML if (!(thisObj is Rhino.Xmlimpl.XMLObjectImpl)) { throw IncompatibleCallError(f); } Rhino.Xmlimpl.XMLObjectImpl realThis = (Rhino.Xmlimpl.XMLObjectImpl)thisObj; XML xml = realThis.GetXML(); switch (id) { case Id_appendChild: { if (xml == null) { XmlMethodNotFound(realThis, "appendChild"); } return xml.AppendChild(Arg(args, 0)); } case Id_addNamespace: { if (xml == null) { XmlMethodNotFound(realThis, "addNamespace"); } Namespace ns = lib.CastToNamespace(cx, Arg(args, 0)); return xml.AddNamespace(ns); } case Id_childIndex: { if (xml == null) { XmlMethodNotFound(realThis, "childIndex"); } return ScriptRuntime.WrapInt(xml.ChildIndex()); } case Id_inScopeNamespaces: { if (xml == null) { XmlMethodNotFound(realThis, "inScopeNamespaces"); } return cx.NewArray(scope, ToObjectArray(xml.InScopeNamespaces())); } case Id_insertChildAfter: { if (xml == null) { XmlMethodNotFound(realThis, "insertChildAfter"); } object arg0 = Arg(args, 0); if (arg0 == null || arg0 is XML) { return xml.InsertChildAfter((XML)arg0, Arg(args, 1)); } return Undefined.instance; } case Id_insertChildBefore: { if (xml == null) { XmlMethodNotFound(realThis, "insertChildBefore"); } object arg0 = Arg(args, 0); if (arg0 == null || arg0 is XML) { return xml.InsertChildBefore((XML)arg0, Arg(args, 1)); } return Undefined.instance; } case Id_localName: { if (xml == null) { XmlMethodNotFound(realThis, "localName"); } return xml.LocalName(); } case Id_name: { if (xml == null) { XmlMethodNotFound(realThis, "name"); } return xml.Name(); } case Id_namespace: { if (xml == null) { XmlMethodNotFound(realThis, "namespace"); } string prefix = (args.Length > 0) ? ScriptRuntime.ToString(args[0]) : null; Namespace rv = xml.Namespace(prefix); if (rv == null) { return Undefined.instance; } else { return rv; } goto case Id_namespaceDeclarations; } case Id_namespaceDeclarations: { if (xml == null) { XmlMethodNotFound(realThis, "namespaceDeclarations"); } Namespace[] array = xml.NamespaceDeclarations(); return cx.NewArray(scope, ToObjectArray(array)); } case Id_nodeKind: { if (xml == null) { XmlMethodNotFound(realThis, "nodeKind"); } return xml.NodeKind(); } case Id_prependChild: { if (xml == null) { XmlMethodNotFound(realThis, "prependChild"); } return xml.PrependChild(Arg(args, 0)); } case Id_removeNamespace: { if (xml == null) { XmlMethodNotFound(realThis, "removeNamespace"); } Namespace ns = lib.CastToNamespace(cx, Arg(args, 0)); return xml.RemoveNamespace(ns); } case Id_replace: { if (xml == null) { XmlMethodNotFound(realThis, "replace"); } XMLName xmlName = lib.ToXMLNameOrIndex(cx, Arg(args, 0)); object arg1 = Arg(args, 1); if (xmlName == null) { // I refuse to believe that this number will exceed 2^31 int index = (int)ScriptRuntime.LastUint32Result(cx); return xml.Replace(index, arg1); } else { return xml.Replace(xmlName, arg1); } goto case Id_setChildren; } case Id_setChildren: { if (xml == null) { XmlMethodNotFound(realThis, "setChildren"); } return xml.SetChildren(Arg(args, 0)); } case Id_setLocalName: { if (xml == null) { XmlMethodNotFound(realThis, "setLocalName"); } string localName; object arg = Arg(args, 0); if (arg is QName) { localName = ((QName)arg).LocalName(); } else { localName = ScriptRuntime.ToString(arg); } xml.SetLocalName(localName); return Undefined.instance; } case Id_setName: { if (xml == null) { XmlMethodNotFound(realThis, "setName"); } object arg = (args.Length != 0) ? args[0] : Undefined.instance; QName qname = lib.ConstructQName(cx, arg); xml.SetName(qname); return Undefined.instance; } case Id_setNamespace: { if (xml == null) { XmlMethodNotFound(realThis, "setNamespace"); } Namespace ns = lib.CastToNamespace(cx, Arg(args, 0)); xml.SetNamespace(ns); return Undefined.instance; } case Id_attribute: { XMLName xmlName = XMLName.Create(lib.ToNodeQName(cx, Arg(args, 0), true), true, false); return realThis.GetMatches(xmlName); } case Id_attributes: { return realThis.GetMatches(XMLName.Create(Rhino.Xmlimpl.XmlNode.QName.Create(null, null), true, false)); } case Id_child: { XMLName xmlName = lib.ToXMLNameOrIndex(cx, Arg(args, 0)); if (xmlName == null) { // Two billion or so is a fine upper limit, so we cast to int int index = (int)ScriptRuntime.LastUint32Result(cx); return realThis.Child(index); } else { return realThis.Child(xmlName); } goto case Id_children; } case Id_children: { return realThis.Children(); } case Id_comments: { return realThis.Comments(); } case Id_contains: { return ScriptRuntime.WrapBoolean(realThis.Contains(Arg(args, 0))); } case Id_copy: { return realThis.Copy(); } case Id_descendants: { Rhino.Xmlimpl.XmlNode.QName qname = (args.Length == 0) ? Rhino.Xmlimpl.XmlNode.QName.Create(null, null) : lib.ToNodeQName(cx, args[0], false); return realThis.GetMatches(XMLName.Create(qname, false, true)); } case Id_elements: { XMLName xmlName = (args.Length == 0) ? XMLName.FormStar() : lib.ToXMLName(cx, args[0]); return realThis.Elements(xmlName); } case Id_hasOwnProperty: { XMLName xmlName = lib.ToXMLName(cx, Arg(args, 0)); return ScriptRuntime.WrapBoolean(realThis.HasOwnProperty(xmlName)); } case Id_hasComplexContent: { return ScriptRuntime.WrapBoolean(realThis.HasComplexContent()); } case Id_hasSimpleContent: { return ScriptRuntime.WrapBoolean(realThis.HasSimpleContent()); } case Id_length: { return ScriptRuntime.WrapInt(realThis.Length()); } case Id_normalize: { realThis.Normalize(); return Undefined.instance; } case Id_parent: { return realThis.Parent(); } case Id_processingInstructions: { XMLName xmlName = (args.Length > 0) ? lib.ToXMLName(cx, args[0]) : XMLName.FormStar(); return realThis.ProcessingInstructions(xmlName); } case Id_propertyIsEnumerable: { return ScriptRuntime.WrapBoolean(realThis.PropertyIsEnumerable(Arg(args, 0))); } case Id_text: { return realThis.Text(); } case Id_toString: { return realThis.ToString(); } case Id_toSource: { int indent = ScriptRuntime.ToInt32(args, 0); return realThis.ToSource(indent); } case Id_toXMLString: { return realThis.ToXMLString(); } case Id_valueOf: { return realThis.ValueOf(); } } throw new ArgumentException(id.ToString()); }
public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { if (!f.HasTag(ITERATOR_TAG)) { return base.ExecIdCall(f, cx, scope, thisObj, args); } int id = f.MethodId(); if (id == Id_constructor) { return JsConstructor(cx, scope, thisObj, args); } if (!(thisObj is NativeIterator)) { throw IncompatibleCallError(f); } NativeIterator iterator = (NativeIterator)thisObj; switch (id) { case Id_next: { return iterator.Next(cx, scope); } case Id___iterator__: { /// XXX: what about argument? SpiderMonkey apparently ignores it return thisObj; } default: { throw new ArgumentException(id.ToString()); } } }
private object EnsureId(int id) { object[] array = valueArray; if (array == null) { lock (this) { array = valueArray; if (array == null) { array = new object[maxId * SLOT_SPAN]; valueArray = array; attributeArray = new short[maxId]; } } } int valueSlot = (id - 1) * SLOT_SPAN; object value = array[valueSlot]; if (value == null) { if (id == constructorId) { InitSlot(constructorId, "constructor", constructor, constructorAttrs); constructor = null; } else { // no need to refer it any longer obj.InitPrototypeId(id); } value = array[valueSlot]; if (value == null) { throw new InvalidOperationException(obj.GetType().FullName + ".initPrototypeId(int id) " + "did not initialize id=" + id); } } return value; }
// #/string_id_map# protected internal override void InitPrototypeId(int id) { string s; int arity; switch (id) { case Id_constructor: { IdFunctionObject ctor; if (this is XML) { ctor = new XMLCtor((XML)this, XMLOBJECT_TAG, id, 1); } else { ctor = new IdFunctionObject(this, XMLOBJECT_TAG, id, 1); } InitPrototypeConstructor(ctor); return; } case Id_addNamespace: { arity = 1; s = "addNamespace"; break; } case Id_appendChild: { arity = 1; s = "appendChild"; break; } case Id_attribute: { arity = 1; s = "attribute"; break; } case Id_attributes: { arity = 0; s = "attributes"; break; } case Id_child: { arity = 1; s = "child"; break; } case Id_childIndex: { arity = 0; s = "childIndex"; break; } case Id_children: { arity = 0; s = "children"; break; } case Id_comments: { arity = 0; s = "comments"; break; } case Id_contains: { arity = 1; s = "contains"; break; } case Id_copy: { arity = 0; s = "copy"; break; } case Id_descendants: { arity = 1; s = "descendants"; break; } case Id_elements: { arity = 1; s = "elements"; break; } case Id_hasComplexContent: { arity = 0; s = "hasComplexContent"; break; } case Id_hasOwnProperty: { arity = 1; s = "hasOwnProperty"; break; } case Id_hasSimpleContent: { arity = 0; s = "hasSimpleContent"; break; } case Id_inScopeNamespaces: { arity = 0; s = "inScopeNamespaces"; break; } case Id_insertChildAfter: { arity = 2; s = "insertChildAfter"; break; } case Id_insertChildBefore: { arity = 2; s = "insertChildBefore"; break; } case Id_length: { arity = 0; s = "length"; break; } case Id_localName: { arity = 0; s = "localName"; break; } case Id_name: { arity = 0; s = "name"; break; } case Id_namespace: { arity = 1; s = "namespace"; break; } case Id_namespaceDeclarations: { arity = 0; s = "namespaceDeclarations"; break; } case Id_nodeKind: { arity = 0; s = "nodeKind"; break; } case Id_normalize: { arity = 0; s = "normalize"; break; } case Id_parent: { arity = 0; s = "parent"; break; } case Id_prependChild: { arity = 1; s = "prependChild"; break; } case Id_processingInstructions: { arity = 1; s = "processingInstructions"; break; } case Id_propertyIsEnumerable: { arity = 1; s = "propertyIsEnumerable"; break; } case Id_removeNamespace: { arity = 1; s = "removeNamespace"; break; } case Id_replace: { arity = 2; s = "replace"; break; } case Id_setChildren: { arity = 1; s = "setChildren"; break; } case Id_setLocalName: { arity = 1; s = "setLocalName"; break; } case Id_setName: { arity = 1; s = "setName"; break; } case Id_setNamespace: { arity = 1; s = "setNamespace"; break; } case Id_text: { arity = 0; s = "text"; break; } case Id_toString: { arity = 0; s = "toString"; break; } case Id_toSource: { arity = 1; s = "toSource"; break; } case Id_toXMLString: { arity = 1; s = "toXMLString"; break; } case Id_valueOf: { arity = 0; s = "valueOf"; break; } default: { throw new ArgumentException(id.ToString()); } } InitPrototypeMethod(XMLOBJECT_TAG, id, s, arity); }
/// <summary> /// 'thisObj' will be null if invoked as constructor, in which case /// instance of Scriptable should be returned. /// </summary> /// <remarks> /// 'thisObj' will be null if invoked as constructor, in which case /// instance of Scriptable should be returned. /// </remarks> public virtual object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args) { throw f.Unknown(); }