123 lines
6.5 KiB
JavaScript
123 lines
6.5 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
|
|
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof 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.
|
|
*/
|
|
|
|
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 }; }
|
|
|
|
function getNodeBufferFromTypedArray(realm, value) {
|
|
var 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 === "undefined" ? "undefined" : _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.");
|
|
}
|
|
var 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)));
|
|
}
|
|
var obj = new _index.ObjectValue(realm, realm.intrinsics.ObjectPrototype, intrinsicName // We use the intrinsic name for Objects to preserve their referential equality
|
|
);
|
|
var names = Object.getOwnPropertyNames(value);
|
|
var _iteratorNormalCompletion = true;
|
|
var _didIteratorError = false;
|
|
var _iteratorError = undefined;
|
|
|
|
try {
|
|
for (var _iterator = names[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
|
var name = _step.value;
|
|
|
|
// We intentionally invoke the getter on value[name] which resolves any
|
|
// lazy getters.
|
|
var newValue = createDeepIntrinsic(realm, value[name], intrinsicName + "." + name);
|
|
copyProperty(realm, value, obj, name, newValue);
|
|
}
|
|
} catch (err) {
|
|
_didIteratorError = true;
|
|
_iteratorError = err;
|
|
} finally {
|
|
try {
|
|
if (!_iteratorNormalCompletion && _iterator.return) {
|
|
_iterator.return();
|
|
}
|
|
} finally {
|
|
if (_didIteratorError) {
|
|
throw _iteratorError;
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
var 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.");
|
|
}
|
|
var newDesc = {
|
|
value: value,
|
|
writable: !!desc.writable,
|
|
configurable: !!desc.configurable,
|
|
enumerable: !!desc.enumerable
|
|
};
|
|
_singletons.Properties.DefinePropertyOrThrow(realm, realmObject, name, newDesc);
|
|
}
|
|
//# sourceMappingURL=utils.js.map
|