static object JsExecute( string script, string[] refs, string type, string func, params object[] args) { JScriptCodeProvider jsc = new JScriptCodeProvider(); CompilerParameters parameters = new CompilerParameters(refs, "test.dll", true); parameters.GenerateExecutable = false; CompilerResults results = jsc.CompileAssemblyFromFile(parameters, new string[] { script }); if (results.Errors.Count > 0) { Console.WriteLine("Errors: "); foreach (CompilerError err in results.Errors) { Console.WriteLine(err.ToString()); } return(null); } Assembly ass = results.CompiledAssembly; Type c = ass.GetType(type); MethodInfo f = c.GetMethod(func); return(f.Invoke(null, args)); }
/// <summary> /// Compile script by file. /// </summary> /// <param name="file"></param> private static bool CompileScript(string file) { var sourceFile = new FileInfo(file); CodeDomProvider provider = null; var csc = new CSharpCodeProvider(new Dictionary<string, string> { { "CompilerVersion", "v4.0" } }); // TODO: maybe allow custom references like CS-Script var parameters = new CompilerParameters(new[] {"mscorlib.dll", "System.Core.dll", "System.Windows.Forms.dll", "NFSScript.dll"}) { GenerateExecutable = false, GenerateInMemory = true }; // TODO this doesn't support C#6 and above // we can fix that with this CodeDom provider from nuget: // https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/ // then we can do this: // new Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider(); // TODO perhaps add support for F# // that comes in the form of another custom package // https://github.com/fsprojects/FSharp.Compiler.CodeDom // since we're at it, why not PowerShell? // https://github.com/adamdriscoll/PowerShellCodeDomProvider switch (sourceFile.Extension.ToUpperInvariant()) { case ".CS": provider = new CSharpCodeProvider(); break; case ".VB": provider = new VBCodeProvider(); break; case ".JS": provider = new JScriptCodeProvider(); break; } if (provider == null) return false; var results = provider.CompileAssemblyFromFile(parameters, file); if (results.Errors.HasErrors) { foreach (CompilerError r in results.Errors) { Log.Print(NFSScriptLoader.ERROR_TAG, r.ToString()); } return false; } var ass = results.CompiledAssembly; var script = new ModScript(ass, Path.GetFileName(file)); _scripts.Add(script); Log.Print(NFSScriptLoader.INFO_TAG, $"Loaded {script.File}"); return true; }
public static CompilerResults CompileFromJScriptSourceFile(String sourceFile, CompilerParameters cParams) { using (CodeDomProvider provider = new JScriptCodeProvider()) { CompilerResults res = provider.CompileAssemblyFromFile(cParams, sourceFile); return(res); } }
public ICompilerResult Compile() { CompilerParameters options = new CompilerParameters(); if (_target != null) { AddCompilerOption("/target:" + _target); } if (_platform != null) { AddCompilerOption("/platform:" + _platform); } //options.GenerateExecutable = _generateExecutable; options.GenerateExecutable = IsTargetExecutable(_target); options.GenerateInMemory = _generateInMemory; options.IncludeDebugInformation = _debugInformation; // WarningLevel : from http://msdn.microsoft.com/en-us/library/13b90fz7.aspx // 0 Turns off emission of all warning messages. // 1 Displays severe warning messages. // 2 Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members. // 3 Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false. // 4 (the default) Displays all level 3 warnings plus informational warnings. options.WarningLevel = _warningLevel; options.OutputAssembly = _outputAssembly; options.CompilerOptions = _compilerOptions; //foreach (string assembly in _referencedAssemblies) foreach (ReferencedAssembly assembly in _referencedAssemblies) { options.ReferencedAssemblies.Add(assembly.File); } foreach (ResourceFile resource in _embeddedResources) { options.EmbeddedResources.Add(resource.File); } CodeDomProvider provider = new JScriptCodeProvider(); CompilerResults result = provider.CompileAssemblyFromFile(options, _sources.ToArray()); provider.Dispose(); return(new CodeDomProviderCompilerResult(result)); }
public static Assembly LoadMacroAssembly(MacroModule mod) { if (mod.Type == MacroType.Assembly) { return(Assembly.LoadFrom(mod.Path)); } else if (mod.Type == MacroType.JavaScript) { ICodeCompiler compiler = new JScriptCodeProvider().CreateCompiler(); CompilerParameters param = new CompilerParameters(); param.CompilerOptions += "/debug"; param.GenerateInMemory = true; param.GenerateExecutable = true; //param.ReferencedAssemblies.Add("mscorlib"); //要るの? param.ReferencedAssemblies.Add("System.Drawing.dll"); param.ReferencedAssemblies.Add(GetMyExePath()); param.ReferencedAssemblies.Add(GetGTerminalPath()); foreach (string x in mod.AdditionalAssemblies) { if (x.Length > 0) { param.ReferencedAssemblies.Add(x); } } CompilerResults result = compiler.CompileAssemblyFromFile(param, mod.Path); if (result.Errors.Count > 0) { StringBuilder bld = new StringBuilder(); bld.Append(GApp.Strings.GetString("Message.MacroExec.FailedToCompileScript")); foreach (CompilerError err in result.Errors) { bld.Append(String.Format("Line {0} Column {1} : {2}\n", err.Line, err.Column, err.ErrorText)); } throw new Exception(bld.ToString()); } return(result.CompiledAssembly); } else { throw new Exception("Unsupported macro module type " + mod.Type.ToString() + " is specified."); } }
public static CompilerResults CompileFromJScriptSourceStream(Stream sourceStream, CompilerParameters cParams) { String tmpFile = Path.GetTempFileName(); using (FileStream output = new FileStream(tmpFile, FileMode.Create)) { byte[] buffer = new byte[sourceStream.Length]; sourceStream.Seek(0, SeekOrigin.Begin); sourceStream.Read(buffer, 0, buffer.Length); output.Write(buffer, 0, buffer.Length); } using (CodeDomProvider provider = new JScriptCodeProvider()) { CompilerResults res = provider.CompileAssemblyFromFile(cParams, tmpFile); File.Delete(tmpFile); return(res); } }
public static Assembly LoadMacroAssembly(MacroModule mod) { if (mod.Type == MacroType.Assembly) { return(Assembly.LoadFrom(mod.Path)); } else if (mod.Type == MacroType.JavaScript) { JScriptCodeProvider compiler = new JScriptCodeProvider(); CompilerParameters param = new CompilerParameters(); param.IncludeDebugInformation = true; param.GenerateInMemory = false; //これがプラグインをロードできるかどうかの決め手になった。周辺をすべて理解したわけではないが、とりあえずこれでよしとする。深入りしている時間はあまりないし param.GenerateExecutable = true; StringCollection sc = param.ReferencedAssemblies; bool[] assyAdded = new bool[9]; foreach (Assembly assy in AppDomain.CurrentDomain.GetAssemblies()) { try { string assyFilePath = new Uri(assy.CodeBase).LocalPath; string assyFileName = Path.GetFileName(assyFilePath).ToLower(CultureInfo.InvariantCulture); switch (assyFileName) { case "system.drawing.dll": assyAdded[0] = true; break; case "system.windows.forms.dll": assyAdded[1] = true; break; case "poderosa.plugin.dll": assyAdded[2] = true; break; case "poderosa.core.dll": assyAdded[3] = true; break; case "granados.dll": assyAdded[4] = true; break; case "poderosa.protocols.dll": assyAdded[5] = true; break; case "poderosa.terminalemulator.dll": assyAdded[6] = true; break; case "poderosa.terminalsession.dll": assyAdded[7] = true; break; case "poderosa.macro.dll": assyAdded[8] = true; break; case "poderosa.monolithic.exe": // FIXME: it is better to use the name of the entry assembly. // but how can we know the entry assembly is the monolithic type ? assyAdded[2] = assyAdded[3] = assyAdded[4] = assyAdded[5] = assyAdded[6] = assyAdded[7] = assyAdded[8] = true; break; default: continue; } Debug.WriteLine("(LoadMacroAssembly) add to ReferencedAssemblies: " + assyFilePath); sc.Add(assyFilePath); } catch (Exception) { } } foreach (bool flag in assyAdded) { if (!flag) { throw new Exception(MacroPlugin.Instance.Strings.GetString("Message.MacroExec.MissingAssemblies")); } } foreach (string x in mod.AdditionalAssemblies) { if (x.Length > 0) { sc.Add(x); } } CompilerResults result = compiler.CompileAssemblyFromFile(param, mod.Path); if (result.Errors.Count > 0) { StringBuilder bld = new StringBuilder(); bld.Append(MacroPlugin.Instance.Strings.GetString("Message.MacroExec.FailedToCompileScript")); foreach (CompilerError err in result.Errors) { bld.Append(String.Format("Line {0} Column {1} : {2}\n", err.Line, err.Column, err.ErrorText)); } throw new Exception(bld.ToString()); } Debug.WriteLineIf(DebugOpt.Macro, "Compiled:" + result.PathToAssembly + " FullName:" + result.CompiledAssembly.FullName); //AppDomain.CurrentDomain.Load(result.CompiledAssembly.FullName, result.Evidence); return(result.CompiledAssembly); } else { throw new Exception("Unsupported macro module type " + mod.Type.ToString() + " is specified."); } }