fix(ses): Number.prototype.toLocaleString radix confusion
Fixes
MODIFY
packages/ses/src/commons.js
packages/ses/src/commons.js
@@ -24,6 +24,7 @@ export const {
JSON,
Map,
Math,
+ Number,
Object,
Promise,
Proxy,

MODIFY
packages/ses/src/tame-locale-methods.js
packages/ses/src/tame-locale-methods.js
import {
+ Number,
String,
TypeError,
defineProperty,
@@ -33,9 +34,14 @@ const tamedMethods = {
assert(s === that, d`expected ${q(s)} and ${q(that)} to compare`);
return 0;
},
+
+ toString() {
+ return `${this}`;
+ },
};
const nonLocaleCompare = tamedMethods.localeCompare;
+const numberToString = tamedMethods.toString;
export default function tameLocaleMethods(intrinsics, localeTaming = 'safe') {
if (localeTaming !== 'safe' && localeTaming !== 'unsafe') {
@@ -70,4 +76,10 @@ export default function tameLocaleMethods(intrinsics, localeTaming = 'safe') {
}
}
}
+
+ // Numbers are special because toString accepts a radix instead of ignoring
+ // all of the arguments that we would otherwise forward.
+ defineProperty(Number.prototype, 'toLocaleString', {
+ value: numberToString,
+ });
}

MODIFY
packages/ses/test/test-tame-locale-methods.js
packages/ses/test/test-tame-locale-methods.js
@@ -5,7 +5,6 @@ lockdown();
test('tame locale methods', t => {
t.is(Object.prototype.toString, Object.prototype.toLocaleString);
- t.is(Number.prototype.toString, Number.prototype.toLocaleString);
t.is(BigInt.prototype.toString, BigInt.prototype.toLocaleString);
t.is(Date.prototype.toDateString, Date.prototype.toLocaleDateString);
t.is(Date.prototype.toString, Date.prototype.toLocaleString);
@@ -20,3 +19,7 @@ test('tame locale methods', t => {
t.is(typeof String.prototype.localeCompare, 'function');
t.not(`${String.prototype.localeCompare}`.includes('[native code]'));
});
+
+test('carefully tame Number#toLocaleString', t => {
+ t.is((37).toLocaleString('en'), '37');
+});

fix(ses): Number.prototype.toLocaleString radix confusion (#975)

Fixes #852