public virtual void InitFromContext(Context cx) { SetErrorReporter(cx.GetErrorReporter()); languageVersion = cx.GetLanguageVersion(); generateDebugInfo = (!cx.IsGeneratingDebugChanged() || cx.IsGeneratingDebug()); reservedKeywordAsIdentifier = cx.HasFeature(Context.FEATURE_RESERVED_KEYWORD_AS_IDENTIFIER); allowMemberExprAsFunctionName = cx.HasFeature(Context.FEATURE_MEMBER_EXPR_AS_FUNCTION_NAME); strictMode = cx.HasFeature(Context.FEATURE_STRICT_MODE); warningAsError = cx.HasFeature(Context.FEATURE_WARNING_AS_ERROR); xmlAvailable = cx.HasFeature(Context.FEATURE_E4X); optimizationLevel = cx.GetOptimizationLevel(); generatingSource = cx.IsGeneratingSource(); activationNames = cx.activationNames; // Observer code generation in compiled code : generateObserverCount = cx.generateObserverCount; }
/// <summary>The eval function property of the global object.</summary> /// <remarks> /// The eval function property of the global object. /// See ECMA 15.1.2.1 /// </remarks> public static object EvalSpecial(Context cx, Scriptable scope, object thisArg, object[] args, string filename, int lineNumber) { if (args.Length < 1) { return Undefined.instance; } object x = args[0]; if (!(x is CharSequence)) { if (cx.HasFeature(Context.FEATURE_STRICT_MODE) || cx.HasFeature(Context.FEATURE_STRICT_EVAL)) { throw Context.ReportRuntimeError0("msg.eval.nonstring.strict"); } string message = ScriptRuntime.GetMessage0("msg.eval.nonstring"); Context.ReportWarning(message); return x; } if (filename == null) { int[] linep = new int[1]; filename = Context.GetSourcePositionFromStack(linep); if (filename != null) { lineNumber = linep[0]; } else { filename = string.Empty; } } string sourceName = ScriptRuntime.MakeUrlForGeneratedScript(true, filename, lineNumber); ErrorReporter reporter; reporter = DefaultErrorReporter.ForEval(cx.GetErrorReporter()); Evaluator evaluator = Context.CreateInterpreter(); if (evaluator == null) { throw new JavaScriptException("Interpreter not present", filename, lineNumber); } // Compile with explicit interpreter instance to force interpreter // mode. Script script = cx.CompileString(x.ToString(), evaluator, reporter, sourceName, 1, null); evaluator.SetEvalScriptFlag(script); Callable c = (Callable)script; return c.Call(cx, scope, (Scriptable)thisArg, ScriptRuntime.emptyArgs); }
public object Run(Context cx) { status.Running(jsFile); testState.errors = new ShellTest.ErrorReporterWrapper(cx.GetErrorReporter()); cx.SetErrorReporter(testState.errors); global.Init(cx); try { ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile().GetParentFile().GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile().GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, new FilePath(jsFile.GetParentFile(), "shell.js")); ShellTest.RunFileIfExists(cx, global, jsFile); status.HadErrors(jsFile, Sharpen.Collections.ToArray(testState.errors.errors, new ShellTest.Status.JsError[0])); } catch (ThreadDeath) { } catch (Exception t) { status.Threw(t); } return null; }
public virtual int RunDoctest(Context cx, Scriptable scope, string session, string sourceName, int lineNumber) { doctestCanonicalizations = new Dictionary<string, string>(); string[] lines = session.Split("[\n\r]+"); string prompt0 = this.prompts[0].Trim(); string prompt1 = this.prompts[1].Trim(); int testCount = 0; int i = 0; while (i < lines.Length && !lines[i].Trim().StartsWith(prompt0)) { i++; } // skip lines that don't look like shell sessions while (i < lines.Length) { string inputString = Sharpen.Runtime.Substring(lines[i].Trim(), prompt0.Length); inputString += "\n"; i++; while (i < lines.Length && lines[i].Trim().StartsWith(prompt1)) { inputString += Sharpen.Runtime.Substring(lines[i].Trim(), prompt1.Length); inputString += "\n"; i++; } string expectedString = string.Empty; while (i < lines.Length && !lines[i].Trim().StartsWith(prompt0)) { expectedString += lines[i] + "\n"; i++; } TextWriter savedOut = this.GetOut(); TextWriter savedErr = this.GetErr(); MemoryStream @out = new MemoryStream(); MemoryStream err = new MemoryStream(); this.SetOut(new TextWriter(@out)); this.SetErr(new TextWriter(err)); string resultString = string.Empty; ErrorReporter savedErrorReporter = cx.GetErrorReporter(); cx.SetErrorReporter(new ToolErrorReporter(false, this.GetErr())); try { testCount++; object result = cx.EvaluateString(scope, inputString, "doctest input", 1, null); if (result != Context.GetUndefinedValue() && !(result is Function && inputString.Trim().StartsWith("function"))) { resultString = Context.ToString(result); } } catch (RhinoException e) { ToolErrorReporter.ReportException(cx.GetErrorReporter(), e); } finally { this.SetOut(savedOut); this.SetErr(savedErr); cx.SetErrorReporter(savedErrorReporter); resultString += err.ToString() + @out.ToString(); } if (!DoctestOutputMatches(expectedString, resultString)) { string message = "doctest failure running:\n" + inputString + "expected: " + expectedString + "actual: " + resultString + "\n"; if (sourceName != null) { throw Context.ReportRuntimeError(message, sourceName, lineNumber + i - 1, null, 0); } else { throw Context.ReportRuntimeError(message); } } } return testCount; }
private static Script Compile(Context cx, string source) { int[] linep = new int[] { 0 }; string filename = Context.GetSourcePositionFromStack(linep); if (filename == null) { filename = "<Script object>"; linep[0] = 1; } ErrorReporter reporter; reporter = DefaultErrorReporter.ForEval(cx.GetErrorReporter()); return cx.CompileString(source, null, reporter, filename, linep[0], null); }
private static object JsConstructor(Context cx, Scriptable scope, object[] args) { int arglen = args.Length; StringBuilder sourceBuf = new StringBuilder(); sourceBuf.Append("function "); if (cx.GetLanguageVersion() != Context.VERSION_1_2) { sourceBuf.Append("anonymous"); } sourceBuf.Append('('); // Append arguments as coma separated strings for (int i = 0; i < arglen - 1; i++) { if (i > 0) { sourceBuf.Append(','); } sourceBuf.Append(ScriptRuntime.ToString(args[i])); } sourceBuf.Append(") {"); if (arglen != 0) { // append function body string funBody = ScriptRuntime.ToString(args[arglen - 1]); sourceBuf.Append(funBody); } sourceBuf.Append("\n}"); string source = sourceBuf.ToString(); int[] linep = new int[1]; string filename = Context.GetSourcePositionFromStack(linep); if (filename == null) { filename = "<eval'ed string>"; linep[0] = 1; } string sourceURI = ScriptRuntime.MakeUrlForGeneratedScript(false, filename, linep[0]); Scriptable global = ScriptableObject.GetTopLevelScope(scope); ErrorReporter reporter; reporter = DefaultErrorReporter.ForEval(cx.GetErrorReporter()); Evaluator evaluator = Context.CreateInterpreter(); if (evaluator == null) { throw new JavaScriptException("Interpreter not present", filename, linep[0]); } // Compile with explicit interpreter instance to force interpreter // mode. return cx.CompileFunction(global, source, evaluator, reporter, sourceURI, 1, null); }