Created
September 22, 2021 20:28
-
-
Save samth/1738d48af297b05d9067fcf38402db8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function _get(target, property, receiver) { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get; } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(receiver); } return desc.value; }; } return _get(target, property, receiver || target); } | |
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; } | |
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } | |
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } | |
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } | |
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } | |
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | |
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | |
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | |
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | |
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | |
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } | |
function _possibleConstructorReturn(self, call) { if (call && (_typeof2(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); } | |
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | |
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } | |
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | |
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | |
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | |
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | |
function _typeof2(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof2 = function _typeof2(obj) { return typeof obj; }; } else { _typeof2 = function _typeof2(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof2(obj); } | |
Object.defineProperty(exports, '__esModule', { | |
value: true | |
}); | |
/** | |
* Returns a non-cryptographic hash code for any object. | |
* Suitable for use in hash tables. | |
* | |
* Primitives are hashed based on their value. | |
* Everything else is hashed based on its identity (a basically unique ID). | |
* | |
* If two values are equal with `===` (ECMAScript spec: SameValueZero), | |
* their hash codes are guaranteed to be the same, | |
* with the exception of Symbol values. | |
* | |
* @param {*} o | |
* @return {!number} a 32-bit integer | |
*/ | |
function hash(o) { | |
if (o === null) return 0; | |
if (o && o.sym) return hashString("sym_".concat(o.toString())); | |
switch (_typeof2(o)) { | |
case 'number': | |
return hashNumber(o); | |
case 'string': | |
return hashString(o); | |
case 'boolean': | |
return o ? 1 : -1; | |
case 'undefined': | |
return 0; | |
case 'object': | |
case 'function': | |
return hashObjectIdentity(o); | |
default: | |
return hashString(o.toString()); | |
} | |
} | |
/** | |
* Calculates the given string's hash. | |
* | |
* Uses the same algorithm as the JVM string hash. | |
* | |
* @param {!String} s | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashString(s) { | |
var h = 0; | |
var n = s.length; | |
for (var i = 0; i < n; ++i) { | |
// Benchmarks of various ways to do this: | |
// https://run.perf.zone/view/String-Hashing-Performance-1504040177726 | |
h = ~~((h << 5) - h + s.charCodeAt(i)); | |
} | |
return h; | |
} // Buffers and arrays for {hashNumber}. | |
var kBuf = new ArrayBuffer(8); | |
var kBufAsF64 = new Float64Array(kBuf); | |
var kBufAsI32 = new Int32Array(kBuf); | |
/** | |
* Calculates the given number's hash. | |
* | |
* If two numbers are equal with `===` (ECMAScript spec: SameValueZero), | |
* the returned hash codes will be the same for them. | |
* | |
* @param {!number} n any number | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashNumber(n) { | |
// Benchmarks of various ways to do this: | |
// https://run.perf.zone/view/Number-Hashing-Performance-v9-1504054910628 | |
// For small numbers, return the number directly. | |
// This slightly increases the potential number of collisions | |
// with large numbers and floats, but increases the performance by 20%. | |
if (~~n === n) { | |
// If `n` is -0, the above check will pass. | |
// `~~` is here only to convert the potential -0 to 0. | |
return ~~n; | |
} | |
kBufAsF64[0] = n; | |
return kBufAsI32[0] ^ kBufAsI32[1]; | |
} | |
/** | |
* A weak map to the object's ID for {hashObjectIdentity}. | |
* | |
* @type {!WeakMap<Object, number>} | |
*/ | |
var objectIds = new WeakMap(); | |
var currentId = 0; | |
/** | |
* Returns an object ID. | |
* | |
* @param {!(Object|null)} o | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashObjectIdentity(o) { | |
var result = objectIds.get(o); | |
if (result === undefined) { | |
currentId = ~~(currentId + 1); | |
objectIds.set(o, currentId); | |
return currentId; | |
} | |
return result; | |
} //= The functions below are not called by `hash`. | |
/** | |
* Returns a hash of the array. | |
* | |
* The elements of the array must either be integers or objects | |
* with `valueOf` that returns an integer. | |
* | |
* @template T | |
* @param {T[]} a | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashIntArray(a) { | |
var h = 0; | |
var n = a.length; | |
for (var i = 0; i < n; ++i) { | |
h = ~~((h << 5) - h + a[i]); | |
} | |
return h; | |
} | |
/** | |
* Base class for various compound data types | |
* such as structs, pairs, values, ... | |
* | |
* Subclasses must override the following methods: | |
* * displayNativeString | |
* * isImmutable | |
* * equals | |
* | |
* @abstract | |
*/ | |
var Primitive = /*#__PURE__*/function () { | |
function Primitive() { | |
_classCallCheck(this, Primitive); | |
} | |
_createClass(Primitive, [{ | |
key: "hashForEqual", | |
value: | |
/** @abstract isImmutable(): boolean; */ | |
/** @abstract equals(*): boolean; */ | |
/** | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashForEqual() { | |
return hashString(this.toString()); | |
} | |
}]); | |
return Primitive; | |
}(); | |
function check(v) { | |
return v instanceof Primitive; | |
} | |
/** | |
* A simplified internal-only version of Ports.NativeOutputStringPort. | |
* | |
* This can be used where dependency on Ports is not possible, | |
* e.g. because Ports are Printable, | |
* and provides a faster no-frills internal implementation. | |
*/ | |
var MiniNativeOutputStringPort = /*#__PURE__*/function () { | |
function MiniNativeOutputStringPort() { | |
_classCallCheck(this, MiniNativeOutputStringPort); | |
this._buffer = []; | |
} | |
/** | |
* @param {String} nativeString | |
*/ | |
_createClass(MiniNativeOutputStringPort, [{ | |
key: "consume", | |
value: function consume(nativeString) { | |
this._buffer.push(nativeString); | |
} | |
/** | |
* @return {String} nativeString | |
*/ | |
}, { | |
key: "getOutputString", | |
value: function getOutputString() { | |
return this._buffer.join(''); | |
} | |
}]); | |
return MiniNativeOutputStringPort; | |
}(); | |
/** | |
* @param {*} bs | |
* @return {!boolean} | |
*/ | |
function check$1(bs) { | |
return _typeof2(bs) === 'object' && bs !== null && bs.constructor === Uint8Array; | |
} | |
/** | |
* @param {!number} non-negative int length | |
* @param {!number} non-negative int less than 256 | |
* @return {!Uint8Array} | |
*/ | |
function make(len, init) { | |
return new Uint8Array(len).fill(init); | |
} | |
/** | |
* | |
* @param {!Uint8Array} a | |
* @param {!Uint8Array} b | |
* @return {!boolean} | |
*/ | |
function eq(a, b) { | |
if (a.length !== b.length) return false; | |
var n = a.length; | |
for (var i = 0; i < n; i++) { | |
if (a[i] !== b[i]) return false; | |
} | |
return true; | |
} | |
var utf8Decoder = new TextDecoder('utf-8'); | |
/** | |
* @param {!Uint8Array} bytes | |
* @return {!String} | |
*/ | |
function toString(bytes) { | |
return utf8Decoder.decode(bytes); | |
} | |
/** | |
* Writes a string representation similar to Racket's `display` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!Uint8Array} bytes | |
*/ | |
function displayNativeString(out, bytes) { | |
out.consume(toString(bytes)); | |
} | |
/** | |
* Writes a string representation similar to Racket's `print` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!Uint8Array} bytes | |
*/ | |
function printNativeString(out, bytes) { | |
out.consume('#"'); | |
out.consume(toString(bytes)); | |
out.consume('"'); | |
} | |
/** | |
* @param {!Uint8Array} bytes | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashForEqual(bytes) { | |
return hashIntArray(bytes); | |
} | |
/** | |
* A single Unicode character. | |
* Value type semantics, immutable and final. | |
* | |
* The parameter type signatures in this class and file are enforced | |
* by RacketScript when called from RacketScript. | |
* | |
* No checks are performed here, allowing us to use it internally | |
* without paying the cost of runtime type checking. | |
* | |
* @property {!number} codepoint | |
* @final | |
*/ | |
var Char = /*#__PURE__*/function (_Primitive) { | |
_inherits(Char, _Primitive); | |
var _super = _createSuper(Char); | |
/** | |
* @param {!number} codepoint a non-negative integer. | |
* @param {(!string|null)} nativeString | |
* @private | |
*/ | |
function Char(codepoint, nativeString) { | |
var _this; | |
_classCallCheck(this, Char); | |
_this = _super.call(this); | |
_this.codepoint = codepoint; | |
_this._nativeString = nativeString; | |
return _this; | |
} | |
/** | |
* @param {*} v | |
* @return {!boolean} | |
*/ | |
_createClass(Char, [{ | |
key: "equals", | |
value: function equals(v) { | |
return check$2(v) && eq$1(this, v); | |
} | |
/** | |
* @return {true} | |
*/ | |
}, { | |
key: "isImmutable", | |
value: function isImmutable() { | |
return true; | |
} | |
/** | |
* @return {!number} this.codepoint. | |
* @override | |
*/ | |
}, { | |
key: "valueOf", | |
value: function valueOf() { | |
return this.codepoint; | |
} | |
/** | |
* @return {!String} | |
* @override | |
*/ | |
}, { | |
key: "toString", | |
value: function toString() { | |
if (this._nativeString === null) { | |
this._nativeString = String.fromCodePoint(this.codepoint); | |
} | |
return this._nativeString; | |
} | |
/** | |
* @return {!number} a non-negative integer. | |
* @override | |
*/ | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
return this.codepoint; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume(this.toString()); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
var c = this.codepoint; | |
switch (c) { | |
// Reference implementation: | |
// https://github.com/racket/racket/blob/cbfcc904ab621a338627e77d8f5a34f930ead0ab/racket/src/racket/src/print.c#L4089 | |
case 0: | |
out.consume('#\\nul'); | |
break; | |
case 8: | |
out.consume('#\\backspace'); | |
break; | |
case 9: | |
out.consume('#\\tab'); | |
break; | |
case 10: | |
out.consume('#\\newline'); | |
break; | |
case 11: | |
out.consume('#\\vtab'); | |
break; | |
case 12: | |
out.consume('#\\page'); | |
break; | |
case 13: | |
out.consume('#\\return'); | |
break; | |
case 32: | |
out.consume('#\\space'); | |
break; | |
case 127: | |
out.consume('#\\rubout'); | |
break; | |
default: | |
if (isGraphic(this)) { | |
out.consume("#\\".concat(this.toString())); | |
} else { | |
out.consume(c > 0xFFFF ? "#\\U".concat(c.toString(16).toUpperCase().padStart(8, '0')) : "#\\u".concat(c.toString(16).toUpperCase().padStart(4, '0'))); | |
} | |
} | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
this.writeNativeString(out); | |
} // displayUstring is defined in unicode_string.js to avoid a circular dependency. | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "printUString", | |
value: function printUString(out) { | |
this.writeUString(out); | |
} | |
}]); | |
return Char; | |
}(Primitive | |
/* implements Printable */ | |
); | |
var INTERN_CACHE_SIZE = 256; | |
/** | |
* @type {!Array<Char|undefined>} A cache for chars with small codepoints. | |
*/ | |
var internedCache = new Array(INTERN_CACHE_SIZE); | |
/** | |
* @param {!number} codepoint a non-negative integer. | |
* @return {!Char} | |
*/ | |
function charFromCodepoint(codepoint) { | |
if (codepoint < INTERN_CACHE_SIZE) { | |
if (internedCache[codepoint] === undefined) { | |
internedCache[codepoint] = new Char(codepoint, null); | |
} | |
return internedCache[codepoint]; | |
} | |
return new Char(codepoint, null); | |
} | |
/** | |
* @param {!string} s A native string exactly one Unicode codepoint long. | |
*/ | |
function charFromNativeString(s) { | |
var codepoint = s.codePointAt(0); | |
if (codepoint < INTERN_CACHE_SIZE) { | |
if (internedCache[codepoint] === undefined) { | |
internedCache[codepoint] = new Char(codepoint, s); | |
} | |
return internedCache[codepoint]; | |
} | |
return new Char(codepoint, s); | |
} | |
/** | |
* @param {*} char | |
* @return {!boolean} | |
*/ | |
function check$2(_char) { | |
// Because Char is final, we can compare the constructor directly | |
// instead of using the much slower `instanceof` operator. | |
return _typeof2(_char) === 'object' && _char !== null && _char.constructor === Char; | |
} // NOTE: | |
// "The Racket documentation only promises `eq?` for characters with | |
// scalar values in the range 0 to 255, but Chez Scheme characters | |
// are always `eq?` when they are `eqv?`." | |
// see: https://groups.google.com/g/racket-users/c/LFFV-xNq1SU/m/s6eoC35qAgAJ | |
// https://docs.racket-lang.org/reference/characters.html | |
/** | |
* @param {!Char} a | |
* @param {!Char} b | |
* @return {!boolean} | |
*/ | |
function eq$1(a, b) { | |
return a.codepoint === b.codepoint; | |
} | |
var IS_GRAPHIC = new RegExp('[\\p{L}\\p{N}\\p{M}\\p{S}\\p{P}\\p{Alphabetic}]', 'u'); | |
var IS_BLANK = new RegExp('[\\p{Zs}\\t]', 'u'); | |
/** | |
* @param {!Char} c | |
* @return {!boolean} | |
*/ | |
function isGraphic(c) { | |
return IS_GRAPHIC.test(c.toString()); | |
} | |
/** | |
* @param {!Char} c | |
* @return {!boolean} | |
*/ | |
function isBlank(c) { | |
return IS_BLANK.test(c.toString()); | |
} // Copyright (C) 2016-2021 Matt Bierner, RacketScript Authors | |
// | |
// Original source and copyright clause: https://github.com/mattbierner/hamt_plus | |
var _typeof = typeof Symbol === 'function' && _typeof2(Symbol.iterator) === 'symbol' ? function (obj) { | |
return _typeof2(obj); | |
} : function (obj) { | |
return obj && typeof Symbol === 'function' && obj.constructor === Symbol && obj !== Symbol.prototype ? 'symbol' : _typeof2(obj); | |
}; | |
/** | |
@fileOverview Hash Array Mapped Trie. | |
Code based on: https://github.com/exclipy/pdata | |
*/ | |
var hamt = {}; // export | |
/* Configuration | |
***************************************************************************** */ | |
var SIZE = 5; | |
var BUCKET_SIZE = Math.pow(2, SIZE); | |
var MASK = BUCKET_SIZE - 1; | |
var MAX_INDEX_NODE = BUCKET_SIZE / 2; | |
var MIN_ARRAY_NODE = BUCKET_SIZE / 4; | |
/* | |
***************************************************************************** */ | |
var nothing = {}; | |
var constant = function constant(x) { | |
return function () { | |
return x; | |
}; | |
}; | |
/** | |
Get 32 bit hash of string. | |
Based on: | |
http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery | |
*/ | |
hamt.hash = function (str) { | |
var type = typeof str === 'undefined' ? 'undefined' : _typeof(str); | |
if (type === 'number') return str; | |
if (type !== 'string') str += ''; | |
var hash = 0; | |
for (var i = 0, len = str.length; i < len; ++i) { | |
var c = str.charCodeAt(i); | |
hash = (hash << 5) - hash + c | 0; | |
} | |
return hash; | |
}; | |
/* Bit Ops | |
***************************************************************************** */ | |
/** | |
Hamming weight. | |
Taken from: http://jsperf.com/hamming-weight | |
*/ | |
var popcount = function popcount(x) { | |
x -= x >> 1 & 0x55555555; | |
x = (x & 0x33333333) + (x >> 2 & 0x33333333); | |
x = x + (x >> 4) & 0x0f0f0f0f; | |
x += x >> 8; | |
x += x >> 16; | |
return x & 0x7f; | |
}; | |
var hashFragment = function hashFragment(shift, h) { | |
return h >>> shift & MASK; | |
}; | |
var toBitmap = function toBitmap(x) { | |
return 1 << x; | |
}; | |
var fromBitmap = function fromBitmap(bitmap, bit) { | |
return popcount(bitmap & bit - 1); | |
}; | |
/* Array Ops | |
***************************************************************************** */ | |
/** | |
Set a value in an array. | |
@param mutate Should the input array be mutated? | |
@param at Index to change. | |
@param v New value | |
@param arr Array. | |
*/ | |
var arrayUpdate = function arrayUpdate(mutate, at, v, arr) { | |
var out = arr; | |
if (!mutate) { | |
var len = arr.length; | |
out = new Array(len); | |
for (var i = 0; i < len; ++i) { | |
out[i] = arr[i]; | |
} | |
} | |
out[at] = v; | |
return out; | |
}; | |
/** | |
Remove a value from an array. | |
@param mutate Should the input array be mutated? | |
@param at Index to remove. | |
@param arr Array. | |
*/ | |
var arraySpliceOut = function arraySpliceOut(mutate, at, arr) { | |
var newLen = arr.length - 1; | |
var i = 0; | |
var g = 0; | |
var out = arr; | |
if (mutate) { | |
i = at; | |
g = at; | |
} else { | |
out = new Array(newLen); | |
while (i < at) { | |
out[g++] = arr[i++]; | |
} | |
} | |
++i; | |
while (i <= newLen) { | |
out[g++] = arr[i++]; | |
} | |
if (mutate) { | |
out.length = newLen; | |
} | |
return out; | |
}; | |
/** | |
Insert a value into an array. | |
@param mutate Should the input array be mutated? | |
@param at Index to insert at. | |
@param v Value to insert, | |
@param arr Array. | |
*/ | |
var arraySpliceIn = function arraySpliceIn(mutate, at, v, arr) { | |
var len = arr.length; | |
if (mutate) { | |
var _i = len; | |
while (_i >= at) { | |
arr[_i--] = arr[_i]; | |
} | |
arr[at] = v; | |
return arr; | |
} | |
var i = 0; | |
var g = 0; | |
var out = new Array(len + 1); | |
while (i < at) { | |
out[g++] = arr[i++]; | |
} | |
out[at] = v; | |
while (i < len) { | |
out[++g] = arr[i++]; | |
} | |
return out; | |
}; | |
/* Node Structures | |
***************************************************************************** */ | |
var LEAF = 1; | |
var COLLISION = 2; | |
var INDEX = 3; | |
var ARRAY = 4; | |
/** | |
Empty node. | |
*/ | |
var empty = { | |
__hamt_isEmpty: true | |
}; | |
var isEmptyNode = function isEmptyNode(x) { | |
return x === empty || x && x.__hamt_isEmpty; | |
}; | |
/** | |
Leaf holding a value. | |
@member edit Edit of the node. | |
@member hash Hash of key. | |
@member key Key. | |
@member value Value stored. | |
*/ | |
var Leaf = function Leaf(edit, hash, key, value) { | |
return { | |
type: LEAF, | |
edit: edit, | |
hash: hash, | |
key: key, | |
value: value, | |
// eslint-disable-next-line no-use-before-define | |
_modify: LeafModify | |
}; | |
}; | |
/** | |
Leaf holding multiple values with the same hash but different keys. | |
@member edit Edit of the node. | |
@member hash Hash of key. | |
@member children Array of collision children node. | |
*/ | |
var Collision = function Collision(edit, hash, children) { | |
return { | |
type: COLLISION, | |
edit: edit, | |
hash: hash, | |
children: children, | |
// eslint-disable-next-line no-use-before-define | |
_modify: CollisionModify | |
}; | |
}; | |
/** | |
Internal node with a sparse set of children. | |
Uses a bitmap and array to pack children. | |
@member edit Edit of the node. | |
@member mask Bitmap that encode the positions of children in the array. | |
@member children Array of child nodes. | |
*/ | |
var IndexedNode = function IndexedNode(edit, mask, children) { | |
return { | |
type: INDEX, | |
edit: edit, | |
mask: mask, | |
children: children, | |
// eslint-disable-next-line no-use-before-define | |
_modify: IndexedNodeModify | |
}; | |
}; | |
/** | |
Internal node with many children. | |
@member edit Edit of the node. | |
@member size Number of children. | |
@member children Array of child nodes. | |
*/ | |
var ArrayNode = function ArrayNode(edit, size, children) { | |
return { | |
type: ARRAY, | |
edit: edit, | |
size: size, | |
children: children, | |
// eslint-disable-next-line no-use-before-define | |
_modify: ArrayNodeModify | |
}; | |
}; | |
/** | |
Is `node` a leaf node? | |
*/ | |
var isLeaf = function isLeaf(node) { | |
return node === empty || node.type === LEAF || node.type === COLLISION; | |
}; | |
/* Internal node operations. | |
***************************************************************************** */ | |
/** | |
Expand an indexed node into an array node. | |
@param edit Current edit. | |
@param frag Index of added child. | |
@param child Added child. | |
@param mask Index node mask before child added. | |
@param subNodes Index node children before child added. | |
*/ | |
var expand = function expand(edit, frag, child, bitmap, subNodes) { | |
var arr = []; | |
var bit = bitmap; | |
var count = 0; | |
for (var i = 0; bit; ++i) { | |
if (bit & 1) arr[i] = subNodes[count++]; | |
bit >>>= 1; | |
} | |
arr[frag] = child; | |
return ArrayNode(edit, count + 1, arr); | |
}; | |
/** | |
Collapse an array node into a indexed node. | |
@param edit Current edit. | |
@param count Number of elements in new array. | |
@param removed Index of removed element. | |
@param elements Array node children before remove. | |
*/ | |
var pack = function pack(edit, count, removed, elements) { | |
var children = new Array(count - 1); | |
var g = 0; | |
var bitmap = 0; | |
for (var i = 0, len = elements.length; i < len; ++i) { | |
if (i !== removed) { | |
var elem = elements[i]; | |
if (elem && !isEmptyNode(elem)) { | |
children[g++] = elem; | |
bitmap |= 1 << i; | |
} | |
} | |
} | |
return IndexedNode(edit, bitmap, children); | |
}; | |
/** | |
Merge two leaf nodes. | |
@param shift Current shift. | |
@param h1 Node 1 hash. | |
@param n1 Node 1. | |
@param h2 Node 2 hash. | |
@param n2 Node 2. | |
*/ | |
var mergeLeaves = function mergeLeaves(edit, shift, h1, n1, h2, n2) { | |
if (h1 === h2) return Collision(edit, h1, [n2, n1]); | |
var subH1 = hashFragment(shift, h1); | |
var subH2 = hashFragment(shift, h2); // eslint-disable-next-line no-nested-ternary,max-len | |
return IndexedNode(edit, toBitmap(subH1) | toBitmap(subH2), subH1 === subH2 ? [mergeLeaves(edit, shift + SIZE, h1, n1, h2, n2)] : subH1 < subH2 ? [n1, n2] : [n2, n1]); | |
}; | |
/** | |
Update an entry in a collision list. | |
@param mutate Should mutation be used? | |
@param edit Current edit. | |
@param keyEq Key compare function. | |
@param hash Hash of collision. | |
@param list Collision list. | |
@param f Update function. | |
@param k Key to update. | |
@param size Size ref. | |
*/ | |
var updateCollisionList = function updateCollisionList(mutate, edit, keyEq, h, list, f, k, size) { | |
var len = list.length; | |
for (var i = 0; i < len; ++i) { | |
var child = list[i]; | |
if (keyEq(k, child.key)) { | |
var value = child.value; | |
var _newValue = f(value); | |
if (_newValue === value) return list; | |
if (_newValue === nothing) { | |
--size.value; | |
return arraySpliceOut(mutate, i, list); | |
} | |
return arrayUpdate(mutate, i, Leaf(edit, h, k, _newValue), list); | |
} | |
} | |
var newValue = f(); | |
if (newValue === nothing) return list; | |
++size.value; | |
return arrayUpdate(mutate, len, Leaf(edit, h, k, newValue), list); | |
}; | |
var canEditNode = function canEditNode(edit, node) { | |
return edit === node.edit; | |
}; | |
/* Editing | |
***************************************************************************** */ | |
var LeafModify = function LeafModify(edit, keyEq, shift, f, h, k, size) { | |
if (keyEq(k, this.key)) { | |
var _v = f(this.value); | |
if (_v === this.value) return this;else if (_v === nothing) { | |
--size.value; | |
return empty; | |
} | |
if (canEditNode(edit, this)) { | |
this.value = _v; | |
return this; | |
} | |
return Leaf(edit, h, k, _v); | |
} | |
var v = f(); | |
if (v === nothing) return this; | |
++size.value; | |
return mergeLeaves(edit, shift, this.hash, this, h, Leaf(edit, h, k, v)); | |
}; | |
var CollisionModify = function CollisionModify(edit, keyEq, shift, f, h, k, size) { | |
if (h === this.hash) { | |
var canEdit = canEditNode(edit, this); | |
var list = updateCollisionList(canEdit, edit, keyEq, this.hash, this.children, f, k, size); | |
if (list === this.children) return this; | |
return list.length > 1 ? Collision(edit, this.hash, list) : list[0]; // collapse single element collision list | |
} | |
var v = f(); | |
if (v === nothing) return this; | |
++size.value; | |
return mergeLeaves(edit, shift, this.hash, this, h, Leaf(edit, h, k, v)); | |
}; | |
var IndexedNodeModify = function IndexedNodeModify(edit, keyEq, shift, f, h, k, size) { | |
var children = this.children, | |
mask = this.mask; | |
var frag = hashFragment(shift, h); | |
var bit = toBitmap(frag); | |
var indx = fromBitmap(mask, bit); | |
var exists = mask & bit; | |
var current = exists ? children[indx] : empty; | |
var child = current._modify(edit, keyEq, shift + SIZE, f, h, k, size); | |
if (current === child) return this; | |
var canEdit = canEditNode(edit, this); | |
var bitmap = mask; | |
var newChildren; | |
if (exists && isEmptyNode(child)) { | |
// remove | |
bitmap &= ~bit; | |
if (!bitmap) return empty; | |
if (children.length <= 2 && isLeaf(children[indx ^ 1])) return children[indx ^ 1]; // collapse | |
newChildren = arraySpliceOut(canEdit, indx, children); | |
} else if (!exists && !isEmptyNode(child)) { | |
// add | |
if (children.length >= MAX_INDEX_NODE) return expand(edit, frag, child, mask, children); | |
bitmap |= bit; | |
newChildren = arraySpliceIn(canEdit, indx, child, children); | |
} else { | |
// modify | |
newChildren = arrayUpdate(canEdit, indx, child, children); | |
} | |
if (canEdit) { | |
this.mask = bitmap; | |
this.children = newChildren; | |
return this; | |
} | |
return IndexedNode(edit, bitmap, newChildren); | |
}; | |
var ArrayNodeModify = function ArrayNodeModify(edit, keyEq, shift, f, h, k, size) { | |
var count = this.size; | |
var children = this.children; | |
var frag = hashFragment(shift, h); | |
var child = children[frag]; | |
var newChild = (child || empty)._modify(edit, keyEq, shift + SIZE, f, h, k, size); | |
if (child === newChild) return this; | |
var canEdit = canEditNode(edit, this); | |
var newChildren; | |
if (isEmptyNode(child) && !isEmptyNode(newChild)) { | |
// add | |
++count; | |
newChildren = arrayUpdate(canEdit, frag, newChild, children); | |
} else if (!isEmptyNode(child) && isEmptyNode(newChild)) { | |
// remove | |
--count; | |
if (count <= MIN_ARRAY_NODE) return pack(edit, count, frag, children); | |
newChildren = arrayUpdate(canEdit, frag, empty, children); | |
} else { | |
// modify | |
newChildren = arrayUpdate(canEdit, frag, newChild, children); | |
} | |
if (canEdit) { | |
this.size = count; | |
this.children = newChildren; | |
return this; | |
} | |
return ArrayNode(edit, count, newChildren); | |
}; | |
empty._modify = function (edit, keyEq, shift, f, h, k, size) { | |
var v = f(); | |
if (v === nothing) return empty; | |
++size.value; | |
return Leaf(edit, h, k, v); | |
}; | |
/* | |
***************************************************************************** */ | |
function Map$1(editable, edit, config, root, size) { | |
this._editable = editable; | |
this._edit = edit; | |
this._config = config; | |
this._root = root; | |
this._size = size; | |
} | |
Map$1.prototype.setTree = function (newRoot, newSize) { | |
if (this._editable) { | |
this._root = newRoot; | |
this._size = newSize; | |
return this; | |
} | |
return newRoot === this._root ? this : new Map$1(this._editable, this._edit, this._config, newRoot, newSize); | |
}; | |
/* Queries | |
***************************************************************************** */ | |
/** | |
Lookup the value for `key` in `map` using a custom `hash`. | |
Returns the value or `alt` if none. | |
*/ | |
hamt.tryGetHash = function (alt, hash, key, map) { | |
var node = map._root; | |
var shift = 0; | |
var keyEq = map._config.keyEq; | |
while (true) { | |
switch (node.type) { | |
case LEAF: | |
{ | |
return keyEq(key, node.key) ? node.value : alt; | |
} | |
case COLLISION: | |
{ | |
if (hash === node.hash) { | |
for (var i = 0, len = node.children.length; i < len; ++i) { | |
var child = node.children[i]; | |
if (keyEq(key, child.key)) return child.value; | |
} | |
} | |
return alt; | |
} | |
case INDEX: | |
{ | |
var frag = hashFragment(shift, hash); | |
var bit = toBitmap(frag); | |
if (node.mask & bit) { | |
node = node.children[fromBitmap(node.mask, bit)]; | |
shift += SIZE; | |
break; | |
} | |
return alt; | |
} | |
case ARRAY: | |
{ | |
node = node.children[hashFragment(shift, hash)]; | |
if (node) { | |
shift += SIZE; | |
break; | |
} | |
return alt; | |
} | |
default: | |
return alt; | |
} | |
} | |
}; | |
Map$1.prototype.tryGetHash = function (alt, hash, key) { | |
return hamt.tryGetHash(alt, hash, key, this); | |
}; | |
/** | |
Lookup the value for `key` in `map` using internal hash function. | |
@see `tryGetHash` | |
*/ | |
hamt.tryGet = function (alt, key, map) { | |
return hamt.tryGetHash(alt, map._config.hash(key), key, map); | |
}; | |
Map$1.prototype.tryGet = function (alt, key) { | |
return hamt.tryGet(alt, key, this); | |
}; | |
/** | |
Lookup the value for `key` in `map` using a custom `hash`. | |
Returns the value or `undefined` if none. | |
*/ | |
hamt.getHash = function (hash, key, map) { | |
return hamt.tryGetHash(undefined, hash, key, map); | |
}; | |
Map$1.prototype.getHash = function (hash, key) { | |
return hamt.getHash(hash, key, this); | |
}; | |
/** | |
Lookup the value for `key` in `map` using internal hash function. | |
@see `get` | |
*/ | |
hamt.get = function (key, map) { | |
return hamt.tryGetHash(undefined, map._config.hash(key), key, map); | |
}; | |
Map$1.prototype.get = function (key, alt) { | |
return hamt.tryGet(alt, key, this); | |
}; | |
/** | |
Does an entry exist for `key` in `map`? Uses custom `hash`. | |
*/ | |
hamt.hasHash = function (hash, key, map) { | |
return hamt.tryGetHash(nothing, hash, key, map) !== nothing; | |
}; | |
Map$1.prototype.hasHash = function (hash, key) { | |
return hamt.hasHash(hash, key, this); | |
}; | |
/** | |
Does an entry exist for `key` in `map`? Uses internal hash function. | |
*/ | |
var has = function has(key, map) { | |
return hamt.hasHash(map._config.hash(key), key, map); | |
}; | |
Map$1.prototype.has = function (key) { | |
return has(key, this); | |
}; | |
var defKeyCompare = function defKeyCompare(x, y) { | |
return x === y; | |
}; | |
/** | |
Create an empty map. | |
@param config Configuration. | |
*/ | |
hamt.make = function (config) { | |
return new Map$1(0, 0, { | |
keyEq: config && config.keyEq || defKeyCompare, | |
hash: config && config.hash || hamt.hash | |
}, empty, 0); | |
}; | |
/** | |
Empty map. | |
*/ | |
hamt.empty = hamt.make(); | |
/** | |
Does `map` contain any elements? | |
*/ | |
hamt.isEmpty = function (map) { | |
return map && !!isEmptyNode(map._root); | |
}; | |
Map$1.prototype.isEmpty = function () { | |
return hamt.isEmpty(this); | |
}; | |
/* Updates | |
***************************************************************************** */ | |
/** | |
Alter the value stored for `key` in `map` using function `f` using | |
custom hash. | |
`f` is invoked with the current value for `k` if it exists, | |
or no arguments if no such value exists. `modify` will always either | |
update or insert a value into the map. | |
Returns a map with the modified value. Does not alter `map`. | |
*/ | |
hamt.modifyHash = function (f, hash, key, map) { | |
var size = { | |
value: map._size | |
}; | |
var newRoot = map._root._modify(map._editable ? map._edit : NaN, map._config.keyEq, 0, f, hash, key, size); | |
return map.setTree(newRoot, size.value); | |
}; | |
Map$1.prototype.modifyHash = function (hash, key, f) { | |
return hamt.modifyHash(f, hash, key, this); | |
}; | |
/** | |
Alter the value stored for `key` in `map` using function `f` using | |
internal hash function. | |
@see `modifyHash` | |
*/ | |
hamt.modify = function (f, key, map) { | |
return hamt.modifyHash(f, map._config.hash(key), key, map); | |
}; | |
Map$1.prototype.modify = function (key, f) { | |
return hamt.modify(f, key, this); | |
}; | |
/** | |
Store `value` for `key` in `map` using custom `hash`. | |
Returns a map with the modified value. Does not alter `map`. | |
*/ | |
hamt.setHash = function (hash, key, value, map) { | |
return hamt.modifyHash(constant(value), hash, key, map); | |
}; | |
Map$1.prototype.setHash = function (hash, key, value) { | |
return hamt.setHash(hash, key, value, this); | |
}; | |
/** | |
Store `value` for `key` in `map` using internal hash function. | |
@see `setHash` | |
*/ | |
hamt.set = function (key, value, map) { | |
return hamt.setHash(map._config.hash(key), key, value, map); | |
}; | |
Map$1.prototype.set = function (key, value) { | |
return hamt.set(key, value, this); | |
}; | |
/** | |
Remove the entry for `key` in `map`. | |
Returns a map with the value removed. Does not alter `map`. | |
*/ | |
var del = constant(nothing); | |
hamt.removeHash = function (hash, key, map) { | |
return hamt.modifyHash(del, hash, key, map); | |
}; | |
Map$1.prototype.removeHash = function (hash, key) { | |
return hamt.removeHash(hash, key, this); | |
}; | |
Map$1.prototype.deleteHash = Map$1.prototype.removeHash; | |
/** | |
Remove the entry for `key` in `map` using internal hash function. | |
@see `removeHash` | |
*/ | |
hamt.remove = function (key, map) { | |
return hamt.removeHash(map._config.hash(key), key, map); | |
}; | |
Map$1.prototype.remove = function (key) { | |
return hamt.remove(key, this); | |
}; | |
Map$1.prototype["delete"] = Map$1.prototype.remove; | |
/* Mutation | |
***************************************************************************** */ | |
/** | |
Mark `map` as mutable. | |
*/ | |
hamt.beginMutation = function (map) { | |
return new Map$1(map._editable + 1, map._edit + 1, map._config, map._root, map._size); | |
}; | |
Map$1.prototype.beginMutation = function () { | |
return hamt.beginMutation(this); | |
}; | |
/** | |
Mark `map` as immutable. | |
*/ | |
hamt.endMutation = function (map) { | |
map._editable = map._editable && map._editable - 1; | |
return map; | |
}; | |
Map$1.prototype.endMutation = function () { | |
return hamt.endMutation(this); | |
}; | |
/** | |
Mutate `map` within the context of `f`. | |
@param f | |
@param map HAMT | |
*/ | |
hamt.mutate = function (f, map) { | |
var _transient = hamt.beginMutation(map); | |
f(_transient); | |
return hamt.endMutation(_transient); | |
}; | |
Map$1.prototype.mutate = function (f) { | |
return hamt.mutate(f, this); | |
}; | |
/* Traversal | |
***************************************************************************** */ | |
/** | |
Apply a continuation. | |
*/ | |
var appk = function appk(k) { | |
// eslint-disable-next-line no-use-before-define | |
return k && lazyVisitChildren(k[0], k[1], k[2], k[3], k[4]); | |
}; | |
/** | |
Recursively visit all values stored in an array of nodes lazily. | |
*/ | |
var lazyVisitChildren = function lazyVisitChildren(len, children, i, f, k) { | |
while (i < len) { | |
var child = children[i++]; // eslint-disable-next-line no-use-before-define | |
if (child && !isEmptyNode(child)) return lazyVisit(child, f, [len, children, i, f, k]); | |
} | |
return appk(k); | |
}; | |
/** | |
Recursively visit all values stored in `node` lazily. | |
*/ | |
var lazyVisit = function lazyVisit(node, f, k) { | |
switch (node.type) { | |
case LEAF: | |
return { | |
value: f(node), | |
rest: k | |
}; | |
case COLLISION: | |
case ARRAY: | |
case INDEX: | |
return lazyVisitChildren(node.children.length, node.children, 0, f, k); | |
default: | |
return appk(k); | |
} | |
}; | |
var DONE = { | |
done: true | |
}; | |
/** | |
Javascript iterator over a map. | |
*/ | |
function MapIterator(v) { | |
this.v = v; | |
} | |
MapIterator.prototype.next = function () { | |
if (!this.v) return DONE; | |
var v0 = this.v; | |
this.v = appk(v0.rest); | |
return v0; | |
}; | |
MapIterator.prototype[Symbol.iterator] = function () { | |
return this; | |
}; | |
/** | |
Lazily visit each value in map with function `f`. | |
*/ | |
var visit = function visit(map, f) { | |
return new MapIterator(lazyVisit(map._root, f)); | |
}; | |
/** | |
Get a Javascsript iterator of `map`. | |
Iterates over `[key, value]` arrays. | |
*/ | |
hamt.entries = function (map) { | |
return visit(map, function (x) { | |
return [x.key, x.value]; | |
}); | |
}; | |
Map$1.prototype.entries = function () { | |
return hamt.entries(this); | |
}; | |
Map$1.prototype[Symbol.iterator] = Map$1.prototype.entries; | |
/** | |
Get array of all keys in `map`. | |
Order is not guaranteed. | |
*/ | |
hamt.keys = function (map) { | |
return visit(map, function (x) { | |
return x.key; | |
}); | |
}; | |
Map$1.prototype.keys = function () { | |
return hamt.keys(this); | |
}; | |
/** | |
Get array of all values in `map`. | |
Order is not guaranteed, duplicates are preserved. | |
*/ | |
hamt.values = function (map) { | |
return visit(map, function (x) { | |
return x.value; | |
}); | |
}; | |
Map$1.prototype.values = function () { | |
return hamt.values(this); | |
}; | |
/* Fold | |
***************************************************************************** */ | |
/** | |
Visit every entry in the map, aggregating data. | |
Order of nodes is not guaranteed. | |
@param f Function mapping accumulated value, value, and key to new value. | |
@param z Starting value. | |
@param m HAMT | |
*/ | |
hamt.fold = function (f, z, m) { | |
var root = m._root; | |
if (root.type === LEAF) return f(z, root.value, root.key); | |
var toVisit = [root.children]; | |
var children = toVisit.pop(); | |
while (children) { | |
for (var i = 0, len = children.length; i < len;) { | |
var child = children[i++]; | |
if (child && child.type) { | |
if (child.type === LEAF) z = f(z, child.value, child.key);else toVisit.push(child.children); | |
} | |
} | |
children = toVisit.pop(); | |
} | |
return z; | |
}; | |
Map$1.prototype.fold = function (f, z) { | |
return hamt.fold(f, z, this); | |
}; | |
/** | |
Visit every entry in the map, aggregating data. | |
Order of nodes is not guaranteed. | |
@param f Function invoked with value and key | |
@param map HAMT | |
*/ | |
hamt.forEach = function (f, map) { | |
return hamt.fold(function (_, value, key) { | |
return f(value, key, map); | |
}, null, map); | |
}; | |
Map$1.prototype.forEach = function (f) { | |
return hamt.forEach(f, this); | |
}; | |
/* Aggregate | |
***************************************************************************** */ | |
/** | |
Get the number of entries in `map`. | |
*/ | |
hamt.count = function (map) { | |
return map._size; | |
}; | |
Map$1.prototype.count = function () { | |
return hamt.count(this); | |
}; | |
Object.defineProperty(Map$1.prototype, 'size', { | |
get: Map$1.prototype.count | |
}); // # sourceMappingURL=hamt.js.map | |
/* --------------------------------------------------------------------------*/ | |
/* Other Helpers */ | |
function argumentsToArray(args) { | |
return Array.prototype.slice.call(args, 0); | |
} | |
function attachReadOnlyProperty(o, k, v) { | |
return Object.defineProperty(o, k, { | |
value: v, | |
writable: false, | |
configurable: false | |
}); | |
} | |
/** | |
* @param {function(String|UString.UString)} f | |
* @return {function(String)} | |
*/ | |
function internedMake(f) { | |
var cache = new Map(); | |
return function (v) { | |
v = v.toString(); | |
var result = cache.get(v); | |
if (result === undefined) { | |
result = f(v); | |
cache.set(v, result); | |
} | |
return result; | |
}; | |
} | |
/** | |
* A sequence of {Char.Char}s. | |
* | |
* See {Char.Char} for implementation notes. | |
* | |
* @abstract | |
*/ | |
var UString = /*#__PURE__*/function (_Primitive2) { | |
_inherits(UString, _Primitive2); | |
var _super2 = _createSuper(UString); | |
/** | |
* @param {!Char.Char[]} chars | |
* @param {(string|null)} nativeString | |
* @private | |
*/ | |
function UString(chars, nativeString) { | |
var _this2; | |
_classCallCheck(this, UString); | |
_this2 = _super2.call(this); | |
_this2.chars = chars; | |
_this2._nativeString = nativeString; | |
_this2._cachedHashCode = null; | |
return _this2; | |
} | |
/** | |
* @return {!number} | |
*/ | |
_createClass(UString, [{ | |
key: "length", | |
get: function get() { | |
return this.chars.length; | |
} | |
/** | |
* @return {!string} | |
*/ | |
}, { | |
key: "toString", | |
value: function toString() { | |
if (this._nativeString === null) { | |
this._nativeString = this.chars.join(''); | |
} | |
return this._nativeString; | |
} | |
/** | |
* @param {!number} i | |
* @return {!Char.Char} | |
*/ | |
}, { | |
key: "charAt", | |
value: function charAt(i) { | |
return this.chars[i]; | |
} | |
/** | |
* @return {!MutableUString} | |
*/ | |
}, { | |
key: "toLowerCase", | |
value: function toLowerCase() { | |
// TODO: Improve Racket compatibility. | |
return makeMutable(this.toString().toLowerCase()); | |
} | |
/** | |
* @return {!MutableUString} | |
*/ | |
}, { | |
key: "toUpperCase", | |
value: function toUpperCase() { | |
// TODO: Improve Racket compatibility. | |
return makeMutable(this.toString().toUpperCase()); | |
} | |
/** | |
* @param {!number} start | |
* @param {!number} end | |
* @return {!MutableUString} | |
*/ | |
}, { | |
key: "substring", | |
value: function substring(start, end) { | |
// RacketScript guarantees: | |
// start >= 0, end >= 0, end <= length, end >= start. | |
// This might be faster if we keep the indices with the chars. | |
return new MutableUString(this.chars.slice(start, end), null); | |
} | |
/** | |
* @param {!(string|RegExp)} sep | |
*/ | |
}, { | |
key: "split", | |
value: function split(sep) { | |
// Known issue: if sep is a regex, it will not support Unicode fully. | |
return this.toString().split(sep).map(function (s) { | |
return makeMutable(s); | |
}); | |
} | |
/** | |
* @param {!number} i | |
*/ | |
}, { | |
key: "isValidIndex", | |
value: function isValidIndex(i) { | |
return i < this.length; | |
} | |
/** | |
* @param {!number} i | |
*/ | |
}, { | |
key: "isEmpty", | |
value: function isEmpty() { | |
return this.length === 0; | |
} | |
/** | |
* Warning: JavaScript string comparison does not work correctly | |
* for Unicode strings with non-BMP characters. | |
* | |
* @return {!string} | |
*/ | |
}, { | |
key: "valueOf", | |
value: function valueOf() { | |
return this.toString(); | |
} | |
/** | |
* @param {*} v | |
* @return {!boolean} | |
*/ | |
}, { | |
key: "equals", | |
value: function equals(v) { | |
return check$3(v) && this.toString() === v.toString(); | |
} | |
/** | |
* @return {!number} a 32-bit integer | |
*/ | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
if (this._cachedHashCode === null) { | |
this._cachedHashCode = hashIntArray(this.chars); | |
} | |
return this._cachedHashCode; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume(this.toString()); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "displayUString", | |
value: function displayUString(out) { | |
out.consume(this); | |
} | |
/** | |
* Writes a string representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
// The JSON representation happens to be readable by Racket's `read`. | |
out.consume('"'); | |
var _iterator = _createForOfIteratorHelper(this.chars), | |
_step; | |
try { | |
for (_iterator.s(); !(_step = _iterator.n()).done;) { | |
var _char2 = _step.value; | |
var c = _char2.codepoint; | |
switch (c) { | |
// Reference implementation: | |
// https://github.com/racket/racket/blob/cbfcc904ab621a338627e77d8f5a34f930ead0ab/racket/src/racket/src/print.c#L3690 | |
case 7: | |
out.consume('\\a'); | |
break; | |
case 8: | |
out.consume('\\b'); | |
break; | |
case 9: | |
out.consume('\\t'); | |
break; | |
case 11: | |
out.consume('\\v'); | |
break; | |
case 12: | |
out.consume('\\f'); | |
break; | |
case 10: | |
out.consume('\\n'); | |
break; | |
case 13: | |
out.consume('\\r'); | |
break; | |
case 27: | |
out.consume('\\e'); | |
break; | |
case 34: | |
out.consume('\\"'); | |
break; | |
case 92: | |
out.consume('\\\\'); | |
break; | |
default: | |
if (isGraphic(_char2) || isBlank(_char2)) { | |
out.consume(_char2.toString()); | |
} else { | |
out.consume(c > 0xFFFF ? "\\U".concat(c.toString(16).toUpperCase().padStart(8, '0')) : "\\u".concat(c.toString(16).toUpperCase().padStart(4, '0'))); | |
} | |
} | |
} | |
} catch (err) { | |
_iterator.e(err); | |
} finally { | |
_iterator.f(); | |
} | |
out.consume('"'); | |
} | |
/** | |
* Writes a UString representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "writeUString", | |
value: function writeUString(out) { | |
var stringOut = new MiniNativeOutputStringPort(); | |
this.writeNativeString(stringOut); | |
out.consume(makeMutable(stringOut.getOutputString())); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
this.writeNativeString(out); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "printUString", | |
value: function printUString(out) { | |
this.writeUString(out); | |
} | |
/** | |
* Whether the string can be parsed as an integer as defined by | |
* Racket's string->number. | |
* | |
* @param {!number} radix an integer in [2, 16] range. | |
* @return {!boolean} | |
*/ | |
}, { | |
key: "isValidInteger", | |
value: function isValidInteger(radix) { | |
var startFrom = this.chars[0].codepoint === | |
/* '-' */ | |
45 ? 1 : 0; | |
if (radix > 10) { | |
var maxLowercase = | |
/* 'a' - 11 */ | |
86 + radix; | |
var maxUppercase = maxLowercase - 32; | |
for (var i = startFrom; i < this.chars.length; ++i) { | |
var cp = this.chars[i].codepoint; | |
if (cp < | |
/* '0' */ | |
48 || cp > maxLowercase || cp > maxUppercase && cp < | |
/* 'a' */ | |
97 || cp > | |
/* '9' */ | |
57 && cp < | |
/* 'A' */ | |
65) return false; | |
} | |
} else { | |
var max = | |
/* '0' - 1 */ | |
47 + radix; | |
for (var _i2 = startFrom; _i2 < this.chars.length; ++_i2) { | |
var _cp = this.chars[_i2].codepoint; | |
if (_cp < | |
/* '0' */ | |
48 || _cp > max) return false; | |
} | |
} | |
return true; | |
} | |
}]); | |
return UString; | |
}(Primitive | |
/* implements Printable */ | |
); | |
/** | |
* An immutable sequence of {Char.Char}s. | |
*/ | |
var ImmutableUString = /*#__PURE__*/function (_UString) { | |
_inherits(ImmutableUString, _UString); | |
var _super3 = _createSuper(ImmutableUString); | |
function ImmutableUString() { | |
_classCallCheck(this, ImmutableUString); | |
return _super3.apply(this, arguments); | |
} | |
_createClass(ImmutableUString, [{ | |
key: "isImmutable", | |
value: | |
/** | |
* @return {true} | |
*/ | |
function isImmutable() { | |
return true; | |
} | |
}]); | |
return ImmutableUString; | |
}(UString); | |
/** | |
* An immutable sequence of {Char.Char}s. | |
* All {Char.Char}s must be in the BMP Unicode plane. | |
* | |
* See {Char.Char} for implementation notes. | |
*/ | |
var ImmutableBMPString = /*#__PURE__*/function (_ImmutableUString) { | |
_inherits(ImmutableBMPString, _ImmutableUString); | |
var _super4 = _createSuper(ImmutableBMPString); | |
function ImmutableBMPString() { | |
_classCallCheck(this, ImmutableBMPString); | |
return _super4.apply(this, arguments); | |
} | |
_createClass(ImmutableBMPString, [{ | |
key: "substring", | |
value: | |
/** | |
* @param {!number} start | |
* @param {!number} end | |
* @return {!MutableUString} | |
* @override | |
*/ | |
function substring(start, end) { | |
return new MutableUString(this.chars.slice(start, end), this.toString().substring(start, end)); | |
} | |
/** | |
* Writes a string representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
// The JSON representation for BMP strings happens to be readable by Racket's `read`. | |
out.consume(JSON.stringify(this.toString())); | |
} | |
}]); | |
return ImmutableBMPString; | |
}(ImmutableUString); | |
/** | |
* An mutable sequence of {Char.Char}s. | |
*/ | |
var MutableUString = /*#__PURE__*/function (_UString2) { | |
_inherits(MutableUString, _UString2); | |
var _super5 = _createSuper(MutableUString); | |
function MutableUString() { | |
_classCallCheck(this, MutableUString); | |
return _super5.apply(this, arguments); | |
} | |
_createClass(MutableUString, [{ | |
key: "isImmutable", | |
value: | |
/** | |
* @return {false} | |
*/ | |
function isImmutable() { | |
return false; | |
} | |
}, { | |
key: "setCharAt", | |
value: function setCharAt(i, _char3) { | |
if (!eq$1(_char3, this.chars[i])) { | |
this.chars[i] = _char3; | |
this._nativeString = null; | |
this._cachedHashCode = null; | |
} | |
} | |
}]); | |
return MutableUString; | |
}(UString); | |
/** | |
* @param {!string} nativeString | |
* @return {!Char.Char[]} | |
*/ | |
function nativeStringToChars(nativeString) { | |
// Array.from splits on codepoints (as per the String iterator spec). | |
return Array.from(nativeString, charFromNativeString); | |
} | |
/** | |
* @param {!string} nativeString | |
* @return {!ImmutableUString} | |
*/ | |
var makeInternedImmutable = internedMake(function (nativeString) { | |
return makeImmutable(nativeString); | |
}); | |
/** | |
* @param {!string} nativeString | |
* @return {!ImmutableUString} | |
*/ | |
var make$1 = makeInternedImmutable; | |
/** | |
* @param {!string} nativeString | |
* @return {!ImmutableUString} | |
*/ | |
function makeImmutable(nativeString) { | |
return makeImmutableFromCharsAndNativeString(nativeStringToChars(nativeString), nativeString); | |
} | |
/** | |
* @param {!Char.Char[]} chars | |
* @param {!string} nativeString | |
* @return {!ImmutableUString} | |
*/ | |
function makeImmutableFromCharsAndNativeString(chars, nativeString) { | |
return chars.length === nativeString.length ? new ImmutableBMPString(chars, nativeString) : new ImmutableUString(chars, nativeString); | |
} | |
/** | |
* @param {!string} nativeString | |
* @return {!MutableUString} | |
*/ | |
function makeMutable(nativeString) { | |
return new MutableUString(nativeStringToChars(nativeString), nativeString); | |
} | |
/** | |
* @param {!Char.Char[]} chars | |
* @return {!MutableUString} | |
*/ | |
function makeMutableFromChars(chars) { | |
return new MutableUString(chars, null); | |
} | |
/** | |
* @param {!UString} str | |
* @return {!MutableUString} | |
*/ | |
function copyAsMutable(str) { | |
return new MutableUString(str.chars, str._nativeString); | |
} | |
function makeMutableFromCharsVarArgs() { | |
for (var _len = arguments.length, chars = new Array(_len), _key = 0; _key < _len; _key++) { | |
chars[_key] = arguments[_key]; | |
} | |
return makeMutableFromChars(chars); | |
} | |
/** | |
* @param {!UString} v | |
* @return {!ImmutableUString} | |
*/ | |
function stringToImmutableString(v) { | |
return v instanceof ImmutableUString ? v : makeImmutableFromCharsAndNativeString(v.chars, v.toString()); | |
} | |
/** | |
* @param {*} v | |
* @return {!boolean} | |
*/ | |
function check$3(v) { | |
return v instanceof UString; | |
} | |
/** | |
* @param {!number} k | |
* @param {!Char.Char} c | |
*/ | |
function repeatChar(k, c) { | |
var chars = new Array(k); | |
chars.fill(c.toString()); | |
return new MutableUString(chars, null); | |
} | |
new TextEncoder(); | |
/** | |
* @param {!UString[]} strs | |
* @return {!MutableUString} | |
*/ | |
function stringAppend() { | |
var _ref; | |
for (var _len2 = arguments.length, strs = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | |
strs[_key2] = arguments[_key2]; | |
} | |
return makeMutableFromChars((_ref = []).concat.apply(_ref, _toConsumableArray(strs.map(function (s) { | |
return s.chars; | |
})))); | |
} // The Char Printable *UString methods are defined here, | |
// because Char cannot depend on UString. | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
Char.prototype.displayUString = function (out) { | |
out.consume(new MutableUString([this], this._nativeString)); | |
}; | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
Char.prototype.writeUString = function (out) { | |
var stringOut = new MiniNativeOutputStringPort(); | |
this.writeNativeString(stringOut); | |
out.consume(makeMutable(stringOut.getOutputString())); | |
}; // PrintablePrimitive subclass avoids circular dependency with UString and Ports. | |
var PRINT_PREFIX_USTRING = makeInternedImmutable("'"); | |
var PrintablePrimitive = /*#__PURE__*/function (_Primitive3) { | |
_inherits(PrintablePrimitive, _Primitive3); | |
var _super6 = _createSuper(PrintablePrimitive); | |
function PrintablePrimitive() { | |
_classCallCheck(this, PrintablePrimitive); | |
return _super6.apply(this, arguments); | |
} | |
_createClass(PrintablePrimitive, [{ | |
key: "displayUString", | |
value: | |
/** | |
* Writes a String representation similar to Racket's `display` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
/* abstract displayNativeString(out); */ | |
/** | |
* Writes a UString representation similar to Racket's `display` to the given port. | |
* | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
function displayUString(out) { | |
var stringOut = new MiniNativeOutputStringPort(); | |
this.displayNativeString(stringOut); | |
out.consume(makeMutable(stringOut.getOutputString())); | |
} | |
/** | |
* Writes a string representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
this.displayNativeString(out); | |
} | |
/** | |
* Writes a UString representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "writeUString", | |
value: function writeUString(out) { | |
var stringOut = new MiniNativeOutputStringPort(); | |
this.writeNativeString(stringOut); | |
out.consume(makeMutable(stringOut.getOutputString())); | |
} | |
/** | |
* Writes a string representation similar to Racket's `print` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
out.consume("'"); | |
this.writeNativeString(out); | |
} | |
/** | |
* Writes a UString representation similar to Racket's `print` to the given port. | |
* | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "printUString", | |
value: function printUString(out) { | |
out.consume(PRINT_PREFIX_USTRING); | |
this.writeUString(out); | |
} | |
/** | |
* @return {!String} a string representation similar to Racket's `display`. | |
*/ | |
}, { | |
key: "toString", | |
value: function toString() { | |
var out = new MiniNativeOutputStringPort(); | |
this.displayNativeString(out); | |
return out.getOutputString(); | |
} | |
}]); | |
return PrintablePrimitive; | |
}(Primitive | |
/* implements Printable */ | |
); | |
var counter = 0; | |
var PrimitiveSymbol = /*#__PURE__*/function (_PrintablePrimitive, _Symbol$toPrimitive) { | |
_inherits(PrimitiveSymbol, _PrintablePrimitive); | |
var _super7 = _createSuper(PrimitiveSymbol); | |
function PrimitiveSymbol(name) { | |
var _this3; | |
_classCallCheck(this, PrimitiveSymbol); | |
_this3 = _super7.call(this); | |
if (name) { | |
// interned | |
_this3.name = name; | |
_this3.sym = Symbol["for"](name); | |
} else { | |
// uninterned | |
_this3.sym = Symbol("_".concat(counter++)); | |
} | |
return _this3; | |
} | |
_createClass(PrimitiveSymbol, [{ | |
key: "isInterned", | |
get: function get() { | |
return Boolean(this.name); | |
} | |
}, { | |
key: "value", | |
get: function get() { | |
return this.sym; | |
} | |
}, { | |
key: "equals", | |
value: function equals(s) { | |
if (s.sym) { | |
return s.value === this.value; | |
} | |
return s === this.value; | |
} | |
}, { | |
key: "lt", | |
value: function lt(s) { | |
if (s === this) { | |
return false; | |
} | |
return this.toString() < s.toString(); | |
} | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
return hashString(this.toString()); | |
} | |
/* String printing */ | |
}, { | |
key: _Symbol$toPrimitive, | |
value: function value(hint) { | |
if (hint === 'number') { | |
return 0; | |
} | |
return this.toString(); | |
} | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
if (this.isInterned) { | |
out.consume(Symbol.keyFor(this.sym)); | |
} else { | |
out.consume(this.sym.toString()); | |
} | |
} | |
}]); | |
return PrimitiveSymbol; | |
}(PrintablePrimitive, Symbol.toPrimitive); | |
function make$2(v) { | |
return new PrimitiveSymbol(v ? v.toString() : ''); | |
} | |
function check$4(v) { | |
return v instanceof PrimitiveSymbol; | |
} | |
/** | |
* @param {*} v1 | |
* @param {*} v2 | |
* @return {!boolean} | |
*/ | |
function isEq(v1, v2) { | |
// Handle Symbols | |
if (check$4(v1)) { | |
return v1.equals(v2); | |
} | |
return v1 === v2; | |
} | |
/** | |
* @param {*} v1 | |
* @param {*} v2 | |
* @return {!boolean} | |
*/ | |
function isEqual(v1, v2) { | |
if (v1 === v2) return true; | |
if (check(v1)) return v1.equals(v2); // Bytes are not a Primitive. | |
if (check$1(v1) && check$1(v2)) return eq(v1, v2); | |
return false; | |
} | |
/** | |
* @param {*} o | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashForEq(o) { | |
return hash(o); | |
} | |
/** | |
* @param {*} o | |
* @return {!number} a 32-bit integer | |
*/ | |
function hashForEqual$1(o) { | |
if (check(o)) return o.hashForEqual(); | |
if (check$1(o)) return hashForEqual(o); | |
return hash(o); | |
} | |
/** | |
* @param {!Function} fn | |
* @param {*} arity | |
*/ | |
// arity is either: | |
// - array of exact arities, | |
// - a number, meaning "arity of at least" | |
function attachProcedureArity(fn, arity) { | |
fn.__rjs_arityValue = arity || fn.length; | |
return fn; | |
} | |
/** | |
* @param {!Function} fn | |
* @param {!String} name | |
*/ | |
// need this to get some fns to print correct Racket name, | |
// eg "cons" instead of "makePair" | |
function attachProcedureName(fn, name) { | |
fn.__rjs_name = name; | |
return fn; | |
} | |
/** | |
* @param {*} v | |
* @return {!boolean} | |
*/ | |
function check$5(v) { | |
return typeof v === 'function'; | |
} | |
/** | |
* @param {!Function} f | |
* @return {!String} A string representation similar to Racket's `display`. | |
*/ | |
function toString$1(f) { | |
var notRjsName = f.name ? "#<procedure:".concat(f.name, ">") : '#<procedure>'; | |
return f.__rjs_name ? "#<procedure:".concat(f.__rjs_name, ">") : notRjsName; | |
} | |
/** | |
* Writes a string representation similar to Racket's `display` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!Function} f | |
*/ | |
function displayNativeString$1(out, f) { | |
out.consume(toString$1(f)); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function displayNativeString$2(out, v) { | |
if (v === true) { | |
out.consume('#t'); | |
} else if (v === false) { | |
out.consume('#f'); | |
} else if (v === undefined || v === null) { | |
out.consume('#<void>'); | |
} else if (check(v)) { | |
v.displayNativeString(out); | |
} else if (check$1(v)) { | |
displayNativeString(out, v); | |
} else if (check$5(v)) { | |
if (v.__rjs_struct_object) { | |
v.__rjs_struct_object.displayNativeString(out); | |
} else { | |
displayNativeString$1(out, v); | |
} | |
} else | |
/* if (typeof v === 'number' || typeof v === 'string') */ | |
{ | |
out.consume(v.toString()); | |
} | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function _writeNativeString(out, v) { | |
if (check(v)) { | |
// Assume `v` implements Printable, as only Values does not, | |
// and it cannot be passed here. | |
v.writeNativeString(out); | |
} else { | |
displayNativeString$2(out, v); | |
} | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
* @param {!boolean} printAsExpression | |
* @param {!(0|1)} quoteDepth | |
*/ | |
function printNativeString$1(out, v, printAsExpression, quoteDepth) { | |
if (printAsExpression && quoteDepth !== 1 && check(v)) { | |
v.printNativeString(out); | |
} else if (check$1(v)) { | |
printNativeString(out, v); | |
} else { | |
_writeNativeString(out, v); | |
} | |
} | |
var TRUE_USTRING = makeInternedImmutable('#t'); | |
var FALSE_USTRING = makeInternedImmutable('#f'); | |
var VOID_USTRING = makeInternedImmutable('#<void>'); | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function _displayUString(out, v) { | |
if (v === true) { | |
out.consume(TRUE_USTRING); | |
} else if (v === false) { | |
out.consume(FALSE_USTRING); | |
} else if (v === undefined || v === null) { | |
out.consume(VOID_USTRING); | |
} else if (typeof v === 'number' || typeof v === 'string') { | |
out.consume(makeMutable(v.toString())); | |
} else if (check(v)) { | |
v.displayUString(out); | |
} else if (check$1(v)) { | |
out.consume(makeMutable(toString(v))); | |
} else if (check$5(v)) { | |
if (v.__rjs_struct_object) { | |
v.__rjs_struct_object.displayUString(out); | |
} else { | |
out.consume(makeMutable(toString$1(v))); | |
} | |
} else | |
/* if (typeof v === 'number' || typeof v === 'string') */ | |
{ | |
out.consume(makeMutable(v.toString())); | |
} | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function _writeUString(out, v) { | |
if (check(v)) { | |
// Assume `v` implements Printable, as only Values does not, | |
// and it cannot be passed here. | |
v.writeUString(out); | |
} else { | |
_displayUString(out, v); | |
} | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
* @param {!boolean} printAsExpression | |
* @param {!(0|1)} quoteDepth | |
*/ | |
function printUString(out, v, printAsExpression, quoteDepth) { | |
if (printAsExpression && quoteDepth !== 1 && check(v)) { | |
v.printUString(out); | |
} else { | |
_writeUString(out, v); | |
} | |
} | |
var BOX_PREFIX_USTRING = makeInternedImmutable('#&'); | |
var Box = /*#__PURE__*/function (_PrintablePrimitive2) { | |
_inherits(Box, _PrintablePrimitive2); | |
var _super8 = _createSuper(Box); | |
function Box(v) { | |
var _this4; | |
_classCallCheck(this, Box); | |
_this4 = _super8.call(this); | |
_this4.value = v; | |
return _this4; | |
} | |
_createClass(Box, [{ | |
key: "set", | |
value: function set(v) { | |
this.value = v; | |
} | |
}, { | |
key: "get", | |
value: function get() { | |
return this.value; | |
} | |
/** | |
* @param {*} v | |
* @return {!boolean} | |
*/ | |
}, { | |
key: "equals", | |
value: function equals(v) { | |
return isEqual(v.value, this.value); | |
} | |
/** | |
* @return {!number} a 32-bit integer | |
*/ | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
return hashForEqual$1(this.value); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume('#&'); | |
displayNativeString$2(out, this.value); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "displayUString", | |
value: function displayUString(out) { | |
out.consume(BOX_PREFIX_USTRING); | |
_displayUString(out, this.value); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
out.consume('#&'); | |
_writeNativeString(out, this.value); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "writeUString", | |
value: function writeUString(out) { | |
out.consume(BOX_PREFIX_USTRING); | |
_writeUString(out, this.value); | |
} | |
}]); | |
return Box; | |
}(PrintablePrimitive); | |
function make$3(v) { | |
return new Box(v); | |
} | |
/** @singleton */ | |
var Empty = /*#__PURE__*/function (_PrintablePrimitive3) { | |
_inherits(Empty, _PrintablePrimitive3); | |
var _super9 = _createSuper(Empty); | |
function Empty() { | |
_classCallCheck(this, Empty); | |
return _super9.apply(this, arguments); | |
} | |
_createClass(Empty, [{ | |
key: "equals", | |
value: function equals(v) { | |
return this === v; | |
} | |
}, { | |
key: "length", | |
get: function get() { | |
return 0; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume('()'); | |
} | |
/** | |
* @return {false} | |
*/ | |
}, { | |
key: "isImmutable", | |
value: function isImmutable() { | |
// As per racket reference, this is always false for Pairs and Lists. | |
// https://docs.racket-lang.org/reference/booleans.html#%28def._%28%28quote._~23~25kernel%29._immutable~3f%29%29 | |
return false; | |
} | |
}]); | |
return Empty; | |
}(PrintablePrimitive); | |
var EMPTY = new Empty(); | |
/** | |
* @param {*} v | |
* @return {boolean} true iff v is the empty list. | |
*/ | |
function isEmpty(v) { | |
return v === EMPTY; | |
} | |
var Pair = /*#__PURE__*/function (_PrintablePrimitive4) { | |
_inherits(Pair, _PrintablePrimitive4); | |
var _super10 = _createSuper(Pair); | |
/** @private */ | |
function Pair(hd, tl) { | |
var _this5; | |
_classCallCheck(this, Pair); | |
_this5 = _super10.call(this); | |
_this5.hd = hd; | |
_this5.tl = tl; | |
_this5._listLength = isList(tl) ? tl.length + 1 : -1; | |
_this5._cachedHashCode = null; | |
return _this5; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {function(Ports.NativeStringOutputPort, *)} itemFn | |
*/ | |
_createClass(Pair, [{ | |
key: "writeToPort", | |
value: function writeToPort(out, itemFn) { | |
out.consume('('); | |
var rest = this; | |
while (true) { | |
if (check$6(rest)) { | |
itemFn(out, rest.hd); | |
} else { | |
out.consume('. '); | |
itemFn(out, rest); | |
break; | |
} | |
rest = rest.tl; | |
if (isEmpty(rest)) { | |
break; | |
} else { | |
out.consume(' '); | |
} | |
} | |
out.consume(')'); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
this.writeToPort(out, displayNativeString$2); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
this.writeToPort(out, _writeNativeString); | |
} | |
}, { | |
key: "equals", | |
value: function equals(v) { | |
if (!check$6(v) || this.length !== v.length) { | |
return false; | |
} | |
var hd1 = this.hd; | |
var tl1 = this.tl; | |
var hd2 = v.hd; | |
var tl2 = v.tl; | |
while (true) { | |
if (!isEqual(hd1, hd2)) { | |
return false; | |
} | |
if (!check$6(tl1) || isEmpty(tl1)) { | |
return isEqual(tl1, tl2); | |
} | |
hd1 = tl1.hd; | |
tl1 = tl1.tl; | |
hd2 = tl2.hd; | |
tl2 = tl2.tl; | |
} | |
} | |
/** | |
* @return {!number} | |
*/ | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
if (this._cachedHashCode === null) { | |
this._cachedHashCode = _get(_getPrototypeOf(Pair.prototype), "hashForEqual", this).call(this); | |
} | |
return this._cachedHashCode; | |
} | |
}, { | |
key: "car", | |
value: function car() { | |
return this.hd; | |
} | |
}, { | |
key: "cdr", | |
value: function cdr() { | |
return this.tl; | |
} | |
}, { | |
key: "length", | |
get: function get() { | |
return this._listLength; | |
} | |
/** | |
* @return {false} | |
*/ | |
}, { | |
key: "isImmutable", | |
value: function isImmutable() { | |
// As per racket reference, this is always false for Pairs and Lists. | |
// https://docs.racket-lang.org/reference/booleans.html#%28def._%28%28quote._~23~25kernel%29._immutable~3f%29%29 | |
return false; | |
} | |
}]); | |
return Pair; | |
}(PrintablePrimitive); | |
/** | |
* @param {*} v | |
* @return {boolean} true iff v is a non-empty list or pair. | |
*/ | |
function check$6(v) { | |
return _typeof2(v) === 'object' && v !== null && v.constructor === Pair; | |
} | |
function make$4(hd, tl) { | |
return new Pair(hd, tl); | |
} | |
function makeList() { | |
for (var _len3 = arguments.length, items = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | |
items[_key3] = arguments[_key3]; | |
} | |
return items.reduceRight(function (result, item) { | |
return make$4(item, result); | |
}, EMPTY); | |
} | |
function listToArray(lst) { | |
var r = []; | |
listForEach(lst, function (x) { | |
return r.push(x); | |
}); | |
return r; | |
} | |
function listFromArray(lst) { | |
return makeList.apply(void 0, _toConsumableArray(lst)); | |
} | |
function listForEach(lst, fn) { | |
while (!isEmpty(lst)) { | |
fn(lst.hd); | |
lst = lst.tl; | |
} | |
} | |
function listFind(lst, fn) { | |
while (!isEmpty(lst)) { | |
var result = fn(lst.hd); | |
if (result !== false) { | |
return result; | |
} | |
lst = lst.tl; | |
} | |
return false; | |
} | |
function listMap(lst, fn) { | |
var result = []; | |
var mapper = function mapper(x) { | |
return result.push(result, fn(x)); | |
}; | |
listForEach(lst, mapper); | |
return listFromArray(result); | |
} | |
/** | |
* @param {*} v | |
* @return {boolean} true iff v is a list. | |
*/ | |
function isList(v) { | |
return v === EMPTY || check$6(v) && v._listLength !== -1; | |
} | |
var pair = /*#__PURE__*/Object.freeze({ | |
__proto__: null, | |
EMPTY: EMPTY, | |
isEmpty: isEmpty, | |
Pair: Pair, | |
check: check$6, | |
make: make$4, | |
makeList: makeList, | |
listToArray: listToArray, | |
listFromArray: listFromArray, | |
listForEach: listForEach, | |
listFind: listFind, | |
listMap: listMap, | |
isList: isList | |
}); | |
function printError(out, msg, args) { | |
out.consume(msg); | |
var _iterator2 = _createForOfIteratorHelper(args), | |
_step2; | |
try { | |
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { | |
var arg = _step2.value; | |
out.consume(' '); | |
if (typeof arg === 'string') { | |
out.consume(arg); | |
} else { | |
printNativeString$1(out, arg, | |
/* printAsExpression */ | |
true, | |
/* quoteDepth */ | |
0); | |
} | |
} | |
} catch (err) { | |
_iterator2.e(err); | |
} finally { | |
_iterator2.f(); | |
} | |
} | |
function makeError(name) { | |
var _this6 = this; | |
/** | |
* The "(error msg v ...)" form. | |
* Besides Racket values, also allows native strings. | |
*/ | |
var e = function e(msg) { | |
this.name = name; | |
var stringOut = new MiniNativeOutputStringPort(); | |
for (var _len4 = arguments.length, args = new Array(_len4 > 1 ? _len4 - 1 : 0), _key4 = 1; _key4 < _len4; _key4++) { | |
args[_key4 - 1] = arguments[_key4]; | |
} | |
printError(stringOut, msg, args); | |
this.message = stringOut.getOutputString(); | |
this.stack = new Error().stack; | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, this.constructor); | |
} else { | |
this.stack = new Error().stack; | |
} | |
}; | |
e.prototype = Object.create(Error.prototype); | |
e.prototype.constructor = e; | |
return function () { | |
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | |
args[_key5] = arguments[_key5]; | |
} | |
return new (Function.prototype.bind.apply(e, [_this6].concat(args)))(); | |
}; | |
} | |
var racketCoreError = makeError('RacketCoreError'); | |
var racketContractError = makeError('RacketContractError'); // this must be here to avoid circular dependency with numbers.js | |
// copied from internet: | |
// https://gist.github.com/jlbruno/1535691/db35b4f3af3dcbb42babc01541410f291a8e8fac | |
function toOrdinal(i) { | |
var j = i % 10; | |
var k = i % 100; | |
if (j === 1 && k !== 11) { | |
return "".concat(i, "st"); | |
} | |
if (j === 2 && k !== 12) { | |
return "".concat(i, "nd"); | |
} | |
if (j === 3 && k !== 13) { | |
return "".concat(i, "rd"); | |
} | |
return "".concat(i, "th"); | |
} // format exn message to exactly match Racket raise-argument-error | |
function makeArgumentError(name, expected) { | |
var stringOut = new MiniNativeOutputStringPort(); // "other" args must be converted to string via `print` | |
// (not `write` or `display`) | |
stringOut.consume("".concat(name.toString(), ": contract violation\n")); | |
stringOut.consume(' expected: '); | |
stringOut.consume(expected.toString()); | |
stringOut.consume('\n'); | |
stringOut.consume(' given: '); | |
for (var _len6 = arguments.length, rest = new Array(_len6 > 2 ? _len6 - 2 : 0), _key6 = 2; _key6 < _len6; _key6++) { | |
rest[_key6 - 2] = arguments[_key6]; | |
} | |
if (rest.length === 1) { | |
printNativeString$1(stringOut, rest[0], true, 0); | |
} else { | |
printNativeString$1(stringOut, rest[rest[0] + 1], true, 0); | |
if (rest.length > 2) { | |
// only print if there are "other" args | |
stringOut.consume('\n'); | |
stringOut.consume(' argument position: '); | |
printNativeString$1(stringOut, toOrdinal(rest[0] + 1), true, 0); | |
stringOut.consume('\n'); | |
stringOut.consume(' other arguments...:'); | |
for (var i = 1; i < rest.length; i++) { | |
// eslint-disable-next-line no-continue | |
if (i === rest[0] + 1) { | |
continue; | |
} | |
stringOut.consume('\n '); | |
printNativeString$1(stringOut, rest[i], true, 0); | |
} | |
} | |
} | |
return racketContractError(stringOut.getOutputString()); | |
} // format exn message to exactly match Racket raise-arguments-error | |
function makeArgumentsError(name, msg, field) { | |
var stringOut = new MiniNativeOutputStringPort(); | |
stringOut.consume("".concat(name.toString(), ": ")); | |
stringOut.consume(msg); | |
stringOut.consume('\n '); | |
stringOut.consume(field); | |
stringOut.consume(': '); | |
printNativeString$1(stringOut, arguments.length <= 3 ? undefined : arguments[3], true, 0); | |
for (var i = 1; i < (arguments.length <= 3 ? 0 : arguments.length - 3); i += 2) { | |
stringOut.consume('\n '); | |
stringOut.consume(i + 3 < 3 || arguments.length <= i + 3 ? undefined : arguments[i + 3]); | |
stringOut.consume(': '); | |
printNativeString$1(stringOut, i + 1 + 3 < 3 || arguments.length <= i + 1 + 3 ? undefined : arguments[i + 1 + 3], true, 0); | |
} | |
return racketContractError(stringOut.getOutputString()); | |
} | |
var Values = /*#__PURE__*/function (_Primitive4) { | |
_inherits(Values, _Primitive4); | |
var _super11 = _createSuper(Values); | |
function Values(vals) { | |
var _this7; | |
_classCallCheck(this, Values); | |
_this7 = _super11.call(this); | |
_this7.v = vals; | |
return _this7; | |
} | |
_createClass(Values, [{ | |
key: "getAt", | |
value: function getAt(i) { | |
return this.v[i]; | |
} | |
}, { | |
key: "getAll", | |
value: function getAll() { | |
return this.v; | |
} | |
}]); | |
return Values; | |
}(Primitive); | |
function make$5(vals) { | |
return new Values(vals); | |
} | |
function check$7(v) { | |
return v instanceof Values; | |
} | |
var values = /*#__PURE__*/Object.freeze({ | |
__proto__: null, | |
make: make$5, | |
check: check$7 | |
}); // TODO: allow ordering of hash traversal | |
function map(hash, proc) { | |
var result = EMPTY; | |
hash._h.forEach(function (value, key) { | |
result = make$4(proc(key, value), result); | |
}); | |
return result; | |
} | |
/* Arithmetic */ | |
function add() { | |
for (var _len7 = arguments.length, operands = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) { | |
operands[_key7] = arguments[_key7]; | |
} | |
return [].reduce.call(operands, function (a, b) { | |
return a + b; | |
}, 0); | |
} | |
function sub() { | |
if (arguments.length === 1) { | |
return -(arguments.length <= 0 ? undefined : arguments[0]); | |
} | |
var result = arguments.length <= 0 ? undefined : arguments[0]; | |
for (var i = 1; i < arguments.length; ++i) { | |
result -= i < 0 || arguments.length <= i ? undefined : arguments[i]; | |
} | |
return result; | |
} | |
function mul() { | |
for (var _len8 = arguments.length, operands = new Array(_len8), _key8 = 0; _key8 < _len8; _key8++) { | |
operands[_key8] = arguments[_key8]; | |
} | |
return [].reduce.call(operands, function (a, b) { | |
return a * b; | |
}, 1); | |
} | |
function div() { | |
if (arguments.length === 1) { | |
return 1 / (arguments.length <= 0 ? undefined : arguments[0]); | |
} | |
var result = arguments.length <= 0 ? undefined : arguments[0]; | |
for (var i = 1; i < arguments.length; ++i) { | |
result /= i < 0 || arguments.length <= i ? undefined : arguments[i]; | |
} | |
return result; | |
} | |
/* Comparison */ | |
function compare(cmp, operands) { | |
if (operands.length < 1) { | |
throw racketCoreError.apply(void 0, ['compare: at least 1 argument required, given'].concat(_toConsumableArray(operands))); | |
} | |
if (operands.length === 1) { | |
return true; | |
} | |
for (var i = 1; i < operands.length; i++) { | |
if (!cmp(operands[i - 1], operands[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function lt() { | |
for (var _len9 = arguments.length, operands = new Array(_len9), _key9 = 0; _key9 < _len9; _key9++) { | |
operands[_key9] = arguments[_key9]; | |
} | |
return compare(function (a, b) { | |
return a < b; | |
}, operands); | |
} | |
function lte() { | |
for (var _len10 = arguments.length, operands = new Array(_len10), _key10 = 0; _key10 < _len10; _key10++) { | |
operands[_key10] = arguments[_key10]; | |
} | |
return compare(function (a, b) { | |
return a <= b; | |
}, operands); | |
} | |
function gt() { | |
for (var _len11 = arguments.length, operands = new Array(_len11), _key11 = 0; _key11 < _len11; _key11++) { | |
operands[_key11] = arguments[_key11]; | |
} | |
return compare(function (a, b) { | |
return a > b; | |
}, operands); | |
} | |
function gte() { | |
for (var _len12 = arguments.length, operands = new Array(_len12), _key12 = 0; _key12 < _len12; _key12++) { | |
operands[_key12] = arguments[_key12]; | |
} | |
return compare(function (a, b) { | |
return a >= b; | |
}, operands); | |
} | |
function equals() { | |
for (var _len13 = arguments.length, operands = new Array(_len13), _key13 = 0; _key13 < _len13; _key13++) { | |
operands[_key13] = arguments[_key13]; | |
} | |
return compare(function (a, b) { | |
return a === b; | |
}, operands); | |
} | |
function check$8(v) { | |
return typeof v === 'number'; | |
} | |
/* Bitwise operators */ | |
function bitwiseOr() { | |
for (var _len14 = arguments.length, operands = new Array(_len14), _key14 = 0; _key14 < _len14; _key14++) { | |
operands[_key14] = arguments[_key14]; | |
} | |
return [].reduce.call(operands, function (a, b) { | |
return a | b; | |
}, 0); | |
} | |
function bitwiseXor() { | |
for (var _len15 = arguments.length, operands = new Array(_len15), _key15 = 0; _key15 < _len15; _key15++) { | |
operands[_key15] = arguments[_key15]; | |
} | |
return [].reduce.call(operands, function (a, b) { | |
return a ^ b; | |
}, 0); | |
} | |
function bitwiseAnd() { | |
for (var _len16 = arguments.length, operands = new Array(_len16), _key16 = 0; _key16 < _len16; _key16++) { | |
operands[_key16] = arguments[_key16]; | |
} | |
return [].reduce.call(operands, function (a, b) { | |
return a & b; | |
}, -1); | |
} | |
/** @abstract */ | |
var Port = /*#__PURE__*/function (_PrintablePrimitive5) { | |
_inherits(Port, _PrintablePrimitive5); | |
var _super12 = _createSuper(Port); | |
function Port() { | |
_classCallCheck(this, Port); | |
return _super12.apply(this, arguments); | |
} | |
_createClass(Port, [{ | |
key: "isOutputPort", | |
value: function isOutputPort() { | |
return false; | |
} | |
}, { | |
key: "isInputPort", | |
value: function isInputPort() { | |
return false; | |
} | |
}, { | |
key: "isStringPort", | |
value: function isStringPort() { | |
return false; | |
} | |
}]); | |
return Port; | |
}(PrintablePrimitive); | |
/** | |
* @abstract | |
* @api private | |
*/ | |
var OutputPort = /*#__PURE__*/function (_Port) { | |
_inherits(OutputPort, _Port); | |
var _super13 = _createSuper(OutputPort); | |
function OutputPort() { | |
_classCallCheck(this, OutputPort); | |
return _super13.apply(this, arguments); | |
} | |
_createClass(OutputPort, [{ | |
key: "isOutputPort", | |
value: function isOutputPort() { | |
return true; | |
} | |
/** | |
* @param {!NativeOutputStringPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume("#<output-port:".concat(this.name, ">")); | |
} | |
/** @abstract isUStringPort(): boolean; */ | |
}]); | |
return OutputPort; | |
}(Port); // Only consumes output via the given `consumeFn` when encountering a newline, | |
// othewise buffers the output. | |
// Writes *native* strings to the output. | |
var NewlineFlushingOutputPort = /*#__PURE__*/function (_OutputPort) { | |
_inherits(NewlineFlushingOutputPort, _OutputPort); | |
var _super14 = _createSuper(NewlineFlushingOutputPort); | |
/** | |
* @param {function(String)} consumeFn | |
* @param {!String} name | |
*/ | |
function NewlineFlushingOutputPort(consumeFn, name) { | |
var _this8; | |
_classCallCheck(this, NewlineFlushingOutputPort); | |
_this8 = _super14.call(this); | |
_this8._buffer = []; | |
_this8._consumeFn = consumeFn; | |
_this8.name = name; | |
return _this8; | |
} | |
/** | |
* @param {!String} nativeString | |
*/ | |
_createClass(NewlineFlushingOutputPort, [{ | |
key: "consume", | |
value: function consume(nativeString) { | |
var lastNewlineIndex = nativeString.lastIndexOf('\n'); | |
if (lastNewlineIndex >= 0) { | |
this._buffer.push(nativeString.slice(0, lastNewlineIndex)); | |
this._consumeFn(this._buffer.join('')); | |
var restChars = nativeString.slice(lastNewlineIndex + 1); | |
this._buffer = []; | |
if (restChars !== '') { | |
this._buffer.push(restChars); | |
} | |
} else { | |
this._buffer.push(nativeString); | |
} | |
} | |
}, { | |
key: "isUStringPort", | |
value: function isUStringPort() { | |
return false; | |
} | |
}]); | |
return NewlineFlushingOutputPort; | |
}(OutputPort); // eslint-disable-next-line no-console | |
var standardOutputPort = new NewlineFlushingOutputPort(function (str) { | |
return console.log(str); | |
}, 'stdout'); // eslint-disable-next-line no-console | |
var standardErrorPort = new NewlineFlushingOutputPort(function (str) { | |
return console.log(str); | |
}, 'stderr'); | |
var OutputStringPort = /*#__PURE__*/function (_OutputPort2) { | |
_inherits(OutputStringPort, _OutputPort2); | |
var _super15 = _createSuper(OutputStringPort); | |
function OutputStringPort() { | |
var _this9; | |
_classCallCheck(this, OutputStringPort); | |
_this9 = _super15.call(this); | |
_this9._buffer = []; | |
return _this9; | |
} | |
/** | |
* @param {!UString.UString} s | |
*/ | |
_createClass(OutputStringPort, [{ | |
key: "consume", | |
value: function consume(s) { | |
this._buffer.push(s); | |
} | |
/** | |
* @return {!UString.UString} | |
*/ | |
}, { | |
key: "getOutputString", | |
value: function getOutputString() { | |
if (this._buffer.length === 0) { | |
return makeMutable(''); | |
} | |
if (this._buffer.length > 1) { | |
this._buffer = [stringAppend.apply(void 0, _toConsumableArray(this._buffer))]; | |
} | |
return copyAsMutable(this._buffer[0]); | |
} | |
}, { | |
key: "name", | |
get: function get() { | |
return 'string'; | |
} | |
}, { | |
key: "isStringPort", | |
value: function isStringPort() { | |
return true; | |
} | |
}, { | |
key: "isUStringPort", | |
value: function isUStringPort() { | |
return true; | |
} | |
}]); | |
return OutputStringPort; | |
}(OutputPort); | |
/** | |
* @return {!OutputStringPort} | |
*/ | |
function openOutputString() { | |
return new OutputStringPort(); | |
} | |
/** | |
* @param {!OutputStringPort} outputStringPort | |
* @return {!UString.UString} | |
*/ | |
function getOutputString(outputStringPort) { | |
return outputStringPort.getOutputString(); | |
} | |
function raise(exp) { | |
for (var _len17 = arguments.length, args = new Array(_len17 > 1 ? _len17 - 1 : 0), _key17 = 1; _key17 < _len17; _key17++) { | |
args[_key17 - 1] = arguments[_key17]; | |
} | |
throw exp.apply(this, args); | |
} | |
function truthy(val, exp) { | |
var msg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | |
if (val !== true) { | |
raise(exp, msg); | |
} | |
return true; | |
} | |
function falsy(val, exp) { | |
var msg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; | |
return truthy(val === false, exp, msg); | |
} | |
function eq$2(val1, val2, exp, msg) { | |
if (val1 !== val2) { | |
raise(exp, msg); | |
} | |
return true; | |
} // This module implements Racket structs via three classes which | |
// directly corresponds to their Racket counterparts. Structure | |
// instances are either Struct or if its applicable structures, i.e. | |
// with a proc-spec parametre, it is a function with Struct wrapped | |
// inside. | |
// | |
// - Struct: This class represents a struct instance. This will | |
// keep a reference to its struct-type-description, to determine | |
// properties, guards etc... | |
// - StructTypeDescriptor: Corresponds to a struct-type-descriptor | |
// which is returned by make-struct-type. This class contains | |
// all data needed to create constructor, accessors, mutators to | |
// structures. Properties are attached when an object is created | |
// using make-struct-type. All super properties of properties that | |
// are directly passed to make-struct-type are added, however, | |
// properties of super struct-type-descriptors are not added, and | |
// searched on-demand. In future we may cache all this for | |
// performance | |
// - StructTypeProperty: Class representing a struct-type-property. | |
// Also contains methods to attach itself to a struct instance. | |
// By specification, guards are applied when being attached to | |
// structs. | |
// | |
// TODO: | |
// - Structure Inspectors | |
// - Prefab | |
var Struct = /*#__PURE__*/function (_PrintablePrimitive6) { | |
_inherits(Struct, _PrintablePrimitive6); | |
var _super16 = _createSuper(Struct); | |
function Struct(desc, fields) { | |
var _this10; | |
var callerName = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | |
_classCallCheck(this, Struct); | |
_this10 = _super16.call(this); | |
_this10._desc = desc; | |
/* struct-type-descriptor */ | |
eq$2(fields.length, _this10._desc._totalInitFields, racketCoreError, 'arity mismatch'); // Guard's are applied starting from subtype to supertype | |
// Later when we instantiate the subtype, its guard will be | |
// called in its constructor, hence maintaining the required | |
// order | |
var guardLambda = _this10._desc._options.guard; | |
var finalCallerName = callerName || _this10._desc._options.constructorName || _this10._desc._options.name; | |
if (guardLambda) { | |
var guardFields = fields.concat(finalCallerName); | |
var newFields = guardLambda.apply(void 0, _toConsumableArray(guardFields)); | |
if (check$7(newFields)) { | |
fields = newFields.getAll(); | |
} else { | |
fields = [newFields]; | |
} | |
} // Initialize current and super instance | |
_this10._superStructInstance = false; | |
/* Struct instance of super-type */ | |
var superType = _this10._desc.getSuperType(); | |
if (superType !== false) { | |
var superInitFields = fields.slice(0, superType._totalInitFields); | |
_this10._fields = fields.slice(superType._totalInitFields); | |
_this10._superStructInstance = superType.getStructConstructor(finalCallerName).apply(void 0, _toConsumableArray(superInitFields)); | |
} else { | |
_this10._fields = fields; | |
} // Auto fields | |
var _this10$_desc$_option = _this10._desc._options, | |
autoV = _this10$_desc$_option.autoV, | |
autoFieldCount = _this10$_desc$_option.autoFieldCount; | |
/* Initial value for auto fields */ | |
for (var i = 0; i < autoFieldCount; i++) { | |
_this10._fields.push(autoV); | |
} | |
return _this10; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {function(Ports.NativeStringOutputPort, *)} itemFn | |
*/ | |
_createClass(Struct, [{ | |
key: "writeToPort", | |
value: function writeToPort(out, itemFn) { | |
if (this._desc._options.inspector) { | |
// Not a transparent inspector | |
// TODO: support prefab | |
out.consume('#<'); | |
out.consume(this._desc.getName()); | |
out.consume('>'); | |
} else { | |
out.consume('#(struct:'); | |
out.consume(this._desc.getName()); | |
var _iterator3 = _createForOfIteratorHelper(this._fields), | |
_step3; | |
try { | |
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { | |
var field = _step3.value; | |
out.consume(' '); | |
itemFn(out, field); | |
} | |
} catch (err) { | |
_iterator3.e(err); | |
} finally { | |
_iterator3.f(); | |
} | |
out.consume(')'); | |
} | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
this.writeToPort(out, displayNativeString$2); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
this.writeToPort(out, _writeNativeString); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
if (this._desc._options.inspector) { | |
// Not a transparent inspector | |
// TODO: support prefab | |
this.writeNativeString(out); | |
} else { | |
out.consume('('); | |
out.consume(this._desc.getName()); | |
var _iterator4 = _createForOfIteratorHelper(this._fields), | |
_step4; | |
try { | |
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { | |
var field = _step4.value; | |
out.consume(' '); | |
printNativeString$1(out, field, | |
/* printAsExpression */ | |
true, | |
/* quoteDepth */ | |
0); | |
} | |
} catch (err) { | |
_iterator4.e(err); | |
} finally { | |
_iterator4.f(); | |
} | |
out.consume(')'); | |
} | |
} | |
}, { | |
key: "equals", | |
value: function equals(v) { | |
if (!check$9(v, this._desc)) { | |
return false; | |
} // check for prop:equal+hash must come before transparent check | |
// eslint-disable-next-line no-use-before-define | |
var p = this._desc._options.props.get(propEqualHash); | |
if (p !== undefined) { | |
var eqhashfn = p.car(); | |
return eqhashfn(this, v, function (a, b) { | |
return a.equals(b); | |
}); // TODO: handle cycles | |
} | |
if (this._desc._options.inspector) { | |
// Not a transparent inspector | |
return this === v; | |
} | |
for (var i = 0; i < this._fields.length; i++) { | |
if (!isEqual(this._fields[i], v._fields[i])) { | |
return false; | |
} | |
} | |
return true; | |
} | |
}, { | |
key: "getField", | |
value: function getField(n) { | |
if (n >= this._fields.length) { | |
throw new Error("TypeError: invalid field at position ".concat(n)); | |
} | |
return this._fields[n]; | |
} | |
}, { | |
key: "setField", | |
value: function setField(n, v) { | |
truthy(n < this._fields.length, racketCoreError, 'invalid field at position'); | |
falsy(this._desc.isFieldImmutable(n), racketCoreError, 'field is immutable'); | |
this._fields[n] = v; | |
} // Return false if targetDesc is not a superType of current struct | |
// or return struct instance of struct-type-descriptor targetDesc | |
}, { | |
key: "_maybeFindSuperInstance", | |
value: function _maybeFindSuperInstance(targetDesc) { | |
for (var s = this; s !== false; s = s._superStructInstance) { | |
if (s._desc === targetDesc) { | |
return s; | |
} | |
} | |
return false; | |
} | |
}]); | |
return Struct; | |
}(PrintablePrimitive); | |
/** ************************************************************************** */ | |
var StructTypeDescriptor = /*#__PURE__*/function (_PrintablePrimitive7) { | |
_inherits(StructTypeDescriptor, _PrintablePrimitive7); | |
var _super17 = _createSuper(StructTypeDescriptor); | |
function StructTypeDescriptor(options) { | |
var _this11; | |
_classCallCheck(this, StructTypeDescriptor); | |
_this11 = _super17.call(this); // Visit makeStructType (or make-struct-type in Racket docs) | |
// to see the structure of options | |
_this11._options = options; // Initialize properties | |
// supers in struct-type-property are also added when | |
// attached. However propeties attached to super types of this | |
// struct are not added here and will have to be followed. | |
var props = options.props && listToArray(options.props); | |
_this11._options.props = new Map(); | |
if (props) { | |
// TODO: If prop is already added, then check associated | |
// values with eq?, else raise contract-errorx | |
var _iterator5 = _createForOfIteratorHelper(props), | |
_step5; | |
try { | |
for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { | |
var prop = _step5.value; | |
prop.hd.attachToStructTypeDescriptor(_assertThisInitialized(_this11), prop.tl); | |
} | |
} catch (err) { | |
_iterator5.e(err); | |
} finally { | |
_iterator5.f(); | |
} | |
} // eslint-disable-next-line no-use-before-define | |
_this11._propProcedure = _this11._findProperty(propProcedure); // Value for auto fields | |
_this11._options.autoV = _this11._options.autoV || false; // Number of intializing fields needed, that is including | |
// those of super types | |
_this11._totalInitFields = options.initFieldCount; | |
if (options.superType) { | |
_this11._totalInitFields += options.superType._totalInitFields; | |
} // Immutables | |
var immutables = options.immutables || EMPTY; | |
_this11._options.immutables = new Set(listToArray(immutables)); | |
_this11._options.immutables.forEach(function (e) { | |
if (e < 0 || e >= options.initFieldCount) { | |
raise('invalid index in immutables provided'); | |
} | |
}); | |
return _this11; | |
} | |
_createClass(StructTypeDescriptor, [{ | |
key: "displayNativeString", | |
value: | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
function displayNativeString(out) { | |
out.consume('#<struct-type:'); | |
out.consume(this._options.name); | |
out.consume('>'); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
this.writeNativeString(out); | |
} | |
}, { | |
key: "getName", | |
value: function getName() { | |
return this._options.name; | |
} | |
}, { | |
key: "getSuperType", | |
value: function getSuperType() { | |
return this._options.superType; | |
} | |
}, { | |
key: "getApplicableStructObject", | |
value: function getApplicableStructObject(structObject, procSpec) { | |
var structfn = function structfn() { | |
// Struct object is also a procedure | |
var proc; | |
for (var _len18 = arguments.length, args = new Array(_len18), _key18 = 0; _key18 < _len18; _key18++) { | |
args[_key18] = arguments[_key18]; | |
} | |
if (typeof procSpec === 'function') { | |
proc = procSpec; // Structure object is sent only when procSpec is | |
// function, not when we get procedure from field. | |
args.unshift(structObject); | |
} else if (Number.isInteger(procSpec)) { | |
proc = structObject.getField(procSpec); | |
} else { | |
throw new Error("ValueError: invalid field at position ".concat(procSpec)); | |
} | |
return proc.apply(void 0, args); | |
}; | |
structfn.__rjs_struct_object = structObject; | |
return structfn; | |
} | |
}, { | |
key: "maybeStructObject", | |
value: function maybeStructObject(s) { | |
if (s instanceof Struct) { | |
return s; | |
} else if (s instanceof Function && s.__rjs_struct_object instanceof Struct) { | |
return s.__rjs_struct_object; | |
} | |
return false; | |
} | |
}, { | |
key: "getStructConstructor", | |
value: function getStructConstructor() { | |
var _this12 = this; | |
var subtype = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; | |
// subtype: Name of subtype which is used to call the constructor | |
// returned here. | |
return attachReadOnlyProperty(function () { | |
for (var _len19 = arguments.length, args = new Array(_len19), _key19 = 0; _key19 < _len19; _key19++) { | |
args[_key19] = arguments[_key19]; | |
} | |
var structObject = new Struct(_this12, args, subtype); | |
var hasPropProc = _this12._propProcedure !== undefined && _this12._propProcedure !== false; | |
var hasProcSpec = _this12._options.procSpec !== undefined && _this12._options.procSpec !== false; | |
if (!hasPropProc && !hasProcSpec) { | |
return structObject; | |
} else if (hasPropProc) { | |
return _this12.getApplicableStructObject(structObject, _this12._propProcedure); | |
} | |
return _this12.getApplicableStructObject(structObject, _this12._options.procSpec); | |
}, 'racketProcedureType', 'struct-constructor'); | |
} | |
}, { | |
key: "getStructPredicate", | |
value: function getStructPredicate() { | |
var _this13 = this; | |
return attachReadOnlyProperty(function (s) { | |
var structObject = _this13.maybeStructObject(s); | |
return structObject && structObject._maybeFindSuperInstance(_this13) && true; | |
}, 'racketProcedureType', 'struct-predicate'); | |
} | |
}, { | |
key: "getStructAccessor", | |
value: function getStructAccessor() { | |
var _this14 = this; | |
return attachReadOnlyProperty(function (s, pos) { | |
var structObject = _this14.maybeStructObject(s); | |
if (!structObject) { | |
raise(TypeError, "(".concat(s, " : ").concat(_typeof2(s), " != ").concat(_this14._options.name, " object)")); | |
} | |
var sobj = structObject._maybeFindSuperInstance(_this14); | |
if (sobj === false) { | |
raise(racketCoreError, 'accessor applied to invalid type'); | |
} | |
return sobj.getField(pos); | |
}, 'racketProcedureType', 'struct-accessor'); | |
} | |
}, { | |
key: "getStructMutator", | |
value: function getStructMutator() { | |
var _this15 = this; | |
return attachReadOnlyProperty(function (s, pos, v) { | |
var structObject = _this15.maybeStructObject(s); | |
if (!structObject) { | |
raise(TypeError, "(".concat(s, " : ").concat(_typeof2(s), " != ").concat(_this15._options.name, " object)")); | |
} | |
var sobj = structObject._maybeFindSuperInstance(_this15); | |
if (sobj === false) { | |
raise(racketCoreError, 'mutator applied to invalid type'); | |
} | |
return sobj.setField(pos, v); | |
}, 'racketProcedureType', 'struct-mutator'); | |
} // Find value associated with property `prop` or return `undefined` | |
// We return undefined, as `false` could be possible Racket value | |
// attached | |
// | |
// The property can be in - | |
// - Current struct-type-descriptor | |
// - Super of current struct-type-descriptor | |
// - Supers of any struct-type-property attached to this | |
// The first and third case can be handled together. See property | |
// initialization in constructor. | |
}, { | |
key: "_findProperty", | |
value: function _findProperty(prop) { | |
for (var desc = this; desc; desc = desc.getSuperType()) { | |
var val = desc._options.props.get(prop); | |
if (val !== undefined) { | |
return val; | |
} | |
} | |
return undefined; | |
} | |
}, { | |
key: "isFieldImmutable", | |
value: function isFieldImmutable(n) { | |
return this._options.immutables.has(n); | |
} | |
}], [{ | |
key: "make", | |
value: function make(options) { | |
return Object.freeze(new StructTypeDescriptor(options)); | |
} | |
}]); | |
return StructTypeDescriptor; | |
}(PrintablePrimitive); | |
/** ************************************************************************** */ | |
var StructTypeProperty = /*#__PURE__*/function (_PrintablePrimitive8) { | |
_inherits(StructTypeProperty, _PrintablePrimitive8); | |
var _super18 = _createSuper(StructTypeProperty); | |
function StructTypeProperty(args) { | |
var _this16; | |
_classCallCheck(this, StructTypeProperty); | |
_this16 = _super18.call(this); | |
_this16._name = args.name.toString(); | |
_this16._guard = args.guard || false; | |
/* applied when attaching */ | |
_this16._canImpersonate = args.canImpersonate || false; | |
/* TODO */ | |
_this16._supers = args.supers && listToArray(args.supers) || []; | |
return _this16; | |
} | |
_createClass(StructTypeProperty, [{ | |
key: "displayNativeString", | |
value: | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
function displayNativeString(out) { | |
out.consume('#<struct-type-property:'); | |
out.consume(this._name); | |
out.consume('>'); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "printNativeString", | |
value: function printNativeString(out) { | |
this.writeNativeString(out); | |
} | |
}, { | |
key: "getPropertyPredicate", | |
value: function getPropertyPredicate() { | |
var _this17 = this; | |
return function (v) { | |
var desc; | |
if (v instanceof StructTypeDescriptor) { | |
desc = v; | |
} else if (v instanceof Struct) { | |
desc = v._desc; | |
} else { | |
return false; | |
} | |
return desc._findProperty(_this17) !== undefined; | |
}; | |
} | |
}, { | |
key: "getPropertyAccessor", | |
value: function getPropertyAccessor() { | |
var _this18 = this; | |
return function (v) { | |
/* property acccessor */ | |
var desc; | |
if (v instanceof StructTypeDescriptor) { | |
desc = v; | |
} else if (v instanceof Struct) { | |
desc = v._desc; | |
} else { | |
raise(racketCoreError, 'invalid argument to accessor'); | |
} | |
return desc._findProperty(_this18) || raise(racketCoreError, 'property not in struct'); | |
}; | |
} // Attaches current property with given struct descriptor | |
// For simplicity all supers of this property will also be | |
// attached directly | |
// | |
// Lambda associated with each item in supers of this | |
// property is called with the result of guard application | |
}, { | |
key: "attachToStructTypeDescriptor", | |
value: function attachToStructTypeDescriptor(desc, v) { | |
var newV = v; | |
if (this._guard) { | |
newV = this._guard(v, listFromArray(structTypeInfo(desc))); | |
} | |
desc._options.props.set(this, newV); | |
this._supers.forEach(function (superEntry) { | |
var prop = superEntry.hd; | |
var proc = superEntry.tl; | |
prop.attachToStructTypeDescriptor(desc, proc(newV)); | |
}); | |
} | |
}], [{ | |
key: "make", | |
value: function make(args) { | |
return Object.freeze(new StructTypeProperty(args)); | |
} | |
}]); | |
return StructTypeProperty; | |
}(PrintablePrimitive); | |
/** ************************************************************************** */ | |
function makeStructTypeProperty(options) { | |
var stProp = StructTypeProperty.make(options); | |
return make$5([stProp, stProp.getPropertyPredicate(), stProp.getPropertyAccessor()]); | |
} | |
/** ************************************************************************** */ | |
function makeStructType(options) { | |
var descriptor = new StructTypeDescriptor(options); | |
return make$5([descriptor, descriptor.getStructConstructor(), descriptor.getStructPredicate(), descriptor.getStructAccessor(), descriptor.getStructMutator()]); | |
} | |
function structTypeInfo(desc) { | |
return [desc._options.name, desc._options.initFieldCount, desc._options.autoFieldCount, desc.getStructAccessor(), desc.getStructMutator(), desc._options.immutables, // TODO: What about supers? | |
desc._options.superType || false, false // TODO: Not sure what this field means? | |
]; | |
} | |
function isStructInstance(v) { | |
return v instanceof Struct || v instanceof Function && v.__rjs_struct_object instanceof Struct; | |
} | |
function check$9(v, desc) { | |
return isStructInstance(v) && v._desc === desc; | |
} | |
/** ************************************************************************** */ | |
// Properties | |
// TODO: find out why changing let to const and moving them to the top breaks tests | |
// eslint-disable-next-line import/no-mutable-exports | |
var propProcedure = makeStructTypeProperty({ | |
name: 'prop:procedure' | |
}).getAt(0); // eslint-disable-next-line import/no-mutable-exports | |
var propEqualHash = makeStructTypeProperty({ | |
name: 'prop:equal+hash' | |
}).getAt(0); // Continuation Marks | |
var __frames; | |
var __prompts = new Map(); | |
var __defaultContinuationPromptTag = makeContinuationPromptTag(make$2('default')); | |
/* --------------------------------------------------------------------------*/ | |
function init() { | |
__frames = EMPTY; | |
savePrompt(__defaultContinuationPromptTag); | |
enterFrame(); | |
} | |
init(); | |
/* --------------------------------------------------------------------------*/ | |
// Prompts are stored in __prompts map. It is a map between | |
// ContinuationPromptTag to [Frame]. The value is used to filter out frames | |
// until given prompt. The most recent frame marked with a prompt is last | |
// item in the array mapped to the prompt tag. | |
// TODO: Handle default-continution-prompt-tag | |
function ContinuationPromptTag(tag) { | |
this.tag = tag; | |
return this; | |
} | |
function AbortCurrentContinuation(promptTag, handlerArgs) { | |
this.name = 'abort-current-continuation'; | |
this.promptTag = promptTag; | |
this.handlerArgs = handlerArgs; | |
this.stack = new Error().stack; | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(this, this.constructor); | |
} else { | |
this.stack = new Error().stack; | |
} | |
} | |
AbortCurrentContinuation.prototype = Object.create(Error.prototype); | |
AbortCurrentContinuation.prototype.constructor = AbortCurrentContinuation; | |
function savePrompt(promptTag) { | |
var promptVal = __prompts.get(promptTag); | |
if (promptVal === undefined) { | |
promptVal = []; | |
__prompts.set(promptTag, promptVal); | |
} // Most recent prompt is at the end of array. | |
promptVal.push(__frames.hd); | |
} | |
function getPromptFrame(promptTag) { | |
if (promptTag === undefined) { | |
return promptTag; | |
} | |
var result = __prompts.get(promptTag); | |
return result && result[result.length - 1] || undefined; | |
} | |
function makeContinuationPromptTag(sym) { | |
return new ContinuationPromptTag(sym); | |
} | |
/* --------------------------------------------------------------------------*/ | |
function getFrames() { | |
return __frames; | |
} | |
function enterFrame() { | |
__frames = make$4({}, __frames); | |
return __frames; | |
} | |
function setMark(key, value) { | |
var frame = __frames.hd; | |
frame[hashForEq(key)] = value; | |
} | |
function getContinuationMarks(promptTag) { | |
promptTag = promptTag || __defaultContinuationPromptTag; | |
var frames = __frames; | |
var promptFrame = getPromptFrame(promptTag); | |
if (promptFrame === undefined && promptTag !== __defaultContinuationPromptTag) { | |
throw racketCoreError('No corresponding tag in continuation!'); | |
} | |
var result = []; | |
while (!isEmpty(frames)) { | |
if (frames.hd === promptFrame) { | |
break; | |
} | |
result.push(frames.hd); | |
frames = frames.tl; | |
} | |
return result; | |
} | |
function getMarks(framesArr, key, promptTag) { | |
promptTag = promptTag || __defaultContinuationPromptTag; | |
var keyHash = hashForEq(key); | |
var promptFrame = getPromptFrame(promptTag); | |
var result = []; | |
for (var ii = 0; ii < framesArr.length; ++ii) { | |
// FIXME: for-of requires polyfill | |
var fr = framesArr[ii]; | |
if (keyHash in fr) { | |
if (fr === promptFrame) { | |
break; | |
} | |
result.push(fr[keyHash]); | |
} | |
} | |
return listFromArray(result); | |
} // TODO: Used by parameterization. Add test cases around promptTag | |
// and parameterization and check if that if this needs | |
function getFirstMark(frames, key, noneV) { | |
var keyHash = hashForEq(key); | |
return listFind(frames, function (fr) { | |
if (keyHash in fr) { | |
return fr[keyHash]; | |
} | |
}) || noneV; | |
} // TODO: All of these functions can be migrated to kernel.rkt after fprintf is. | |
/** | |
* Writes a string representation similar to Racket's `display` to the given port. | |
* | |
* @param {(!Ports.NativeStringOutputPort|!Ports.UStringOutputPort)} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function display(out, v) { | |
if (out.isUStringPort()) { | |
_displayUString(out, v); | |
} else { | |
displayNativeString$2(out, v); | |
} | |
} | |
/** | |
* Writes a string representation that can be read by Racket's `read` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
*/ | |
function write(out, v) { | |
if (out.isUStringPort()) { | |
_writeUString(out, v); | |
} else { | |
_writeNativeString(out, v); | |
} | |
} | |
/** | |
* Writes a string representation similar to Racket's `print` to the given port. | |
* | |
* @param {!Ports.NativeStringOutputPort} out | |
* @param {!(Primitive.Primitive|Uint8Array|Function|String|number|boolean|undefined|null)} v | |
* @param {!boolean} printAsExpression | |
* @param {!(0|1)} quoteDepth | |
*/ | |
function print(out, v, printAsExpression, quoteDepth) { | |
if (out.isUStringPort()) { | |
printUString(out, v, printAsExpression, quoteDepth); | |
} else { | |
printNativeString$1(out, v, printAsExpression, quoteDepth); | |
} | |
} // Exports classes for creating basic data types and operation on them | |
var UnsafeUndefined = /*#__PURE__*/function (_PrintablePrimitive9) { | |
_inherits(UnsafeUndefined, _PrintablePrimitive9); | |
var _super19 = _createSuper(UnsafeUndefined); | |
function UnsafeUndefined() { | |
_classCallCheck(this, UnsafeUndefined); | |
return _super19.apply(this, arguments); | |
} | |
_createClass(UnsafeUndefined, [{ | |
key: "equals", | |
value: function equals(v) { | |
return v === this; | |
} | |
/** | |
* @return {!number} a 32-bit integer | |
*/ | |
}, { | |
key: "hashForEqual", | |
value: function hashForEqual() { | |
return 0; | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "displayNativeString", | |
value: function displayNativeString(out) { | |
out.consume('#<unsafe-undefined>'); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "displayUString", | |
value: function displayUString(out) { | |
out.consume('#<unsafe-undefined>'); | |
} | |
/** | |
* @param {!Ports.NativeStringOutputPort} out | |
*/ | |
}, { | |
key: "writeNativeString", | |
value: function writeNativeString(out) { | |
out.consume('#<unsafe-undefined>'); | |
} | |
/** | |
* @param {!Ports.UStringOutputPort} out | |
*/ | |
}, { | |
key: "writeUString", | |
value: function writeUString(out) { | |
out.consume('#<unsafe-undefined>'); | |
} | |
}]); | |
return UnsafeUndefined; | |
}(PrintablePrimitive); // eslint-disable-next-line no-unused-vars | |
var theUnsafeUndefined = new UnsafeUndefined(); | |
/* --------------------------------------------------------------------------*/ | |
// Parameterization data structure is a HAMT Map keyed by parameter | |
// and the value is stored in a box. The parameter is function object | |
// which also acts as a key to look into parameterization map. When | |
// using `parameterize` form, extendParameterization creates a new box | |
// foreground the parameter to be used in context. Even though, its a | |
// new map, since each value is a box, any updates to other parameters | |
// is retained throughout its context, regardless whatever other | |
// parameter context we have created. | |
// | |
// When a parameter is created, it keeps an stores the initial value | |
// inside. An entry is not added to parameterization map. An entry is | |
// created in this map only when a context is created using | |
// `with-continuation-mark` or `parameterize`. `__top` keeps the top | |
// level parameter value local to current async callback. When a | |
// callback is created current parameterization should be copied using | |
// ffi's "=>$" form. | |
var ParameterizationKey = {}; | |
/* a unique reference that can act as key */ | |
var ExceptionHandlerKey = {}; | |
/* a unique reference that can act as key */ | |
var __top; | |
function getCurrentParameterization() { | |
return getFirstMark(getFrames(), ParameterizationKey, false); | |
} | |
function makeParameter(initValue) { | |
var param = function param(maybeSetVal) { | |
// Get current value box from parameterization. The function | |
// `param` that we result is the key. | |
var current = getCurrentParameterization(); | |
var pv = current && current.get(param, false) || __top.get(param, false); // Create entry in __top if its a mutation. | |
if (!pv && maybeSetVal !== undefined) { | |
pv = make$3(initValue); | |
__top.set(param, pv); | |
} // Get/Set | |
if (maybeSetVal === undefined) { | |
return pv ? pv.get() : initValue; | |
} | |
pv.set(maybeSetVal); | |
}; | |
return param; | |
} | |
/* --------------------------------------------------------------------------*/ | |
// Init parameterization | |
(function () { | |
var p = getCurrentParameterization(); | |
if (p !== false) { | |
return; | |
} | |
setMark(ParameterizationKey, hamt.make()); | |
__top = new Map(); | |
})(); | |
/* --------------------------------------------------------------------------*/ | |
// String construction and manipulation | |
// fprintf forms that do not require an argument. | |
var NO_ARG_FORM_RE = /^~[\s~n%]/; | |
function fprintf(isPrintAsExpression, out, form) { | |
// TODO: Missing forms: ~.[asv], ~e, ~c. | |
// TODO: The ~whitespace form should match Unicode whitespace. | |
var regex = /~(?:[aAeEsSvVbBoOxX~n%]|\s+)/g; | |
var formStr = form.toString(); | |
var reExecResult; | |
var currentMatchIndex = 0; | |
var prevIndex = 0; | |
var lastMatch = ''; | |
var matches = formStr.match(regex); | |
var numExpected = matches ? matches.filter(function (m) { | |
return !NO_ARG_FORM_RE.test(m); | |
}).length : 0; | |
for (var _len20 = arguments.length, args = new Array(_len20 > 3 ? _len20 - 3 : 0), _key20 = 3; _key20 < _len20; _key20++) { | |
args[_key20 - 3] = arguments[_key20]; | |
} | |
if (numExpected !== args.length) { | |
throw racketContractError.apply(void 0, ["fprintf: format string requires ".concat(numExpected, " arguments, ") + "given ".concat(args.length, "; arguments were:"), out, form].concat(args)); | |
} // eslint-disable-next-line no-cond-assign | |
while ((reExecResult = regex.exec(formStr)) !== null) { | |
display(out, formStr.slice(prevIndex + lastMatch.length, reExecResult.index)); | |
prevIndex = reExecResult.index; | |
lastMatch = reExecResult[0]; // eslint-disable-line prefer-destructuring | |
if (/^~\s/.test(lastMatch)) continue; // eslint-disable-line no-continue | |
// eslint-disable-next-line default-case | |
switch (lastMatch.charAt(1)) { | |
case '~': | |
display(out, '~'); | |
continue; | |
// eslint-disable-line no-continue | |
case 'n': | |
case '%': | |
display(out, '\n'); | |
continue; | |
// eslint-disable-line no-continue | |
} | |
var v = args[currentMatchIndex]; | |
currentMatchIndex += 1; | |
switch (lastMatch.charAt(1)) { | |
case 'a': | |
case 'A': | |
display(out, v); | |
break; | |
case 'e': // FIXME: should use error-value->string-handler | |
case 'E': | |
display(out, v); | |
break; | |
case 's': | |
case 'S': | |
write(out, v); | |
break; | |
case 'v': | |
case 'V': | |
print(out, v, isPrintAsExpression, 0); | |
break; | |
case 'b': | |
case 'B': | |
// TODO: raise exn:fail:contract if the number is not exact. | |
display(out, v.toString(2)); | |
break; | |
case 'o': | |
case 'O': | |
// TODO: raise exn:fail:contract if the number is not exact. | |
display(out, v.toString(8)); | |
break; | |
case 'x': | |
case 'X': | |
// TODO: raise exn:fail:contract if the number is not exact. | |
display(out, v.toString(16)); | |
break; | |
default: | |
throw racketContractError('Unsupported format:', lastMatch); | |
} | |
} | |
if (lastMatch.length + prevIndex < form.length) { | |
display(out, formStr.slice(lastMatch.length + prevIndex)); | |
} | |
} | |
/* --------------------------------------------------------------------------*/ | |
// Errors | |
/** | |
* @param {Core.PrimitiveSymbol|Core.UString|String} firstArg | |
* @param {*[]} rest | |
*/ | |
function error(firstArg) { | |
for (var _len21 = arguments.length, rest = new Array(_len21 > 1 ? _len21 - 1 : 0), _key21 = 1; _key21 < _len21; _key21++) { | |
rest[_key21 - 1] = arguments[_key21]; | |
} | |
if (check$4(firstArg)) { | |
if (rest.length === 0) { | |
throw racketCoreError(firstArg.toString()); | |
} else { | |
throw racketCoreError.apply(void 0, ["".concat(firstArg.toString(), ":")].concat(rest)); | |
} | |
} else if (check$3(firstArg) || typeof firstArg === 'string') { | |
throw racketCoreError.apply(void 0, [firstArg.toString()].concat(rest)); | |
} else { | |
throw racketContractError('error: invalid arguments'); | |
} | |
} | |
/** | |
* @param {Core.Error} e | |
*/ | |
// somewhat duplicates continuation-mark-set-first in kernel.rkt | |
// (but less general, eg ignore prompt tag for now); | |
// must be here to avoid circular dependency | |
function doraise(e) { | |
var markset = getContinuationMarks(); | |
var marks = getMarks(markset, ExceptionHandlerKey); | |
if (marks.length === 0) { | |
throw e; | |
} else { | |
marks.hd(e); | |
} | |
} | |
var Values$1 = values; | |
var unsafe_undefined = theUnsafeUndefined; | |
var equal_p = isEqual; | |
var eq_p = isEq; | |
var values$1 = attachProcedureName(attachProcedureArity(function () { | |
var vals102 = argumentsToArray(arguments); | |
if (vals102.length === 1 !== false) { | |
var if_res8 = vals102[0]; | |
} else { | |
var if_res8 = Values$1.make(vals102); | |
} | |
return if_res8; | |
}, 0), make$1("values")); | |
var call_with_values = function call_with_values(generator103, receiver104) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var vals105 = generator103(); | |
if (Values$1.check(vals105) !== false) { | |
var if_res10 = receiver104.apply(this, vals105.getAll()); | |
} else { | |
if (not(eq_p(vals105, undefined) || eq_p(vals105, null)) !== false) { | |
var if_res9 = receiver104.apply(this, [vals105]); | |
} else { | |
var if_res9 = rvoid(); | |
} | |
var if_res10 = if_res9; | |
} | |
return if_res10; | |
}; | |
var rvoid = attachProcedureArity(function () { | |
for (var _len22 = arguments.length, _10711 = new Array(_len22), _key22 = 0; _key22 < _len22; _key22++) { | |
_10711[_key22] = arguments[_key22]; | |
} | |
listFromArray(_10711); | |
return null; | |
}); | |
var void_p = function void_p(v108) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return v108 === null || v108 === undefined; | |
}; | |
var number_p = check$8; | |
var zero_p = function zero_p(v109) { | |
if (number_p(v109) !== false) { | |
rvoid(); | |
} else { | |
doraise(makeArgumentError(make$2("zero?"), make$1("number?"), v109)); | |
} | |
return v109 === 0; | |
}; | |
var sub1 = function sub1(v113) { | |
if (number_p(v113) !== false) { | |
rvoid(); | |
} else { | |
doraise(makeArgumentError(make$2("sub1"), make$1("number?"), v113)); | |
} | |
return v113 - 1; | |
}; | |
var exact_integer_p = function exact_integer_p(v119) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return Number.isInteger(v119); | |
}; | |
attachProcedureArity(mul, 0); | |
attachProcedureArity(div, 1); | |
attachProcedureArity(add, 0); | |
var __ = attachProcedureArity(sub, 1); | |
var __lt_ = attachProcedureArity(lt, 1); | |
attachProcedureArity(gt, 1); | |
var __lt__eq_ = attachProcedureArity(lte, 1); | |
var __gt__eq_ = attachProcedureArity(gte, 1); | |
attachProcedureArity(equals, 1); | |
var cl28 = function cl28(v129) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return Math.atan(v129); | |
}; | |
var cl29 = function cl29(x130, y131) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return Math.atan2(x130, y131); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam30 = { | |
'1': cl28, | |
'2': cl29 | |
}[arguments.length]; | |
if (fixed_lam30 !== undefined) { | |
return fixed_lam30.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
attachProcedureName(attachProcedureArity(bitwiseAnd, 1), make$1("bitwise-and")); | |
attachProcedureName(attachProcedureArity(bitwiseOr, 1), make$1("bitwise-ior")); | |
attachProcedureName(attachProcedureArity(bitwiseXor, 1), make$1("bitwise-xor")); | |
var not = function not(v159) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return equal_p(v159, false) || false; | |
}; | |
attachProcedureName(make$4, make$1("cons")); | |
attachProcedureName(check$6, make$1("pair?")); | |
var rnull = EMPTY; | |
attachProcedureName(attachProcedureArity(makeList, 0), make$1("list")); | |
var null_p = isEmpty; | |
var list_p = isList; | |
var length = function length(v186) { | |
if (list_p(v186) !== false) { | |
rvoid(); | |
} else { | |
doraise(makeArgumentError(make$2("length"), make$1("list?"), v186)); | |
} | |
return v186.length; | |
}; | |
var for_each = function for_each(lam202) { | |
if (arguments.length < 1 !== false) { | |
throw racketContractError(make$1("arity mismatch")); | |
} else { | |
rvoid(); | |
} | |
values$1(); | |
var lsts203 = Array.prototype.slice.call(arguments, 1); | |
if (procedure_p(lam202) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("Expected:"), make$1("procedure?"), make$1(", given:"), lam202, make$1(", at:"), 0); | |
} | |
map$1.apply(null, [lam202].concat(lsts203)); | |
return null; | |
}; | |
var make_struct_type = function make_struct_type(name213, super_type214, init_field_count215, auto_field_count216, auto_v217, props218, inspector219, proc_spec220, immutables221, guard222, constructor_name223) { | |
return makeStructType({ | |
'name': name213.toString(), | |
'superType': super_type214, | |
'initFieldCount': init_field_count215, | |
'autoFieldCount': auto_field_count216, | |
'autoV': auto_v217, | |
'props': props218, | |
'inspector': inspector219, | |
'procSpec': proc_spec220, | |
'immutables': immutables221, | |
'guard': guard222, | |
'constructorName': constructor_name223 | |
}); | |
}; | |
var make_struct_field_accessor = function make_struct_field_accessor(ref224, index225, field_name226) { | |
return function (s227) { | |
if (arguments.length !== 1 !== false) { | |
throw racketContractError(make$1("arity mismatch")); | |
} else { | |
rvoid(); | |
} | |
return ref224(s227, index225); | |
}; | |
}; | |
var make_struct_type_property = function make_struct_type_property(name233, guard234, supers235, can_impersonate_p236) { | |
return makeStructTypeProperty({ | |
'name': name233, | |
'guard': guard234, | |
'supers': supers235, | |
'canImpersonate': can_impersonate_p236 | |
}); | |
}; | |
var cl110 = function cl110(h276, k277) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (h276.hasKey(k277) !== false) { | |
var if_res113 = h276._h.get(k277); | |
} else { | |
var if_res113 = raise$1(makeArgumentsError(make$1("hash-ref"), make$1("no value found for key"), make$1("key"), k277)); | |
} | |
return if_res113; | |
}; | |
var cl111 = function cl111(h278, k279, fail280) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return h278.ref(k279, fail280); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam112 = { | |
'2': cl110, | |
'3': cl111 | |
}[arguments.length]; | |
if (fixed_lam112 !== undefined) { | |
return fixed_lam112.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [2, 3]); | |
var cl114 = function cl114(h283, k284) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (h283.hasKey(k284) !== false) { | |
var if_res117 = h283.refKey(k284); | |
} else { | |
var if_res117 = raise$1(makeArgumentsError(make$1("hash-ref-key"), make$1("hash does not contain key"), make$1("key"), k284)); | |
} | |
return if_res117; | |
}; | |
var cl115 = function cl115(h285, k286, fail287) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return h285.refKey(k286, fail287); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam116 = { | |
'2': cl114, | |
'3': cl115 | |
}[arguments.length]; | |
if (fixed_lam116 !== undefined) { | |
return fixed_lam116.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [2, 3]); | |
var cl120 = function cl120(h293, proc294) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return map(h293, proc294); | |
}; | |
var cl121 = function cl121(h295, proc296, try_order_p297) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return map(h295, proc296); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam122 = { | |
'2': cl120, | |
'3': cl121 | |
}[arguments.length]; | |
if (fixed_lam122 !== undefined) { | |
return fixed_lam122.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [2, 3]); | |
var apply = function apply(lam320) { | |
values$1(); | |
var args321 = Array.prototype.slice.call(arguments, 1); | |
if (procedure_p(lam320) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("Expected:"), make$1("procedure?"), make$1(", given:"), lam320, make$1(", at:"), 0); | |
} | |
values$1(); | |
if (zero_p(args321.length) !== false) { | |
throw racketContractError(make$1("arity mismatch")); | |
var if_res133 = null; | |
} else { | |
if (equal_p(args321.length, 1) !== false) { | |
if (null_p(args321[0]) !== false) { | |
rvoid(); | |
} else { | |
if (check$6(args321[0]) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("expected a"), pair, make$1(", but given"), args321[0]); | |
} | |
} | |
var if_res132 = listToArray(args321[0]); | |
} else { | |
var if_res132 = args321.concat(listToArray(args321.pop())); | |
} | |
var if_res133 = if_res132; | |
} | |
var final_args322 = if_res133; | |
return lam320.apply(null, final_args322); | |
}; | |
var map$1 = function map$1(fn323) { | |
if (arguments.length < 1 !== false) { | |
throw racketContractError(make$1("arity mismatch")); | |
} else { | |
rvoid(); | |
} | |
values$1(); | |
var lists324 = Array.prototype.slice.call(arguments, 1); | |
if (procedure_p(fn323) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("Expected:"), make$1("procedure?"), make$1(", given:"), fn323, make$1(", at:"), 0); | |
} | |
values$1(); | |
if (__lt__eq_(lists324.length, 0) !== false) { | |
error$1(make$2("map"), make$1("need at-least two arguments")); | |
} else { | |
rvoid(); | |
} | |
values$1(); | |
var lst_len325 = length(lists324[0]); | |
var loop326 = function loop326(i327) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (i327 < lists324.length !== false) { | |
var v328 = lists324[i327]; | |
if (eq_p(length(v328), lst_len325) !== false) { | |
rvoid(); | |
} else { | |
error$1(make$2("map"), make$1("all input lists must have equal length")); | |
} | |
var if_res141 = loop326(i327 + 1); | |
} else { | |
var if_res141 = rvoid(); | |
} | |
return if_res141; | |
}; | |
loop326(1); | |
values$1(); | |
var result329 = Array(lst_len325); | |
var args330 = Array(lists324.length); | |
var loop331 = function loop331(result_i332) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (result_i332 < lst_len325 !== false) { | |
var loop333 = function loop333(lst_j334) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (lst_j334 < lists324.length !== false) { | |
var lst335 = lists324[lst_j334]; | |
args330[lst_j334] = lst335.hd; | |
lists324[lst_j334] = lst335.tl; | |
var if_res143 = loop333(lst_j334 + 1); | |
} else { | |
var if_res143 = rvoid(); | |
} | |
return if_res143; | |
}; | |
loop333(0); | |
result329[result_i332] = fn323.apply(null, args330); | |
var if_res144 = loop331(result_i332 + 1); | |
} else { | |
var if_res144 = rvoid(); | |
} | |
return if_res144; | |
}; | |
loop331(0); | |
return listFromArray(result329); | |
}; | |
var cl166 = function cl166(end364) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return range(0, end364, 1); | |
}; | |
var cl167 = function cl167(start365, end366) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (__lt_(start365, end366) !== false) { | |
var if_res170 = 1; | |
} else { | |
var if_res170 = -1; | |
} | |
return range(start365, end366, if_res170); | |
}; | |
var cl168 = function cl168(start367, end368, step369) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
var result370 = []; | |
if ((__gt__eq_(step369, 0) && __lt_(step369, end368)) !== false) { | |
var loop371 = function loop371(i372) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (i372 < end368 !== false) { | |
result370.push(i372); | |
var if_res171 = loop371(i372 + step369); | |
} else { | |
var if_res171 = rvoid(); | |
} | |
return if_res171; | |
}; | |
loop371(start367); | |
} else { | |
if ((__lt__eq_(step369, 0) && __lt_(end368, start367)) !== false) { | |
var loop373 = function loop373(i374) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (i374 < __(end368) !== false) { | |
result370.push(__(i374)); | |
var if_res172 = loop373(i374 + __(step369)); | |
} else { | |
var if_res172 = rvoid(); | |
} | |
return if_res172; | |
}; | |
loop373(__(start367)); | |
} else { | |
rvoid(); | |
} | |
} | |
return listFromArray(result370); | |
}; | |
var range = attachProcedureArity(function () { | |
var fixed_lam169 = { | |
'1': cl166, | |
'2': cl167, | |
'3': cl168 | |
}[arguments.length]; | |
if (fixed_lam169 !== undefined) { | |
return fixed_lam169.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2, 3]); | |
attachProcedureName(makeMutableFromCharsVarArgs, make$1("string")); | |
attachProcedureName(stringAppend, make$1("string-append")); | |
var string_p = attachProcedureName(check$3, make$1("string?")); | |
var fprintf$1 = attachProcedureArity(function (out443, form444) { | |
for (var _len23 = arguments.length, args445213 = new Array(_len23 > 2 ? _len23 - 2 : 0), _key23 = 2; _key23 < _len23; _key23++) { | |
args445213[_key23 - 2] = arguments[_key23]; | |
} | |
if (arguments.length < 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var args445 = listFromArray(args445213); | |
return apply(fprintf, print_as_expression(), out443, form444, args445); | |
}); | |
attachProcedureArity(function (form446) { | |
for (var _len24 = arguments.length, args447214 = new Array(_len24 > 1 ? _len24 - 1 : 0), _key24 = 1; _key24 < _len24; _key24++) { | |
args447214[_key24 - 1] = arguments[_key24]; | |
} | |
if (arguments.length < 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
var args447 = listFromArray(args447214); | |
return apply(fprintf, print_as_expression(), current_error_port(), form446, args447); | |
}); | |
attachProcedureArity(function (form448) { | |
for (var _len25 = arguments.length, args449215 = new Array(_len25 > 1 ? _len25 - 1 : 0), _key25 = 1; _key25 < _len25; _key25++) { | |
args449215[_key25 - 1] = arguments[_key25]; | |
} | |
if (arguments.length < 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
var args449 = listFromArray(args449215); | |
return apply(fprintf, print_as_expression(), current_output_port(), form448, args449); | |
}); | |
attachProcedureArity(function (form450) { | |
for (var _len26 = arguments.length, args451216 = new Array(_len26 > 1 ? _len26 - 1 : 0), _key26 = 1; _key26 < _len26; _key26++) { | |
args451216[_key26 - 1] = arguments[_key26]; | |
} | |
if (arguments.length < 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
var args451 = listFromArray(args451216); | |
var out452 = open_output_string(); | |
apply(fprintf$1, out452, form450, args451); | |
return get_output_string(out452); | |
}); | |
var make_string453 = function make_string453(k2454, c1455) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var k456 = k2454; | |
var if_res217; | |
{ | |
var if_res217 = c1455; | |
} | |
var c457 = if_res217; | |
return repeatChar(k456, c457); | |
}; | |
var cl218 = function cl218(k458) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return make_string453(k458, charFromCodepoint(0)); | |
}; | |
var cl219 = function cl219(k459, c1460) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return make_string453(k459, c1460); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam220 = { | |
'1': cl218, | |
'2': cl219 | |
}[arguments.length]; | |
if (fixed_lam220 !== undefined) { | |
return fixed_lam220.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var string__gt_immutable_string462 = function string__gt_immutable_string462(s3463) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (eq_p(s3463, unsafe_undefined) !== false) { | |
var if_res221 = string_p; | |
} else { | |
var if_res221 = s3463; | |
} | |
var s464 = if_res221; | |
return stringToImmutableString(s464); | |
}; | |
var cl222 = function cl222() { | |
if (arguments.length !== 0) { | |
throw racketContractError("arity mismatch"); | |
} | |
return string__gt_immutable_string462(unsafe_undefined); | |
}; | |
var cl223 = function cl223(s3465) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return string__gt_immutable_string462(s3465); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam224 = { | |
'0': cl222, | |
'1': cl223 | |
}[arguments.length]; | |
if (fixed_lam224 !== undefined) { | |
return fixed_lam224.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [0, 1]); | |
var string__gt_number470 = function string__gt_number470(s5471, radix4472) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var s473 = s5471; | |
var if_res229; | |
{ | |
var if_res229 = radix4472; | |
} | |
var radix474 = if_res229; | |
var integer_in475 = function integer_in475(lo476, hi477) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return function (v478) { | |
return exact_integer_p(v478) && __gt__eq_(v478, lo476) && __lt__eq_(v478, hi477); | |
}; | |
}; | |
if (string_p(s473) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("Expected:"), make$1("string?"), make$1(", given:"), s473, make$1(", at:"), 0); | |
} | |
if (integer_in475(2, 16)(radix474) !== false) { | |
rvoid(); | |
} else { | |
throw racketContractError(make$1("Expected:"), make$1("(integer-in 2 16)"), make$1(", given:"), radix474, make$1(", at:"), 1); | |
} | |
var result479 = parseInt(s473, radix474); | |
if ((isNaN(result479) || not(s473.isValidInteger(radix474))) !== false) { | |
var if_res232 = false; | |
} else { | |
var if_res232 = result479; | |
} | |
return if_res232; | |
}; | |
var cl233 = function cl233(s480) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return string__gt_number470(s480, 10); | |
}; | |
var cl234 = function cl234(s481, radix4482) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return string__gt_number470(s481, radix4482); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam235 = { | |
'1': cl233, | |
'2': cl234 | |
}[arguments.length]; | |
if (fixed_lam235 !== undefined) { | |
return fixed_lam235.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var substring493 = function substring493(str7494, start8495, end6496) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
var str497 = str7494; | |
var start498 = start8495; | |
var if_res240; | |
{ | |
var if_res240 = end6496; | |
} | |
var end499 = if_res240; | |
if (not(check$3(str497)) !== false) { | |
throw racketContractError(make$1("expected a string")); | |
} else { | |
if (start498 < 0 !== false) { | |
throw racketContractError(make$1("invalid start index")); | |
} else { | |
if ((end499 !== false && (end499 < 0 || end499 > str497.length || end499 < start498)) !== false) { | |
throw racketContractError(make$1("invalid end index")); | |
} else { | |
if (end499 === false !== false) { | |
end499 = str497.length; | |
} else { | |
rvoid(); | |
} | |
} | |
} | |
} | |
return str497.substring(start498, end499); | |
}; | |
var cl245 = function cl245(str500, start501) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return substring493(str500, start501, false); | |
}; | |
var cl246 = function cl246(str502, start503, end6504) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return substring493(str502, start503, end6504); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam247 = { | |
'2': cl245, | |
'3': cl246 | |
}[arguments.length]; | |
if (fixed_lam247 !== undefined) { | |
return fixed_lam247.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [2, 3]); | |
var let_result280 = makeStructTypeProperty({ | |
'name': make$1("prop:evt") | |
}); | |
let_result280.getAt(0); | |
let_result280.getAt(1); | |
make_struct_type_property(make$1("prop:checked-procedure")).getAt(0); | |
make_struct_type_property(make$1("prop:impersonator-of")).getAt(0); | |
make_struct_type_property(make$1("prop:arity-string")).getAt(0); | |
make_struct_type_property(make$1("prop:incomplete-arity")).getAt(0); | |
make_struct_type_property(make$1("prop:method-arity-error")).getAt(0); | |
make_struct_type_property(make$1("prop:exn:srclocs")).getAt(0); | |
make_struct_type_property(make$1("prop:authentic")).getAt(0); | |
make_struct_type_property(make$1("prop:serialize")).getAt(0); | |
make_struct_type_property(make$1("prop:custom-write")).getAt(0); | |
var error$1 = error; | |
var make_bytes553 = function make_bytes553(len10554, b9555) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var len556 = len10554; | |
var if_res281; | |
{ | |
var if_res281 = b9555; | |
} | |
var b557 = if_res281; | |
return make(len556, b557); | |
}; | |
var cl282 = function cl282(len558) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return make_bytes553(len558, 0); | |
}; | |
var cl283 = function cl283(len559, b9560) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return make_bytes553(len559, b9560); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam284 = { | |
'1': cl282, | |
'2': cl283 | |
}[arguments.length]; | |
if (fixed_lam284 !== undefined) { | |
return fixed_lam284.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var make_parameter = makeParameter; | |
var raise$1 = doraise; | |
var current_output_port = make_parameter(standardOutputPort); | |
var current_error_port = make_parameter(standardErrorPort); | |
var current_print = make_parameter(function (p584) { | |
if (void_p(p584) !== false) { | |
var if_res305 = rvoid(); | |
} else { | |
print$1(p584); | |
var if_res305 = newline(); | |
} | |
return if_res305; | |
}); | |
var open_output_string = function open_output_string() { | |
if (arguments.length !== 0) { | |
throw racketContractError("arity mismatch"); | |
} | |
return openOutputString(); | |
}; | |
var get_output_string = function get_output_string(p589) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return getOutputString(p589); | |
}; | |
var print_as_expression = make_parameter(true); | |
var display590 = function display590(datum12591, out11592) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum593 = datum12591; | |
if (eq_p(out11592, unsafe_undefined) !== false) { | |
var if_res306 = current_output_port(); | |
} else { | |
var if_res306 = out11592; | |
} | |
var out594 = if_res306; | |
return display(out594, datum593); | |
}; | |
var cl307 = function cl307(datum595) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return display590(datum595, unsafe_undefined); | |
}; | |
var cl308 = function cl308(datum596, out11597) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return display590(datum596, out11597); | |
}; | |
var display$1 = attachProcedureArity(function () { | |
var fixed_lam309 = { | |
'1': cl307, | |
'2': cl308 | |
}[arguments.length]; | |
if (fixed_lam309 !== undefined) { | |
return fixed_lam309.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var displayln598 = function displayln598(datum14599, out13600) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum601 = datum14599; | |
if (eq_p(out13600, unsafe_undefined) !== false) { | |
var if_res310 = current_output_port(); | |
} else { | |
var if_res310 = out13600; | |
} | |
var out602 = if_res310; | |
display$1(datum601, out602); | |
return newline(out602); | |
}; | |
var cl311 = function cl311(datum603) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return displayln598(datum603, unsafe_undefined); | |
}; | |
var cl312 = function cl312(datum604, out13605) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return displayln598(datum604, out13605); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam313 = { | |
'1': cl311, | |
'2': cl312 | |
}[arguments.length]; | |
if (fixed_lam313 !== undefined) { | |
return fixed_lam313.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var write606 = function write606(datum16607, out15608) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum609 = datum16607; | |
if (eq_p(out15608, unsafe_undefined) !== false) { | |
var if_res314 = current_output_port(); | |
} else { | |
var if_res314 = out15608; | |
} | |
var out610 = if_res314; | |
return write(out610, datum609); | |
}; | |
var cl315 = function cl315(datum611) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return write606(datum611, unsafe_undefined); | |
}; | |
var cl316 = function cl316(datum612, out15613) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return write606(datum612, out15613); | |
}; | |
var write$1 = attachProcedureArity(function () { | |
var fixed_lam317 = { | |
'1': cl315, | |
'2': cl316 | |
}[arguments.length]; | |
if (fixed_lam317 !== undefined) { | |
return fixed_lam317.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var writeln614 = function writeln614(datum18615, out17616) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum617 = datum18615; | |
if (eq_p(out17616, unsafe_undefined) !== false) { | |
var if_res318 = current_output_port(); | |
} else { | |
var if_res318 = out17616; | |
} | |
var out618 = if_res318; | |
write$1(datum617, out618); | |
return newline(out618); | |
}; | |
var cl319 = function cl319(datum619) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return writeln614(datum619, unsafe_undefined); | |
}; | |
var cl320 = function cl320(datum620, out17621) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return writeln614(datum620, out17621); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam321 = { | |
'1': cl319, | |
'2': cl320 | |
}[arguments.length]; | |
if (fixed_lam321 !== undefined) { | |
return fixed_lam321.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var print622 = function print622(datum21623, out19624, quote_depth20625) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum626 = datum21623; | |
if (eq_p(out19624, unsafe_undefined) !== false) { | |
var if_res322 = current_output_port(); | |
} else { | |
var if_res322 = out19624; | |
} | |
var out627 = if_res322; | |
var if_res323; | |
{ | |
var if_res323 = quote_depth20625; | |
} | |
var quote_depth628 = if_res323; | |
return print(out627, datum626, print_as_expression(), quote_depth628); | |
}; | |
var cl324 = function cl324(datum629) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return print622(datum629, unsafe_undefined, 0); | |
}; | |
var cl325 = function cl325(datum630, out631, quote_depth20632) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return print622(datum630, out631, quote_depth20632); | |
}; | |
var cl326 = function cl326(datum633, out19634) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return print622(datum633, out19634, 0); | |
}; | |
var print$1 = attachProcedureArity(function () { | |
var fixed_lam327 = { | |
'1': cl324, | |
'3': cl325, | |
'2': cl326 | |
}[arguments.length]; | |
if (fixed_lam327 !== undefined) { | |
return fixed_lam327.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2, 3]); | |
var println635 = function println635(datum23636, out22637) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
var datum638 = datum23636; | |
if (eq_p(out22637, unsafe_undefined) !== false) { | |
var if_res328 = current_output_port(); | |
} else { | |
var if_res328 = out22637; | |
} | |
var out639 = if_res328; | |
print$1(datum638, out639); | |
return newline(out639); | |
}; | |
var cl329 = function cl329(datum640) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return println635(datum640, unsafe_undefined); | |
}; | |
var cl330 = function cl330(datum641, out22642) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
return println635(datum641, out22642); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam331 = { | |
'1': cl329, | |
'2': cl330 | |
}[arguments.length]; | |
if (fixed_lam331 !== undefined) { | |
return fixed_lam331.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [1, 2]); | |
var newline643 = function newline643(out24644) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (eq_p(out24644, unsafe_undefined) !== false) { | |
var if_res332 = current_output_port(); | |
} else { | |
var if_res332 = out24644; | |
} | |
var out645 = if_res332; | |
return display$1(make$1("\n"), out645); | |
}; | |
var cl333 = function cl333() { | |
if (arguments.length !== 0) { | |
throw racketContractError("arity mismatch"); | |
} | |
return newline643(unsafe_undefined); | |
}; | |
var cl334 = function cl334(out24646) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return newline643(out24646); | |
}; | |
var newline = attachProcedureArity(function () { | |
var fixed_lam335 = { | |
'0': cl333, | |
'1': cl334 | |
}[arguments.length]; | |
if (fixed_lam335 !== undefined) { | |
return fixed_lam335.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [0, 1]); | |
attachProcedureArity(function () { | |
for (var _len27 = arguments.length, _647336 = new Array(_len27), _key27 = 0; _key27 < _len27; _key27++) { | |
_647336[_key27] = arguments[_key27]; | |
} | |
listFromArray(_647336); | |
return false; | |
}); | |
var let_result340 = make_struct_type(make$2("kernel:arity-at-least"), false, 1, 0, false, rnull, false, false, makeList(0), false, make$2("kernel:arity-at-least")); | |
var struct_661 = let_result340.getAt(0); | |
var make_662 = let_result340.getAt(1); | |
var __p663 = let_result340.getAt(2); | |
var __ref664 = let_result340.getAt(3); | |
let_result340.getAt(4); | |
var let_result341 = values$1(struct_661, make_662, __p663, make_struct_field_accessor(__ref664, 0, make$2("value"))); | |
let_result341.getAt(0); | |
let_result341.getAt(1); | |
let_result341.getAt(2); | |
let_result341.getAt(3); | |
var procedure_p = function procedure_p(f666) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
return typeof f666 === "function"; | |
}; | |
var environment_variables_set_bang_692 = function environment_variables_set_bang_692(e26693, n27694, v28695, fail25696) { | |
if (arguments.length !== 4) { | |
throw racketContractError("arity mismatch"); | |
} | |
return rvoid(); | |
}; | |
var cl354 = function cl354(e701, n702, v703) { | |
if (arguments.length !== 3) { | |
throw racketContractError("arity mismatch"); | |
} | |
return environment_variables_set_bang_692(e701, n702, v703, false); | |
}; | |
var cl355 = function cl355(e704, n705, v706, fail25707) { | |
if (arguments.length !== 4) { | |
throw racketContractError("arity mismatch"); | |
} | |
return environment_variables_set_bang_692(e704, n705, v706, fail25707); | |
}; | |
attachProcedureArity(function () { | |
var fixed_lam356 = { | |
'3': cl354, | |
'4': cl355 | |
}[arguments.length]; | |
if (fixed_lam356 !== undefined) { | |
return fixed_lam356.apply(null, arguments); | |
} else { | |
return error$1(make$1("case-lambda: invalid case")); | |
} | |
}, [3, 4]); | |
var print_values = attachProcedureArity(function () { | |
for (var _len28 = arguments.length, vs755360 = new Array(_len28), _key28 = 0; _key28 < _len28; _key28++) { | |
vs755360[_key28] = arguments[_key28]; | |
} | |
var vs755 = listFromArray(vs755360); | |
for_each(current_print(), vs755); | |
return apply(values$1, vs755); | |
}); | |
var __rjs_quoted__ = {}; | |
__rjs_quoted__.print_values = print_values; | |
var factorial = function factorial(n1) { | |
if (arguments.length !== 1) { | |
throw racketContractError("arity mismatch"); | |
} | |
var loop2 = function loop2(n3, a4) { | |
if (arguments.length !== 2) { | |
throw racketContractError("arity mismatch"); | |
} | |
if (zero_p(n3) !== false) { | |
var if_res1 = a4; | |
} else { | |
var if_res1 = loop2(sub1(n3), n3 * a4); | |
} | |
return if_res1; | |
}; | |
return loop2(n1, 1); | |
}; | |
call_with_values(function () { | |
if (arguments.length !== 0) { | |
throw racketContractError("arity mismatch"); | |
} | |
return factorial(6); | |
}, __rjs_quoted__.print_values); | |
var __rjs_quoted__$1 = {}; | |
exports.__rjs_quoted__ = __rjs_quoted__$1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment