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%'); +});