UNPKG

prepack

Version:

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

210 lines (150 loc) 6.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = function (realm) { let obj = new _index.ObjectValue(realm, realm.intrinsics.ObjectPrototype, "Math"); // ECMA262 20.2.1.9 obj.defineNativeProperty(realm.intrinsics.SymbolToStringTag, new _index.StringValue(realm, "Math"), { writable: false }); // ECMA262 20.2.1.1 obj.defineNativeConstant("E", new _index.NumberValue(realm, 2.7182818284590452354)); // ECMA262 20.2.1.2 obj.defineNativeConstant("LN10", new _index.NumberValue(realm, 2.302585092994046)); // ECMA262 20.2.1.3 obj.defineNativeConstant("LN2", new _index.NumberValue(realm, 0.6931471805599453)); // ECMA262 20.2.1.4 obj.defineNativeConstant("LOG10E", new _index.NumberValue(realm, 0.4342944819032518)); // ECMA262 20.2.1.5 obj.defineNativeConstant("LOG2E", new _index.NumberValue(realm, 1.4426950408889634)); // ECMA262 20.2.1.6 obj.defineNativeConstant("PI", new _index.NumberValue(realm, 3.1415926535897932)); // ECMA262 20.2.1.7 obj.defineNativeConstant("SQRT1_2", new _index.NumberValue(realm, 0.7071067811865476)); // ECMA262 20.2.1.8 obj.defineNativeConstant("SQRT2", new _index.NumberValue(realm, 1.4142135623730951)); let functions = [ // ECMA262 20.2.2.1 ["abs", 1], // ECMA262 20.2.2.2 ["acos", 1], // ECMA262 20.2.2.3 ["acosh", 1], // ECMA262 20.2.2.4 ["asin", 1], // ECMA262 20.2.2.5 ["asinh", 1], // ECMA262 20.2.2.6 ["atan", 1], // ECMA262 20.2.2.7 ["atanh", 1], // ECMA262 20.2.2.8 ["atan2", 2], // ECMA262 20.2.2.9 ["cbrt", 1], // ECMA262 20.2.2.10 ["ceil", 1], // ECMA262 20.2.2.12 ["cos", 1], // ECMA262 20.2.2.13 ["cosh", 1], // ECMA262 20.2.2.14 ["exp", 1], // ECMA262 20.2.2.15 ["expm1", 1], // ECMA262 20.2.2.16 ["floor", 1], // ECMA262 20.2.2.17 ["fround", 1], // ECMA262 20.2.2.18 ["hypot", 2], // ECMA262 20.2.2.20 ["log", 1], // ECMA262 20.2.2.21 ["log1p", 1], // ECMA262 20.2.2.22 ["log10", 1], // ECMA262 20.2.2.23 ["log2", 1], // ECMA262 20.2.2.24 ( _value1_, _value2_, ..._values_ ) ["max", 2], // ECMA262 20.2.2.25 ["min", 2], // ECMA262 20.2.2.26 ["pow", 2], // ECMA262 20.2.2.28 ["round", 1], // ECMA262 20.2.2.30 ["sin", 1], // ECMA262 20.2.2.31 ["sinh", 1], // ECMA262 20.2.2.32 ["sqrt", 1], // ECMA262 20.2.2.33 ["tan", 1], // ECMA262 20.2.2.34 ["tanh", 1], // ECMA262 20.2.2.35 ["trunc", 1]]; // ECMA262 20.2.2.11 if (!realm.isCompatibleWith(realm.MOBILE_JSC_VERSION) && !realm.isCompatibleWith("mobile")) functions.push(["clz32", 1]); // ECMA262 20.2.2.29 (_x_) if (!realm.isCompatibleWith(realm.MOBILE_JSC_VERSION) && !realm.isCompatibleWith("mobile")) functions.push(["sign", 1]); for (let [name, length] of functions) { obj.defineNativeMethod(name, length, (context, args, originalLength) => { (0, _invariant2.default)(originalLength >= 0); args.length = originalLength; if (originalLength <= 26 && args.some(arg => arg instanceof _index.AbstractValue) && args.every(arg => _singletons.To.IsToNumberPure(realm, arg))) { let r = buildMathTemplates.get(name); if (r === undefined) { let params = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".substring(0, originalLength * 2 - 1); let templateSource = `Math.${name}(${params})`; let template = (0, _builder2.default)(templateSource); buildMathTemplates.set(name, r = { template, templateSource }); } return _index.AbstractValue.createFromTemplate(realm, r.template, _index.NumberValue, args, r.templateSource); } return new _index.NumberValue(realm, Math[name].apply(null, args.map((arg, i) => _singletons.To.ToNumber(realm, arg.throwIfNotConcrete())))); }); } const imulTemplateSrc = "global.Math.imul(A, B)"; const imulTemplate = (0, _builder2.default)(imulTemplateSrc); // ECMA262 20.2.2.19 obj.defineNativeMethod("imul", 2, (context, [x, y]) => { if ((x instanceof _index.AbstractValue || y instanceof _index.AbstractValue) && _singletons.To.IsToNumberPure(realm, x) && _singletons.To.IsToNumberPure(realm, y)) { return _index.AbstractValue.createFromTemplate(realm, imulTemplate, _index.NumberValue, [x, y], imulTemplateSrc); } return new _index.NumberValue(realm, Math.imul(_singletons.To.ToUint32(realm, x.throwIfNotConcrete()), _singletons.To.ToUint32(realm, y.throwIfNotConcrete()))); }); const mathRandomTemplateSrc = "global.Math.random()"; const mathRandomTemplate = (0, _builder2.default)(mathRandomTemplateSrc); // ECMA262 20.2.2.27 obj.defineNativeMethod("random", 0, context => { if (realm.mathRandomGenerator !== undefined) { return new _index.NumberValue(realm, realm.mathRandomGenerator()); } else if (realm.useAbstractInterpretation) { return _index.AbstractValue.createTemporalFromTemplate(realm, mathRandomTemplate, _index.NumberValue, [], { isPure: true, skipInvariant: true }); } else { return new _index.NumberValue(realm, Math.random()); } }); return obj; }; var _index = require("../../values/index.js"); var _singletons = require("../../singletons.js"); var _invariant = require("../../invariant.js"); var _invariant2 = _interopRequireDefault(_invariant); var _builder = require("../../utils/builder.js"); var _builder2 = _interopRequireDefault(_builder); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } let buildMathTemplates = new Map(); /** * 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. */ //# sourceMappingURL=Math.js.map