public object Eval(string scriptCode) { object ret = null; try { this.SetParent((ScriptObject)engine.globalScope.GetObject()); StackFrame sf = new StackFrame(this, null, new object[0], this); engine.Globals.ScopeStack.Push(sf); Context context = new Context(new DocumentContext("eval code", engine), ((IConvertible)scriptCode).ToString()); JSParser p = new JSParser(context); Block b = p.ParseEvalBody(); AST a = b.PartiallyEvaluate(); Completion result = (Completion)a.Evaluate(); ret = ((Completion)result).value; } finally { engine.Globals.ScopeStack.Pop(); } return(ret); }
// Test parser creation. public void TestParserCreate() { JSParser parser = JSParserTest.TestCreateParser(""); AssertNotNull("Create (1)", parser); AssertNotNull("Create (2)", parser.Parse()); parser = JSParserTest.TestCreateParser(""); AssertNotNull("Create (3)", parser); AssertNotNull("Create (4)", parser.ParseEvalBody()); }
// Parse an eval statement and check for failure. private static void No(String tag, String source) { try { JSParser parser = JSParserTest.TestCreateParser(source); Block block = parser.ParseEvalBody(); Fail(tag + " [1]"); } catch (JScriptException e) { AssertEquals(tag + "[2]", ((int)(JSError.SyntaxError)) + unchecked ((int)0x800A0000), e.ErrorNumber); } }
// Test a simple parse to see if the JNode tree is created correctly. public void TestParserSimple() { JSParser parser = JSParserTest.TestCreateParser("x = 3"); Block block = parser.ParseEvalBody(); Object jnode = JSParserTest.TestJNodeGet(block); // Top-level jnode should be JBlock. AssertEquals("Simple (1)", "JBlock", JSParserTest.TestJNodeGetKind(jnode)); // Should have a single expression statement. Object statements = JSParserTest.TestJNodeGetField(jnode, "statements"); AssertEquals("Simple (2)", "JExprStmt", JSParserTest.TestJNodeGetKind(statements)); // The expression statement should contain an assignment. Object assign = JSParserTest.TestJNodeGetField(statements, "expr"); AssertEquals("Simple (3)", "JAssign", JSParserTest.TestJNodeGetKind(assign)); // Left-hand side should be the identifier "x". Object lhs = JSParserTest.TestJNodeGetField(assign, "expr1"); AssertEquals("Simple (4)", "JIdentifier", JSParserTest.TestJNodeGetKind(lhs)); AssertEquals("Simple (5)", "x", JSParserTest.TestJNodeGetField(lhs, "name")); // Right-hand side should be the integer 3. Object rhs = JSParserTest.TestJNodeGetField(assign, "expr2"); AssertEquals("Simple (6)", "JConstant", JSParserTest.TestJNodeGetKind(rhs)); Object value = JSParserTest.TestJNodeGetField(rhs, "value"); Assert("Simple (7)", (value is Int32)); AssertEquals("Simple (7)", 3, (int)value); }
public static xblock GetCodeBlock(string code) { xblock ret = null; Microsoft.JScript.Vsa.VsaEngine engine = new Microsoft.JScript.Vsa.VsaEngine(); StringWriter sw = new StringWriter(); engine.InitVsaEngine("test", new VsaSite(sw)); DocumentContext docContext = new DocumentContext("", engine); Context context = new Context(docContext, code); JSParser parser = new JSParser(context); Block block = parser.ParseEvalBody(); ret = new xjscript.xblock(block); engine.Close(); // MessageBox.Show(((Completion)block.Evaluate()).value.ToString()); return(ret); }
// Parse an eval statement and check for success. private static void Yes(String tag, String source) { // Parse the source. JSParser parser = JSParserTest.TestCreateParser(source); Block block = parser.ParseEvalBody(); Object jnode = JSParserTest.TestJNodeGet(block); // Determine the location of the source end. int posn = 0; int line = 1; int temp; int endColumn = source.Length; while (posn < source.Length) { temp = source.IndexOf('\n', posn); if (temp == -1) { endColumn = source.Length - posn; break; } ++line; posn = temp + 1; } // Check that the full node's context is appropriate. Context context = JSParserTest.TestJNodeGetContext(jnode); AssertEquals(tag + " [1]", 0, context.StartColumn); AssertEquals(tag + " [2]", 1, context.StartLine); AssertEquals(tag + " [3]", 0, context.StartPosition); AssertEquals(tag + " [4]", endColumn, context.EndColumn); AssertEquals(tag + " [5]", line, context.EndLine); AssertEquals(tag + " [6]", source.Length, context.EndPosition); AssertEquals(tag + " [7]", source, context.GetCode()); }