Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 156 additions & 31 deletions lib/web/webidl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ webidl.util.Stringify = function (V) {
}
}

webidl.util.IsResizableArrayBuffer = function (V) {
if (types.isArrayBuffer(V)) {
return V.resizable
}

if (types.isSharedArrayBuffer(V)) {
return V.growable
}

throw webidl.errors.exception({
header: 'IsResizableArrayBuffer',
message: `"${webidl.util.Stringify(V)}" is not an array buffer.`
})
}

// https://webidl.spec.whatwg.org/#es-sequence
webidl.sequenceConverter = function (converter) {
return (V, prefix, argument, Iterable) => {
Expand Down Expand Up @@ -638,14 +653,15 @@ webidl.converters['unsigned short'] = function (V, prefix, argument, opts) {

// https://webidl.spec.whatwg.org/#idl-ArrayBuffer
webidl.converters.ArrayBuffer = function (V, prefix, argument, opts) {
// 1. If Type(V) is not Object, or V does not have an
// 1. If V is not an Object, or V does not have an
// [[ArrayBufferData]] internal slot, then throw a
// TypeError.
// 2. If IsSharedArrayBuffer(V) is true, then throw a
// TypeError.
// see: https://tc39.es/ecma262/#sec-properties-of-the-arraybuffer-instances
// see: https://tc39.es/ecma262/#sec-properties-of-the-sharedarraybuffer-instances
if (
webidl.util.Type(V) !== OBJECT ||
!types.isAnyArrayBuffer(V)
!types.isArrayBuffer(V)
) {
throw webidl.errors.conversionFailed({
prefix,
Expand All @@ -654,34 +670,59 @@ webidl.converters.ArrayBuffer = function (V, prefix, argument, opts) {
})
}

// 2. If the conversion is not to an IDL type associated
// with the [AllowShared] extended attribute, and
// IsSharedArrayBuffer(V) is true, then throw a
// 3. If the conversion is not to an IDL type associated
// with the [AllowResizable] extended attribute, and
// IsResizableArrayBuffer(V) is true, then throw a
// TypeError.
if (opts?.allowShared === false && types.isSharedArrayBuffer(V)) {
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'SharedArrayBuffer is not allowed.'
header: prefix,
message: `${argument} cannot be a resizable ArrayBuffer.`
})
}

// 4. Return the IDL ArrayBuffer value that is a
// reference to the same object as V.
return V
}

// https://webidl.spec.whatwg.org/#idl-SharedArrayBuffer
webidl.converters.SharedArrayBuffer = function (V, prefix, argument, opts) {
// 1. If V is not an Object, or V does not have an
// [[ArrayBufferData]] internal slot, then throw a
// TypeError.
// 2. If IsSharedArrayBuffer(V) is false, then throw a
// TypeError.
// see: https://tc39.es/ecma262/#sec-properties-of-the-sharedarraybuffer-instances
if (
webidl.util.Type(V) !== OBJECT ||
!types.isSharedArrayBuffer(V)
) {
throw webidl.errors.conversionFailed({
prefix,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: ['SharedArrayBuffer']
})
}

// 3. If the conversion is not to an IDL type associated
// with the [AllowResizable] extended attribute, and
// IsResizableArrayBuffer(V) is true, then throw a
// TypeError.
if (V.resizable || V.growable) {
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'Received a resizable ArrayBuffer.'
header: prefix,
message: `${argument} cannot be a resizable SharedArrayBuffer.`
})
}

// 4. Return the IDL ArrayBuffer value that is a
// 4. Return the IDL SharedArrayBuffer value that is a
// reference to the same object as V.
return V
}

webidl.converters.TypedArray = function (V, T, prefix, name, opts) {
// https://webidl.spec.whatwg.org/#dfn-typed-array-type
webidl.converters.TypedArray = function (V, T, prefix, argument, opts) {
// 1. Let T be the IDL type V is being converted to.

// 2. If Type(V) is not Object, or V does not have a
Expand All @@ -694,7 +735,7 @@ webidl.converters.TypedArray = function (V, T, prefix, name, opts) {
) {
throw webidl.errors.conversionFailed({
prefix,
argument: `${name} ("${webidl.util.Stringify(V)}")`,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: [T.name]
})
}
Expand All @@ -703,21 +744,21 @@ webidl.converters.TypedArray = function (V, T, prefix, name, opts) {
// with the [AllowShared] extended attribute, and
// IsSharedArrayBuffer(V.[[ViewedArrayBuffer]]) is
// true, then throw a TypeError.
if (opts?.allowShared === false && types.isSharedArrayBuffer(V.buffer)) {
if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'SharedArrayBuffer is not allowed.'
header: prefix,
message: `${argument} cannot be a view on a shared array buffer.`
})
}

// 4. If the conversion is not to an IDL type associated
// with the [AllowResizable] extended attribute, and
// IsResizableArrayBuffer(V.[[ViewedArrayBuffer]]) is
// true, then throw a TypeError.
if (V.buffer.resizable || V.buffer.growable) {
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'Received a resizable ArrayBuffer.'
header: prefix,
message: `${argument} cannot be a view on a resizable array buffer.`
})
}

Expand All @@ -726,35 +767,37 @@ webidl.converters.TypedArray = function (V, T, prefix, name, opts) {
return V
}

webidl.converters.DataView = function (V, prefix, name, opts) {
// https://webidl.spec.whatwg.org/#idl-DataView
webidl.converters.DataView = function (V, prefix, argument, opts) {
// 1. If Type(V) is not Object, or V does not have a
// [[DataView]] internal slot, then throw a TypeError.
if (webidl.util.Type(V) !== OBJECT || !types.isDataView(V)) {
throw webidl.errors.exception({
header: prefix,
message: `${name} is not a DataView.`
throw webidl.errors.conversionFailed({
prefix,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: ['DataView']
})
}

// 2. If the conversion is not to an IDL type associated
// with the [AllowShared] extended attribute, and
// IsSharedArrayBuffer(V.[[ViewedArrayBuffer]]) is true,
// then throw a TypeError.
if (opts?.allowShared === false && types.isSharedArrayBuffer(V.buffer)) {
if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'SharedArrayBuffer is not allowed.'
header: prefix,
message: `${argument} cannot be a view on a shared array buffer.`
})
}

// 3. If the conversion is not to an IDL type associated
// with the [AllowResizable] extended attribute, and
// IsResizableArrayBuffer(V.[[ViewedArrayBuffer]]) is
// true, then throw a TypeError.
if (V.buffer.resizable || V.buffer.growable) {
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: 'ArrayBuffer',
message: 'Received a resizable ArrayBuffer.'
header: prefix,
message: `${argument} cannot be a view on a resizable array buffer.`
})
}

Expand All @@ -763,6 +806,88 @@ webidl.converters.DataView = function (V, prefix, name, opts) {
return V
}

// https://webidl.spec.whatwg.org/#ArrayBufferView
webidl.converters.ArrayBufferView = function (V, prefix, argument, opts) {
if (
webidl.util.Type(V) !== OBJECT ||
!types.isArrayBufferView(V)
) {
throw webidl.errors.conversionFailed({
prefix,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: ['ArrayBufferView']
})
}

if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: prefix,
message: `${argument} cannot be a view on a shared array buffer.`
})
}

if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
throw webidl.errors.exception({
header: prefix,
message: `${argument} cannot be a view on a resizable array buffer.`
})
}

return V
}

// https://webidl.spec.whatwg.org/#BufferSource
webidl.converters.BufferSource = function (V, prefix, argument, opts) {
if (types.isArrayBuffer(V)) {
return webidl.converters.ArrayBuffer(V, prefix, argument, opts)
}

if (types.isArrayBufferView(V)) {
return webidl.converters.ArrayBufferView(V, prefix, argument, {
...opts,
allowShared: false
})
}

// Make this explicit for easier debugging
if (types.isSharedArrayBuffer(V)) {
throw webidl.errors.exception({
header: prefix,
message: `${argument} cannot be a SharedArrayBuffer.`
})
}

throw webidl.errors.conversionFailed({
prefix,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: ['ArrayBuffer', 'ArrayBufferView']
})
}

// https://webidl.spec.whatwg.org/#AllowSharedBufferSource
webidl.converters.AllowSharedBufferSource = function (V, prefix, argument, opts) {
if (types.isArrayBuffer(V)) {
return webidl.converters.ArrayBuffer(V, prefix, argument, opts)
}

if (types.isSharedArrayBuffer(V)) {
return webidl.converters.SharedArrayBuffer(V, prefix, argument, opts)
}

if (types.isArrayBufferView(V)) {
return webidl.converters.ArrayBufferView(V, prefix, argument, {
...opts,
allowShared: true
})
}

throw webidl.errors.conversionFailed({
prefix,
argument: `${argument} ("${webidl.util.Stringify(V)}")`,
types: ['ArrayBuffer', 'SharedArrayBuffer', 'ArrayBufferView']
})
}

webidl.converters['sequence<ByteString>'] = webidl.sequenceConverter(
webidl.converters.ByteString
)
Expand Down
Loading
Loading