"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepackNodeCLI = prepackNodeCLI; exports.prepackNodeCLISync = prepackNodeCLISync; var _invariant = require("./invariant.js"); var _invariant2 = _interopRequireDefault(_invariant); var _realm = require("./realm.js"); var _index = require("./serializer/index.js"); var _index2 = _interopRequireDefault(_index); var _completions = require("./completions.js"); var _values = require("./values"); var _construct_realm = require("./construct_realm.js"); var _construct_realm2 = _interopRequireDefault(_construct_realm); var _globals = require("./globals.js"); var _globals2 = _interopRequireDefault(_globals); var _prepackOptions = require("./prepack-options"); var _errors = require("./errors.js"); var _bootstrap = require("./intrinsics/node/bootstrap.js"); var _bootstrap2 = _interopRequireDefault(_bootstrap); var _process = require("./intrinsics/node/process.js"); var _process2 = _interopRequireDefault(_process); var _options = require("./options"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * Copyright (c) 2017-present, Facebook, Inc. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ /* API functions for running Prepack on code that expects to run on Node */ function prepackNodeCLI(filename) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _options.defaultOptions; var callback = arguments[2]; var serialized = void 0; try { serialized = prepackNodeCLISync(filename, options); } catch (err) { callback(err); return; } callback(null, serialized); } function prepackNodeCLISync(filename) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _options.defaultOptions; if (process.version !== "v7.9.0") { console.warn("Prepack's node-cli mode currently only works on Node v7.9.0.\n" + ("You are running version " + process.version + " which will likely fail.")); } var realm = (0, _construct_realm2.default)((0, _prepackOptions.getRealmOptions)(options)); (0, _globals2.default)(realm); var processObj = (0, _process2.default)(realm, ["node", filename]); var bootstrapFn = (0, _bootstrap2.default)(realm); var serializer = new _index2.default(realm, (0, _prepackOptions.getSerializerOptions)(options)); var context = new _realm.ExecutionContext(); context.lexicalEnvironment = realm.$GlobalEnv; context.variableEnvironment = realm.$GlobalEnv; context.realm = realm; realm.pushContext(context); var res = void 0; try { if (bootstrapFn.$Call) { res = bootstrapFn.$Call(realm.intrinsics.null, [processObj]); } } catch (err) { if (err instanceof _completions.Completion) { res = err; } else if (err instanceof Error) { throw err; } else { throw new _errors.FatalError(err); } } finally { realm.popContext(context); } if (res instanceof _completions.Completion) { context = new _realm.ExecutionContext(); realm.pushContext(context); try { serializer.logger.logCompletion(res); } finally { realm.popContext(context); realm.onDestroyScope(realm.$GlobalEnv); } } // Hack: Turn these objects generated by the bootstrap script into // intrinsics that exist in a preinitialized environment. This ensures // that we don't end up with duplicates of these. This won't work in an // uninitialized environment. var nextTick = realm.$GlobalEnv.execute("process.nextTick", "", ""); (0, _invariant2.default)(nextTick instanceof _values.Value); nextTick.intrinsicName = "process.nextTick"; var tickCallback = realm.$GlobalEnv.execute("process._tickCallback", "", ""); (0, _invariant2.default)(tickCallback instanceof _values.Value); tickCallback.intrinsicName = "process._tickCallback"; var tickDomainCallback = realm.$GlobalEnv.execute("process._tickDomainCallback", "", ""); (0, _invariant2.default)(tickDomainCallback instanceof _values.Value); tickDomainCallback.intrinsicName = "process._tickDomainCallback"; // Serialize var sources = [{ filePath: "", fileContents: "" }]; var serialized = serializer.init(sources, options.sourceMaps); if (!serialized) { throw new _errors.FatalError("serializer failed"); } return serialized; } //# sourceMappingURL=prepack-node-environment.js.map