예제 #1
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified name, resource constraints, and options.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 public V8Runtime(string name, V8RuntimeConstraints constraints, V8RuntimeFlags flags)
     : this(name, constraints, flags, 0)
 {
 }
예제 #2
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified name, resource constraints, options, and debug port.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="debugPort">A TCP port on which to listen for a debugger connection.</param>
 public V8Runtime(string name, V8RuntimeConstraints constraints, V8RuntimeFlags flags, int debugPort)
 {
     this.name = nameManager.GetUniqueName(name, GetType().GetRootName());
     proxy     = V8IsolateProxy.Create(this.name, constraints, flags, debugPort);
 }
예제 #3
0
        internal V8ScriptEngine(V8Runtime runtime, string name, V8RuntimeConstraints constraints, V8ScriptEngineFlags flags, int debugPort)
            : base((runtime != null) ? runtime.Name + ":" + name : name)
        {
            using (var localRuntime = (runtime != null) ? null : new V8Runtime(name, constraints))
            {
                var activeRuntime = runtime ?? localRuntime;
                hostItemCollateral = activeRuntime.HostItemCollateral;

                engineFlags = flags;
                proxy       = V8ContextProxy.Create(activeRuntime.IsolateProxy, Name, flags.HasFlag(V8ScriptEngineFlags.EnableDebugging), flags.HasFlag(V8ScriptEngineFlags.DisableGlobalMembers), debugPort);
                script      = GetRootItem();

                var engineInternal = Evaluate(
                    MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name),
                    false,
                    @"
                        EngineInternal = (function () {

                            function convertArgs(args) {
                                var result = [];
                                var count = args.Length;
                                for (var i = 0; i < count; i++) {
                                    result.push(args[i]);
                                }
                                return result;
                            }

                            function construct(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) {
                                return new this(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
                            }

                            return {

                                getCommandResult: function (value) {
                                    if (value != null) {
                                        if (((typeof(value) == 'object') && !value.hasOwnProperty('{c2cf47d3-916b-4a3f-be2a-6ff567425808}')) || (typeof(value) == 'function')) {
                                            if (typeof(value.toString) == 'function') {
                                                return value.toString();
                                            }
                                        }
                                    }
                                    return value;
                                },

                                invokeConstructor: function (constructor, args) {
                                    if (typeof(constructor) != 'function') {
                                        throw new Error('Function expected');
                                    }
                                    return construct.apply(constructor, convertArgs(args));
                                },

                                invokeMethod: function (target, method, args) {
                                    if (typeof(method) != 'function') {
                                        throw new Error('Function expected');
                                    }
                                    return method.apply(target, convertArgs(args));
                                },

                                getStackTrace: function () {
                                    try {
                                        throw new Error('[stack trace]');
                                    }
                                    catch (exception) {
                                        return exception.stack;
                                    }
                                    return '';
                                },

                                itemToInspect: null,
                                getTypeId: function() {
                                    var obj = EngineInternal.itemToInspect;
                                    EngineInternal.itemToInspect = null;
                                    return EngineInternal.getTypeIdInternal(obj);
                                },
                                getTypeIdInternal: function(obj) {
                                    if (obj instanceof Array) {
                                        return 1;
                                    }
                                    if (obj instanceof Date) {
                                        return 2;
                                    }
                                    if (obj instanceof Function) {
                                        return 3;
                                    }
                                    if (obj === null) {
                                        return 4;
                                    }
                                    if (obj === undefined) {
                                        return 5;
                                    }
                                    if (Object.prototype.toString.call(obj) == '[object Arguments]') {
                                        return 6;
                                    }
                                    if (obj instanceof ArrayBuffer) {
                                        return 8;
                                    }
                                    if (obj instanceof Error) {
                                        return 7;
                                    }
                                    return 0;
                                }
                            };
                        })();
                    "
                    );

                ((IDisposable)engineInternal).Dispose();
            }
        }
예제 #4
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints, options, and debug port.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="debugPort">A TCP port on which to listen for a debugger connection.</param>
 public V8Runtime(V8RuntimeConstraints constraints, V8RuntimeFlags flags, int debugPort)
     : this(null, constraints, flags, debugPort)
 {
 }
예제 #5
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints and options.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 public V8Runtime(V8RuntimeConstraints constraints, V8RuntimeFlags flags)
     : this(constraints, flags, 0)
 {
 }
예제 #6
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified name, resource constraints, and options.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 public V8Runtime(string name, V8RuntimeConstraints constraints, V8RuntimeFlags flags)
     : this(name, constraints, flags, 0)
 {
 }
예제 #7
0
 /// <summary>
 /// Initializes a new V8 script engine instance with the specified name, resource constraints, options, and debug port.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the V8 runtime (see remarks).</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="debugPort">A TCP/IP port on which to listen for a debugger connection.</param>
 /// <remarks>
 /// A separate V8 runtime is created for the new script engine instance.
 /// </remarks>
 public V8ScriptEngine(string name, V8RuntimeConstraints constraints, V8ScriptEngineFlags flags, int debugPort)
     : this(null, name, constraints, flags, debugPort)
 {
 }
예제 #8
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 public V8Runtime(V8RuntimeConstraints constraints)
     : this(null, constraints)
 {
 }
예제 #9
0
 public static V8IsolateProxy Create(string name, V8RuntimeConstraints constraints, bool enableDebugging, bool enableRemoteDebugging, int debugPort)
 {
     return(CreateImpl <V8IsolateProxy>(name, constraints, enableDebugging, enableRemoteDebugging, debugPort));
 }
예제 #10
0
 /// <summary>
 /// Initializes a new V8 script engine instance with the specified resource constraints and options.
 /// </summary>
 /// <param name="constraints">Resource constraints for the V8 runtime (see remarks).</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <remarks>
 /// A separate V8 runtime is created for the new script engine instance.
 /// </remarks>
 public V8ScriptEngine(V8RuntimeConstraints constraints, V8ScriptEngineFlags flags)
     : this(constraints, flags, 0)
 {
 }
 public static V8IsolateProxy Create(string name, V8RuntimeConstraints constraints, V8RuntimeFlags flags, int debugPort)
 {
     return(CreateImpl <V8IsolateProxy>(name, constraints, flags, debugPort));
 }
        public void V8ScriptEngine_ResourceConstraints_Dual()
        {
            const int limit = 4 * 1024 * 1024;
            const string code = @"x = []; for (i = 0; i < 1024 * 1024; i++) { x.push(x); }";

            engine.Execute(code);
            engine.CollectGarbage(true);
            var usedHeapSize = engine.GetRuntimeHeapInfo().UsedHeapSize;

            var constraints = new V8RuntimeConstraints
            {
                MaxYoungSpaceSize = limit,
                MaxOldSpaceSize = limit,
                MaxExecutableSize = limit
            };

            engine.Dispose();
            engine = new V8ScriptEngine(constraints);

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.Execute(code);
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });

            engine.CollectGarbage(true);
            Assert.IsTrue(usedHeapSize > engine.GetRuntimeHeapInfo().UsedHeapSize);
        }
        public void V8ScriptEngine_ResourceConstraints()
        {
            const int limit = 4 * 1024 * 1024;
            const string code = @"x = []; while (true) { x.push(x); }";

            var constraints = new V8RuntimeConstraints
            {
                MaxYoungSpaceSize = limit,
                MaxOldSpaceSize = limit,
                MaxExecutableSize = limit
            };

            engine.Dispose();
            engine = new V8ScriptEngine(constraints);

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.Execute(code);
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.CollectGarbage(true);
                    engine.Execute("x = 5");
                }
                catch (ScriptEngineException exception)
                {
                    Assert.IsTrue(exception.IsFatal);
                    throw;
                }
            });
        }
예제 #14
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 public V8Runtime(V8RuntimeConstraints constraints)
     : this(null, constraints)
 {
 }
예제 #15
0
        internal V8ScriptEngine(V8Runtime runtime, string name, V8RuntimeConstraints constraints, V8ScriptEngineFlags flags, int debugPort)
            : base((runtime != null) ? runtime.Name + ":" + name : name)
        {
            using (var localRuntime = (runtime != null) ? null : new V8Runtime(name, constraints))
            {
                var activeRuntime = runtime ?? localRuntime;
                hostItemCollateral = activeRuntime.HostItemCollateral;

                engineFlags = flags;
                proxy       = V8ContextProxy.Create(activeRuntime.IsolateProxy, Name, flags.HasFlag(V8ScriptEngineFlags.EnableDebugging), flags.HasFlag(V8ScriptEngineFlags.DisableGlobalMembers), debugPort);
                script      = GetRootItem();

                var engineInternal = Evaluate(
                    MiscHelpers.FormatInvariant("{0} [internal]", GetType().Name),
                    false,
                    @"
                        EngineInternal = (function () {

                            function convertArgs(args) {
                                var result = [];
                                var count = args.Length;
                                for (var i = 0; i < count; i++) {
                                    result.push(args[i]);
                                }
                                return result;
                            }

                            function construct(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15) {
                                return new this(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15);
                            }

                            var isHostObjectKey = this.isHostObjectKey;
                            delete this.isHostObjectKey;

                            return {

                                getCommandResult: function (value) {
                                    if (value == null) {
                                        return value;
                                    }
                                    if (typeof(value.hasOwnProperty) != 'function') {
                                        return '[external]';
                                    }
                                    if (value[isHostObjectKey] === true) {
                                        return value;
                                    }
                                    if (typeof(value.toString) != 'function') {
                                        return '[' + typeof(value) + ']';
                                    }
                                    return value.toString();
                                },

                                invokeConstructor: function (constructor, args) {
                                    if (typeof(constructor) != 'function') {
                                        throw new Error('Function expected');
                                    }
                                    return construct.apply(constructor, convertArgs(args));
                                },

                                invokeMethod: function (target, method, args) {
                                    if (typeof(method) != 'function') {
                                        throw new Error('Function expected');
                                    }
                                    return method.apply(target, convertArgs(args));
                                },

                                getStackTrace: function () {
                                    try {
                                        throw new Error('[stack trace]');
                                    }
                                    catch (exception) {
                                        return exception.stack;
                                    }
                                    return '';
                                }
                            };
                        })();
                    "
                    );

                ((IDisposable)engineInternal).Dispose();
            }
        }
예제 #16
0
 public static V8IsolateProxy Create(string name, V8RuntimeConstraints constraints, bool enableDebugging, int debugPort)
 {
     return CreateImpl<V8IsolateProxy>(name, constraints, enableDebugging, debugPort);
 }
예제 #17
0
 /// <summary>
 /// Initializes a new V8 script engine instance with the specified resource constraints.
 /// </summary>
 /// <param name="constraints">Resource constraints for the V8 runtime (see remarks).</param>
 /// <remarks>
 /// A separate V8 runtime is created for the new script engine instance.
 /// </remarks>
 public V8ScriptEngine(V8RuntimeConstraints constraints)
     : this(null, constraints)
 {
 }
예제 #18
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified name and resource constraints.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the instance.</param>
 public V8Runtime(string name, V8RuntimeConstraints constraints)
     : this(name, constraints, V8RuntimeFlags.None)
 {
 }
예제 #19
0
 /// <summary>
 /// Initializes a new V8 script engine instance with the specified name and resource constraints.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the V8 runtime (see remarks).</param>
 /// <remarks>
 /// A separate V8 runtime is created for the new script engine instance.
 /// </remarks>
 public V8ScriptEngine(string name, V8RuntimeConstraints constraints)
     : this(name, constraints, V8ScriptEngineFlags.None)
 {
 }
예제 #20
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints, options, and debug port.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="debugPort">A TCP/IP port on which to listen for a debugger connection.</param>
 public V8Runtime(V8RuntimeConstraints constraints, V8RuntimeFlags flags, int debugPort)
     : this(null, constraints, flags, debugPort)
 {
 }
예제 #21
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified resource constraints and options.
 /// </summary>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 public V8Runtime(V8RuntimeConstraints constraints, V8RuntimeFlags flags)
     : this(constraints, flags, 0)
 {
 }
예제 #22
0
 /// <summary>
 /// Initializes a new V8 runtime instance with the specified name, resource constraints, options, and debug port.
 /// </summary>
 /// <param name="name">A name to associate with the instance. Currently this name is used only as a label in presentation contexts such as debugger user interfaces.</param>
 /// <param name="constraints">Resource constraints for the instance.</param>
 /// <param name="flags">A value that selects options for the operation.</param>
 /// <param name="debugPort">A TCP/IP port on which to listen for a debugger connection.</param>
 public V8Runtime(string name, V8RuntimeConstraints constraints, V8RuntimeFlags flags, int debugPort)
 {
     this.name = nameManager.GetUniqueName(name, GetType().GetRootName());
     proxy = V8IsolateProxy.Create(this.name, constraints, flags.HasFlag(V8RuntimeFlags.EnableDebugging), debugPort);
 }
예제 #23
0
        public void BugFix_V8RuntimeConstraintScale()
        {
            const int maxNewSpaceSize = 16;
            const int maxOldSpaceSize = 512;

            var constraints = new V8RuntimeConstraints
            {
                MaxNewSpaceSize = maxNewSpaceSize,
                MaxOldSpaceSize = maxOldSpaceSize
            };

            using (var tempEngine = new V8ScriptEngine(constraints))
            {
                Assert.AreEqual(Math.PI, tempEngine.Evaluate("Math.PI"));
                Assert.AreEqual(Convert.ToUInt64(maxNewSpaceSize * 4 + maxOldSpaceSize), tempEngine.GetRuntimeHeapInfo().HeapSizeLimit / (1024 * 1024));
            }

            constraints = new V8RuntimeConstraints
            {
                MaxNewSpaceSize = maxNewSpaceSize * 1024 * 1024,
                MaxOldSpaceSize = maxOldSpaceSize * 1024 * 1024
            };

            using (var tempEngine = new V8ScriptEngine(constraints))
            {
                Assert.AreEqual(Math.E, tempEngine.Evaluate("Math.E"));
                Assert.AreEqual(Convert.ToUInt64(maxNewSpaceSize * 4 + maxOldSpaceSize), tempEngine.GetRuntimeHeapInfo().HeapSizeLimit / (1024 * 1024));
            }
        }