public void NumberInputFormats() { IScriptContext context = new ScriptContext(); object result = Script.RunCode( @" h = 0xAAFFAA; u = 3u; l = 31231231278l; ul = 23423234548ul; d = 3.2312d; f = 3424.123f; m = 23123.25434543m; n1 = 4e+3; n2 = 6.32e-3; ", context); Assert.AreEqual(0xAAFFAA, context.GetItem("h", true)); Assert.AreEqual(3u, context.GetItem("u", true)); Assert.AreEqual(31231231278L, context.GetItem("l", true)); Assert.AreEqual(23423234548ul, context.GetItem("ul", true)); Assert.AreEqual(3424.123f, context.GetItem("f", true)); Assert.AreEqual(3.2312d, context.GetItem("d", true)); Assert.AreEqual(23123.25434543m, context.GetItem("m", true)); Assert.AreEqual(4e+3, context.GetItem("n1", true)); Assert.AreEqual(6.32e-3, context.GetItem("n2", true)); }
public void SimpleFunctionScopeWithContract() { ScriptContext context = new ScriptContext(); IScriptScope scope = context.Scope; Script.RunCode(@" a = 4; b = 2; c = 3; function test1(a,b) global(c) [ pre(a>0); post(); invariant(); ] { c = a+b; a = 15; } test1(2,3);", context); Assert.AreEqual(scope, context.Scope); Assert.AreEqual(5, context.GetItem("c", true)); Assert.AreEqual(4, context.GetItem("a", true)); Assert.AreEqual(2, context.GetItem("b", true)); }
public void UsingScopeAccessingMethods() { IScriptContext ctx = new ScriptContext(); object rez = Script.RunCode(@"using(Math){ return Pow(2,10); }", ctx); Assert.AreEqual(Math.Pow(2,10), rez); Assert.AreEqual(RuntimeHost.NoVariable, ctx.Scope.GetItem("Pow", false)); }
public void TestTypeExprGenerics() { RuntimeHost.AddType("Int", typeof(int)); ScriptContext context = new ScriptContext(); object rez = Script.RunCode("a = new List<|Int|>(); return a is List<|Int|>;", context); Assert.IsTrue((bool)rez); }
public void TestTypeExpr() { RuntimeHost.AddType("Int", typeof(int)); ScriptContext context = new ScriptContext(); object rez = Script.RunCode("a = 2; return a is Int;", context); Assert.IsTrue((bool)rez); }
public void AstDomCompiler_CompileBinaryOperator1() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("x=1+1+1;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.AreEqual(3, context.GetItem("x", true)); Assert.AreEqual(3, context.Result); }
public void UsingScopeAccessingPropretySetter() { IScriptContext ctx = new ScriptContext(); UsingTest1 us = new UsingTest1(); ctx.SetItem("a", us); object rez = Script.RunCode(@"a.Width = 25;", ctx); Assert.AreEqual(25, us.Width); }
public void Benchmark() { IScriptContext context = new ScriptContext(); //Example 0: Machine ExecutableMachine machine = ExecutableMachine.Create(); int iterations = 2;// 10000000; //loops = 10000000 ValueOperation op0 = machine.CreateOperation<ValueOperation>(); op0.Value = iterations; SetValueOperation op1 = machine.CreateOperation<SetValueOperation>(); op1.Id = "loops"; // counter = 0 ValueOperation op2 = machine.CreateOperation<ValueOperation>(); op2.Value = 0; SetValueOperation op3 = machine.CreateOperation<SetValueOperation>(); op3.Id = "counter"; //while (loops > 0) RegisterOperation op4 = machine.CreateOperation<RegisterOperation>(); op4.Destination = MachineRegisters.BX; op4.Value = 0; GetValueOperation op5 = machine.CreateOperation<GetValueOperation>(); op5.Id = "loops"; CmpOperation op6 = machine.CreateOperation<CmpOperation>(); JmpIfOperation op7 = machine.CreateOperation<JmpIfOperation>(); op7.Offset = 8; //loops = loops -1; GetValueOperation op8 = machine.CreateOperation<GetValueOperation>(); op8.Id = "loops"; DecOperation op9 = machine.CreateOperation<DecOperation>(); SetValueOperation op10 = machine.CreateOperation<SetValueOperation>(); op10.Id = "loops"; //counter = counter + 1; GetValueOperation op11 = machine.CreateOperation<GetValueOperation>(); op11.Id = "counter"; IncOperation op12 = machine.CreateOperation<IncOperation>(); SetValueOperation op13 = machine.CreateOperation<SetValueOperation>(); op13.Id = "counter"; JmpOperation op14 = machine.CreateOperation<JmpOperation>(); op14.Offset = -10; machine.CreateOperation<RetOperation>(); machine.Execute(context); object rez = context.GetItem("counter", true); Assert.AreEqual(iterations, rez); }
public void BaseOperatorIs() { ScriptContext context = new ScriptContext(); Script.RunCode(@" a = 4; result = a is int; ", context); Assert.AreEqual(true, context.GetItem("result", true)); }
public void BaseOperatorsUnary() { ScriptContext context = new ScriptContext(); Script.RunCode(@" a = 4; b = 2; a++; b--; ", context); Assert.AreEqual(5, context.GetItem("a", true)); Assert.AreEqual(1, context.GetItem("b", true)); }
public void CodeValueReferenceTest() { IScriptContext context = new ScriptContext(); CodeProgram prog1 = new CodeProgram(); prog1.Statements.Add( new CodeExpressionStatement( new CodeAssignExpression("b", new CodeValueReference("Test B")))); CodeDomCompiler.Compile(prog1).Execute(context); Assert.AreEqual("Test B", context.GetItem("b", true)); }
public void CodeVariableReferenceTest() { object testObject = new object(); IScriptContext context = new ScriptContext(); context.SetItem("a", testObject); CodeProgram prog = new CodeProgram(); prog.Statements.Add( new CodeExpressionStatement( new CodeVariableReference("a"))); CodeDomCompiler.Compile(prog).Execute(context); Assert.AreEqual(testObject, context.Result); }
public void ContextReusageProblem() { var context = new ScriptContext(); string code1 = "'test';"; string code2 = "3*5; return 1+1;"; var result1 = Script.RunCode(code1, context, true); var script = Script.Compile(code2); script.Context = context; script.Execute(); Assert.AreEqual(2, context.Result); }
public void ChangingTheScope() { Script sc = Script.Compile("return A == 1 && B == 10;"); ScriptContext c1 = new ScriptContext(); c1.SetItem("A", 1); c1.SetItem("B", 2); sc.Context = c1; bool b1 = (bool)sc.Execute();// should give false Assert.IsFalse(b1); c1 = new ScriptContext(); c1.SetItem("A", 1); c1.SetItem("B", 10); sc.Context = c1; bool b2 = (bool)sc.Execute(); // should give true Assert.IsTrue(b2); }
public void MachineExection() { IScriptContext context = new ScriptContext(); //Example 0: Machine ExecutableMachine machine = ExecutableMachine.Create(); SetValueOperation sv = machine.CreateOperation<SetValueOperation>(); sv.Id = "a"; machine.AX = "Hello World"; machine.CreateOperation<RetOperation>(); machine.Execute(context); object rez = context.GetItem("a", true); Assert.AreEqual("Hello World", rez); }
public void ContextDependantEventsAndContextSwitching() { ScriptContext c = new ScriptContext(); c.SetItem("invoked", 0); ScriptContext c1 = new ScriptContext(); c1.SetItem("invoked", 10); Script s = Script.Compile( @" function handler(s,e) global(invoked) { invoked++; } ce = new ContextEvent(); ce.NameChanged += handler; return ce; " ); s.Context = c; ContextEvent resultVal = (ContextEvent)s.Execute(); resultVal.Name = "TestName"; Assert.AreEqual(1, c.GetItem("invoked", false)); s.Context = c1; resultVal.Name = "TestName2"; Assert.AreEqual(11, c1.GetItem("invoked", false)); s.Dispose(); //TODO: Event Broker should be refactored try { resultVal.Name = "TestName 4"; } catch (ScriptEventException e) { Assert.AreEqual(Strings.ContextNotFoundExceptionMessage, e.Message); } }
public void ArithmeticExpressions() { IScriptContext context = new ScriptContext(); object result = Script.RunCode( @" a=1.0; b = 2.0; c = 3.0; d = 2.0; e = 18.0; f = 6.0; p = 2.0; u = 3.0; v = 1.0; r = 2.0; s = 5.0; t = 12.0; // r1 = 9 r1 = a + b + c*d; // r2 = -2.5 r2 = a*(b - c/d )- e/f; //r3 = -4.5 r3 = a*b*((c - d )*a - p*(u - v)*(r + s))/t; //r4 = 65536 r4 = 2 * d^(c*5); //r5 = 2 r5 = 5 % 3; v1 = -3; ", context); Assert.AreEqual(9.0, context.GetItem("r1", true)); Assert.AreEqual(-2.5, context.GetItem("r2", true)); Assert.AreEqual(-4.5, context.GetItem("r3", true)); Assert.AreEqual((double)65536, context.GetItem("r4", true)); Assert.AreEqual((Int32)2, context.GetItem("r5", true)); Assert.AreEqual(-3, context.GetItem("v1", true)); }
public void VarLocalScope() { IScriptContext context = new ScriptContext(); object resultVal = Script.RunCode( @" var sum = 0; for (var x=0; x<10; x++){ var temp = x; sum += x; } return sum; ", context ); Assert.AreEqual(45, context.GetItem("sum", true)); Assert.AreEqual(RuntimeHost.NoVariable, context.GetItem("x", false)); }
public void VarLocalNestedScopes() { IScriptContext context = new ScriptContext(); object resultVal = Script.RunCode( @" var b; { a = 2; b = 4; { var b = 3; { var a; a = 2; } } } ", context ); Assert.AreEqual(2, context.GetItem("a", true)); Assert.AreEqual(4, context.GetItem("b", true)); }
public void AstDomCompiler_InvokeMember() { int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] b = new int[9]; IScriptContext context = new ScriptContext(); context.SetItem("a", a); context.SetItem("b", b); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return a.CopyTo(b, 0);", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.AreEqual(a[4], b[4]); }
/// <summary> /// Default constructor /// </summary> protected Script() { var ctxt = new ScriptContext(); RuntimeHost.InitializeScript(ctxt); Context = ctxt; }
public void ScopingExample_TestingVariable() { IScriptContext context = new ScriptContext(); context.SetFunction<DefinedFunction>("Defined"); object resultVal = Script.RunCode( @" x = 0; function g() { Defined('x'); } return g(); ", context); Assert.IsTrue((bool)resultVal); }
public void CreatingGlobalVariableDifferentCases() { IScriptContext context = new ScriptContext(); object resultVal = Script.RunCode( @" a = 2; ", context ); Assert.AreEqual(2, context.GetItem("a", true)); context = new ScriptContext(); resultVal = Script.RunCode( @" //Global scope { //Local scope 1 {//Local scope 2 //Create global variable from local scope a = 4; } } // a = 4 will be evaluated in global scope return a; ", context ); Assert.AreEqual(4, context.GetItem("a", true)); context = new ScriptContext(); resultVal = Script.RunCode( @" //Global scope { //Local scope 1 var a; //Create empty variable in local scope 1 {//Local scope 2 //This will set variable to top-most scope which contains //definition for variable, which is Local scope 1 a = 4; } } //Global scope still empty ", context ); Assert.AreEqual(RuntimeHost.NoVariable, context.GetItem("a", false)); context = new ScriptContext(); resultVal = Script.RunCode( @" //Global scope { //Local scope 1 var a; //Create empty variable in local scope 1 {//Local scope 2 //This will set variable to top-most scope which contains //definition for variable, which is Local scope 1 a = 5; { //Local scope 3 var a; //Create empty variable in local scope 3 //This will set variable to top-most scope which contains //definition for variable, which is Local scope 1 global:a = 4; a = 3; // Set local variable } } //Create variable in global scope b = a; } //b = 4; return b; ", context ); Assert.AreEqual(4, context.GetItem("b", false)); }
public void AstDomCompiler_PlusPlus_MinusMinus() { IScriptContext context = new ScriptContext(); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=0; i<10; i++) sum = sum + i;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.AreEqual(45, context.GetItem("sum", true)); domTree = AstDomCompiler.Compile(Script.Compile("sum=0;for(i=9; i>=0; i--) sum = sum + i;", null, false).Ast); CodeDomCompiler.Compile(domTree).Execute(context); Assert.AreEqual(45, context.GetItem("sum", true)); }
public void DecimalPlusTests() { System.Int16 i16 = 10; System.Int32 i32 = 10; System.Int64 i64 = 10; System.Double d = 10; System.Single f = 10; System.Decimal dc = 2; object rez = null; IScriptContext context = new ScriptContext(); context.SetItem("i16", i16); context.SetItem("i32", i32); context.SetItem("i64", i64); context.SetItem("d", d); context.SetItem("f", f); context.SetItem("dc", dc); rez = Script.RunCode(@" return i16 + dc; ", context); Assert.AreEqual(i16 + dc, rez); rez = Script.RunCode(@" return i32 + dc; ", context); Assert.AreEqual(i32 + dc, rez); rez = Script.RunCode(@" return i64 + dc; ", context); Assert.AreEqual(i64 + dc, rez); rez = Script.RunCode(@" return new decimal(f) + dc; ", context); Assert.AreEqual(new decimal(f) + dc, rez); rez = Script.RunCode(@" return new decimal(d) + dc; ", context); Assert.AreEqual(new decimal(d) + dc, rez); // Reverse rez = Script.RunCode(@" return dc + i16; ", context); Assert.AreEqual(dc + i16, rez); rez = Script.RunCode(@" return dc + i32; ", context); Assert.AreEqual(dc + i32, rez); rez = Script.RunCode(@" return dc + i64; ", context); Assert.AreEqual(dc + i64, rez); rez = Script.RunCode(@" return dc + new decimal(f); ", context); Assert.AreEqual(dc + new decimal(f), rez); rez = Script.RunCode(@" return dc + new decimal(d); ", context); Assert.AreEqual(dc + new decimal(d), rez); }
public void ContextSwitching() { Script s = Script.Compile("return A;"); ScriptContext sc = new ScriptContext(); sc.SetItem("A", 1); s.Context = sc; Assert.AreEqual(1, s.Execute()); sc = new ScriptContext(); sc.SetItem("A", 10); s.Context = sc; Assert.AreEqual(10, s.Execute()); }
private void SwitchContext(ScriptContext ctxt, bool isAssigning) { if (ctxt == null) return; if (isAssigning) { ctxt.SetOwner(this); } else { var refs = _referenceCache.ToArray(); foreach (var r in refs) r.Remove(); //Verify all references were cleared Diagnostics.Assumes.IsTrue(_referenceCache.Count == 0); ctxt.SetOwner(null); } }
public void StringParsingError() { IScriptContext context = new ScriptContext(); Script s = Script.Compile( @" a = 'aaabbbccc; b = '\'aaa'; c = 'cc\\aaa'; d = @'hello world \\'; "); s.Context = context; s.Execute(); Assert.AreEqual("aaabbbccc", context.GetItem("a", true)); Assert.AreEqual("'aaa", context.GetItem("b", true)); Assert.AreEqual("cc\\aaa", context.GetItem("c", true)); Assert.AreEqual("hello world \\\\", context.GetItem("d", true)); }
public void VarCreatesNullVariable() { IScriptContext context = new ScriptContext(); object resultVal = Script.RunCode( @" var sum; ", context ); Assert.IsNull(context.GetItem("sum", true)); resultVal = Script.RunCode( @" var sum = 2; ", context ); Assert.AreEqual(2, context.GetItem("sum", true)); }
public void AstDomCompiler_InvokeMember3() { VM_Test1 vt1 = new VM_Test1(); IScriptContext context = new ScriptContext(); context.SetItem("v", vt1); CodeProgram domTree = AstDomCompiler.Compile(Script.Compile("return v.Next.Next;", null, false).Ast); ExecutableMachine vm = CodeDomCompiler.Compile(domTree); vm.Execute(context); Assert.AreEqual(2, ((VM_Test1)context.Result).Level); }