internal V8ScriptEngine()

in ClearScript/V8/V8ScriptEngine.cs [214:358]


        internal V8ScriptEngine(V8Runtime runtime, string name, V8RuntimeConstraints constraints, V8ScriptEngineFlags flags, int debugPort)
            : base((runtime != null) ? runtime.Name + ":" + name : name, "js")
        {
            if (runtime != null)
            {
                this.runtime = runtime;
            }
            else
            {
                this.runtime = runtime = new V8Runtime(name, constraints);
                usingPrivateRuntime = true;
            }

            DocumentNameManager = runtime.DocumentNameManager;
            HostItemCollateral = runtime.HostItemCollateral;

            engineFlags = flags;
            proxy = V8ContextProxy.Create(runtime.IsolateProxy, Name, flags, debugPort);
            script = (V8ScriptItem)GetRootItem();

            Execute(initScriptInfo,
                @"
                    Object.defineProperty(this, 'EngineInternal', { value: (function () {

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

                        function construct() {
                            return new this(...arguments);
                        }

                        const isHostObjectKey = this.isHostObjectKey;
                        delete this.isHostObjectKey;

                        const savedPromise = Promise;

                        return Object.freeze({

                            commandHolder: {
                            },

                            getCommandResult: function (value) {
                                if (value == null) {
                                    return value;
                                }
                                if (typeof(value.hasOwnProperty) != 'function') {
                                    if (value[Symbol.toStringTag] == 'Module') {
                                        return '[module]';
                                    }
                                    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));
                            },

                            createPromise: function () {
                                return new savedPromise(...arguments);
                            },

                            isPromise: function (value) {
                                return value instanceof savedPromise;
                            },

                            completePromiseWithResult: function (getResult, resolve, reject) {
                                try {
                                    resolve(getResult());
                                }
                                catch (exception) {
                                    reject(exception);
                                }
                                return undefined;
                            },

                            completePromise: function (wait, resolve, reject) {
                                try {
                                    wait();
                                    resolve();
                                }
                                catch (exception) {
                                    reject(exception);
                                }
                                return undefined;
                            },

                            throwValue: function (value) {
                                throw value;
                            },

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

                            toIterator: function* (enumerator) {
                                while (enumerator.MoveNext()) {
                                    yield enumerator.Current;
                                }
                            },

                            toAsyncIterator: async function* (asyncEnumerator) {
                                while (await asyncEnumerator.MoveNextPromise()) {
                                    yield asyncEnumerator.Current;
                                }
                            }

                        });
                    })() });
                "
            );

            if (flags.HasFlag(V8ScriptEngineFlags.EnableDebugging | V8ScriptEngineFlags.AwaitDebuggerAndPauseOnStart))
            {
                awaitDebuggerAndPause = true;
            }
        }