UNPKG

prepack

Version:

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

135 lines (89 loc) 5.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = _default; 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. */ /* strict-local */ // ECMA262 13.3.1.4 function letAndConst(ast, strictCode, env, realm) { for (let declar of ast.declarations) { let Initializer = declar.init; if (declar.id.type === "Identifier" && !Initializer) { (0, _invariant.default)(ast.kind !== "const", "const without an initializer"); // 1. Let lhs be ResolveBinding(StringValue of BindingIdentifier). let bindingId = declar.id.name; let lhs = _singletons.Environment.ResolveBinding(realm, bindingId, strictCode); // 2. Return InitializeReferencedBinding(lhs, undefined). _singletons.Environment.InitializeReferencedBinding(realm, lhs, realm.intrinsics.undefined); continue; } else if (declar.id.type === "Identifier" && Initializer) { // 1. Let bindingId be StringValue of BindingIdentifier. let bindingId = declar.id.name; // 2. Let lhs be ResolveBinding(bindingId). let lhs = _singletons.Environment.ResolveBinding(realm, bindingId, strictCode); // 3. Let rhs be the result of evaluating Initializer. let rhs = env.evaluate(Initializer, strictCode); // 4. Let value be ? GetValue(rhs). let value = _singletons.Environment.GetValue(realm, rhs); // 5. If IsAnonymousFunctionDefinition(Initializer) is true, then if ((0, _index2.IsAnonymousFunctionDefinition)(realm, Initializer)) { (0, _invariant.default)(value instanceof _index.ObjectValue); // a. Let hasNameProperty be ? HasOwnProperty(value, "name"). let hasNameProperty = (0, _index2.HasOwnProperty)(realm, value, "name"); // b. If hasNameProperty is false, perform SetFunctionName(value, bindingId). if (!hasNameProperty) _singletons.Functions.SetFunctionName(realm, value, new _index.StringValue(realm, bindingId)); } // 6. Return InitializeReferencedBinding(lhs, value). _singletons.Environment.InitializeReferencedBinding(realm, lhs, value); } else if ((declar.id.type === "ObjectPattern" || declar.id.type === "ArrayPattern") && Initializer) { // 1. Let rhs be the result of evaluating Initializer. let rhs = env.evaluate(Initializer, strictCode); // 2. Let rval be ? GetValue(rhs). let rval = _singletons.Environment.GetValue(realm, rhs); // 3. Let env be the running execution context’s LexicalEnvironment. // 4. Return the result of performing BindingInitialization for BindingPattern using value and env as the arguments. _singletons.Environment.BindingInitialization(realm, declar.id, rval, strictCode, env); } else { (0, _invariant.default)(false, "unrecognized declaration"); } } return realm.intrinsics.empty; } // ECMA262 13.3.2.4 function _default(ast, strictCode, env, realm) { if (ast.kind === "let" || ast.kind === "const") { return letAndConst(ast, strictCode, env, realm); } for (let declar of ast.declarations) { let Initializer = declar.init; if (declar.id.type === "Identifier" && !Initializer) { // VariableDeclaration : BindingIdentifier // 1. Return NormalCompletion(empty). continue; } else if (declar.id.type === "Identifier" && Initializer) { // VariableDeclaration : BindingIdentifier Initializer // 1. Let bindingId be StringValue of BindingIdentifier. let bindingId = declar.id.name; // 2. Let lhs be ? ResolveBinding(bindingId). let lhs = _singletons.Environment.ResolveBinding(realm, bindingId, strictCode); // 3. Let rhs be the result of evaluating Initializer. let rhs = env.evaluate(Initializer, strictCode); // 4. Let value be ? GetValue(rhs). let value = _singletons.Environment.GetValue(realm, rhs); if (declar.id && declar.id.name !== undefined) value.__originalName = bindingId; // 5. If IsAnonymousFunctionDefinition(Initializer) is true, then if ((0, _index2.IsAnonymousFunctionDefinition)(realm, Initializer)) { (0, _invariant.default)(value instanceof _index.ObjectValue); // a. Let hasNameProperty be ? HasOwnProperty(value, "name"). let hasNameProperty = (0, _index2.HasOwnProperty)(realm, value, "name"); // b. If hasNameProperty is false, perform SetFunctionName(value, bindingId). if (!hasNameProperty) _singletons.Functions.SetFunctionName(realm, value, new _index.StringValue(realm, bindingId)); } // 6. Return ? PutValue(lhs, value). _singletons.Properties.PutValue(realm, lhs, value); } else if ((declar.id.type === "ObjectPattern" || declar.id.type === "ArrayPattern") && Initializer) { // 1. Let rhs be the result of evaluating Initializer. let rhs = env.evaluate(Initializer, strictCode); // 2. Let rval be ? GetValue(rhs). let rval = _singletons.Environment.GetValue(realm, rhs); // 3. Return the result of performing BindingInitialization for BindingPattern passing rval and undefined as arguments. _singletons.Environment.BindingInitialization(realm, declar.id, rval, strictCode, undefined); } else { (0, _invariant.default)(false, "unrecognized declaration"); } } return realm.intrinsics.empty; } //# sourceMappingURL=VariableDeclaration.js.map