/// <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); }
/// <summary> Convert the value to a number. /// /// See ECMA 9.3. /// </summary> public static double ToNumber (object val) { for (; ; ) { if (val is double) return (double)val; if (CliHelper.IsNumber (val)) return Convert.ToDouble (val); if (val == null) return +0.0; if (val == Undefined.Value) return double.NaN; if (val is string) return ToNumber ((string)val); if (val is bool) return ((bool)val) ? 1 : +0.0; if (val is IScriptable) { val = ((IScriptable)val).GetDefaultValue (typeof (long)); if (val is IScriptable) throw ScriptRuntime.errorWithClassName ("msg.primitive.expected", val); continue; } ScriptRuntime.WarnAboutNonJSObject (val); return double.NaN; } }
/// <summary> Convert the value to a string. /// /// See ECMA 9.8. /// </summary> public static string ToString (object val) { for (; ; ) { if (val == null) { return "null"; } if (val == Undefined.Value) { return "undefined"; } if (val is string) { return (string)val; } if (val is Boolean) return ((bool)val) ? "true" : "false"; if (CliHelper.IsNumber (val)) { // TODO: should we just teach NativeNumber.stringValue() // TODO: about Numbers? return ToString (Convert.ToDouble (val), 10); } if (val is IScriptable) { val = ((IScriptable)val).GetDefaultValue (typeof (string)); if (val is IScriptable) { throw ScriptRuntime.errorWithClassName ("msg.primitive.expected", val); } continue; } return val.ToString (); } }
/// <summary> Convert the value to a boolean. /// /// See ECMA 9.2. /// </summary> public static bool ToBoolean (object val) { for (; ; ) { if (val is bool) return ((bool)val); if (val == null || val == Undefined.Value) return false; if (val is string) return ((string)val).Length != 0; if (CliHelper.IsNumber (val)) { double d = Convert.ToDouble (val); return (!double.IsNaN (d) && d != 0.0); } if (val is IScriptable) { if (Context.CurrentContext.VersionECMA1) { // pure ECMA return true; } // ECMA extension val = ((IScriptable)val).GetDefaultValue (typeof (bool)); if (val is IScriptable) throw ScriptRuntime.errorWithClassName ("msg.primitive.expected", val); continue; } ScriptRuntime.WarnAboutNonJSObject (val); return true; } }