Use native private fields
MODIFY
packages/evaluator-shim/src/evaluator.js
packages/evaluator-shim/src/evaluator.js
import { defineProperties } from './commons';
-import { getPrivateFields, setPrivateFields } from './privateFields';
import { createGlobalObject } from './globalObject';
import { performEval } from './evaluate';
import { getCurrentRealmRec } from './realmRec';
@@ -14,22 +13,22 @@ const realmRec = getCurrentRealmRec();
* code in a context bound to a new global creates a new evaluator.
*/
export default class Evaluator {
+ #globalTransforms;
+
+ #globalObject;
+
constructor(options = {}) {
// Extract options, and shallow-clone transforms.
const { transforms = [] } = options;
- const globalTransforms = [...transforms];
-
- const globalObject = createGlobalObject(realmRec, { globalTransforms });
+ this.#globalTransforms = [...transforms];
- setPrivateFields(this, {
- globalObject,
- globalTransforms,
+ this.#globalObject = createGlobalObject(realmRec, {
+ globalTransforms: this.#globalTransforms,
});
}
get global() {
- const { globalObject } = getPrivateFields(this);
- return globalObject;
+ return this.#globalObject;
}
/**
@@ -49,9 +48,8 @@ export default class Evaluator {
const { transforms = [], sloppyGlobalsMode = false } = options;
const localTransforms = [...transforms];
- const { globalTransforms, globalObject } = getPrivateFields(this);
- return performEval(realmRec, x, globalObject, endowments, {
- globalTransforms,
+ return performEval(realmRec, x, this.#globalObject, endowments, {
+ globalTransforms: this.#globalTransforms,
localTransforms,
sloppyGlobalsMode,
});

DELETE
packages/evaluator-shim/src/privateFields.js
-import { assert } from './assertions';
-import { weakmapGet, weakmapHas, weakmapSet } from './commons';
-
-// Module "privateFields"
-// Mimic the private state of a class instance.
-
-// Hidden storage for private fields.
-const privateFields = new WeakMap();
-
-export function setPrivateFields(classInstance, fields) {
- // Attempt to change an existing record of private members on
- // a Class instance: should not proceed.
- assert(
- !weakmapHas(privateFields, classInstance),
- 'private fields already defined',
- );
-
- weakmapSet(privateFields, classInstance, fields);
-}
-
-export function getPrivateFields(classInstance) {
- // Class instance has no private members: should not proceed.
- assert(
- weakmapHas(privateFields, classInstance),
- 'private fields not defined',
- );
-
- return weakmapGet(privateFields, classInstance);
-}

DELETE
packages/evaluator-shim/test/unit/privateFields.test.js
-import test from 'tape';
-import sinon from 'sinon';
-import { getPrivateFields, setPrivateFields } from '../../src/privateFields';
-
-test('privateFields - get/set fields', t => {
- t.plan(1);
-
- const instance = {};
- const fields = {};
- setPrivateFields(instance, fields);
-
- t.equals(getPrivateFields(instance), fields);
-});
-
-test('privateFields - fields not defined', t => {
- t.plan(1);
-
- sinon.stub(console, 'error').callsFake();
-
- const instance = {};
- t.throws(() => getPrivateFields(instance), /not defined/);
-
- sinon.restore();
-});
-
-test('privateFields - fields already defined', t => {
- t.plan(2);
-
- sinon.stub(console, 'error').callsFake();
-
- const instance = {};
- const fields = {};
- t.doesNotThrow(() => setPrivateFields(instance, fields));
- t.throws(() => setPrivateFields(instance, fields), /already defined/);
-
- sinon.restore();
-});

Use native private fields