private static void Test1(DWSContext context) { var t = new DWSTypeDefinition("TPoint"); t.Fields.Add(new DWSFieldDefinition("x", "Integer")); t.Fields.Add(new DWSFieldDefinition("y", "Integer")); var m = new DWSMethodDefinition("ReturnFirstArg", (c, a) => { var v = c.CreateTypedValue("TPoint"); object o = v.Value; v["x"] = 10; return(v); }); m.Args.Add(new DWSParameterDefinition("a", "Integer")); m.ReturnTypeName = "TPoint"; context.DefineType(t); context.DefineMethod(m); string s = context.EvaluateScript("var t : TPoint = ReturnFirstArg(2); ReturnFirstArg(t.x);"); GC.KeepAlive(m); }
public void DefineMethod_AsRecordFunction() { var t = new DWSTypeDefinition("TCustomType"); t.Fields.Add(new DWSFieldDefinition("x", "Integer")); t.Fields.Add(new DWSFieldDefinition("y", "Integer")); t.IsStruct = true; context.DefineType(t); var f = new DWSMethodDefinition("Test", (x, a) => { var v = x.CreateTypedValue("TCustomType"); v["x"] = 123; return(v); }); f.ReturnTypeName = "TCustomType"; context.DefineMethod(f); string rv = context.EvaluateScript(@"var a : TCustomType = Test(); Print(IntToStr(a.x));"); Assert.IsNull(this.LastErrorMessage); Assert.AreEqual("123", rv); GC.KeepAlive(f); }
public void EvaluateScript_ExecuteAndThrowCLRException() { var exception = new InvalidOperationException(); var action = new DWSMethodDefinition("Test", (x, a) => { throw exception; }); context.DefineMethod(action); context.EvaluateScript(@"Test();"); Assert.AreEqual(string.Format("Runtime Error: {0} in Test [line: 1, column: 1]\r\n", exception.Message.Replace(".", "")), this.LastErrorMessage); }
public void DefineMethod_AsProcedure() { var action = new DWSMethodDefinition("Test", (x, a) => { }); context.DefineMethod(action); context.EvaluateScript("Test();"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(action); }
public void DefineMethod_AsIntegerFunction() { var f = new DWSMethodDefinition("Test", (x, a) => 1); f.ReturnTypeName = "Integer"; context.DefineMethod(f); context.EvaluateScript("Test();"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(f); }
private static void Test3(DWSContext context) { var a = new DWSArrayDefinition("TIntArray", "Integer"); context.DefineType(a); var method = new DWSMethodDefinition("CreateArrayTest", new NativeAction(CreteArrayTest)); context.DefineMethod(method); context.EvaluateScript("CreateArrayTest();"); GC.KeepAlive(method); }
public void DynamicArray_TestStringArrayLength() { context.DefineType(new DWSArrayDefinition("TStringArray", "String")); var action = new DWSMethodDefinition("Test", (x, a) => { var array = new DWSArray(a[0]); int length = 0; Assert.DoesNotThrow(() => length = array.Length); Assert.AreEqual(3, length); }); action.Args.Add(new DWSParameterDefinition("a", "TStringArray")); context.DefineMethod(action); context.EvaluateScript("Test(['a', 'b', 'c']);"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(action); }
public void DynamicArray_TestCreateDynamicStringArrayInScript() { bool isCallied = false; var action = new DWSMethodDefinition("Test", (x, a) => isCallied = true); context.DefineMethod(action); context.EvaluateScript(@" var a : array of string; a.Add('asdf'); Test(); "); Assert.IsNull(this.LastErrorMessage); Assert.IsTrue(isCallied); GC.KeepAlive(action); }
public void DynamicArray_TestStringArrayElements() { context.DefineType(new DWSArrayDefinition("TStringArray", "String")); var action = new DWSMethodDefinition("Test", (x, a) => { var array = new DWSArray(a[0]); Assert.AreEqual("a", array[0]); Assert.AreEqual("b", array[1]); Assert.AreEqual("c", array[2]); }); action.Args.Add(new DWSParameterDefinition("a", "TStringArray")); context.DefineMethod(action); context.EvaluateScript("Test(['a', 'b', 'c']);"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(action); }
public void DefineMethod_WithEmptyArrayAsDefaultParam() { var action = new DWSMethodDefinition("Test", (x, a) => { }); action.Args.Add(new DWSParameterDefinition("a", "array of string") { DefaultValue = "[]" }); context.DefineMethod(action); context.EvaluateScript(@"Test(['hello']);"); Assert.IsNull(this.LastErrorMessage); this.ResetErrors(); context.EvaluateScript("Test();"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(action); }
public void DefineMethod_AsIntegerFunctionWithParam() { var f = new DWSMethodDefinition("Test", (x, a) => a[0]); f.Args.Add(new DWSParameterDefinition("a", "Integer")); f.ReturnTypeName = "Integer"; context.DefineMethod(f); string rv = context.EvaluateScript(@"var a : Integer = Test(123); Print(IntToStr(a));"); Assert.IsNull(this.LastErrorMessage); Assert.AreEqual("123", rv); this.ResetErrors(); context.EvaluateScript("a := Test();"); Assert.IsNotNull(this.LastErrorMessage); GC.KeepAlive(f); }
public void DefineMethod_AsOverloadedProcedure() { var actionA = new DWSMethodDefinition("Test", (x, a) => { }); actionA.Overloaded = true; context.DefineMethod(actionA); var actionB = new DWSMethodDefinition("Test", (x, a) => { }); actionB.Args.Add(new DWSParameterDefinition("a", "Integer")); actionB.Overloaded = true; context.DefineMethod(actionB); context.EvaluateScript("Test(); Test(1);"); Assert.IsNull(this.LastErrorMessage); GC.KeepAlive(actionA); GC.KeepAlive(actionB); }
private static void Test4(DWSContext context) { var a = new DWSEnumDefinition("TEnum"); a.Add("ENUM_VALUE1", 0); a.Add("ENUM_VALUE2", 1); a.Add("ENUM_VALUE3", 2); context.DefineType(a); var method = new DWSMethodDefinition("CreateArrayTest", new NativeAction((cx, args) => { var v = new DWSElement(args[0]); bool s = false; })); method.Args.Add(new DWSParameterDefinition("a", "TEnum") { DefaultValue = "ENUM_VALUE3" }); context.DefineMethod(method); context.EvaluateScript("CreateArrayTest();"); GC.KeepAlive(method); }