Esempio n. 1
0
    public void Execute()
    {
        Console.WriteLine("Testing pre-compiled script ...\r\n");

        Engine.Execute("var i = 0;");
        var pcScript = Engine.Compile("i = i + 1;");

        for (var i = 0; i < 100; i++)
        {
            Engine.Execute(pcScript, true);
        }

        Engine.ConsoleExecute("assert('Testing i==100', i, 100)", this.GetType().Name, true);

        Console.WriteLine("\r\nTesting JS function call from native side ...\r\n");

        ObjectHandle f       = (ObjectHandle)Engine.ConsoleExecute("f = function(arg1) { return arg1; }");
        var          fresult = f.StaticCall(Engine.CreateValue(10));

        Console.WriteLine("f(10) == " + fresult);
        if (fresult != 10)
        {
            throw new Exception("CLR handle call to native function failed.");
        }

        Console.WriteLine("\r\nTesting JS function call exception from native side ...\r\n");

        f       = (ObjectHandle)Engine.ConsoleExecute("f = function() { return thisdoesntexist; }");
        fresult = f.StaticCall();
        Console.WriteLine("f() == " + fresult);
        if (!fresult.ToString().Contains("Error"))
        {
            throw new Exception("Native exception error did not come through.");
        }
        else
        {
            Console.WriteLine("Expected exception came through - pass.\r\n");
        }

        Console.WriteLine("\r\nPress any key to begin testing properties on 'this.tester' ...\r\n");
        Console.ReadKey();

        // ... test the non-function/object propertied ...

        Engine.ConsoleExecute("assert('Testing property testProperty1', tester.testProperty1, 'Test property 1')", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty2', tester.testProperty2, true)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty3', tester.testProperty3, tester.testProperty1)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty4', tester.testProperty4, '" + MyClassProperty4 + "')", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty5', tester.testProperty5, 'Test property 5')", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty6', tester.testProperty6, 'Test property 6')", this.GetType().Name, true);

        Console.WriteLine("\r\nAll properties initialized ok.  Testing property change ...\r\n");

        Engine.ConsoleExecute("assert('Setting testProperty2 to integer (123)', (tester.testProperty2=123), 123)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Setting testProperty2 to number (1.2)', (tester.testProperty2=1.2), 1.2)", this.GetType().Name, true);

        // ... test non-function object properties ...

        Console.WriteLine("\r\nSetting property 1 to an object, which should also set property 3 to the same object ...\r\n");

        Engine.VerboseConsoleExecute("dump(tester.testProperty1 = {x:0});", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing property testProperty1.x === testProperty3.x', tester.testProperty1.x, tester.testProperty3.x)", this.GetType().Name, true);

        // ... test function properties ...

        Engine.ConsoleExecute("assert('Testing property tester.testFunction1 with argument 100', tester.testFunction1(100), 100)", this.GetType().Name, true);

        // ... test function properties ...

        Console.WriteLine("\r\nCreating 'this.obj1' with a new instance of tester.testFunction1 and testing the expected values ...\r\n");

        Engine.VerboseConsoleExecute("obj1 = new tester.testFunction1(321);");
        Engine.ConsoleExecute("assert('Testing obj1.x', obj1.x, 321)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1.y', obj1.y, 0)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1[0]', obj1[0], 100)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1[1]', obj1[1], 100.2)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1[2]', obj1[2], '300')", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1[3] is undefined?', obj1[3] === undefined, true)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj1[4].toUTCString()', obj1[4].toUTCString(), 'Wed, 02 Jan 2013 03:04:05 GMT')", this.GetType().Name, true);

        Console.WriteLine("\r\nPress any key to test dynamic handle property access ...\r\n");
        Console.ReadKey();

        // ... get a handle to an in-script only object and test the dynamic handle access ...

        Engine.VerboseConsoleExecute("var obj = { x:0, y:0, o2:{ a:1, b:2, o3: { x:0 } } }", this.GetType().Name, true);
        dynamic handle = Engine.DynamicGlobalObject.obj;

        handle.x       = 1;
        handle.y       = 2;
        handle.o2.o3.x = 3;
        Engine.ConsoleExecute("assert('Testing obj.x', obj.x, 1)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj.y', obj.y, 2)", this.GetType().Name, true);
        Engine.ConsoleExecute("assert('Testing obj.o2.o3.x', obj.o2.o3.x, 3)", this.GetType().Name, true);

        Console.WriteLine("\r\nPress any key to test handle reuse ...");
        Console.WriteLine("(1000 native object handles will be created, but one V8NativeObject wrapper will be used)");
        Console.ReadKey();
        Console.Write("Running ...");
        var obj = new V8NativeObject();

        for (var i = 0; i < 1000; i++)
        {
            obj.Handle = Engine.GlobalObject.GetProperty("obj");
        }
        Console.WriteLine(" Done.");
    }
Esempio n. 2
0
 public void Initialize(V8NativeObject owner, bool isConstructCall, params InternalHandle[] args)
 {
 }