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.
This commit is contained in:
Philip Chimento 2025-02-11 15:31:34 -08:00 committed by Philip Chimento
parent e133564cfa
commit 2af27af915
2 changed files with 41 additions and 1 deletions

View File

@ -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);
}

View File

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