public void breaking_and_restarting_an_evaluation(string s) { ScriptEngine engine = new ScriptEngine(); Expr e = ExprAnalyser.AnalyseString(s); RuntimeObj syncResult; using (var r1 = engine.Execute(e)) { Assert.That(r1.Status, Is.EqualTo(ScriptEngineStatus.IsFinished)); syncResult = r1.CurrentResult; } engine.Breakpoints.BreakAlways = true; using (var r2 = engine.Execute(e)) { int nbStep = 0; while (r2.CanContinue) { ++nbStep; r2.Continue(); } Assert.That(r2.Status, Is.EqualTo(ScriptEngineStatus.IsFinished)); Assert.That(new RuntimeObjComparer(r2.CurrentResult, syncResult).AreEqualStrict(engine.Context)); Console.WriteLine("String '{0}' = {1} evaluated in {2} steps.", s, syncResult.ToString(), nbStep); } }
public void EmptyParsing() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset(""); Assert.That(p.IsEndOfInput); Expr e = a.Analyse(p); Assert.That(e is SyntaxErrorExpr); } { p.Reset(" \r\n \n \r \n \t "); Assert.That(p.IsEndOfInput); Expr e = a.Analyse(p); Assert.That(e is SyntaxErrorExpr); } }
static public void RunNormalAndStepByStep(string script, Action <RuntimeObj> test, GlobalContext ctx = null) { var e = ExprAnalyser.AnalyseString(script); // Tests the empty, default, visitor: no change must have been made to the AST. var emptyVisitor = new ExprVisitor(); Assert.That(emptyVisitor.VisitExpr(e), Is.SameAs(e)); // Evaluates result directly. RuntimeObj syncResult = ScriptEngine.Evaluate(e, ctx); test(syncResult); // Step-by-step evaluation. ScriptEngine engine = new ScriptEngine(ctx); engine.Breakpoints.BreakAlways = true; ExecAsync(script, test, null, e, syncResult, engine, true); }
public void BadNumbers() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset("45DD"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset("45.member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset(".45.member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset("45.01member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset(".45.member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset(".45.01member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } { p.Reset("45.01e23member"); Assert.That(p.IsErrorOrEndOfInput, Is.True); Assert.That(p.ErrorCode, Is.EqualTo(JSTokenizerError.ErrorNumberIdentifierStartsImmediately)); } }
public void ArraySupport() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset("a[9]"); Assert.That(p.IsErrorOrEndOfInput, Is.False); Expr e = a.Analyse(p); Assert.That(e is AccessorIndexerExpr); AccessorIndexerExpr ac = e as AccessorIndexerExpr; IsConstant(ac.Index, 9); } { p.Reset("array['Hello World!']"); Assert.That(p.IsErrorOrEndOfInput, Is.False); Expr e = a.Analyse(p); Assert.That(e is AccessorIndexerExpr); AccessorIndexerExpr ac = e as AccessorIndexerExpr; IsConstant(ac.Index, "Hello World!"); } }
public void SimpleExpression() { ExprAnalyser a = new ExprAnalyser(); JSTokenizer p = new JSTokenizer(); { p.Reset("value"); Assert.That(p.IsErrorOrEndOfInput, Is.False); Expr e = a.Analyse(p); Assert.That(e is AccessorMemberExpr); AccessorMemberExpr ac = e as AccessorMemberExpr; Assert.That(ac.IsUnbound == true); } { p.Reset("!"); Expr e = a.Analyse(p); Assert.That(e is UnaryExpr); UnaryExpr u = e as UnaryExpr; Assert.That(u.TokenType == JSTokenizerToken.Not); Assert.That(u.Expression is SyntaxErrorExpr); Assert.That(SyntaxErrorCollector.Collect(e, null).Count == 1); } { p.Reset("!value"); Expr e = a.Analyse(p); Assert.That(e is UnaryExpr); UnaryExpr u = e as UnaryExpr; Assert.That(u.TokenType == JSTokenizerToken.Not); Assert.That(u.Expression is AccessorExpr); Assert.That(SyntaxErrorCollector.Collect(e, Util.ActionVoid).Count == 0); } { p.Reset(" 0.12e43 && ~b "); Expr e = a.Analyse(p); Assert.That(e is BinaryExpr); BinaryExpr and = e as BinaryExpr; Assert.That(and.BinaryOperatorToken == JSTokenizerToken.And); IsConstant(and.Left, 0.12e43); Assert.That(and.Right is UnaryExpr); UnaryExpr u = and.Right as UnaryExpr; Assert.That(u.TokenType == JSTokenizerToken.BitwiseNot); Assert.That(u.Expression is AccessorExpr); Assert.That(SyntaxErrorCollector.Collect(e, Util.ActionVoid).Count == 0); } { p.Reset(@"!a||~""x"""); Expr e = a.Analyse(p); Assert.That(e is BinaryExpr); BinaryExpr or = e as BinaryExpr; Assert.That(or.BinaryOperatorToken == JSTokenizerToken.Or); Assert.That(or.Left is UnaryExpr); Assert.That(or.Right is UnaryExpr); UnaryExpr u = or.Right as UnaryExpr; Assert.That(u.TokenType == JSTokenizerToken.BitwiseNot); IsConstant(u.Expression, "x"); Assert.That(SyntaxErrorCollector.Collect(e, Util.ActionVoid).Count == 0); } { p.Reset("(3)"); Expr e = a.Analyse(p); IsConstant(e, 3); } { p.Reset("(3+typeof 'x')"); Expr e = a.Analyse(p); Assert.That(e is BinaryExpr); BinaryExpr b = e as BinaryExpr; IsConstant(b.Left, 3); Assert.That(b.Right is UnaryExpr); UnaryExpr u = b.Right as UnaryExpr; Assert.That(u.TokenType == JSTokenizerToken.TypeOf); IsConstant(u.Expression, "x"); Assert.That(SyntaxErrorCollector.Collect(e, Util.ActionVoid).Count == 0); } { p.Reset("1 ? 2 : 3"); Expr e = a.Analyse(p); Assert.That(e is IfExpr); IfExpr i = e as IfExpr; Assert.That(i.IsTernaryOperator == true); IsConstant(i.Condition, 1); IsConstant(i.WhenTrue, 2); IsConstant(i.WhenFalse, 3); } }