public override object ChangeType(object value, Type type, CultureInfo culture) { object ret; if (!ScriptObjectHelper.TryChangeType(value, type, culture, out ret)) { throw new InvalidOperationException(string.Format("Can't change type from {0} to {1}", value.GetType().FullName, type.FullName)); } return(ret); }
public static object FromValue(Value v) { // When the target type is a number or equivalent, SL converts ints // to doubles to wash out // browser differences. (Safari apparently always returns doubles, FF // ints and doubles, depending on the value). // See: http://msdn.microsoft.com/en-us/library/cc645079(VS.95).aspx switch (v.Kind) { case Kind.BOOL: return(v.u.i32 != 0); case Kind.INT32: return((double)v.u.i32); case Kind.DOUBLE: return(v.u.d); case Kind.STRING: return(Marshal.PtrToStringAnsi(v.u.p)); case Kind.NPOBJ: if (v.IsNull) { return(null); } ScriptObject reference = ScriptObject.LookupScriptObject(v.u.p); if (reference != null) { return(reference); } if (isChrome) { if (NativeMethods.html_object_has_property(PluginHost.Handle, v.u.p, "_internal_moonlight_marker")) { Value val; NativeMethods.html_object_get_property(PluginHost.Handle, v.u.p, "_internal_moonlight_marker", out val); reference = ScriptObject.LookupScriptObject(new IntPtr((int)val.u.d)); if (reference != null) { Mono.Value [] vargs = new Mono.Value [2]; vargs[0] = v; ToValue(ref vargs[1], reference); Mono.Value res; if (NativeMethods.html_object_invoke_self(PluginHost.Handle, comparer.Handle, vargs, 2, out res)) { return(reference); } } } } if (NativeMethods.html_object_has_property(PluginHost.Handle, v.u.p, "nodeType")) { Value val; NativeMethods.html_object_get_property(PluginHost.Handle, v.u.p, "nodeType", out val); double r = (double)ScriptObjectHelper.FromValue(val); val.Dispose(); if (isChrome) { ScriptObject.SetPropertyInternal(v.u.p, "_internal_moonlight_marker", (double)v.u.p); } if (r == (double)HtmlElement.NodeType.Document) { return(new HtmlDocument(v.u.p)); } else if (r == (double)HtmlElement.NodeType.Element || r == (double)HtmlElement.NodeType.Comment || r == (double)HtmlElement.NodeType.Text) { return(new HtmlElement(v.u.p)); } } else if (NativeMethods.html_object_has_property(PluginHost.Handle, v.u.p, "location")) { if (isChrome) { ScriptObject.SetPropertyInternal(v.u.p, "_internal_moonlight_marker", (double)v.u.p); } return(new HtmlWindow(v.u.p)); } else if (NativeMethods.html_object_has_property(PluginHost.Handle, v.u.p, "length") && NativeMethods.html_object_has_method(PluginHost.Handle, v.u.p, "item")) { return(new ScriptObjectCollection(v.u.p)); } return(new ScriptObject(v.u.p)); default: Console.WriteLine("unsupported Kind.{0}", v.Kind); throw new NotSupportedException(); } }