diff --git a/THIRD-PARTY-NOTICES.txt b/THIRD-PARTY-NOTICES.txt deleted file mode 100644 index defb29eba5..0000000000 --- a/THIRD-PARTY-NOTICES.txt +++ /dev/null @@ -1,26 +0,0 @@ -"antlr4" uses third-party libraries or other resources that may be distributed under licenses different than "antlr4". - -1. String.prototype.codePointAt (https://github.com/mathiasbynens/String.prototype.codePointAt) - -%% License notice for String.prototype.codePointAt -================================================== -Copyright Mathias Bynens - -Permission is hereby granted, free of charge, to any person obtaining -a copy of this software and associated documentation files (the -"Software"), to deal in the Software without restriction, including -without limitation the rights to use, copy, modify, merge, publish, -distribute, sublicense, and/or sell copies of the Software, and to -permit persons to whom the Software is furnished to do so, subject to -the following conditions: - -The above copyright notice and this permission notice shall be -included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/runtime/JavaScript/src/antlr4/CharStream.js b/runtime/JavaScript/src/antlr4/CharStream.js index 408244e6c6..718aa2d961 100644 --- a/runtime/JavaScript/src/antlr4/CharStream.js +++ b/runtime/JavaScript/src/antlr4/CharStream.js @@ -4,8 +4,6 @@ */ import Token from './Token.js'; -import './polyfills/codepointat.js'; -import './polyfills/fromcodepoint.js'; /** * If decodeToUnicodeCodePoints is true, the input is treated diff --git a/runtime/JavaScript/src/antlr4/index.node.js b/runtime/JavaScript/src/antlr4/index.node.js index 8566e178bd..6f22b4ad6c 100644 --- a/runtime/JavaScript/src/antlr4/index.node.js +++ b/runtime/JavaScript/src/antlr4/index.node.js @@ -58,9 +58,3 @@ export { RecognitionException, NoViableAltException, FailedPredicateException, ErrorListener, DiagnosticErrorListener, BailErrorStrategy, arrayToString } - -/* eslint no-unused-vars: [ "off"] */ -// need to import unused to force loading -import StringHashCode from './utils/stringHashCode.js'; -import CodePointAt from './polyfills/codepointat.js'; -import FromCodePoint from './polyfills/fromcodepoint.js'; diff --git a/runtime/JavaScript/src/antlr4/index.web.js b/runtime/JavaScript/src/antlr4/index.web.js index c7906e0e16..0429a6689f 100644 --- a/runtime/JavaScript/src/antlr4/index.web.js +++ b/runtime/JavaScript/src/antlr4/index.web.js @@ -57,9 +57,3 @@ export { RecognitionException, NoViableAltException, FailedPredicateException, ErrorListener, DiagnosticErrorListener, BailErrorStrategy, arrayToString } - -/* eslint no-unused-vars: [ "off"] */ -// need to import unused to force loading -import StringHashCode from './utils/stringHashCode.js'; -import CodePointAt from './polyfills/codepointat.js'; -import FromCodePoint from './polyfills/fromcodepoint.js'; diff --git a/runtime/JavaScript/src/antlr4/misc/HashCode.js b/runtime/JavaScript/src/antlr4/misc/HashCode.js index 4264f6591e..1de3c69529 100644 --- a/runtime/JavaScript/src/antlr4/misc/HashCode.js +++ b/runtime/JavaScript/src/antlr4/misc/HashCode.js @@ -2,6 +2,8 @@ * Use is of this file is governed by the BSD 3-clause license that * can be found in the LICENSE.txt file in the project root. */ +import { stringHashCode } from "../utils/stringHashCode.js"; + export default class HashCode { constructor() { @@ -27,7 +29,7 @@ export default class HashCode { k = value; break; case 'string': - k = value.hashCode(); + k = stringHashCode(value); break; default: if(value.updateHashCode) diff --git a/runtime/JavaScript/src/antlr4/polyfills/codepointat.js b/runtime/JavaScript/src/antlr4/polyfills/codepointat.js deleted file mode 100644 index 45a886c18d..0000000000 --- a/runtime/JavaScript/src/antlr4/polyfills/codepointat.js +++ /dev/null @@ -1,64 +0,0 @@ -/* Copyright (c) 2012-2022 The ANTLR Project Contributors. All rights reserved. - * Use is of this file is governed by the BSD 3-clause license that - * can be found in the LICENSE.txt file in the project root. - */ -/*! https://mths.be/codepointat v0.2.0 by @mathias */ -if (!String.prototype.codePointAt) { - (function() { - 'use strict'; // needed to support `apply`/`call` with `undefined`/`null` - var defineProperty = (function() { - // IE 8 only supports `Object.defineProperty` on DOM elements - let result; - try { - const object = {}; - const $defineProperty = Object.defineProperty; - result = $defineProperty(object, object, object) && $defineProperty; - } catch(error) { - /* eslint no-empty: [ "off" ] */ - } - return result; - }()); - const codePointAt = function(position) { - if (this == null) { - throw TypeError(); - } - const string = String(this); - const size = string.length; - // `ToInteger` - let index = position ? Number(position) : 0; - if (index !== index) { // better `isNaN` - index = 0; - } - // Account for out-of-bounds indices: - if (index < 0 || index >= size) { - return undefined; - } - // Get the first code unit - const first = string.charCodeAt(index); - let second; - if ( // check if it’s the start of a surrogate pair - first >= 0xD800 && first <= 0xDBFF && // high surrogate - size > index + 1 // there is a next code unit - ) { - second = string.charCodeAt(index + 1); - if (second >= 0xDC00 && second <= 0xDFFF) { // low surrogate - // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae - return (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000; - } - } - return first; - }; - if (defineProperty) { - defineProperty(String.prototype, 'codePointAt', { - 'value': codePointAt, - 'configurable': true, - 'writable': true - }); - } else { - String.prototype.codePointAt = codePointAt; - } - }()); -} - -const CodePointAt = String.prototype.codePointAt; -export default CodePointAt; diff --git a/runtime/JavaScript/src/antlr4/polyfills/fromcodepoint.js b/runtime/JavaScript/src/antlr4/polyfills/fromcodepoint.js deleted file mode 100644 index c85b417079..0000000000 --- a/runtime/JavaScript/src/antlr4/polyfills/fromcodepoint.js +++ /dev/null @@ -1,72 +0,0 @@ -/* Copyright (c) 2012-2022 The ANTLR Project Contributors. All rights reserved. - * Use is of this file is governed by the BSD 3-clause license that - * can be found in the LICENSE.txt file in the project root. - */ -/*! https://mths.be/fromcodepoint v0.2.1 by @mathias */ -if (!String.fromCodePoint) { - (function() { - const defineProperty = (function() { - // IE 8 only supports `Object.defineProperty` on DOM elements - let result; - try { - const object = {}; - const $defineProperty = Object.defineProperty; - result = $defineProperty(object, object, object) && $defineProperty; - } catch(error) { - /* eslint no-empty: [ "off" ] */ - } - return result; - }()); - const stringFromCharCode = String.fromCharCode; - const floor = Math.floor; - const fromCodePoint = function(_) { - const MAX_SIZE = 0x4000; - const codeUnits = []; - let highSurrogate; - let lowSurrogate; - let index = -1; - const length = arguments.length; - if (!length) { - return ''; - } - let result = ''; - while (++index < length) { - let codePoint = Number(arguments[index]); - if ( - !isFinite(codePoint) || // `NaN`, `+Infinity`, or `-Infinity` - codePoint < 0 || // not a valid Unicode code point - codePoint > 0x10FFFF || // not a valid Unicode code point - floor(codePoint) !== codePoint // not an integer - ) { - throw RangeError('Invalid code point: ' + codePoint); - } - if (codePoint <= 0xFFFF) { // BMP code point - codeUnits.push(codePoint); - } else { // Astral code point; split in surrogate halves - // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae - codePoint -= 0x10000; - highSurrogate = (codePoint >> 10) + 0xD800; - lowSurrogate = (codePoint % 0x400) + 0xDC00; - codeUnits.push(highSurrogate, lowSurrogate); - } - if (index + 1 === length || codeUnits.length > MAX_SIZE) { - result += stringFromCharCode.apply(null, codeUnits); - codeUnits.length = 0; - } - } - return result; - }; - if (defineProperty) { - defineProperty(String, 'fromCodePoint', { - 'value': fromCodePoint, - 'configurable': true, - 'writable': true - }); - } else { - String.fromCodePoint = fromCodePoint; - } - }()); -} - -const FromCodePoint = String.prototype.fromCodePoint; -export default FromCodePoint; diff --git a/runtime/JavaScript/src/antlr4/utils/standardHashCodeFunction.js b/runtime/JavaScript/src/antlr4/utils/standardHashCodeFunction.js index eb54993862..7edbdb373f 100644 --- a/runtime/JavaScript/src/antlr4/utils/standardHashCodeFunction.js +++ b/runtime/JavaScript/src/antlr4/utils/standardHashCodeFunction.js @@ -2,6 +2,8 @@ * Use is of this file is governed by the BSD 3-clause license that * can be found in the LICENSE.txt file in the project root. */ +import { stringHashCode } from "./stringHashCode.js"; + export default function standardHashCodeFunction(a) { - return a ? a.hashCode() : -1; + return a ? typeof a === 'string' ? stringHashCode(a) : a.hashCode() : -1; } diff --git a/runtime/JavaScript/src/antlr4/utils/stringHashCode.js b/runtime/JavaScript/src/antlr4/utils/stringHashCode.js index c9dc0de054..d646363e73 100644 --- a/runtime/JavaScript/src/antlr4/utils/stringHashCode.js +++ b/runtime/JavaScript/src/antlr4/utils/stringHashCode.js @@ -5,15 +5,20 @@ export const StringSeedHashCode = Math.round(Math.random() * Math.pow(2, 32)); -String.prototype.seed = StringSeedHashCode; - -export default function StringHashCode () { - const key = this.toString(); +export function stringHashCode (value) { + if (!value) { + return 0; + } + const type = typeof value; + const key = type === 'string' ? value : type === 'object' && value.toString ? value.toString() : false; + if (!key) { + return 0; + } let h1b, k1; const remainder = key.length & 3; // key.length % 4 const bytes = key.length - remainder; - let h1 = String.prototype.seed; + let h1 = StringSeedHashCode; const c1 = 0xcc9e2d51; const c2 = 0x1b873593; let i = 0; @@ -63,5 +68,3 @@ export default function StringHashCode () { return h1 >>> 0; } - -String.prototype.hashCode = StringHashCode;