Exemple #1
0
 public InternalHandle TestJSFunction1(V8Engine engine, bool isConstructCall, InternalHandle _this, params InternalHandle[] args)
 {
     // ... there can be two different returns based on the call mode! ...
     // (tip: if a new object is created and returned instead (such as V8ManagedObject or an object derived from it), then that object will be the new object (instead of "_this"))
     if (isConstructCall)
     {
         var obj = engine.GetObject(_this);
         obj.AsDynamic.x  = args[0];
         ((dynamic)obj).y = 0; // (native objects in this case will always be V8NativeObject dynamic objects)
         obj.SetProperty(0, engine.CreateValue(100));
         obj.SetProperty("1", engine.CreateValue(100.2));
         obj.SetProperty("2", engine.CreateValue("300"));
         obj.SetProperty(4, engine.CreateValue(new DateTime(2013, 1, 2, 3, 4, 5, DateTimeKind.Utc)));
         return(_this);
     }
     else
     {
         return(args.Length > 0 ? args[0] : InternalHandle.Empty);
     }
 }
Exemple #2
0
        static void Main(string[] args)
        {
            V8Engine v8Engine = new V8Engine();

            var result = v8Engine.Execute(
                @"var gs = (function () {  // NOTE: 'result' WILL ALWAYS BE 'undefined' BECAUSE OF 'var' before 'gs'.
                    var myClassObject = function() {};
                    myClassObject.prototype.getCompanyInfo = function (a, b, c) {
                        var staff = [
                            {
                                'name': 'John',
                                'address': '1 Walk Way',
                                'dob': 'July '+a+', 1970'
                            },
                            {
                                'name': 'Peter',
                                'address': '241 Otoforwan Rd',
                                'dob': 'January '+b+', 1953'
                            },
                            {
                                'name': 'Marry',
                                'address': '1 Contrary Lane',
                                'dob': 'August '+c+', 1984'
                            }
                        ];
                        var result = {
                            'isEnabled': true,
                            'staff': staff
                        };
                        return result;
                    };
                    return new myClassObject();
                })();"
                );

            //create parameter
            Handle day1 = v8Engine.CreateValue("1");
            Handle day2 = v8Engine.CreateValue("2");
            Handle day3 = v8Engine.CreateValue("3");

            var     gs           = v8Engine.GlobalObject.GetProperty("gs");
            var     resultHandle = gs.Call("getCompanyInfo", null, day1, day2, day3); // NOTE: The object context is already known, so pass 'null' for '_this'.
            Company completion   = v8Engine.GetObject <Company>(resultHandle);

            //examine result
            var    test0              = resultHandle.GetProperty("isEnable");
            Handle test1              = resultHandle.GetProperty("staff"); // NOTE: "ObjectHandle" is a special handle for objects (which also obviously includes arrays, etc.).
            var    arrayLength        = test1._.ArrayLength;
            Handle arrayItem1         = test1._.GetProperty(0);
            var    arrayItem1_name    = arrayItem1._.GetProperty("name");
            var    arrayItem1_address = arrayItem1._.GetProperty("address");
            var    arrayItem1_dob     = (~arrayItem1).GetProperty("dob");
            Handle arrayItem2         = test1._.GetProperty(1); // (arrays are treated same as objects here)
            Handle arrayItem3         = test1._.GetProperty(2); // (arrays are treated same as objects here)

            //  ==================================================================== OR  ====================================================================

            v8Engine.RegisterType <Company2>(null, true, ScriptMemberSecurity.Locked); // (this line is NOT required, but allows more control over the settings)
            v8Engine.GlobalObject.SetProperty(typeof(Company2));                       // <= THIS IS IMPORTANT! It sets the type on the global object (though you can put this anywhere like any property)

            var gs2 = v8Engine.Execute(
                @"(function () {
                    var myClassObject = function() {};
                    myClassObject.prototype.getCompanyInfo = function (a, b, c) {
                        return new Company2(a, b, c);
                    };
                    return new myClassObject();
                })();"
                );

            var resultHandle2             = gs2.Call("getCompanyInfo", null, day1, day2, day3); // NOTE: The object context is already known, so pass 'null' for '_this'.
            var objectBindingModeIfNeeded = resultHandle2.BindingMode;
            var ci2 = (Company2)resultHandle2.BoundObject;                                      // (when a CLR class is bound, it is tracked by the handle in a special way)

            //  =============================================================================================================================================
            // Take your pick. ;)

            System.Console.WriteLine("Script executions completed. Press any key to exit.");
            System.Console.ReadKey(true);
        }
Exemple #3
0
 public InternalHandle ConstructV8DotNetTesterWrapper(V8Engine engine, bool isConstructCall, InternalHandle _this, params InternalHandle[] args)
 {
     return(isConstructCall ? engine.GetObject <V8DotNetTesterWrapper>(_this, true, false).Initialize(isConstructCall, args).AsInternalHandle : InternalHandle.Empty);
     // (note: V8DotNetTesterWrapper would cause an error here if derived from V8ManagedObject)
 }