UNPKG

prepack

Version:

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

100 lines (89 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNodeBufferFromTypedArray = getNodeBufferFromTypedArray; exports.createDeepIntrinsic = createDeepIntrinsic; exports.copyProperty = copyProperty; var _invariant = require("../../invariant.js"); var _invariant2 = _interopRequireDefault(_invariant); var _errors = require("../../errors.js"); var _index = require("../../values/index.js"); var _singletons = require("../../singletons.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. */ function getNodeBufferFromTypedArray(realm, value) { let buffer = value.$ViewedArrayBuffer; (0, _invariant2.default)(buffer instanceof _index.ObjectValue && buffer.$ArrayBufferData); return buffer.$ArrayBufferData; } // Takes a value from the host realm and create it into a Prepack Realm. // TODO: Move this to a bigger general purpose proxy between the environments. // See issue #644 for more details. function createDeepIntrinsic(realm, value, intrinsicName) { switch (typeof value) { case "undefined": return realm.intrinsics.undefined; case "boolean": return new _index.BooleanValue(realm, value, intrinsicName); case "number": return new _index.NumberValue(realm, value, intrinsicName); case "string": return new _index.StringValue(realm, value, intrinsicName); // $FlowFixMe flow doesn't understand symbols. case "symbol": throw new _errors.FatalError("Symbol cannot be safely cloned."); case "function": throw new _errors.FatalError("Functions could be supported but are not yet."); case "object": { if (value === null) { return realm.intrinsics.null; } if (Array.isArray(value)) { throw new _errors.FatalError("Arrays are not supported yet."); } let prototype = Object.getPrototypeOf(value); if (prototype !== Object.prototype) { throw new _errors.FatalError("Only simple objects are supported for now. Got: " + (typeof prototype.constructor === "function" && prototype.constructor.name || Object.prototype.toString.call(prototype))); } let obj = new _index.ObjectValue(realm, realm.intrinsics.ObjectPrototype, intrinsicName // We use the intrinsic name for Objects to preserve their referential equality ); let names = Object.getOwnPropertyNames(value); for (let name of names) { // We intentionally invoke the getter on value[name] which resolves any // lazy getters. let newValue = createDeepIntrinsic(realm, value[name], intrinsicName + "." + name); copyProperty(realm, value, obj, name, newValue); } return obj; } default: (0, _invariant2.default)(false); } } // Define a value with the same descriptor settings as the original object. function copyProperty(realm, originalObject, realmObject, name, value) { let desc = Object.getOwnPropertyDescriptor(originalObject, name); if (!desc) { return; } if (desc.get || desc.set) { throw new _errors.FatalError("Getter/setters are not supported because functions are not supported yet."); } let newDesc = { value: value, writable: !!desc.writable, configurable: !!desc.configurable, enumerable: !!desc.enumerable }; _singletons.Properties.DefinePropertyOrThrow(realm, realmObject, name, newDesc); } //# sourceMappingURL=utils.js.map