From 2af27af9158d1c7173b001dfd87b111a7990b08b Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Tue, 11 Feb 2025 15:31:34 -0800 Subject: [PATCH] harness: Add a function to get well-known intrinsic objects wellKnownIntrinsicObjects.js now exposes a getWellKnownIntrinsicObject() function which returns the object corresponding to a key like %Array%. If the object is not provided by the implementation, or not accessible, it throws a Test262Error. This is so that tests depending on that intrinsic object can easily fail. --- harness/wellKnownIntrinsicObjects.js | 21 ++++++++++++++++++++- test/harness/wellKnownIntrinsicObjects.js | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 test/harness/wellKnownIntrinsicObjects.js diff --git a/harness/wellKnownIntrinsicObjects.js b/harness/wellKnownIntrinsicObjects.js index 6e598180d2..4ad1824788 100644 --- a/harness/wellKnownIntrinsicObjects.js +++ b/harness/wellKnownIntrinsicObjects.js @@ -3,7 +3,7 @@ /*--- description: | An Array of all representable Well-Known Intrinsic Objects -defines: [WellKnownIntrinsicObjects] +defines: [WellKnownIntrinsicObjects, getWellKnownIntrinsicObject] ---*/ const WellKnownIntrinsicObjects = [ @@ -306,3 +306,22 @@ WellKnownIntrinsicObjects.forEach((wkio) => { wkio.value = actual; }); + +/** + * Returns a well-known intrinsic object, if the implementation provides it. + * Otherwise, throws. + * @param {string} key - the specification's name for the intrinsic, for example + * "%Array%" + * @returns {object} the well-known intrinsic object. + */ +function getWellKnownIntrinsicObject(key) { + for (var ix = 0; ix < WellKnownIntrinsicObjects.length; ix++) { + if (WellKnownIntrinsicObjects[ix].name === key) { + var value = WellKnownIntrinsicObjects[ix].value; + if (value !== undefined) + return value; + throw new Test262Error('this implementation could not obtain ' + key); + } + } + throw new Test262Error('unknown well-known intrinsic ' + key); +} diff --git a/test/harness/wellKnownIntrinsicObjects.js b/test/harness/wellKnownIntrinsicObjects.js new file mode 100644 index 0000000000..0a1db0e176 --- /dev/null +++ b/test/harness/wellKnownIntrinsicObjects.js @@ -0,0 +1,21 @@ +// Copyright (C) 2025 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: Basic tests for getWellKnownIntrinsicObject harness function +includes: [wellKnownIntrinsicObjects.js] +---*/ + +// Accessible in every implementation +var intrinsicArray = getWellKnownIntrinsicObject('%Array%'); +assert(Object.is(Array, intrinsicArray)); + +assert.throws(Test262Error, function () { + // Exists but is not accessible in any implementation + getWellKnownIntrinsicObject('%AsyncFromSyncIteratorPrototype%'); +}); + +assert.throws(Test262Error, function () { + // Does not exist in any implementation + getWellKnownIntrinsicObject('%NotSoWellKnownIntrinsicObject%'); +});