private static List <JsVal> ConvertTypes(object[] paramObjects, AutoJSContext context) { var collection = new List <JsVal>(); foreach (var p in paramObjects) { JsVal val; if (p is nsAString || p is nsACString || p is nsAUTF8String || p is String) { SpiderMonkey.JS_ExecuteScript(context.ContextPointer, '"' + p.ToString() + '"', out val); } else if (p is nsISupports) { // This returns a [xpconnect wrapped nsISupports] - why may or may not be good enought - if not could try and access the objects wrappedJSObject property? // val = SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, "valueOf"); // Replaced CallFunctionName 'valueOf' method with 'managed convert' (for speed reasons) val = JsVal.FromPtr(context.ConvertCOMObjectToJSObject((nsISupports)p)); } else if (p is bool) { SpiderMonkey.JS_ExecuteScript(context.ContextPointer, ((bool)p) ? "true;" : "false;", out val); } else if (p is double) { val = JsVal.FromDouble((double)p); } else { SpiderMonkey.JS_ExecuteScript(context.ContextPointer, p.ToString(), out val); } collection.Add(val); } return(collection); }
public bool GetBooleanValue() { using (var context = new AutoJSContext(_proxy)) using (var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)_xpathResult.Instance)) { return(SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject.JSObject, "booleanValue").ToBoolean()); } }
public string GetStringValue() { using (var context = new AutoJSContext()) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)_xpathResult.Instance); return(SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "stringValue").ToString()); } }
public GeckoNode GetSingleNodeValue() { using (var context = new AutoJSContext()) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)xpathResult.Instance); return((SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "singleNodeValue").ToComObject(context.ContextPointer) as nsIDOMNode).Wrap(GeckoNode.Create)); } }
public double GetNumberValue() { using (var context = new AutoJSContext()) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)xpathResult.Instance); return(SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "numberValue").ToDouble()); } }
public XPathResultType GetResultType() { using (var context = new AutoJSContext()) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)xpathResult.Instance); return((XPathResultType)SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, "resultType").ToInteger()); } }
public void CallVoidMethod(string methodName, params object[] paramObjects) { using (var context = new AutoJSContext(_globalWindow)) { var jsObject = context.ConvertCOMObjectToJSObject(_thisObject); var collection = ConvertTypes(paramObjects, context); SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, methodName, collection.ToArray()); } }
public void CallVoidMethod(string methodName, params object[] paramObjects) { using (var context = new AutoJSContext(_globalWindowProxy)) using (var jsObject = context.ConvertCOMObjectToJSObject(_thisObject, true)) { DisposablCollection disposablCollection; var collection = ConvertTypes(paramObjects, context, out disposablCollection); SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject.JSObject, methodName, collection.ToArray()); disposablCollection.Dispose(); } }
public T CallMethod <T>(string methodName, params object[] paramObjects) { using (var context = new AutoJSContext(_globalWindow)) { var jsObject = context.ConvertCOMObjectToJSObject(_thisObject); var collection = ConvertTypes(paramObjects, context); var retObject = SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, methodName, collection.ToArray()) .ToObject(); return(ConvertObject <T>(retObject)); } }
public void SetProperty(string propertyName, object value) { using (var context = new AutoJSContext(_globalWindow)) { var jsObject = context.ConvertCOMObjectToJSObject(_thisObject); if (!SpiderMonkey.JS_SetProperty(context.ContextPointer, jsObject, propertyName, ConvertTypes(new[] { value }, context).First())) { throw new GeckoException(String.Format("Property '{0}' of value '{1}' could not be set on object", propertyName, value)); } } }
public T GetProperty <T>(string propertyName) { using (var context = new AutoJSContext(_globalWindow)) { var jsObject = context.ConvertCOMObjectToJSObject(_thisObject); var result = SpiderMonkey.JS_GetProperty(context.ContextPointer, jsObject, propertyName); if (result.IsUndefined) { throw new GeckoException(String.Format("Property '{0}' of type '{1}' does not exist on object", propertyName, typeof(T).Name)); } var retObject = result.ToObject(); return(ConvertObject <T>(retObject)); } }
public void FromPtr_JsObject_ConvertedToJsValOfExpectedObject() { var browser = new GeckoWebBrowser(); var unused = browser.Handle; Assert.IsNotNull(browser); browser.TestLoadHtml("hello world."); Assert.Null(browser.Editor); using (var context = new AutoJSContext(browser.Window)) { var jsObject = context.ConvertCOMObjectToJSObject((nsISupports)browser.Document.DomObject); var jsVal = JsVal.FromPtr(jsObject); Assert.IsTrue(jsVal.IsObject); Assert.AreEqual(JSType.JSTYPE_OBJECT, jsVal.Type); Assert.AreEqual("[object HTMLDocument]", jsVal.ToString()); // Verify that converting back to ComObject yields expected object. var documentObject = (nsIDOMHTMLDocument)jsVal.ToObject(); Assert.AreEqual(browser.Document.TextContent, new GeckoDocument(documentObject).TextContent); } browser.Dispose(); }
public void SetProperty(string propertyName, object value) { using (var context = new AutoJSContext(_globalWindowProxy)) using (var jsObject = context.ConvertCOMObjectToJSObject(_thisObject, true)) { DisposablCollection disposablCollection; var types = ConvertTypes(new[] { value }, context, out disposablCollection).First(); var jso = jsObject.JSObject; try { if (!SpiderMonkey.JS_SetProperty(context.ContextPointer, ref jso, propertyName, ref types)) { throw new GeckoException( String.Format("Property '{0}' of value '{1}' could not be set on object", propertyName, value)); } } finally { disposablCollection.Dispose(); } } }
private static List <JsVal> ConvertTypes(object[] paramObjects, AutoJSContext context, out DisposablCollection toDispose) { List <IDisposable> list = new List <IDisposable>(); toDispose = new DisposablCollection(list); var collection = new List <JsVal>(); if (paramObjects == null) { return(collection); } foreach (var p in paramObjects) { JsVal val; if (p is nsAString || p is nsACString || p is nsAUTF8String || p is String) { val = SpiderMonkey.StringToJsVal(context.ContextPointer, p.ToString()); } else if (p is nsISupports) { // This returns a [xpconnect wrapped nsISupports] - why may or may not be good enought - if not could try and access the objects wrappedJSObject property? // val = SpiderMonkey.JS_CallFunctionName(context.ContextPointer, jsObject, "valueOf"); // Replaced CallFunctionName 'valueOf' method with 'managed convert' (for speed reasons) var jso = context.ConvertCOMObjectToJSObject((nsISupports)p, false); list.Add(jso); val = JsVal.FromPtr(jso.JSObject); } else if (p is bool) { val = CreateBoolJsVal(context, (bool)p); } else if (p is double) { val = JsVal.FromDouble((double)p); } else if (p is int) { val = JsVal.FromDouble(Convert.ToDouble(p)); } else if (p is WebIDLUnionBase) { WebIDLUnionBase b = (WebIDLUnionBase)p; if (b.IsNull()) { SpiderMonkey.JS_ExecuteScript(context.ContextPointer, "null", out val); } else if (b.IsComObject()) { var item = ((WebIDLUnionBase)p).ToComObject(); var jso = context.ConvertCOMObjectToJSObject(item, false); list.Add(jso); val = JsVal.FromPtr(jso.JSObject); } else if (b.IsBool()) { val = CreateBoolJsVal(context, b.ToBool()); } else { throw new NotImplementedException("WebIDLUnion are currently only supported for nsISupport and bool types and null's."); } } else { SpiderMonkey.JS_ExecuteScript(context.ContextPointer, (p ?? "null").ToString(), out val); } collection.Add(val); } return(collection); }