Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions THIRD-PARTY-NOTICES.txt

This file was deleted.

2 changes: 0 additions & 2 deletions runtime/JavaScript/src/antlr4/CharStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/

import Token from './Token.js';
import './polyfills/codepointat.js';
Copy link
Contributor

@ericvergnaud ericvergnaud Mar 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does removing this relate to the PR ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those polyfills are modifying the String.prototype if you look at the code.

For example here:

https://github.com/antlr/antlr4/blob/master/runtime/JavaScript/src/antlr4/polyfills/codepointat.js#L58

I did check that these polyfills are no longer needed, we've come a long way since they were added around 6 years ago! Now those functions that were polyfilled are available everywhere.

https://caniuse.com/mdn-javascript_builtins_string_codepointat
https://caniuse.com/mdn-javascript_builtins_string_fromcodepoint

import './polyfills/fromcodepoint.js';

/**
* If decodeToUnicodeCodePoints is true, the input is treated
Expand Down
6 changes: 0 additions & 6 deletions runtime/JavaScript/src/antlr4/index.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
6 changes: 0 additions & 6 deletions runtime/JavaScript/src/antlr4/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
4 changes: 3 additions & 1 deletion runtime/JavaScript/src/antlr4/misc/HashCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -27,7 +29,7 @@ export default class HashCode {
k = value;
break;
case 'string':
k = value.hashCode();
k = stringHashCode(value);
break;
default:
if(value.updateHashCode)
Expand Down
64 changes: 0 additions & 64 deletions runtime/JavaScript/src/antlr4/polyfills/codepointat.js

This file was deleted.

72 changes: 0 additions & 72 deletions runtime/JavaScript/src/antlr4/polyfills/fromcodepoint.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 10 additions & 7 deletions runtime/JavaScript/src/antlr4/utils/stringHashCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,5 +68,3 @@ export default function StringHashCode () {

return h1 >>> 0;
}

String.prototype.hashCode = StringHashCode;