Skip to content

Commit a058d58

Browse files
committed
fixup! lib: refactor internal webidl converters
1 parent e52fe5f commit a058d58

1 file changed

Lines changed: 19 additions & 30 deletions

File tree

lib/internal/webidl.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const {
44
ArrayBufferIsView,
5-
ArrayBufferPrototypeGetByteLength,
65
ArrayBufferPrototypeGetResizable,
76
ArrayPrototypePush,
87
ArrayPrototypeToSorted,
@@ -812,35 +811,20 @@ function getViewedArrayBuffer(V) {
812811
TypedArrayPrototypeGetBuffer(V) : DataViewPrototypeGetBuffer(V);
813812
}
814813

815-
/**
816-
* Returns whether buffer is a SharedArrayBuffer.
817-
*
818-
* Web IDL specifies this as IsSharedArrayBuffer(buffer). Use the
819-
* ArrayBuffer.prototype.byteLength getter as an equivalent brand check:
820-
* it succeeds only for ArrayBuffers and throws for SharedArrayBuffers.
821-
* This keeps the common ArrayBuffer-backed view path in JS without calling
822-
* the C++ isSharedArrayBuffer binding, while preserving the prototype-chain
823-
* independent behavior required by the spec.
824-
* @param {ArrayBuffer|SharedArrayBuffer} buffer Backing buffer.
825-
* @returns {boolean}
826-
*/
827-
function isSharedArrayBufferBacking(buffer) {
828-
try {
829-
ArrayBufferPrototypeGetByteLength(buffer);
830-
return false;
831-
} catch {
832-
return true;
833-
}
834-
}
835-
836814
/**
837815
* Validates [AllowShared] and [AllowResizable] backing-store constraints.
838816
* @param {ArrayBuffer|SharedArrayBuffer} buffer Backing buffer.
839817
* @param {ConversionOptions} options Conversion options.
840818
* @returns {void}
841819
*/
842820
function validateBufferSourceBacking(buffer, options) {
843-
if (isSharedArrayBufferBacking(buffer)) {
821+
let resizable;
822+
try {
823+
// ArrayBuffer.prototype.resizable is an ArrayBuffer brand check and the
824+
// [AllowResizable] value we need. For SharedArrayBuffer-backed views it
825+
// throws, which lets this path avoid a separate IsSharedArrayBuffer check.
826+
resizable = ArrayBufferPrototypeGetResizable(buffer);
827+
} catch {
844828
// ArrayBufferView conversion step 2: reject SharedArrayBuffer
845829
// backing stores unless [AllowShared] is present.
846830
if (!options.allowShared) {
@@ -853,10 +837,11 @@ function validateBufferSourceBacking(buffer, options) {
853837
validateAllowGrowableSharedArrayBuffer(buffer, options);
854838
return;
855839
}
840+
856841
// ArrayBuffer conversion step 3 and ArrayBufferView conversion step 3:
857842
// reject non-fixed ArrayBuffer backing stores unless [AllowResizable]
858843
// is present.
859-
validateAllowResizableArrayBuffer(buffer, options);
844+
validateAllowResizableArrayBuffer(resizable, options);
860845
}
861846

862847
/**
@@ -882,16 +867,16 @@ function validateAllowGrowableSharedArrayBuffer(buffer, options) {
882867

883868
/**
884869
* Validates the [AllowResizable] constraint for resizable ArrayBuffer.
885-
* @param {ArrayBuffer} buffer ArrayBuffer backing buffer.
870+
* @param {boolean} resizable ArrayBuffer [[ArrayBufferResizable]] value.
886871
* @param {ConversionOptions} options Conversion options.
887872
* @returns {void}
888873
*/
889-
function validateAllowResizableArrayBuffer(buffer, options) {
874+
function validateAllowResizableArrayBuffer(resizable, options) {
890875
// ArrayBuffer and ArrayBufferView conversion step 3:
891876
// IsFixedLengthArrayBuffer(buffer) must be true without [AllowResizable].
892877
// Read [[ArrayBufferResizable]] first so fixed buffers skip the options
893878
// property lookup on this hot path.
894-
if (ArrayBufferPrototypeGetResizable(buffer) && !options.allowResizable) {
879+
if (resizable && !options.allowResizable) {
895880
throw makeException(
896881
'is backed by a resizable ArrayBuffer, which is not allowed.',
897882
options);
@@ -935,9 +920,13 @@ converters.BufferSource = (V, options = kEmptyObject) => {
935920
// and return a reference to the same view.
936921
// Keep this logic inline instead of calling validateBufferSourceBacking().
937922
// BufferSource conversion is hot, and this avoids a helper call while still
938-
// using primordial getters for the backing-buffer internal-slot checks.
923+
// using a primordial getter for the backing-buffer internal-slot check.
924+
let resizable;
939925
try {
940-
ArrayBufferPrototypeGetByteLength(buffer);
926+
// ArrayBuffer.prototype.resizable is both the ArrayBuffer brand check
927+
// and the step 3 value. It throws for SharedArrayBuffer, which gives us
928+
// the step 2 branch without an extra byteLength getter call.
929+
resizable = ArrayBufferPrototypeGetResizable(buffer);
941930
} catch {
942931
if (!options.allowShared) {
943932
throw makeException(
@@ -947,7 +936,7 @@ converters.BufferSource = (V, options = kEmptyObject) => {
947936
validateAllowGrowableSharedArrayBuffer(buffer, options);
948937
return V;
949938
}
950-
if (ArrayBufferPrototypeGetResizable(buffer) && !options.allowResizable) {
939+
if (resizable && !options.allowResizable) {
951940
throw makeException(
952941
'is backed by a resizable ArrayBuffer, which is not allowed.',
953942
options);

0 commit comments

Comments
 (0)