public void Merge(SilverClass klass) { foreach (string key in klass.ClassMethods.Keys) { SilverMethodTable table; if (ClassMethods.ContainsKey(key)) { table = ClassMethods[key]; } else { table = new SilverMethodTable(key); } foreach (SilverFunction func in klass.ClassMethods[key].Functions) { table.AddFunction(func); } } foreach (string key in klass.InstanceMethods.Keys) { SilverMethodTable table; if (InstanceMethods.ContainsKey(key)) { table = InstanceMethods[key]; } else { table = new SilverMethodTable(key); } foreach (SilverFunction func in klass.InstanceMethods[key].Functions) { table.AddFunction(func); } } Context.MergeWithScope(klass.Context); }
public void TestMethodTableTwo() { var expect = new SilverArray {200, 400, 60}; ScriptEngine engine = GetRuntime().GetEngine("IronSilver"); ScriptScope silverscope1 = engine.CreateScope(); ScriptSource source1 = engine.CreateScriptSourceFromString( "def test1(x=10) { return x * 20; }; def test2(x,z,y=20) { return (x/y)*z; }; def test3(a,b,*c) { res = a; for(z in c) { res += (z-b); }; res; };"); source1.Execute(silverscope1); dynamic test1 = silverscope1.GetVariable("test1"); dynamic test2 = silverscope1.GetVariable("test2"); dynamic test3 = silverscope1.GetVariable("test3"); var table = new SilverMethodTable("test"); table.AddFunction(test1); table.AddFunction(test2); table.AddFunction(test3); ScriptScope silverscope2 = engine.CreateScope(); silverscope2.SetVariable("test", table); ScriptSource source2 = engine.CreateScriptSourceFromString("[test(),test(400,20),test(10,5,10,15,20,25)];"); dynamic real = source2.Execute(silverscope2); Assert.That(real, Is.EqualTo(expect)); }
public void TestMethodTableResolveFail() { ScriptEngine engine = GetRuntime().GetEngine("IronSilver"); ScriptScope silverscope1 = engine.CreateScope(); ScriptSource source1 = engine.CreateScriptSourceFromString( "def test1(x,y,z) { return (x%y)+z; }; def test2(a,b,c) { return a+b+c; }"); source1.Execute(silverscope1); dynamic test1 = silverscope1.GetVariable("test1"); dynamic test2 = silverscope1.GetVariable("test2"); var table = new SilverMethodTable("test"); table.AddFunction(test1); table.AddFunction(test2); ScriptScope silverscope2 = engine.CreateScope(); silverscope2.SetVariable("test", table); ScriptSource source2 = engine.CreateScriptSourceFromString("test(2,3);"); object val = source2.Execute(silverscope2); Assert.That(val, Is.Null); }
public void TestMethodTableNativeFunction() { var expect = new SilverArray {17, 37}; ScriptEngine engine = GetRuntime().GetEngine("IronSilver"); ScriptScope scope = engine.CreateScope(); var table = new SilverMethodTable("test"); table.AddFunction(new SilverNativeFunction(typeof (NativeHelper), typeof (NativeHelper).GetMethod("MethodTableTest", new[] {typeof (int)}))); table.AddFunction(new SilverNativeFunction(typeof (NativeHelper), typeof (NativeHelper).GetMethod("MethodTableTest", new[] {typeof (string)}))); scope.SetVariable("test", table); ScriptSource source = engine.CreateScriptSourceFromString("[test('hello'),test(27)];"); dynamic real = source.Execute(scope); Assert.That(real, Is.EqualTo(expect)); }
public void TestMethodTableNameMatch() { var expect = new SilverArray {6, 18}; ScriptEngine engine = GetRuntime().GetEngine("IronSilver"); ScriptScope silverscope1 = engine.CreateScope(); ScriptSource source1 = engine.CreateScriptSourceFromString( "def test1(x,y,z) { return (x%y)+z; }; def test2(a,b,c) { return a+b+c; }"); source1.Execute(silverscope1); dynamic test1 = silverscope1.GetVariable("test1"); dynamic test2 = silverscope1.GetVariable("test2"); var table = new SilverMethodTable("test"); table.AddFunction(test1); table.AddFunction(test2); ScriptScope silverscope2 = engine.CreateScope(); silverscope2.SetVariable("test", table); ScriptSource source2 = engine.CreateScriptSourceFromString("[test(4,y:3,5),test(a:7,6,5)];"); dynamic real = source2.Execute(silverscope2); Assert.That(real, Is.EqualTo(expect)); }
public void TestMethodTableCSharp() { var expect = new List<object> {200, 5, 23}; ScriptEngine engine = GetRuntime().GetEngine("IronSilver"); ScriptScope silverscope1 = engine.CreateScope(); ScriptSource source1 = engine.CreateScriptSourceFromString( "def test1(x) { return x * 20; }; def test2(x,y) { return x/y; }; def test3(a,b,c) { return a+(b-c); };"); source1.Execute(silverscope1); dynamic test1 = silverscope1.GetVariable("test1"); dynamic test2 = silverscope1.GetVariable("test2"); dynamic test3 = silverscope1.GetVariable("test3"); var table = new SilverMethodTable("test"); table.AddFunction(test1); table.AddFunction(test2); table.AddFunction(test3); dynamic test = table; var real = new List<object> {test(10), test(10, 2), test(10, 20, 7)}; Assert.That(real, Is.EqualTo(expect)); }