public Program() { reporter = new ToolErrorReporter(true); compilerEnv = new CompilerEnvirons(); compilerEnv.SetErrorReporter(reporter); compiler = new ClassCompiler(compilerEnv); }
/// <summary> /// Asserts that the value returned by /// <see cref="Rhino.Ast.AstNode.ToSource()">Rhino.Ast.AstNode.ToSource()</see> /// after /// the given input source was parsed equals the specified expected output source. /// </summary> /// <param name="source">the JavaScript source to be parsed</param> /// <param name="expectedOutput"> /// the JavaScript source that is expected to be /// returned by /// <see cref="Rhino.Ast.AstNode.ToSource()">Rhino.Ast.AstNode.ToSource()</see> /// </param> private void AssertSource(string source, string expectedOutput) { CompilerEnvirons env = new CompilerEnvirons(); env.SetLanguageVersion(Context.VERSION_1_7); Parser parser = new Parser(env); AstRoot root = parser.Parse(source, null, 0); NUnit.Framework.Assert.AreEqual(expectedOutput, root.ToSource()); }
private AstRoot Parse(CharSequence cs) { CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.InitFromContext(cx); ErrorReporter compilationErrorReporter = compilerEnv.GetErrorReporter(); Parser p = new Parser(compilerEnv, compilationErrorReporter); return p.Parse(cs.ToString(), "<eval>", 1); }
/// <summary> /// Construct ClassCompiler that uses the specified compiler environment /// when generating classes. /// </summary> /// <remarks> /// Construct ClassCompiler that uses the specified compiler environment /// when generating classes. /// </remarks> public ClassCompiler(CompilerEnvirons compilerEnv) { if (compilerEnv == null) { throw new ArgumentException(); } this.compilerEnv = compilerEnv; this.mainMethodClassName = Codegen.DEFAULT_MAIN_METHOD_CLASS; }
/// <summary> /// Compiles /// <code>source</code> /// and returns the transformed and optimized /// <see cref="Rhino.Ast.ScriptNode">Rhino.Ast.ScriptNode</see> /// </summary> protected internal virtual ScriptNode Compile(CharSequence source) { string mainMethodClassName = "Main"; string scriptClassName = "Main"; CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.InitFromContext(cx); ErrorReporter compilationErrorReporter = compilerEnv.GetErrorReporter(); Parser p = new Parser(compilerEnv, compilationErrorReporter); AstRoot ast = p.Parse(source.ToString(), "<eval>", 1); IRFactory irf = new IRFactory(compilerEnv); ScriptNode tree = irf.TransformTree(ast); Codegen codegen = new Codegen(); codegen.SetMainMethodClass(mainMethodClassName); codegen.CompileToClassFile(compilerEnv, scriptClassName, tree, tree.GetEncodedSource(), false); return tree; }
public virtual object Compile(CompilerEnvirons compilerEnv, ScriptNode tree, string encodedSource, bool returnFunction) { int serial; lock (globalLock) { serial = ++globalSerialClassCounter; } string baseName = "c"; if (tree.GetSourceName().Length > 0) { baseName = tree.GetSourceName().ReplaceAll("\\W", "_"); if (!char.IsJavaIdentifierStart(baseName[0])) { baseName = "_" + baseName; } } string mainClassName = "org.mozilla.javascript.gen." + baseName + "_" + serial; byte[] mainClassBytes = CompileToClassFile(compilerEnv, mainClassName, tree, encodedSource, returnFunction); return new object[] { mainClassName, mainClassBytes }; }
// fixupTable[i] = (label_index << 32) | fixup_site // ECF_ or Expression Context Flags constants: for now only TAIL public virtual InterpreterData Compile(CompilerEnvirons compilerEnv, ScriptNode tree, string encodedSource, bool returnFunction) { this.compilerEnv = compilerEnv; new NodeTransformer().Transform(tree); if (returnFunction) { scriptOrFn = tree.GetFunctionNode(0); } else { scriptOrFn = tree; } itsData = new InterpreterData(compilerEnv.GetLanguageVersion(), scriptOrFn.GetSourceName(), encodedSource, ((AstRoot)tree).IsInStrictMode()); itsData.topLevel = true; if (returnFunction) { GenerateFunctionICode(); } else { GenerateICodeFromTree(scriptOrFn); } return itsData; }
public object Compile(CompilerEnvirons compilerEnv, ScriptNode tree, string encodedSource, bool returnFunction) { CodeGenerator cgen = new CodeGenerator(); itsData = cgen.Compile(compilerEnv, tree, encodedSource, returnFunction); return itsData; }
public virtual byte[] CompileToClassFile(CompilerEnvirons compilerEnv, string mainClassName, ScriptNode scriptOrFn, string encodedSource, bool returnFunction) { this.compilerEnv = compilerEnv; Transform(scriptOrFn); if (returnFunction) { scriptOrFn = scriptOrFn.GetFunctionNode(0); } InitScriptNodesData(scriptOrFn); this.mainClassName = mainClassName; this.mainClassSignature = ClassFileWriter.ClassNameToSignature(mainClassName); try { return GenerateCode(encodedSource); } catch (ClassFileWriter.ClassFileFormatException e) { throw ReportClassFileFormatException(scriptOrFn, e.Message); } }
protected virtual void SetUp() { environment = new CompilerEnvirons(); }
public IRFactory(CompilerEnvirons env, ErrorReporter errorReporter) : base(env, errorReporter) { }
public IRFactory(CompilerEnvirons env) : this(env, env.GetErrorReporter()) { }
/// <exception cref="System.IO.IOException"></exception> private object CompileImpl(Scriptable scope, TextReader sourceReader, string sourceString, string sourceName, int lineno, object securityDomain, bool returnFunction, Evaluator compiler, ErrorReporter compilationErrorReporter) { if (sourceName == null) { sourceName = "unnamed script"; } if (securityDomain != null && GetSecurityController() == null) { throw new ArgumentException("securityDomain should be null if setSecurityController() was never called"); } // One of sourceReader or sourceString has to be null if (!(sourceReader == null ^ sourceString == null)) { Kit.CodeBug(); } // scope should be given if and only if compiling function if (!(scope == null ^ returnFunction)) { Kit.CodeBug(); } CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.InitFromContext(this); if (compilationErrorReporter == null) { compilationErrorReporter = compilerEnv.GetErrorReporter(); } if (debugger != null) { if (sourceReader != null) { sourceString = Kit.ReadReader(sourceReader); sourceReader = null; } } Parser p = new Parser(compilerEnv, compilationErrorReporter); if (returnFunction) { p.calledByCompileFunction = true; } AstRoot ast; if (sourceString != null) { ast = p.Parse(sourceString, sourceName, lineno); } else { ast = p.Parse(sourceReader, sourceName, lineno); } if (returnFunction) { // parser no longer adds function to script node if (!(ast.GetFirstChild() != null && ast.GetFirstChild().GetType() == Token.FUNCTION)) { // XXX: the check just looks for the first child // and allows for more nodes after it for compatibility // with sources like function() {};;; throw new ArgumentException("compileFunction only accepts source with single JS function: " + sourceString); } } IRFactory irf = new IRFactory(compilerEnv, compilationErrorReporter); ScriptNode tree = irf.TransformTree(ast); // discard everything but the IR tree p = null; ast = null; irf = null; if (compiler == null) { compiler = CreateCompiler(); } object bytecode = compiler.Compile(compilerEnv, tree, tree.GetEncodedSource(), returnFunction); if (debugger != null) { if (sourceString == null) { Kit.CodeBug(); } if (bytecode is DebuggableScript) { DebuggableScript dscript = (DebuggableScript)bytecode; NotifyDebugger_r(this, dscript, sourceString); } else { throw new Exception("NOT SUPPORTED"); } } object result; if (returnFunction) { result = compiler.CreateFunctionObject(this, scope, bytecode, securityDomain); } else { result = compiler.CreateScriptObject(bytecode, securityDomain); } return result; }
/// <summary>Check whether a string is ready to be compiled.</summary> /// <remarks> /// Check whether a string is ready to be compiled. /// <p> /// stringIsCompilableUnit is intended to support interactive compilation of /// JavaScript. If compiling the string would result in an error /// that might be fixed by appending more source, this method /// returns false. In every other case, it returns true. /// <p> /// Interactive shells may accumulate source lines, using this /// method after each new line is appended to check whether the /// statement being entered is complete. /// </remarks> /// <param name="source">the source buffer to check</param> /// <returns>whether the source is ready for compilation</returns> /// <since>1.4 Release 2</since> public bool StringIsCompilableUnit(string source) { bool errorseen = false; CompilerEnvirons compilerEnv = new CompilerEnvirons(); compilerEnv.InitFromContext(this); // no source name or source text manager, because we're just // going to throw away the result. compilerEnv.SetGeneratingSource(false); Parser p = new Parser(compilerEnv, DefaultErrorReporter.instance); try { p.Parse(source, null, 1); } catch (EvaluatorException) { errorseen = true; } // Return false only if an error occurred as a result of reading past // the end of the file, i.e. if the source could be fixed by // appending more source. if (errorseen && p.Eof()) { return false; } else { return true; } }
/// <summary> /// Returns a /// <code>CompilerEnvirons</code> /// suitable for using Rhino /// in an IDE environment. Most features are enabled by default. /// The /// <see cref="ErrorReporter">ErrorReporter</see> /// is set to an /// <see cref="Rhino.Ast.ErrorCollector">Rhino.Ast.ErrorCollector</see> /// . /// </summary> public static Rhino.CompilerEnvirons IdeEnvirons() { Rhino.CompilerEnvirons env = new Rhino.CompilerEnvirons(); env.SetRecoverFromErrors(true); env.SetRecordingComments(true); env.SetStrictMode(true); env.SetWarnTrailingComma(true); env.SetLanguageVersion(170); env.SetReservedKeywordAsIdentifier(true); env.SetIdeMode(true); env.SetErrorReporter(new ErrorCollector()); return env; }