コード例 #1
0
        static void CSharpObjects()
        {
            Console.Write("\nC# object exposure ");
            Engine engine = CreateEngine();

            // test the empty interface
            {
                NI.CreateObject(engine.ctx, IntPtr.Zero, IObjectBase._sgsNullObjectInterface);
                engine.Stat(Stat.XDumpStack);
                engine.Pop(1);
            }

            // init & disown object
            IObject obj = new EmptyObject(engine);

            engine.Push(obj);
            AssertN(obj._sgsObject, IntPtr.Zero);
            engine.Stat(Stat.XDumpStack);               // should have refcount = 2 (one for IObject and one for stack)
            Variable si0 = engine.StackItem(0);

            Assert(engine.Call <string>("typeof", si0), "EmptyObject");
            si0.Release();                // for accurate refcount (otherwise this reference is retained for an unknown amount of time)
            obj.DisownClassObject();      // free IObject reference
            engine.Stat(Stat.XDumpStack); // should have refcount = 1 (stack) and name = <nullObject>
            Assert(engine.Call <string>("typeof", engine.StackItem(0)), "<nullObject>");
            engine.Pop(1);

            // test metamethods
            IObject  obj1    = new FullObject1(engine);
            Variable obj1var = engine.Var(obj1);

            Assert(engine.Call <bool>("tobool", obj1), false);                          // test "convert"(tobool)/ConvertToBool
            Assert(engine.Call <string>("tostring", obj1), "[Full_Object_1]");          // test "convert"(tostring)/ConvertToString
            Assert(engine.Call <string>("dumpvar", obj1), "[this is a FULL OBJECT]\n"); // test "dump"/OnDump

            Assert(obj1var.SetProp("prop1", engine.Var(15)), true);                     // test "setindex"(isprop=true)
            Assert(obj1var.SetProp("prop2", engine.Var(16)), true);
            Assert(obj1var.SetProp("prop3", engine.Var(17)), true);

            AssertVar(obj1var.GetProp("prop1"), engine.Var(15));                   // test "getindex"(isprop=true)
            AssertVar(obj1var.GetProp("prop2"), engine.Var(16.0f));
            AssertVar(obj1var.GetProp("prop3"), engine.Var("17"));
            AssertVar(obj1var.GetProp("invprop1"), null);                 // - HideProperty()
            AssertVar(obj1var.GetProp("invprop2"), engine.Var(234));      // - HideProperty()
            AssertVar(obj1var.GetProp("invprop3"), null);                 // - HideProperty()

            // test method wrapper
            DNMethod dnm1    = new DNMethod(engine, typeof(FullObject1).GetMethod("TestMethod"));
            Variable dnm1var = engine.Var(dnm1);

            Assert(engine.ThisCall <string>(dnm1var, obj1var, "test"), "17|test");
            dnm1.DisownClassObject();

            // test static method wrapper
            DNMethod dnm2    = new DNMethod(engine, typeof(FullObject1).GetMethod("StaticTestMethod"));
            Variable dnm2var = engine.Var(dnm2);

            Assert(engine.Call <string>(dnm2var, "sTest"), "PFX:sTest");
            dnm2.DisownClassObject();

            // test static method dictionary
            Assert(engine.Call <string>("tostring", IObjectBase.CreateStaticDict(engine, typeof(FullObject1))),
                   "{_useProp3=DNMethod,StaticTestMethod=DNMethod,TestMethod=DNMethod,TestMsgMethod=DNMethod}");

            // test static (meta-)object
            Variable movar = engine._GetMetaObject(typeof(FullObject1)).GetVariable();

            AssertVar(movar, obj1var.GetMetaObj());
            Assert(movar.GetProp("_useProp3").ConvertToString(), "SGScript.DNMethod(APITest.APITest+FullObject1._useProp3)");
            Assert(movar.GetProp("pfxstr").GetString(), "PFX:");

            // test instance meta object lookup
            Assert(obj1var.GetProp("_useProp3").ConvertToString(), "SGScript.DNMethod(APITest.APITest+FullObject1._useProp3)");
            Assert(obj1var.GetProp("pfxstr").GetString(), "PFX:");
            Assert(obj1var.GetMetaObj().SetProp("pfxstr", engine.Var("[PFX]:")), true);
            Assert(obj1var.GetMetaObj().GetProp("pfxstr").GetString(), "[PFX]:");
            Assert(obj1var.GetProp("pfxstr").GetString(), "[PFX]:");

            // register type
            engine.SetGlobal("FullObject1", engine._GetMetaObject(typeof(FullObject1)).GetVariable());

            // test extension method
            engine.Exec("function FullObject1.customMethod( txt ){ return this.pfxstr .. txt; }");
            AssertN(obj1var.GetMetaObj().GetProp("customMethod"), null);
            Assert(engine.ThisCall <string>("customMethod", obj1var, "cmTest"), "[PFX]:cmTest");

            // test method messaging
            engine.ThisCall <Nothing>("TestMsgMethod", obj1var, 121, 754);

            DestroyEngine(engine);
            FullObject1.pfxstr = "PFX:";             // restore
        }