UNPKG

prepack

Version:

Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.

155 lines (116 loc) 5.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepackSources = prepackSources; exports.prepackVersion = void 0; var _index = _interopRequireDefault(require("./serializer/index.js")); var _construct_realm = _interopRequireDefault(require("./construct_realm.js")); var _globals = _interopRequireDefault(require("./globals.js")); var _index2 = require("./methods/index.js"); var _prepackOptions = require("./prepack-options"); var _errors = require("./errors.js"); var _types = require("./types.js"); var _completions = require("./completions.js"); var _options = require("./options"); var _invariant = _interopRequireDefault(require("./invariant.js")); var _package = require("../package.json"); var _statistics = require("./serializer/statistics.js"); var _ResidualHeapVisitor = require("./serializer/ResidualHeapVisitor.js"); var _modules = require("./utils/modules.js"); var _logger = require("./utils/logger.js"); var _generator = require("./utils/generator.js"); var _index3 = require("./values/index.js"); 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. */ /* APIs for running Prepack for code where a model of the environment is supplied as part of the code. */ function prepackSources(sourceFileCollection, options = _options.defaultOptions, statistics = undefined) { if (Array.isArray(sourceFileCollection)) sourceFileCollection = new _types.SourceFileCollection(sourceFileCollection); let realmOptions = (0, _prepackOptions.getRealmOptions)(options); realmOptions.errorHandler = options.errorHandler; let realm = (0, _construct_realm.default)(realmOptions, options.debuggerConfigArgs, statistics || new _statistics.SerializerStatistics(), options.debugReproArgs); (0, _globals.default)(realm); if (typeof options.additionalGlobals === "function") { options.additionalGlobals(realm); } if (options.check) { realm.generator = new _generator.Generator(realm, "main", realm.pathConditions); let logger = new _logger.Logger(realm, !!options.internalDebug); let modules = new _modules.Modules(realm, logger, !!options.logModules); let [result] = realm.$GlobalEnv.executeSources(sourceFileCollection.toArray()); if (result instanceof _completions.AbruptCompletion) throw result; (0, _invariant.default)(options.check); checkResidualFunctions(modules, options.check[0], options.check[1]); return { code: "", map: undefined }; } else { let serializer = new _index.default(realm, (0, _prepackOptions.getSerializerOptions)(options)); let serialized = serializer.init(sourceFileCollection, options.sourceMaps, options.onParse, options.onExecute); //Turn off the debugger if there is one if (realm.debuggerInstance) { realm.debuggerInstance.shutdown(); } if (!serialized) { throw new _errors.FatalError("serializer failed"); } if (realm.debugReproManager) { let localManager = realm.debugReproManager; let sourcePaths = { sourceFiles: localManager.getSourceFilePaths(), sourceMaps: localManager.getSourceMapPaths() }; serialized.sourceFilePaths = sourcePaths; } return serialized; } } function checkResidualFunctions(modules, startFunc, totalToAnalyze) { let realm = modules.realm; let env = realm.$GlobalEnv; realm.$GlobalObject.makeSimple(); let errorHandler = realm.errorHandler; if (!errorHandler) errorHandler = (diag, suppressDiagnostics) => realm.handleError(diag); realm.errorHandler = (diag, suppressDiagnostics) => { (0, _invariant.default)(errorHandler); if (diag.severity === "FatalError") return errorHandler(diag, realm.suppressDiagnostics);else return "Recover"; }; modules.resolveInitializedModules(); let residualHeapVisitor = new _ResidualHeapVisitor.ResidualHeapVisitor(realm, modules.logger, modules, new Map()); residualHeapVisitor.visitRoots(); if (modules.logger.hasErrors()) return; let totalFunctions = 0; let nonFatalFunctions = 0; for (let fi of residualHeapVisitor.functionInstances.values()) { totalFunctions++; if (totalFunctions <= startFunc) continue; let fv = fi.functionValue; console.log("analyzing: " + totalFunctions); let thisValue = realm.intrinsics.null; let n = fv.getLength() || 0; let args = []; for (let i = 0; i < n; i++) { let name = "dummy parameter"; let ob = _index3.AbstractValue.createFromType(realm, _index3.ObjectValue, name); ob.makeSimple("transitive"); ob.intrinsicName = name; args[i] = ob; } // todo: eventually join these effects, apply them to the global state and iterate to a fixed point try { realm.evaluateForEffectsInGlobalEnv(() => (0, _index2.EvaluateDirectCallWithArgList)(modules.realm, true, env, fv, fv, thisValue, args)); nonFatalFunctions++; } catch (e) {} if (totalFunctions >= startFunc + totalToAnalyze) break; } console.log(`Analyzed ${totalToAnalyze} functions starting at ${startFunc} of which ${nonFatalFunctions} did not have fatal errors.`); } const prepackVersion = _package.version; exports.prepackVersion = prepackVersion; //# sourceMappingURL=prepack-standalone.js.map