prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
126 lines (91 loc) • 5.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.EvalPropertyName = EvalPropertyName;
exports.default = _default;
var _errors = require("../errors.js");
var _index = require("../values/index.js");
var _index2 = require("../methods/index.js");
var _singletons = require("../singletons.js");
var _invariant = _interopRequireDefault(require("../invariant.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.
*/
// Returns the result of evaluating PropertyName.
function EvalPropertyName(prop, env, realm, strictCode) {
let result = EvalPropertyNamePartial(prop, env, realm, strictCode);
if (result instanceof _index.AbstractValue) {
let error = new _errors.CompilerDiagnostic("unknown computed property name", prop.loc, "PP0014", "FatalError");
realm.handleError(error);
throw new _errors.FatalError();
}
return result;
}
function EvalPropertyNamePartial(prop, env, realm, strictCode) {
if (prop.computed) {
let propertyKeyName = _singletons.Environment.GetValue(realm, env.evaluate(prop.key, strictCode));
if (propertyKeyName instanceof _index.AbstractValue) return propertyKeyName;
(0, _invariant.default)(propertyKeyName instanceof _index.ConcreteValue);
return _singletons.To.ToPropertyKey(realm, propertyKeyName);
} else {
if (prop.key.type === "Identifier") {
return new _index.StringValue(realm, prop.key.name);
} else {
let propertyKeyName = _singletons.Environment.GetValue(realm, env.evaluate(prop.key, strictCode));
(0, _invariant.default)(propertyKeyName instanceof _index.ConcreteValue); // syntax only allows literals if !prop.computed
return _singletons.To.ToString(realm, propertyKeyName);
}
}
} // ECMA262 12.2.6.8
function _default(ast, strictCode, env, realm) {
// 1. Let obj be ObjectCreate(%ObjectPrototype%).
let obj = _singletons.Create.ObjectCreate(realm, realm.intrinsics.ObjectPrototype); // 2. Let status be the result of performing PropertyDefinitionEvaluation of PropertyDefinitionList with arguments obj and true.
for (let prop of ast.properties) {
if (prop.type === "ObjectProperty") {
// 12.2.6.9 case 3
// 1. Let propKey be the result of evaluating PropertyName.
let propKey = EvalPropertyNamePartial(prop, env, realm, strictCode); // 2. ReturnIfAbrupt(propKey).
// 3. Let exprValueRef be the result of evaluating AssignmentExpression.
let exprValueRef = env.evaluate(prop.value, strictCode); // 4. Let propValue be ? GetValue(exprValueRef).
let propValue = _singletons.Environment.GetValue(realm, exprValueRef); // 5. If IsAnonymousFunctionDefinition(AssignmentExpression) is true, then
if ((0, _index2.IsAnonymousFunctionDefinition)(realm, prop.value)) {
(0, _invariant.default)(propValue instanceof _index.ObjectValue); // a. Let hasNameProperty be ? HasOwnProperty(propValue, "name").
let hasNameProperty = (0, _index2.HasOwnProperty)(realm, propValue, "name"); // b. If hasNameProperty is false, perform SetFunctionName(propValue, propKey).
(0, _invariant.default)(!hasNameProperty); // No expression that passes through IsAnonymousFunctionDefinition can have it here
_singletons.Functions.SetFunctionName(realm, propValue, propKey);
} // 6. Assert: enumerable is true.
// 7. Return CreateDataPropertyOrThrow(object, propKey, propValue).
if (propKey instanceof _index.AbstractValue) {
if (propKey.mightNotBeString()) {
let error = new _errors.CompilerDiagnostic("property key value is unknown", prop.loc, "PP0011", "FatalError");
if (realm.handleError(error) === "Fail") throw new _errors.FatalError();
continue; // recover by ignoring the property, which is only ever safe to do if the property is dead,
// which is assuming a bit much, hence the designation as a FatalError.
}
obj.$SetPartial(propKey, propValue, obj);
} else {
_singletons.Create.CreateDataPropertyOrThrow(realm, obj, propKey, propValue);
}
} else if (prop.type === "SpreadElement") {
// 1. Let exprValue be the result of evaluating AssignmentExpression.
let exprValue = env.evaluate(prop.argument, strictCode); // 2. Let fromValue be GetValue(exprValue).
let fromValue = _singletons.Environment.GetValue(realm, exprValue); // 3. ReturnIfAbrupt(fromValue).
// 4. Let excludedNames be a new empty List.
let excludedNames = []; // 4. Return ? CopyDataProperties(object, fromValue, excludedNames).
_singletons.Create.CopyDataProperties(realm, obj, fromValue, excludedNames);
} else {
(0, _invariant.default)(prop.type === "ObjectMethod");
_singletons.Properties.PropertyDefinitionEvaluation(realm, prop, obj, env, strictCode, true);
}
} // 3. ReturnIfAbrupt(status).
// 4. Return obj.
return obj;
}
//# sourceMappingURL=ObjectExpression.js.map