Cleanup taming modules
MODIFY
packages/tame-global-date-object/src/main.js
packages/tame-global-date-object/src/main.js
const { defineProperties, getOwnPropertyDescriptors } = Object;
export default function tameGlobalDateObject() {
- // Capture the original constructor.
- const unsafeDate = Date; // TODO freeze
-
// Tame the %Date% and %DatePrototype% intrinsic.
- const { now } = {
+
+ // Use a concise method to obtain a named function without constructor.
+ const DateStatic = {
now() {
return NaN;
},
};
- unsafeDate.now = now;
+ Date.now = DateStatic.now;
- const { toLocaleString: toLocaleString1 } = {
+ // Use a concise method to obtain a named function without constructor.
+ const DatePrototype = {
toLocaleString() {
return NaN;
},
};
- unsafeDate.prototype.toLocaleString = toLocaleString1;
+ // eslint-disable-next-line no-extend-native
+ Date.prototype.toLocaleString = DatePrototype.toLocaleString;
// Date(anything) gives a string with the current time
// new Date(x) coerces x into a number and then returns a Date
// new Date() returns the current time, as a Date object
// new Date(undefined) returns a Date object which stringifies to 'Invalid Date'
+ // Capture the original constructor.
+ const unsafeDate = Date; // TODO freeze
+
// Tame the Date constructor.
- const safeDate = function Date() {
+ const tamedDate = function Date() {
if (new.target === undefined) {
// We were not called as a constructor
// this would normally return a string with the current time
@@ -44,23 +48,25 @@ export default function tameGlobalDateObject() {
// Copy static properties.
const dateDescs = getOwnPropertyDescriptors(unsafeDate);
- defineProperties(safeDate, dateDescs);
+ defineProperties(tamedDate, dateDescs);
// Copy prototype properties.
const datePrototypeDescs = getOwnPropertyDescriptors(unsafeDate.prototype);
- datePrototypeDescs.constructor.value = safeDate;
- defineProperties(safeDate.prototype, datePrototypeDescs);
+ datePrototypeDescs.constructor.value = tamedDate;
+ defineProperties(tamedDate.prototype, datePrototypeDescs);
- // Done with Date
- globalThis.Date = safeDate;
+ // Done with Date constructor
+ globalThis.Date = tamedDate;
// Tame the %ObjectPrototype% intrinsic.
- const { toLocaleString: toLocaleString2 } = {
+
+ // Use a concise method to obtain a named function without constructor.
+ const ObjectPrototype = {
toLocaleString() {
- throw new Error('Object.prototype.toLocaleString is suppressed');
+ throw new TypeError('Object.prototype.toLocaleString is disabled');
},
};
// eslint-disable-next-line no-extend-native
- Object.prototype.toLocaleString = toLocaleString2;
+ Object.prototype.toLocaleString = ObjectPrototype.toLocaleString;
}

MODIFY
packages/tame-global-date-object/test/tameGlobalDateObject.test.js
packages/tame-global-date-object/test/tameGlobalDateObject.test.js
@@ -10,25 +10,27 @@ test('tameGlobalDateObject - constructor without argument', t => {
const restore = captureGlobals('Date');
tameGlobalDateObject();
+ t.equal(Date.name, 'Date');
+
const date = new Date();
t.ok(date instanceof Date);
- t.equal(date.toString(), 'Invalid Date');
+ // eslint-disable-next-line no-proto
+ t.equal(date.__proto__.constructor, Date);
- const date2 = new Date.prototype.constructor();
-
- t.ok(date2 instanceof Date);
- t.equal(date2.toString(), 'Invalid Date');
+ t.equal(date.toString(), 'Invalid Date');
restore();
});
test('tameGlobalDateObject - now', t => {
- t.plan(1);
+ t.plan(2);
const restore = captureGlobals('Date');
tameGlobalDateObject();
+ t.equal(Date.now.name, 'now');
+
const date = Date.now();
t.ok(Number.isNaN(date));
@@ -36,16 +38,19 @@ test('tameGlobalDateObject - now', t => {
restore();
});
-test('tameGlobalIntlObject - toLocaleString', { skip: true }, t => {
- t.plan(1);
+test('tameGlobalObject - toLocaleString', t => {
+ t.plan(4);
const restore = captureGlobals('Error');
tameGlobalDateObject();
+ t.equal(Date.prototype.toLocaleString.name, 'toLocaleString');
+ t.equal(Object.prototype.toLocaleString.name, 'toLocaleString');
+
const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
- t.throws(() => date.toLocaleString(), 'suppressed');
- t.throws(() => Object.prototype.toLocaleString.apply(date), 'suppressed');
+ t.ok(Number.isNaN(date.toLocaleString()));
+ t.throws(() => Object.prototype.toLocaleString.apply(date), TypeError);
restore();
});

MODIFY
packages/tame-global-math-object/src/main.js
packages/tame-global-math-object/src/main.js
export default function tameGlobalMathObject() {
// Tame the %Math% intrinsic.
- const { random } = {
+
+ // Use a concise method to obtain a named function without constructor.
+ const MathStatic = {
random() {
- throw Error('disabled');
+ throw TypeError('Math.random() is disabled');
},
};
- Math.random = random;
+ Math.random = MathStatic.random;
}

MODIFY
packages/tame-global-math-object/test/tameGlobalMathObject.test.js
packages/tame-global-math-object/test/tameGlobalMathObject.test.js
@@ -5,12 +5,14 @@ import tameGlobalMathObject from '../src/main.js';
const { test } = tap;
test('tameGlobalMathObject - tamed properties', t => {
- t.plan(1);
+ t.plan(2);
const restore = captureGlobals('Math');
tameGlobalMathObject();
- t.throws(() => Math.random(), 'disabled');
+ t.equal(Math.random.name, 'random');
+
+ t.throws(() => Math.random(), TypeError);
restore();
});

MODIFY
packages/tame-global-regexp-object/src/main.js
packages/tame-global-regexp-object/src/main.js
@@ -6,28 +6,29 @@ const {
} = Object;
export default function tameGlobalRegExpObject() {
+ // Tame the %RegExp% intrinsic.
+
+ delete RegExp.prototype.compile;
+
// Capture the original constructor.
const unsafeRegExp = RegExp; // TODO freeze
// RegExp has non-writable static properties we need to remove.
-
// Tame RegExp constructor.
- const safeRegExp = function RegExp() {
+ const tamedRegExp = function RegExp() {
// eslint-disable-next-line prefer-rest-params
return Reflect.construct(unsafeRegExp, arguments, new.target);
};
// Whitelist static properties.
const desc = getOwnPropertyDescriptor(unsafeRegExp, Symbol.species);
- defineProperties(safeRegExp, Symbol.species, desc);
+ defineProperties(tamedRegExp, Symbol.species, desc);
// Copy prototype properties.
const prototypeDescs = getOwnPropertyDescriptors(unsafeRegExp.prototype);
- prototypeDescs.constructor.value = safeRegExp;
- defineProperties(safeRegExp.prototype, prototypeDescs);
-
- globalThis.RegExp = safeRegExp;
+ prototypeDescs.constructor.value = tamedRegExp;
+ defineProperties(tamedRegExp.prototype, prototypeDescs);
- delete safeRegExp.prototype.compile;
- delete unsafeRegExp.prototype.compile;
+ // Done with RegExp constructor.
+ globalThis.RegExp = tamedRegExp;
}

MODIFY
packages/tame-global-regexp-object/test/tameGlobalRegExpObject.test.js
packages/tame-global-regexp-object/test/tameGlobalRegExpObject.test.js
@@ -5,30 +5,29 @@ import tameGlobalRegExpObject from '../src/main.js';
const { test } = tap;
test('tameGlobalRegExpObject - constructor', t => {
- t.plan(4);
+ t.plan(6);
const restore = captureGlobals('RegExp');
tameGlobalRegExpObject();
- const properties = Reflect.ownKeys(RegExp);
- const properties2 = Reflect.ownKeys(RegExp.prototype.constructor);
+ t.equal(RegExp.name, 'RegExp');
+ t.equal(RegExp.prototype.constructor, RegExp);
+ t.equal(
+ Object.getOwnPropertyDescriptor(RegExp.prototype, 'compile'),
+ undefined,
+ );
+ const properties = Reflect.ownKeys(RegExp);
t.deepEqual(
properties.sort(),
['length', 'name', 'prototype'].sort(),
'RegExp should not have static properties',
);
- t.deepEqual(
- properties2.sort(),
- ['length', 'name', 'prototype'].sort(),
- 'RegExp should not have static properties',
- );
const regexp = new RegExp();
- const regexp2 = new RegExp.prototype.constructor();
-
t.ok(regexp instanceof RegExp);
- t.ok(regexp2 instanceof RegExp);
+ // eslint-disable-next-line no-proto
+ t.equal(regexp.__proto__.constructor, RegExp);
restore();
});

Cleanup taming modules