Ejemplo n.º 1
0
		public void TableAddWithMetatable()
		{
			string script = @"    
				v1 = { 'aaaa' }
				v2 = { 'aaaaaa' } 

				meta = { }

				function meta.__add(t1, t2)
					local o1 = #t1[1];
					local o2 = #t2[1];
	
					return o1 * o2;
				end


				setmetatable(v1, meta);


				return(v1 + v2);";

			var S = new Script();
			Table globalCtx = S.Globals;

			globalCtx.RegisterModuleType<TableIteratorsModule>();
			globalCtx.RegisterModuleType<MetaTableModule>();

			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(24, res.Number);
		}
Ejemplo n.º 2
0
		public void Test_ConstIntFieldSetter(InteropAccessMode opt)
		{
			try
			{
				string script = @"    
				myobj.ConstIntProp = 1;
				return myobj.ConstIntProp;";

				Script S = new Script();

				SomeClass obj = new SomeClass() { IntProp = 321 };

				UserData.UnregisterType<SomeClass>();
				UserData.RegisterType<SomeClass>(opt);

				S.Globals.Set("myobj", UserData.Create(obj));

				DynValue res = S.DoString(script);

				Assert.AreEqual(DataType.Number, res.Type);
				Assert.AreEqual(115, res.Number);
			}
			catch (ScriptRuntimeException)
			{
				return;
			}

			Assert.Fail();
		}
		void Do(string code, Action<DynValue, RegCollMethods> asserts)
		{
			try
			{
				UserData.RegisterType<RegCollMethods>();
				UserData.RegisterType<RegCollItem>();
				UserData.RegisterType(typeof(IList<>));

				Script s = new Script();

				var obj = new RegCollMethods();
				s.Globals["o"] = obj;
				s.Globals["ctor"] = UserData.CreateStatic<RegCollItem>();

				DynValue res = s.DoString(code);

				asserts(res, obj);
			}
			catch (ScriptRuntimeException ex)
			{
				Debug.WriteLine(ex.DecoratedMessage);
				throw;
			}
			finally
			{
				UserData.UnregisterType<RegCollMethods>();
				UserData.UnregisterType<RegCollItem>();
				UserData.UnregisterType<Array>();
				UserData.UnregisterType(typeof(IList<>));
				UserData.UnregisterType(typeof(IList<RegCollItem>));
				UserData.UnregisterType(typeof(IList<int>));
				//UserData.UnregisterType<IEnumerable>();
			}
		}
Ejemplo n.º 4
0
		public void TailCallFromCLR()
		{
			string script = @"
				function getResult(x)
					return 156*x;  
				end

				return clrtail(9)";


			Script S = new Script();

			S.Globals.Set("clrtail", DynValue.NewCallback((xc, a) =>
			{
				DynValue fn = S.Globals.Get("getResult");
				DynValue k3 = DynValue.NewNumber(a[0].Number / 3);

				return DynValue.NewTailCallReq(fn, k3);
			}));

			var res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(468, res.Number);
		}
Ejemplo n.º 5
0
		private void DoTest(string code, string expectedResult)
		{
			Script S = new Script();
			
			S.DoString(@"
function f(a,b)
	local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
	return debug
end


function g(a, b, ...)
	local debug = 'a: ' .. tostring(a) .. ' b: ' .. tostring(b)
	local arg = {...}
	debug = debug .. ' arg: {'
	for k, v in pairs(arg) do
		debug = debug .. tostring(v) .. ', '
	end
	debug = debug .. '}'
	return debug
end

function r()
	return 1, 2, 3
end

function h(...)
	return g(...)
end

function i(...)
	return g('extra', ...)
end
");
			DynValue res = S.DoString("return " + code);

			Assert.AreEqual(res.Type, DataType.String);
			Assert.AreEqual(expectedResult, res.String);
		}
Ejemplo n.º 6
0
		public void PCallMultipleReturns()
		{
			string script = @"return pcall(function() return 1,2,3 end)";

			Script S = new Script();
			var res = S.DoString(script);

			Assert.AreEqual(DataType.Tuple, res.Type);
			Assert.AreEqual(4, res.Tuple.Length);
			Assert.AreEqual(true, res.Tuple[0].Boolean);
			Assert.AreEqual(1, res.Tuple[1].Number);
			Assert.AreEqual(2, res.Tuple[2].Number);
			Assert.AreEqual(3, res.Tuple[3].Number);
		}
Ejemplo n.º 7
0
		private DynValue Script_LoadFunc(string script, string funcname)
		{
			Script s1 = new Script();
			DynValue v1 = s1.DoString(script);
			DynValue func = s1.Globals.Get(funcname);

			using (MemoryStream ms = new MemoryStream())
			{
				s1.Dump(func, ms);
				ms.Seek(0, SeekOrigin.Begin);

				Script s2 = new Script();
				return s2.LoadStream(ms);
			}
		}
		private void IndexerTest(string code, int expected)
		{
			Script S = new Script();

			IndexerTestClass obj = new IndexerTestClass();

			UserData.RegisterType<IndexerTestClass>();

			S.Globals.Set("o", UserData.Create(obj));

			DynValue v = S.DoString(code);

			Assert.AreEqual(DataType.Number, v.Type);
			Assert.AreEqual(expected, v.Number);
		}
Ejemplo n.º 9
0
		public void CSharpStaticFunctionCall4()
		{
			string script = "return callback()();";

			var callback2 = DynValue.NewCallback(new CallbackFunction((_x, a) => { return DynValue.NewNumber(1234.0); }));
			var callback = DynValue.NewCallback(new CallbackFunction((_x, a) => { return callback2; }));

			var S = new Script();
			S.Globals.Set("callback", callback);

			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(1234.0, res.Number);
		}
Ejemplo n.º 10
0
		public void ProxyTest()
		{
			UserData.RegisterProxyType<Proxy, Random>(r => new Proxy(r));

			Script S = new Script();

			S.Globals["R"] = new Random();
			S.Globals["func"] = (Action<Random>)(r => { Assert.IsNotNull(r); Assert.IsTrue(r is Random); });

			S.DoString(@"
				x = R.GetValue();
				func(R);
			");

			Assert.AreEqual(3.0, S.Globals.Get("x").Number);
		}
Ejemplo n.º 11
0
		private MyClass Test(string tableDef)
		{
			Script s = new Script(CoreModules.None);

			DynValue table = s.DoString("return " + tableDef);

			Assert.AreEqual(DataType.Table, table.Type);

			PropertyTableAssigner<MyClass> pta = new PropertyTableAssigner<MyClass>("class");

			MyClass o = new MyClass();

			pta.AssignObject(o, table.Table);

			return o;
		}
Ejemplo n.º 12
0
		public void CSharpStaticFunctionCallRedef()
		{
			IList<DynValue> args = null;

			string script = "local print = print; print(\"hello\", \"world\");";

			var S = new Script();
			S.Globals.Set("print", DynValue.NewCallback(new CallbackFunction((_x, a) => { args = a.GetArray(); return DynValue.NewNumber(1234.0); })));

			DynValue res = S.DoString(script);

			Assert.AreEqual(2, args.Count);
			Assert.AreEqual(DataType.String, args[0].Type);
			Assert.AreEqual("hello", args[0].String);
			Assert.AreEqual(DataType.String, args[1].Type);
			Assert.AreEqual("world", args[1].String);
			Assert.AreEqual(DataType.Void, res.Type);
		}
Ejemplo n.º 13
0
		public void Test_IntPropertyGetter(InteropAccessMode opt)
		{
			string script = @"    
				x = myobj.IntProp;
				return x;";

			Script S = new Script();

			SomeClass obj = new SomeClass() { IntProp = 321 };

			UserData.UnregisterType<SomeClass>();
			UserData.RegisterType<SomeClass>(opt);

			S.Globals.Set("myobj", UserData.Create(obj));

			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(321, res.Number);
		}
Ejemplo n.º 14
0
		public void TcoTest_Pre()
		{
			// this just verifies the algorithm for TcoTest_Big
			string script = @"
				function recsum(num, partial)
					if (num == 0) then
						return partial
					else
						return recsum(num - 1, partial + num)
					end
				end
				
				return recsum(10, 0)";


			Script S = new Script();
			var res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(55, res.Number);
		}
Ejemplo n.º 15
0
		public void TcoTest_Big()
		{
			// calc the sum of the first N numbers in the most stupid way ever to waste stack and trigger TCO..
			// (this could be a simple X*(X+1) / 2... )
			string script = @"
				function recsum(num, partial)
					if (num == 0) then
						return partial
					else
						return recsum(num - 1, partial + num)
					end
				end
				
				return recsum(70000, 0)";


			Script S = new Script();
			var res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(2450035000.0, res.Number);
		}
Ejemplo n.º 16
0
		void RunTest(string script)
		{
			HashSet<string> failedTests = new HashSet<string>();
			int i = 0;

			Script S = new Script();

			var globalCtx = S.Globals;
			globalCtx.Set(DynValue.NewString("xassert"), DynValue.NewCallback(new CallbackFunction(
				(x, a) =>
				{
					if (!a[1].CastToBool())
						failedTests.Add(a[0].String);

					return DynValue.Nil;
				})));
			globalCtx.Set(DynValue.NewString("assert"), DynValue.NewCallback(new CallbackFunction(
				(x, a) =>
				{
					++i;

					if (!a[0].CastToBool())
						failedTests.Add(string.Format("assert #{0}", i));

					return DynValue.Nil;
				})));

			globalCtx.Set(DynValue.NewString("print"), DynValue.NewCallback(new CallbackFunction((x, a) =>
			{
				// Debug.WriteLine(string.Join(" ", a.Select(v => v.AsString()).ToArray()));
				return DynValue.Nil;
			})));


			DynValue res = S.DoString(script);

			Assert.IsFalse(failedTests.Any(), string.Format("Failed asserts {0}",
				string.Join(", ", failedTests.Select(xi => xi.ToString()).ToArray())));
		}
Ejemplo n.º 17
0
		public void Interop_Event_Simple()
		{
			int invocationCount = 0;
			UserData.RegisterType<SomeClass>();
			UserData.RegisterType<EventArgs>();

			Script s = new Script(CoreModules.None);

			var obj = new SomeClass();
			s.Globals["myobj"] = obj;
			s.Globals["ext"] = DynValue.NewCallback((c, a) => { invocationCount += 1; return DynValue.Void; });

			s.DoString(@"
				function handler(o, a)
					ext();
				end

				myobj.MyEvent.add(handler);
				");

			obj.Trigger_MyEvent();

			Assert.AreEqual(1, invocationCount);
		}
Ejemplo n.º 18
0
		public void CSharpStaticFunctionCall2()
		{
			IList<DynValue> args = null;

			string script = "return callback 'hello';";

			var S = new Script();
			S.Globals.Set("callback", DynValue.NewCallback(new CallbackFunction((_x, a) => { args = a.GetArray(); return DynValue.NewNumber(1234.0); })));

			DynValue res = S.DoString(script);

			Assert.AreEqual(1, args.Count);
			Assert.AreEqual(DataType.String, args[0].Type);
			Assert.AreEqual("hello", args[0].String);
			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(1234.0, res.Number);
		}
Ejemplo n.º 19
0
		public void IterativeFactorialWithRepeatUntilAndScopeCheck()
		{
			string script = @"    
				function fact (n)
					local result = 1;
					repeat
						local checkscope = 1;
						result = result * n;
						n = n - 1;
					until (n == 0 and checkscope == 1)
					return result;
				end
    
				return fact(5)";

			Script s = new Script(CoreModules.None);
			DynValue res = s.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(120.0, res.Number);
		}
Ejemplo n.º 20
0
		public void OperatorPrecedence7()
		{
			string script = @"return -7 / 0.5";
			Script S = new Script(CoreModules.None);

			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(-14, res.Number);
		}
Ejemplo n.º 21
0
		public void OperatorPrecedence1()
		{
			string script = @"return 1+2*3";

			Script s = new Script(CoreModules.None);
			DynValue res = s.DoString(script);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(7, res.Number);
		}
Ejemplo n.º 22
0
		public void SelectNegativeIndex()
		{
			string script = @"    
				return select(-1,'a','b','c');
			";

			Script S = new Script();
			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.String, res.Type);
			Assert.AreEqual("c", res.String);
		}
Ejemplo n.º 23
0
		public void FunctionOrOperator()
		{
			string script = @"    
				loadstring = loadstring or load;

				return loadstring;
			";

			Script S = new Script();
			DynValue res = S.DoString(script);

			Assert.AreEqual(DataType.ClrFunction, res.Type);

		}
Ejemplo n.º 24
0
		public void SimpleBoolShortCircuit()
		{
			string script = @"    
				x = true or crash();
				y = false and crash();
			";

			Script S = new Script();
			S.Globals.Set("crash", DynValue.NewCallback(new CallbackFunction((_x, a) =>
			{
				throw new Exception("FAIL!");
			})));

			S.DoString(script);
		}
Ejemplo n.º 25
0
		public void FunctionCallWrappers()
		{
			string script = @"    
				function boh(x) 
					return 1912 + x;
				end
			";

			Script s = new Script();
			s.DoString(script);

			DynValue res = s.Globals.Get("boh").Function.Call(82);

			Assert.AreEqual(DataType.Number, res.Type);
			Assert.AreEqual(1994, res.Number);
		}
Ejemplo n.º 26
0
		public void MissingArgsDefaultToNil()
		{
			Script S = new Script(CoreModules.None);
			DynValue res = S.DoString(@"
				function test(a)
					return a;
				end

				test();
				");
		}
Ejemplo n.º 27
0
		public void Simple_Delegate_Interop_2()
		{
			try
			{
				UserData.RegistrationPolicy = Interop.InteropRegistrationPolicy.Automatic;

				int a = 3;
				var script = new Script();
				script.Globals["action"] = new Action(() => a = 5);
				script.DoString("action()");
				Assert.AreEqual(5, a);
			}
			finally
			{
				UserData.RegistrationPolicy = Interop.InteropRegistrationPolicy.Explicit;
			}
		}
Ejemplo n.º 28
0
		public void Interop_NestedTypes_Public_Enum()
		{
			Script S = new Script();

			UserData.RegisterType<SomeType>();

			S.Globals.Set("o", UserData.CreateStatic<SomeType>());

			DynValue res = S.DoString("return o:Get()");

			Assert.AreEqual(DataType.UserData, res.Type);
		}
Ejemplo n.º 29
0
		public void Interop_NestedTypes_Public_Ref()
		{
			Script S = new Script();

			UserData.RegisterType<SomeType>();

			S.Globals.Set("o", UserData.CreateStatic<SomeType>());

			DynValue res = S.DoString("return o.SomeNestedType:Get()");

			Assert.AreEqual(DataType.String, res.Type);
			Assert.AreEqual("Ciao from SomeNestedType", res.String);
		}
Ejemplo n.º 30
0
		public void Simple_Delegate_Interop_1()
		{
			int a = 3;
			var script = new Script();
			script.Globals["action"] = new Action(() => a = 5);
			script.DoString("action()");
			Assert.AreEqual(5, a);
		}