public object Evaluate(string expression) { var p = _inner as IActiveScriptParse; object result; EXCEPINFO excepinfo; p.ParseScriptText(expression, "", IntPtr.Zero, "", 0, 0, 32 /*Expression*/, out result, out excepinfo); _inner.SetScriptState(SCRIPTSTATE.SCRIPTSTATE_CONNECTED); return(result); }
private void ScriptFunc() { _scriptEngineObject = new VBScriptEngine(); _scriptEngine = (IActiveScript)_scriptEngineObject; _scriptParser = new ActiveScriptParseWrapper(_scriptEngine); _scriptParser.InitNew(); _scriptEngine.SetScriptSite(this); _scriptEngine.SetScriptState(ScriptState.Started); _scriptEngine.SetScriptState(ScriptState.Connected); while (true) { if (_running == 0) { break; } string code = ""; lock (_runnables) { _runnables.ForEach(snippet => code += snippet + "\n"); _runnables.Clear(); } if (code.Trim().Length > 0) { try { /*_scriptEngine.GetScriptThreadState(SCRIPTTHREADID_BASE, out ScriptThreadState state); * Console.WriteLine(string.Format("Scipt thread state for thread '{0}' is {1}", _processThread.Name, state)); * _scriptEngine.GetScriptState(out ScriptState sciptState); * Console.WriteLine(string.Format("Scipt state for thread '{0}' is {1}", _processThread.Name, sciptState));*/ _scriptParser.ParseScriptText(code, null, null, null, IntPtr.Zero, 0, ScriptText.IsVisible, out object result, out EXCEPINFO ei); } catch (Exception e) { Console.WriteLine(string.Format("Exception while parsing following code for thread '{0}'\nException: {1}\nCode:\n{2}", _processThread.Name, e, code)); } } else if (_running == 2) { break; } Thread.Sleep(100); } _scriptEngine.Close(); Marshal.ReleaseComObject(_scriptEngine); Marshal.ReleaseComObject(_scriptEngineObject); _scriptParser.ReleaseComObject(); _scriptEngine = null; _scriptParser = null; }
/// <summary> /// Constructs an instance of the ActiveScript JavaScript engine /// </summary> /// <param name="clsid">CLSID of JavaScript engine</param> /// <param name="engineMode">JavaScript engine mode</param> /// <param name="lowerIeVersion">Lowest supported version of Internet Explorer</param> /// <param name="languageVersion">Version of script language</param> /// <param name="useEcmaScript5Polyfill">Flag for whether to use the ECMAScript 5 Polyfill</param> /// <param name="useJson2Library">Flag for whether to use the JSON2 library</param> protected ActiveScriptJsEngineBase(string clsid, JsEngineMode engineMode, string lowerIeVersion, ScriptLanguageVersion languageVersion, bool useEcmaScript5Polyfill, bool useJson2Library) { _engineMode = engineMode; _engineModeName = JsEngineModeHelpers.GetModeName(engineMode); _documentVersion = DateTime.UtcNow.ToString("o"); _dispatcher.Invoke(() => { _pActiveScript = IntPtr.Zero; try { _pActiveScript = ComHelpers.CreateInstanceByClsid <IActiveScript>(clsid); _activeScript = (IActiveScript)Marshal.GetObjectForIUnknown(_pActiveScript); } catch (Exception e) { throw new JsEngineLoadException( string.Format(CommonStrings.Runtime_IeJsEngineNotLoaded, _engineModeName, lowerIeVersion, e.Message), _engineModeName); } if (languageVersion != ScriptLanguageVersion.None) { var activeScriptProperty = _activeScript as IActiveScriptProperty; if (activeScriptProperty != null) { object scriptLanguageVersion = (int)languageVersion; uint result = activeScriptProperty.SetProperty((uint)ScriptProperty.InvokeVersioning, IntPtr.Zero, ref scriptLanguageVersion); if (result != (uint)ScriptHResult.Ok) { throw new JsEngineLoadException( string.Format(NetFrameworkStrings.Runtime_ActiveScriptLanguageVersionSelectionFailed, languageVersion)); } } } _activeScriptParse = new ActiveScriptParseWrapper(_pActiveScript, _activeScript); _activeScriptParse.InitNew(); _pActiveScriptGarbageCollector = ComHelpers.QueryInterfaceNoThrow <IActiveScriptGarbageCollector>(_pActiveScript); _activeScriptGarbageCollector = _activeScript as IActiveScriptGarbageCollector; _activeScript.SetScriptSite(this); _activeScript.SetScriptState(ScriptState.Started); InitScriptDispatch(); }); LoadResources(useEcmaScript5Polyfill, useJson2Library); }
/// <summary> /// Constructs instance of <see cref="ActiveScriptSiteWrapper"/> /// </summary> /// <param name="pActiveScript">Pointer to an instance of native JavaScript engine</param> /// <param name="activeScript">Instance of native JavaScript engine</param> /// <param name="documentVersion">Host-defined document version string</param> public ActiveScriptSiteWrapper(IntPtr pActiveScript, IActiveScript activeScript, string documentVersion) { _activeScript = activeScript; _activeScriptParse = new ActiveScriptParseWrapper(pActiveScript, _activeScript); _activeScriptParse.InitNew(); _activeScript.SetScriptSite(this); _activeScript.SetScriptState(ScriptState.Started); InitScriptDispatch(); DocumentVersion = documentVersion; }
public void Initialize() { try { engine = new ChakraJavaScriptEngine() as IActiveScript; } catch { engine = new JavaScriptEngine() as IActiveScript; } if (engine == null) { throw new Exception("Could not create IE JavaScript engine."); } parser = new ActiveScriptParseWrapper(engine); parser.InitNew(); engine.SetScriptSite(this); engine.SetScriptState(ScriptState.Started); }
/// <summary> /// /// </summary> /// <param name="code"></param> /// <returns></returns> public Script Parse(string code) { _engine.SetScriptState(ScriptState.Connected); try { object result; System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo; if (_parse32 != null) { _parse32.ParseScriptText(code, null, IntPtr.Zero, null, 0, 0, ScriptText.None, out result, out exceptionInfo); } else { _parse64.ParseScriptText(code, null, IntPtr.Zero, null, 0, 0, ScriptText.None, out result, out exceptionInfo); } } catch { if (Site.LastException != null) { throw Site.LastException; } throw; } if (Site.LastException != null) { throw Site.LastException; } IntPtr dispatch; _engine.GetScriptDispatch(null, out dispatch); Script script = new Script(this, dispatch); return(script); }
private object Parse(string text, bool expression) { const string varName = "x___"; object result; _engine.SetScriptState(ScriptState.Connected); ScriptText flags = ScriptText.None; if (expression) { flags |= ScriptText.IsExpression; } try { // immediate expression computation seems to work only for 64-bit // so hack something for 32-bit... System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo; if (_parse32 != null) { if (expression) { // should work for jscript & vbscript at least... text = varName + "=" + text; } _parse32.ParseScriptText(text, null, IntPtr.Zero, null, 0, 0, flags, out result, out exceptionInfo); } else { _parse64.ParseScriptText(text, null, IntPtr.Zero, null, 0, 0, flags, out result, out exceptionInfo); } } catch { if (Site.LastException != null) { throw Site.LastException; } throw; } IntPtr dispatch; if (expression) { // continue our 32-bit hack... if (_parse32 != null) { _engine.GetScriptDispatch(null, out dispatch); object dp = Marshal.GetObjectForIUnknown(dispatch); try { return(dp.GetType().InvokeMember(varName, BindingFlags.GetProperty, null, dp, null)); } catch { if (Site.LastException != null) { throw Site.LastException; } throw; } } return(result); } _engine.GetScriptDispatch(null, out dispatch); ParsedScript parsed = new ParsedScript(this, dispatch); return(parsed); }
public override void SetScriptState(ScriptState state) { activeScript.SetScriptState(state); }
/// <summary> /// Puts the scripting engine into the given state. This method can be called from non-base /// threads without resulting in a non-base callout to host objects or to the /// <see cref="IActiveScriptSite"/> interface. /// </summary> /// <param name="state">Sets the scripting engine to the given state</param> public void SetScriptState(ScriptState state) { _activeScript.SetScriptState(state); }
/// <summary> /// Puts the scripting engine into the Started state. /// At this point code will be executed in the order they were added to the script engine. /// </summary> public void Start() { activeScript.SetScriptState(ScriptState.Started); activeScript.SetScriptState(ScriptState.Connected); }
private object Parse(string text, bool expression) { const string varName = "x___"; System.Runtime.InteropServices.ComTypes.EXCEPINFO exceptionInfo; object result; _engine.SetScriptState(ScriptState.Connected); ScriptText flags = ScriptText.None; if (expression) { flags |= ScriptText.IsExpression; } try { if (_parse32 != null) { if (expression) { text = varName + "=" + text; } _parse32.ParseScriptText(text, null, null, null, IntPtr.Zero, 0, flags, out result, out exceptionInfo); } else { _parse64.ParseScriptText(text, null, null, null, IntPtr.Zero, 0, flags, out result, out exceptionInfo); } } catch { if (_site._lastException != null) { throw _site._lastException; } throw; } IntPtr dispatch; if (expression) { if (_parse32 != null) { _engine.GetScriptDispatch(null, out dispatch); object dp = Marshal.GetObjectForIUnknown(dispatch); try { return(dp.GetType().InvokeMember(varName, BindingFlags.GetProperty, null, dp, null)); } catch { if (_site._lastException != null) { throw _site._lastException; } throw; } } return(result); } _engine.GetScriptDispatch(null, out dispatch); ParsedScript parsed = new ParsedScript(this, dispatch); return(parsed); }