/// <summary> /// Unittest helper method to create a String JsVal /// </summary> /// <param name="value"></param> /// <returns></returns> private static JsVal CreateJsVal(string jscript) { using (AutoJSContext cx = new AutoJSContext(GlobalJSContextHolder.BackstageJSContext)) { var ptr = new JsVal(); var _securityManager = Xpcom.GetService<nsIScriptSecurityManager>("@mozilla.org/scriptsecuritymanager;1"); var _systemPrincipal = _securityManager.GetSystemPrincipal(); IntPtr globalObject = SpiderMonkey.JS_GetGlobalForScopeChain(cx.ContextPointer); bool ret = SpiderMonkey.JS_EvaluateScript(cx.ContextPointer, globalObject, jscript, (uint)jscript.Length, "script", 1, ref ptr); Assert.IsTrue(ret); Marshal.ReleaseComObject(_securityManager); return ptr; } }
public static IntPtr JS_ValueToString(IntPtr cx, JsVal v) { if (Xpcom.Is32Bit) { if (Xpcom.IsLinux) return JS_ValueToString_Linux32(cx, ref v); return JS_ValueToString_Win32(cx, ref v); } else { if (Xpcom.IsLinux) return JS_ValueToString_Linux64(cx, ref v); return JS_ValueToString_Win64(cx, ref v); } }
public static JSType JS_TypeOfValue(IntPtr cx, JsVal jsVal) { if (Xpcom.Is32Bit) { if (Xpcom.IsLinux) return JS_TypeOfValue_Linux32(cx, ref jsVal); return JS_TypeOfValue_Win32(cx, ref jsVal); } else { if (Xpcom.IsLinux) return JS_TypeOfValue_Linux64(cx, ref jsVal); return JS_TypeOfValue_Win64(cx, ref jsVal); } }
public JsVal EvaluateScript(string javaScript) { string msg = String.Empty; var old = SpiderMonkey.JS_SetErrorReporter(_cx, (cx, message, report) => { msg = message; }); try { var jsValue = new JsVal(); var ret = SpiderMonkey.JS_EvaluateScript(_cx, PeekCompartmentScope(), javaScript, (uint) javaScript.Length, "script", 1, ref jsValue); if (!ret) { throw new GeckoJavaScriptException(String.Format("JSError : {0}", msg)); } return jsValue; } finally { SpiderMonkey.JS_SetErrorReporter(_cx, old); } }
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="thisObject">a nsISupports com object that this is set too.</param> /// <param name="result"></param> /// <returns></returns> public bool EvaluateScript(string jsScript, nsISupports thisObject, out string result) { try { Guid guid = typeof(nsISupports).GUID; IntPtr globalObject = SpiderMonkey.JS_GetGlobalForScopeChain(_cx); var ptr = new JsVal(); var wrapper = XPConnect.WrapNative(_cx, globalObject, thisObject, ref guid); bool ret = SpiderMonkey.JS_EvaluateScript(_cx, wrapper.GetJSObjectAttribute(), jsScript, (uint)jsScript.Length, "script", 1, ref ptr); IntPtr jsStringPtr = SpiderMonkey.JS_ValueToString(_cx, ptr); result = Marshal.PtrToStringAnsi(SpiderMonkey.JS_EncodeString(_cx, jsStringPtr)); return ret; } catch (Exception e) { Console.WriteLine("Exception {0}", e); result = String.Empty; return false; } }
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="jsval"></param> /// <returns></returns> public bool EvaluateFullTrustScriptForCurrentContext(string jsScript, out string result) { var ptr = new JsVal(); IntPtr globalObject = SpiderMonkey.JS_GetGlobalForScopeChain(_cx); bool ret; using (var security = new FullTrustSecMan(XPConnect, _cx)) { ret = SpiderMonkey.JS_EvaluateScript(_cx, globalObject, jsScript, (uint)jsScript.Length, "script", 1, ref ptr); IntPtr jsStringPtr = SpiderMonkey.JS_ValueToString(_cx, ptr); result = Marshal.PtrToStringAnsi(SpiderMonkey.JS_EncodeString(_cx, jsStringPtr)); } return ret; }
private static extern bool JS_EvaluateScript_Win64(IntPtr cx, IntPtr obj, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval);
public static extern bool JS_EvaluateScript(IntPtr cx, IntPtr obj, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval);
private static extern IntPtr JS_ValueToString_Win32(IntPtr cx, JsVal v);
public void SetOndeviceorientationAttribute(JsVal aOndeviceorientation, IntPtr jsContext) { throw new NotImplementedException(); }
public static string JsValToString(IntPtr cx, JsVal jsVal) { IntPtr ptr = SpiderMonkey.ToString(cx, ref jsVal); return(StringFromNativeUtf8(ptr)); }
public static bool JS_SetProperty(IntPtr cx, IntPtr jsObject, string name, JsVal value) { return(JS_SetProperty(cx, ref jsObject, name, ref value)); }
public static JSType JS_TypeOfValue(IntPtr cx, JsVal jsVal) { return(JS_TypeOfValue(cx, ref jsVal)); }
public MutableJSVal(JsVal val) { _val = val; }
public static bool JS_EvaluateScript(IntPtr cx, IntPtr obj, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval) { if (Xpcom.IsLinux) { throw new NotImplementedException(); } return(JS_EvaluateScript_Win32(cx, obj, src, length, filename, lineno, ref jsval)); }
private static extern bool JS_EvaluateScriptForPrincipals_Win32(IntPtr cx, IntPtr obj, IntPtr principals, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval);
internal string ConvertValueToString(JsVal value) { IntPtr jsp = SpiderMonkey.JS_ValueToString(_cx, value); if (jsp != IntPtr.Zero) { uint length; var chars = SpiderMonkey.JS_GetStringCharsAndLength(_cx, jsp, out length); if (chars != IntPtr.Zero) return Marshal.PtrToStringUni(chars, (int)length); } return null; }
public void SetOnafterprintAttribute(JsVal aOnafterprint, IntPtr jsContext) { throw new NotImplementedException(); }
public static IntPtr ToStringSlow(IntPtr cx, JsVal v) { return(ToStringSlow(cx, ref v)); }
public void SetOnmouseenterAttribute(JsVal aOnmouseenter, IntPtr jsContext) { throw new NotImplementedException(); }
internal static extern bool JS_CallFunctionValue(IntPtr cx, IntPtr jsObject, ref JsVal fval, IntPtr passZero, ref MutableHandleValue jsValue);
public static extern IntPtr JS_ValueToString(IntPtr cx, JsVal v);
internal static extern IntPtr ToString(IntPtr cx, ref JsVal jsValue);
// JS_ValueToObject(JSContext *cx, JS::HandleValue v, JS::MutableHandleObject objp); private static extern bool JS_ValueToObject_Win64(IntPtr cx, ref JsVal jsValue, ref MutableHandle jsObject);
public static extern bool JS_SetProperty(IntPtr cx, ref IntPtr jsObject, string name, ref JsVal jsValue);
private static extern bool JS_GetProperty_Linux32(IntPtr cx, ref IntPtr jsObject, string name, ref JsVal jsValue);
public static extern JSType JS_TypeOfValue(IntPtr cx, ref JsVal jsVal);
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="jsval"></param> /// <returns></returns> public bool EvaluateScript(string jsScript, out string result) { var ptr = new JsVal(); IntPtr globalObject = SpiderMonkey.JS_GetGlobalForScopeChain(_cx); bool ret = SpiderMonkey.JS_EvaluateScript(_cx, globalObject, jsScript, (uint)jsScript.Length, "script", 1, ref ptr); // TODO: maybe getting JS_EvaluateScriptForPrincipals working would increase priviliges of the running script. //bool ret = SpiderMonkey.JS_EvaluateScriptForPrincipals(_cx, globalObject, ..., jsScript, (uint)jsScript.Length,"script", 1, ref ptr); IntPtr jsStringPtr = SpiderMonkey.JS_ValueToString(_cx, ptr); result = Marshal.PtrToStringAnsi(SpiderMonkey.JS_EncodeString(_cx, jsStringPtr)); return ret; }
internal static extern bool JS_ValueToObject(IntPtr cx, ref JsVal jsValue, ref MutableHandle jsObject);
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="jsval"></param> /// <returns></returns> public bool EvaluateScript(string jsScript, out string result) { var ptr = new JsVal(); bool ret = SpiderMonkey.JS_EvaluateScript(_cx, GetGlobalObject(), jsScript, (uint)jsScript.Length, "script", 1, ref ptr); //bool ret = SpiderMonkey.JS_EvaluateScriptForPrincipals(_cx, globalObject, ..., jsScript, (uint)jsScript.Length,"script", 1, ref ptr); result = ret ? ConvertValueToString(ptr) : null; return ret; }
public static extern IntPtr ToStringSlow(IntPtr cx, ref JsVal v);
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="thisObject">a nsISupports com object that this is set too.</param> /// <param name="result"></param> /// <returns></returns> public bool EvaluateScript(string jsScript, nsISupports thisObject, out string result) { try { Guid guid = typeof(nsISupports).GUID; var ptr = new JsVal(); IntPtr globalObject = ConvertCOMObjectToJSObject(thisObject); using (new JSAutoCompartment(_cx, globalObject)) { bool ret = SpiderMonkey.JS_EvaluateScript(_cx, globalObject, jsScript, (uint)jsScript.Length, "script", 1, ref ptr); result = ret ? ConvertValueToString(ptr) : null; return ret; } } catch (Exception e) { Console.WriteLine("Exception {0}", e); result = String.Empty; return false; } }
public static bool JS_EvaluateScriptForPrincipals(IntPtr cx, IntPtr obj, IntPtr principals, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval) { src = EncodeUnicodeScript(src); length = (uint)src.Length; if (Xpcom.Is32Bit) { if (Xpcom.IsLinux) return JS_EvaluateScriptForPrincipals_Linux32(cx, obj, principals, src, length, filename, lineno, ref jsval); return JS_EvaluateScriptForPrincipals_Win32(cx, obj, principals, src, length, filename, lineno, ref jsval); } else { if (Xpcom.IsLinux) return JS_EvaluateScriptForPrincipals_Linux64(cx, obj, principals, src, length, filename, lineno, ref jsval); return JS_EvaluateScriptForPrincipals_Win64(cx, obj, principals, src, length, filename, lineno, ref jsval); } }
public void PostMessage(JsVal message, nsAStringBase targetOrigin, IntPtr jsContext) { throw new NotImplementedException(); }
public static JsVal JS_GetProperty(IntPtr cx, IntPtr jsObject, string name) { JsVal Value = new JsVal(); if (Xpcom.Is32Bit) { if (Xpcom.IsLinux) JS_GetProperty_Linux32(cx, ref jsObject, name, ref Value); else JS_GetProperty_Win32(cx, ref jsObject, name, ref Value); } else { if (Xpcom.IsLinux) JS_GetProperty_Linux64(cx, ref jsObject, name, ref Value); else throw new NotImplementedException(); } return Value; }
public void SetOnbeforeunloadAttribute(JsVal aOnbeforeunload, IntPtr jsContext) { throw new NotImplementedException(); }
public void SetOnresizeAttribute(JsVal aOnresize, IntPtr jsContext) { throw new NotImplementedException(); }
public void SetOndeviceproximityAttribute(JsVal aOndeviceproximity, IntPtr jsContext) { throw new NotImplementedException(); }
public static extern bool JS_EvaluateScriptForPrincipals(IntPtr cx, IntPtr obj, IntPtr principals, string src, UInt32 length, string filename, UInt32 lineno, ref JsVal jsval);
public void SetOnpageshowAttribute(JsVal aOnpageshow, IntPtr jsContext) { throw new NotImplementedException(); }
private static extern JSType JS_TypeOfValue_Win32(IntPtr cx, JsVal jsVal);
public static IntPtr JS_ValueToObject(IntPtr cx, JsVal v) { if (Xpcom.Is32Bit) { MutableHandle mutableHandle = new MutableHandle(); if (Xpcom.IsLinux) JS_ValueToObject_Linux32(cx, ref v, ref mutableHandle); else JS_ValueToObject_Win32(cx, ref v, ref mutableHandle); return mutableHandle.Handle; } else { MutableHandle mutableHandle = new MutableHandle(); if (Xpcom.IsLinux) JS_ValueToObject_Linux64(cx, ref v, ref mutableHandle); else JS_ValueToObject_Win64(cx, ref v, ref mutableHandle); return mutableHandle.Handle; } }
private static extern JSType JS_TypeOfValue_Win64(IntPtr cx, ref JsVal jsVal);
public static extern JSType JS_TypeOfValue(IntPtr cx, JsVal jsVal);
private static extern IntPtr JS_ValueToString_Win64(IntPtr cx, ref JsVal v);
/// <summary> /// Evaluate javascript in the current context. /// </summary> /// <param name="jsScript"></param> /// <param name="jsval"></param> /// <returns></returns> public bool EvaluateScript(string jsScript, out string result) { var ptr = new JsVal(); IntPtr globalObject = SpiderMonkey.JS_GetGlobalForScopeChain(_cx); bool ret = SpiderMonkey.JS_EvaluateScript(_cx, globalObject, jsScript, (uint)jsScript.Length, "script", 1, ref ptr); IntPtr jsStringPtr = SpiderMonkey.JS_ValueToString(_cx, ptr); result = Marshal.PtrToStringAnsi(SpiderMonkey.JS_EncodeString(_cx, jsStringPtr)); return ret; }