コード例 #1
0
 public SilverInstance(SilverClass @class)
 {
     _class = @class;
     SingletonMethods = new Dictionary<string, SilverFunction>();
     UndefinedMethods = new List<string>();
     RemovedMethods = new List<string>();
     InstanceVariables = new SilverScope();
 }
コード例 #2
0
 public SilverClass(string name, SilverClass parent, List<SilverFunction> classMethods,
     List<SilverFunction> instanceMethods)
 {
     Name = name;
     ClassMethods = new Dictionary<string, SilverMethodTable>();
     classMethods.ForEach(func => AddMethod(ClassMethods, func));
     if (!ClassMethods.ContainsKey("new")) {
         AddMethod(ClassMethods, new SilverFunction("new", new List<FunctionArgument>(),
             AstExpression.Block(
                 AstExpression.Return(new List<FunctionArgument> {
                     new FunctionArgument(null, AstExpression.Variable(Expression.Constant("self")))
                 }),
                 Expression.Label(SilverParser.ReturnTarget, Expression.Constant(null, typeof (object)))),
             new SilverScope()));
     }
     InstanceMethods = new Dictionary<string, SilverMethodTable>();
     instanceMethods.ForEach(func => AddMethod(InstanceMethods, func));
     UndefinedMethods = new List<string>();
     RemovedMethods = new List<string>();
     Context = new SilverScope();
     Parent = parent;
 }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void TestSingleton()
        {
            var expect = new SilverArray {null, 5};
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("test")});

            ScriptScope nscope = engine.CreateScope();
            nscope.SetVariable("Test", testClass);

            ScriptSource nsource =
                engine.CreateScriptSourceFromString(
                    "x = Test();  y = Test(); def x.single(y) { return y - 5; }; [y.single(10),x.single(10)]");
            Assert.That(nsource.Execute(nscope), Is.EqualTo(expect));
        }
コード例 #5
0
 internal SilverBoxedInstance(object obj, SilverScope scope, SilverClass @class)
     : base(@class)
 {
     BoxedObject = obj;
     BoxedScope = scope;
 }
コード例 #6
0
        public void TestSilverObjectBinaryCSharp()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            dynamic testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("+")});

            dynamic x = testClass();
            x.x = 17;
            Assert.AreEqual(27, x + 10);
        }
コード例 #7
0
        public void TestSilverObjectBinaryIronRuby()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            dynamic testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("+")});

            scope.RemoveVariable("+");
            scope.SetVariable("TestClass", testClass);

            ScriptEngine rubyengine = GetRuntime().GetEngine("IronRuby");
            ScriptSource rubysource = rubyengine.CreateScriptSourceFromString("x = silver.TestClass; x.x = 17; x + 10;");

            ScriptScope rubyscope = rubyengine.CreateScope();
            rubyscope.SetVariable("silver", scope);

            Assert.That(rubysource.Execute(rubyscope), Is.EqualTo(27));
        }
コード例 #8
0
        public void TestSetGetMemberInBody()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source =
                engine.CreateScriptSourceFromString("def test() { return @x + @y; }; def new() { @x = 12; @y = 10; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)),
                new List<SilverFunction> {scope.GetVariable("new")},
                new List<SilverFunction> {scope.GetVariable("test")});

            ScriptScope nscope = engine.CreateScope();
            nscope.SetVariable("Test", testClass);

            ScriptSource nsource = engine.CreateScriptSourceFromString("x = Test();  x.test;");
            Assert.That(nsource.Execute(nscope), Is.EqualTo(22));
        }
コード例 #9
0
        public void TestSilverObjectBinary()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def +(x) { return @x + x; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("+")});

            ScriptScope nscope = engine.CreateScope();
            nscope.SetVariable("Test", testClass);

            ScriptSource nsource = engine.CreateScriptSourceFromString("x = Test();  x.x = 17; x + 10;");
            Assert.That(nsource.Execute(nscope), Is.EqualTo(27));
        }
コード例 #10
0
        public void TestInvokeMemberPipe()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)), new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("test")});

            ScriptScope nscope = engine.CreateScope();
            nscope.SetVariable("Test", testClass);

            ScriptSource nsource = engine.CreateScriptSourceFromString("x = Test();  17 |> x.test;");
            Assert.That(nsource.Execute(nscope), Is.EqualTo(27));
        }
コード例 #11
0
        public void TestInvokeMemberSuperIronRuby()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source = engine.CreateScriptSourceFromString("def test(x) { return x + 10; };");
            ScriptSource source2 = engine.CreateScriptSourceFromString("def test(x) { super.test(x) + 15; };");

            ScriptScope scope = engine.CreateScope();
            ScriptScope scope2 = engine.CreateScope();
            source.Execute(scope);
            source2.Execute(scope2);

            var testClassBase = new SilverClass("TestBase", Silver.Box(typeof (object)),
                new List<SilverFunction>(),
                new List<SilverFunction> {scope.GetVariable("test")});

            dynamic testClass = new SilverClass("Test", testClassBase, new List<SilverFunction>(),
                new List<SilverFunction> {scope2.GetVariable("test")});

            ScriptScope scope3 = engine.CreateScope();
            scope3.SetVariable("TestBase", testClassBase);
            scope3.SetVariable("Test", testClass);

            ScriptEngine rubyengine = GetRuntime().GetEngine("IronRuby");
            ScriptSource rubysource = rubyengine.CreateScriptSourceFromString("x = silver.Test; x.test(10);");

            ScriptScope rubyscope = rubyengine.CreateScope();
            rubyscope.SetVariable("silver", scope3);
            Assert.That((int)rubysource.Execute(rubyscope), Is.EqualTo(35));
        }
コード例 #12
0
        public void TestInvokeMemberPartialFunctionIVarTestCSharp()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source =
                engine.CreateScriptSourceFromString("def new(z) { @z = z; }; def test(x,y) { return x + y + @z; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            dynamic testClass = new SilverClass("Test", Silver.Box(typeof (object)),
                new List<SilverFunction> {scope.GetVariable("new")},
                new List<SilverFunction> {scope.GetVariable("test")});

            dynamic x = testClass(10);
            dynamic px = x.test(10);

            Assert.That(px(17), Is.EqualTo(37));
        }
コード例 #13
0
        public void TestInvokeMemberIronRubyWithNewArg()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source =
                engine.CreateScriptSourceFromString("def new(z) { @y = z; }; def test(x) { return x + @y; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            dynamic testClass = new SilverClass("Test", Silver.Box(typeof (object)),
                new List<SilverFunction> {scope.GetVariable("new")},
                new List<SilverFunction> {scope.GetVariable("test")});

            scope.RemoveVariable("test");
            scope.SetVariable("TestClass", testClass);

            ScriptEngine rubyengine = GetRuntime().GetEngine("IronRuby");
            ScriptSource rubysource = rubyengine.CreateScriptSourceFromString("x = silver.TestClass(22); x.test(17);");

            ScriptScope rubyscope = rubyengine.CreateScope();
            rubyscope.SetVariable("silver", scope);

            Assert.That(rubysource.Execute(rubyscope), Is.EqualTo(39));
        }
コード例 #14
0
        public void TestCustomOp()
        {
            ScriptEngine engine = GetRuntime().GetEngine("IronSilver");
            ScriptSource source =
                engine.CreateScriptSourceFromString(
                    "def new(x) { @x = x; }; def <==>(other) { return ((@x + other.x) / (other.x/2)); }; };");

            ScriptScope scope = engine.CreateScope();
            source.Execute(scope);

            var testClass = new SilverClass("Test", Silver.Box(typeof (object)),
                new List<SilverFunction> {scope.GetVariable("new")},
                new List<SilverFunction> {scope.GetVariable("<==>")});
            scope.SetVariable("Test", testClass);
            scope.RemoveVariable("new");
            scope.RemoveVariable("<==>");
            source = engine.CreateScriptSourceFromString("x = Test(8); y = Test(8); x <==> y;");
            Assert.That(source.Execute(scope), Is.EqualTo(4));
        }
コード例 #15
0
        private static bool _inClassDefine; // for nested class

        #endregion Fields

        #region Methods

        internal static dynamic DefineCategory(SilverClass @class, List<Expression> contents, object rawScope)
        {
            lock (_classDefineLock) {
                var scope = (SilverScope) rawScope;
                _inClassDefine = true;
                _className = @class.Name;

                scope["self"] = @class;
                scope[_className] = @class;
                _currentClassScope = scope;

                contents.ForEach(content => {
                    if (content is IncludeExpression) {
                        // We only include modules here so make sure this include references a module
                        List<string> names = ((IncludeExpression) content).Names;

                        dynamic module = null;

                        int index = 0;
                        names.ForEach(mname => {
                            if (module != null && (module is SilverModule)) {
                                module = module.Context[mname];
                            }
                            else if (index == 0) {
                                module = scope[mname];
                            }
                            index = index + 1;
                        });

                        if (module != null) {
                            if (module is SilverModule) {
                                ((SilverModule) module).Contents.ForEach(mcon => {
                                    if (mcon is SilverFunction) {
                                        if ((mcon as SilverFunction).IsSingleton ||
                                            (mcon as SilverFunction).Name == "new") {
                                            SilverClass.AddMethod(@class.ClassMethods, mcon as SilverFunction);
                                        }
                                        else {
                                            SilverClass.AddMethod(@class.InstanceMethods, mcon as SilverFunction);
                                        }
                                        if (@class.RemovedMethods.Contains((mcon as SilverFunction).Name))
                                        {
                                            @class.RemovedMethods.Remove((mcon as SilverFunction).Name);
                                        }
                                        if (@class.UndefinedMethods.Contains((mcon as SilverFunction).Name))
                                        {
                                            @class.UndefinedMethods.Remove((mcon as SilverFunction).Name);
                                        }
                                    }
                                });

                                scope.MergeWithScope(module.Context);
                            }
                            else if (module is SilverClass) {
                                scope[((SilverClass) module).Name] = module;
                            }
                        }
                    }
                });

                contents.ForEach(content => {
                    if (!(content is IncludeExpression)) {
                        dynamic result = CompilerServices.CompileExpression(content, scope);
                        if (result is SilverFunction) {
                            if ((result as SilverFunction).IsSingleton) {
                                SilverClass.AddMethod(@class.ClassMethods, result as SilverFunction);
                            }
                            else {
                                SilverClass.AddMethod(@class.InstanceMethods, result as SilverFunction);
                            }
                            if (@class.RemovedMethods.Contains((result as SilverFunction).Name))
                            {
                                @class.RemovedMethods.Remove((result as SilverFunction).Name);
                            }
                            if (@class.UndefinedMethods.Contains((result as SilverFunction).Name))
                            {
                                @class.UndefinedMethods.Remove((result as SilverFunction).Name);
                            }
                        }
                    }
                });

                @class.Context.MergeWithScope(scope);
                return @class;
            }
        }
コード例 #16
0
        internal static dynamic DefineClass(object rawName, object rawParent, List<Expression> contents, object rawScope)
        {
            lock (_classDefineLock) {
                if (Resolve(rawName, rawScope) != null) {
                    return DefineCategory(Resolve(rawName, rawScope), contents, rawScope);
                }
                var scope = (SilverScope) rawScope;
                var defineScope = _inClassDefine ? scope : scope.GlobalScope;
                _inClassDefine = true;
                SilverClass parent;
                if (rawParent == null) {
                    if (scope.GlobalScope["Object"] == null) {
                        scope.GlobalScope["Object"] = Silver.Box(typeof (object));
                    }
                    parent = scope.GlobalScope["Object"];
                }
                else {
                    var dParent = Resolve(rawParent as string, scope);
                    if (dParent == null) {
                        _inClassDefine = false;
                        return null;
                    }
                    if (dParent is Type) {
                        parent = Silver.Box(dParent);
                    }
                    else {
                        parent = dParent as SilverClass;
                    }
                    if (parent == null) {
                        _inClassDefine = false;
                        return null;
                    }
                }

                var name = (string) rawName;
                _className = name;

                var @class = new SilverClass {Name = _className, Parent = parent};
                var xScope = new SilverScope(scope);
                xScope["self"] = @class;
                xScope[_className] = @class;
                _currentClassScope = xScope;

                contents.ForEach(content => {
                    if (content is IncludeExpression) {
                        // We only include modules here so make sure this include references a module
                        var names = ((IncludeExpression) content).Names;

                        dynamic module = null;

                        var index = 0;
                        names.ForEach(mname => {
                            if ((module is SilverModule)) {
                                module = module.Context[mname];
                            }
                            else if (index == 0) {
                                module = scope[mname];
                            }
                            index = index + 1;
                        });

                        if (module != null) {
                            if (module is SilverModule) {
                                ((SilverModule) module).Contents.ForEach(mcon => {
                                    if (mcon is SilverFunction) {
                                        if ((mcon as SilverFunction).IsSingleton ||
                                            (mcon as SilverFunction).Name == "new") {
                                            SilverClass.AddMethod(@class.ClassMethods, mcon as SilverFunction);
                                        }
                                        else {
                                            SilverClass.AddMethod(@class.InstanceMethods, mcon as SilverFunction);
                                        }
                                        if (@class.RemovedMethods.Contains((mcon as SilverFunction).Name))
                                        {
                                            @class.RemovedMethods.Remove((mcon as SilverFunction).Name);
                                        }
                                        if (@class.UndefinedMethods.Contains((mcon as SilverFunction).Name))
                                        {
                                            @class.UndefinedMethods.Remove((mcon as SilverFunction).Name);
                                        }
                                    }
                                });

                                xScope.MergeWithScope(module.Context);
                            }
                            else if (module is SilverClass) {
                                xScope[((SilverClass) module).Name] = module;
                            }
                        }
                    }
                });

                contents.ForEach(content => {
                    if (!(content is IncludeExpression)) {
                        dynamic result = CompilerServices.CompileExpression(content, xScope);
                        if (result is SilverFunction) {
                            if ((result as SilverFunction).IsSingleton || (result as SilverFunction).Name == "new") {
                                SilverClass.AddMethod(@class.ClassMethods, result as SilverFunction);
                            }
                            else {
                                SilverClass.AddMethod(@class.InstanceMethods, result as SilverFunction);
                            }
                            if (@class.RemovedMethods.Contains((result as SilverFunction).Name))
                            {
                                @class.RemovedMethods.Remove((result as SilverFunction).Name);
                            }
                            if (@class.UndefinedMethods.Contains((result as SilverFunction).Name))
                            {
                                @class.UndefinedMethods.Remove((result as SilverFunction).Name);
                            }
                        }
                    }
                });

                if ([email protected]("new")) {
                    SilverClass.AddMethod(@class.ClassMethods, new SilverFunction("new", new List<FunctionArgument>(),
                        AstExpression.Block(
                            AstExpression.Return(new List<FunctionArgument> {
                                new FunctionArgument(null, AstExpression.Variable(Expression.Constant("self")))
                            }),
                            Expression.Label(SilverParser.ReturnTarget, Expression.Constant(null, typeof (object)))),
                        new SilverScope()));
                }
                @class.Context = xScope;
                defineScope[@class.Name] = @class;
                _inClassDefine = false;
                return @class;
            }
        }