/// <summary> Convert the value to an object. /// /// See ECMA 9.9. /// </summary> public static IScriptable ToObject(Context cx, IScriptable scope, object val) { if (val is IScriptable) { return((IScriptable)val); } if (val == null) { throw ScriptRuntime.TypeErrorById("msg.null.to.object"); } if (val == Undefined.Value) { throw ScriptRuntime.TypeErrorById("msg.undef.to.object"); } string className = val is string? "String" : (CliHelper.IsNumber(val) ? "Number" : (val is bool? "Boolean" : null)); if (className != null) { object [] args = new object [] { val }; scope = ScriptableObject.GetTopLevelScope(scope); return(ScriptRuntime.NewObject(cx == null ? Context.CurrentContext : cx, scope, className, args)); } // Extension: Wrap as a LiveConnect object. object wrapped = cx.Wrap(scope, val, null); if (wrapped is IScriptable) { return((IScriptable)wrapped); } throw ScriptRuntime.errorWithClassName("msg.invalid.type", val); }
private BaseFunction RealFunction(IScriptable thisObj, IdFunctionObject f) { object x = thisObj.GetDefaultValue(typeof(IFunction)); if (x is BaseFunction) { return((BaseFunction)x); } throw ScriptRuntime.TypeErrorById("msg.incompat.call", f.FunctionName); }
/// <summary> Implements the instanceof operator for JavaScript Function objects. /// <p> /// <code> /// foo = new Foo();<br> /// foo instanceof Foo; // true<br> /// </code> /// /// </summary> /// <param name="instance">The value that appeared on the LHS of the instanceof /// operator /// </param> /// <returns> true if the "prototype" property of "this" appears in /// value's prototype chain /// /// </returns> public override bool HasInstance(IScriptable instance) { object protoProp = ScriptableObject.GetProperty(this, "prototype"); if (protoProp is IScriptable) { return(ScriptRuntime.jsDelegatesTo(instance, (IScriptable)protoProp)); } throw ScriptRuntime.TypeErrorById("msg.instanceof.bad.prototype", FunctionName); }
internal static object ToPrimitive (object val) { if (!(val is IScriptable)) { return val; } IScriptable s = (IScriptable)val; object result = s.GetDefaultValue (null); if (result is IScriptable) throw ScriptRuntime.TypeErrorById ("msg.bad.default.value"); return result; }
public override IScriptable CreateObject(Context cx, IScriptable scope) { if (useCallAsConstructor) { return(null); } // Throw error if not explicitly coded to be used as constructor, // to satisfy ECMAScript standard (see bugzilla 202019). // To follow current (2003-05-01) SpiderMonkey behavior, change it to: // return super.createObject(cx, scope); throw ScriptRuntime.TypeErrorById("msg.not.ctor", functionName); }
/// <summary> /// 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. /// </summary> /// <param name="readOnly">specify if the function f does not change state of /// object. /// </param> /// <returns> Scriptable object suitable for a check by the instanceof /// operator. /// </returns> /// <throws> RuntimeException if no more instanceof target can be found </throws> protected internal static EcmaScriptError IncompatibleCallError(IdFunctionObject f) { throw ScriptRuntime.TypeErrorById("msg.incompat.call", f.FunctionName); }