public override void Execute(string code) { try { if (engine.IsRunning) { engine.Reset(); } // Load the script code IVsaCodeItem codeItem = (IVsaCodeItem)items.CreateItem("Script", VsaItemType.Code, VsaItemFlag.None); codeItem.SourceText = code; // compile and run if (engine.Compile()) { engine.Run(); } } catch (Exception e) { //XXX: AAAHHH // MessageBox.Show(e.Message + "\n" + e.StackTrace + "\n" + e.ToString()); Console.WriteLine(e.Message + "\n" + e.StackTrace + "\n" + e.ToString()); } }
/// <summary> /// Sets the script from a string /// </summary> /// <param name="scriptText">Script text</param> /// <param name="language">Script language</param> public void SetScript(string scriptText) { _isInitialized = false; // Make sure the last allocated engine is stopped. if (_engine != null) { _engine.Close(); } _scriptText = scriptText; // Initialize globale vars _globalObs.Clear(); _engine = new Microsoft.VisualBasic.Vsa.VsaEngine(); // Initialize Engine _engine.RootMoniker = "nScriptHost://VSAScript/Instance" + NewInstanceID.ToString(); _engine.Site = this; _engine.InitNew(); _engine.RootNamespace = "__Script__"; _references.Clear(); _globalObItems.Clear(); AddDefaultReferences(); // Set the script code. IVsaCodeItem item = _engine.Items.CreateItem("Code", VsaItemType.Code, VsaItemFlag.None) as IVsaCodeItem; item.SourceText = _scriptText; }
public override void Initialise() { // Get the Items items = engine.Items; // Load the preamble code string preamble = @" var document = window.document; function setTimeout(script : String, delay : ulong) : String { return window.setTimeout(script, delay); } function clearTimeout(token : String) : void { window.clearTimeout(token); } function setInterval(script : String, delay : ulong) : String { return window.setInterval(script, delay); } function clearInterval(token : String) : void { window.clearInterval(token); } function getSrc() : String { return window.getSrc(); } function printNode(node : Object) : String { return window.printNode(node); } function parseXML(xml : String, owner : Object) : Object { return window.parseXML(xml, owner); } function alert(msg : String) : void { window.alert(msg); } function browserEval(src : String, evt : Object) : String { return eval( src, 'unsafe'); } window.registerEval(browserEval); "; IVsaCodeItem codeItem = (IVsaCodeItem)items.CreateItem("Preamble", VsaItemType.Code, VsaItemFlag.None); codeItem.SourceText = preamble; // Add the global "window" item IVsaGlobalItem globalItem = (IVsaGlobalItem)items.CreateItem("window", VsaItemType.AppGlobal, VsaItemFlag.None); globalItem.TypeString = "SharpVectors.Scripting.IScriptableSvgWindow"; globalItem.ExposeMembers = true; }
/// <summary> /// スクリプトコードを追加する /// </summary> /// <param name="name">スクリプトコード名</param> /// <param name="code">追加するスクリプトコード</param> public void AddScriptCode(string name, string code) { IVsaCodeItem codeItem = (IVsaCodeItem)vsaEngine.Items.CreateItem (name, VsaItemType.Code, VsaItemFlag.None); codeItem.SourceText = code; }
private void AddSourceFile(IVsaEngine engine, string fileName, int codepage, bool fForceCodepage){ // We don't use the filename as the item name because filenames are allowed to contain // things that identifiers aren't (like commas and periods). string itemName = "$SourceFile_" + this.codeItemCounter++; IVsaCodeItem item = (IVsaCodeItem)engine.Items.CreateItem(itemName, VsaItemType.Code, VsaItemFlag.None); item.SetOption("codebase", fileName); item.SourceText = this.ReadFile(fileName, codepage, fForceCodepage); }
public void AddCodeBlock(string name, string code) { if (m_Engine.IsRunning) { m_Engine.Reset(); } IVsaCodeItem item = (IVsaCodeItem)m_Engine.Items.CreateItem( name, (JSVsaItemType)VsaItemType.Code, (JSVsaItemFlag)VsaItemFlag.Module); item.SourceText = code; }
// specify script public void setScript(string script) { //vsaEngine.s foreach (VsaItem currentItem in vsaEngine.Items) { if (currentItem.Name == "code") { vsaEngine.Items.Remove("code"); break; } } IVsaCodeItem codeItem = (IVsaCodeItem)vsaEngine.Items.CreateItem("code", VsaItemType.Code, VsaItemFlag.None); codeItem.SourceText = script; }
// // Entry point // private static void Main(string [] args) { if (args.Length < 1) { Usage(); Environment.Exit(0); } MainDriver(args); VsaEngine engine = new VsaEngine(); engine.InitVsaEngine("mjs:com.mono-project", new MonoEngineSite()); foreach (string asm in references) { IVsaReferenceItem item = (IVsaReferenceItem)engine.Items.CreateItem(asm, VsaItemType.Reference, VsaItemFlag.None); item.AssemblyName = asm; } string asm_name = String.Empty; foreach (Assembly assembly in assemblies) { asm_name = assembly.GetName().FullName; IVsaReferenceItem item = (IVsaReferenceItem)engine.Items.CreateItem(asm_name, VsaItemType.Reference, VsaItemFlag.None); item.AssemblyName = asm_name; } foreach (string file in files) { IVsaCodeItem item = (IVsaCodeItem)engine.Items.CreateItem(file, VsaItemType.Code, VsaItemFlag.None); item.SourceText = GetCodeFromFile(file); } engine.SetOption("debug", want_debugging_support); engine.SetOption("link_path", link_paths); engine.SetOption("first_source", first_source); engine.SetOption("assemblies", assemblies); engine.SetOption("out", output_file); if (warning_level != -1) { engine.SetOption("WarningLevel", warning_level); } engine.Compile(); }
public void Init() { // UNDONE: this is crap. The whole engine shouldn't have to be recreated and // reinitialized for each compile. Unfortunately it's the only way I've found // to work around the "Variable 'AED' has not been declared" problem. // A better solution needs to be found as this one seems to consume roughly // 240K of RAM each compile. m_vsae = new Microsoft.JScript.Vsa.VsaEngine(); m_vsae.RootMoniker = "com.spiffcode://script"; m_vsae.Site = this; m_vsae.InitNew(); m_vsae.SetOption("print", true); // Enable the 'print' function for scripters m_vsae.SetOption("fast", false); // Enable legacy mode m_vsae.RootNamespace = "script"; m_vsaciScript = (IVsaCodeItem)m_vsae.Items.CreateItem("scriptspace", VsaItemType.Code, VsaItemFlag.None); m_vsae.Items.CreateItem("mscorlib.dll", VsaItemType.Reference, VsaItemFlag.None); m_vsae.Items.CreateItem("System.Windows.Forms.dll", VsaItemType.Reference, VsaItemFlag.None); // m_vsae.Items.CreateItem("System.dll", VsaItemType.Reference, VsaItemFlag.None); string[] astrNames = new String[m_htGlobals.Count]; m_htGlobals.Keys.CopyTo(astrNames, 0); for (int i = 0; i < m_htGlobals.Count; i++) { IVsaGlobalItem vsagi = (IVsaGlobalItem)m_vsae.Items.CreateItem(astrNames[i], VsaItemType.AppGlobal, VsaItemFlag.None); // UNDONE: this doesn't seem to be working vsagi.ExposeMembers = true; // object obInstance = m_htGlobals[astrNames[i]]; // vsagi.TypeString = obInstance.GetType().FullName; } }
private bool _LoadScript(string sScriptFilename) { if (!CONFIG.bLoadScript) { return(false); } if (string.IsNullOrEmpty(sScriptFilename)) { return(false); } if (!File.Exists(sScriptFilename)) { return(false); } _scriptCount++; if (this._BeforeRulesCompile != null) { this._BeforeRulesCompile(sScriptFilename); } foreach (object obj2 in htMenuScripts.Keys) { ((MenuItem)obj2).Parent.MenuItems.RemoveAt(((MenuItem)obj2).Index); } htMenuScripts.Clear(); foreach (PreferenceBag.PrefWatcher watcher in this.listWeaklyHeldWatchers) { FiddlerApplication.Prefs.RemoveWatcher(watcher); } this.listWeaklyHeldWatchers.Clear(); StreamReader reader = new StreamReader(new FileStream(sScriptFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8, true); string str = reader.ReadToEnd(); reader.Close(); try { this._engine = new VsaEngine(); this._engine.RootMoniker = "fiddler://script/" + _scriptCount; this._engine.Site = this.objVSASite; this._engine.InitNew(); this._engine.RootNamespace = "Fiddler.ScriptNamespace"; this._engine.GenerateDebugInfo = false; } catch (EntryPointNotFoundException) { FiddlerApplication.DoNotifyUser("Unable to initialize FiddlerScript. This typically indicates that you are attempting to run Fiddler on .NET Framework v4.\n\nYour machine may have the unsupported OnlyUseLatestCLR registry key set.", "Unsupported configuration", MessageBoxIcon.Hand); this.Ready = false; return(false); } IVsaItems items = this._engine.Items; foreach (string str2 in CONFIG.sScriptReferences.Split(new char[] { ';' })) { if (str2.Trim().Length > 0) { IVsaReferenceItem item = (IVsaReferenceItem)items.CreateItem(str2, VsaItemType.Reference, VsaItemFlag.None); item.AssemblyName = str2; } } IVsaGlobalItem item2 = (IVsaGlobalItem)items.CreateItem("FiddlerObject", VsaItemType.AppGlobal, VsaItemFlag.None); item2.TypeString = "Fiddler.FiddlerScript"; item2 = (IVsaGlobalItem)items.CreateItem("FiddlerScript", VsaItemType.AppGlobal, VsaItemFlag.None); item2.TypeString = "Fiddler.FiddlerScript"; IVsaCodeItem item3 = (IVsaCodeItem)items.CreateItem("Handlers", VsaItemType.Code, VsaItemFlag.None); item3.SourceText = str; if (!this._engine.Compile()) { this.Ready = false; return(false); } this._engine.Run(); this.Ready = true; this._typeScriptHandlers = this._engine.Assembly.GetType("Fiddler.ScriptNamespace.Handlers"); if (this._typeScriptHandlers == null) { this._ClearCachedMethodPointers(); } else { this._ExtractMethodPointersFromScript(); foreach (FieldInfo info in this._typeScriptHandlers.GetFields(BindingFlags.Public | BindingFlags.Static)) { if (info.FieldType == typeof(bool)) { RulesOption customAttribute = (RulesOption)Attribute.GetCustomAttribute(info, typeof(RulesOption)); if (customAttribute != null) { this.CreateRulesMenuItem(info, customAttribute); } } else if (info.FieldType == typeof(string)) { RulesString oRule = (RulesString)Attribute.GetCustomAttribute(info, typeof(RulesString)); if (oRule != null) { RulesStringValue[] customAttributes = (RulesStringValue[])Attribute.GetCustomAttributes(info, typeof(RulesStringValue)); if ((customAttributes != null) && (customAttributes.Length > 0)) { Array.Sort <RulesStringValue>(customAttributes); this.CreateRulesMenuForStrings(info, oRule, customAttributes); } } } } bool flag = true; foreach (MethodInfo info2 in this._typeScriptHandlers.GetMethods()) { MenuItem item4; ToolsAction action = (ToolsAction)Attribute.GetCustomAttribute(info2, typeof(ToolsAction)); if (action != null) { item4 = new MenuItem(action.Name); htMenuScripts.Add(item4, info2); item4.Click += new EventHandler(this.HandleScriptToolsClick); FiddlerApplication._frmMain.mnuTools.MenuItems.Add(item4); } ContextAction action2 = (ContextAction)Attribute.GetCustomAttribute(info2, typeof(ContextAction)); if (action2 != null) { if (flag) { item4 = new MenuItem("-"); htMenuScripts.Add(item4, null); FiddlerApplication._frmMain.mnuSessionContext.MenuItems.Add(0, item4); flag = false; } item4 = new MenuItem(action2.Name); htMenuScripts.Add(item4, info2); item4.Click += new EventHandler(this.HandleScriptToolsClick); FiddlerApplication._frmMain.mnuSessionContext.MenuItems.Add(0, item4); } BindUIColumn column = (BindUIColumn)Attribute.GetCustomAttribute(info2, typeof(BindUIColumn)); if (column != null) { getColumnStringDelegate delFn = (getColumnStringDelegate)Delegate.CreateDelegate(typeof(getColumnStringDelegate), info2); FiddlerApplication._frmMain.lvSessions.AddBoundColumn(column._colName, column._iColWidth, delFn); } QuickLinkMenu menu = (QuickLinkMenu)Attribute.GetCustomAttribute(info2, typeof(QuickLinkMenu)); if (menu != null) { QuickLinkItem[] array = (QuickLinkItem[])Attribute.GetCustomAttributes(info2, typeof(QuickLinkItem)); if ((array != null) && (array.Length > 0)) { Array.Sort <QuickLinkItem>(array); this.CreateQuickLinkMenu(menu.Name, info2, menu.Name, array); } } } MenuExt.ReadRegistry(FiddlerApplication._frmMain.mnuTools, htMenuScripts); try { MethodInfo method = this._typeScriptHandlers.GetMethod("Main"); if (method != null) { method.Invoke(null, null); } } catch (Exception exception) { FiddlerApplication.DoNotifyUser(string.Concat(new object[] { "There was a problem with your FiddlerScript.\n\n", exception.Message, "\n", exception.StackTrace, "\n\n", exception.InnerException }), "JScript main() failed."); } } if (this._AfterRulesCompile != null) { this._AfterRulesCompile(); } return(true); }
/// <summary> /// Complies and runs the specified script code /// </summary> /// <param name="script">The script code to execute</param> public virtual bool Execute(string script) { try { // raise the event that assembly references are needed this.OnAssemblyReferencesNeeded(this, new VsaScriptingHostEventArgs(this)); // raise the event that global items are needed this.OnGlobalItemsNeeded(this, new VsaScriptingHostEventArgs(this)); // create the one and only code item that will be executed by this engine _codeItem = this.CreateCodeItem(@"Script"); if (script != null) { // set the source text to the script contents _codeItem.SourceText = script; // compile the script _engine.Compile(); // run the script _engine.Run(); } return true; } catch (Exception ex) { Debug.WriteLine(ex); } return false; // // Call the VB entry point // if( et == EngineType.VBScript ) // { // // Execute ClockScript.Script.Main() // Assembly assem = _engine.Assembly; // Type type = assem.GetType("ClockScript.Script"); // MethodInfo method = type.GetMethod("Main"); // method.Invoke(null, null); // } }