|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +require('../common'); |
| 5 | +const assert = require('assert'); |
| 6 | +const { test } = require('node:test'); |
| 7 | + |
| 8 | +const { converters } = require('internal/webidl'); |
| 9 | + |
| 10 | +const TYPED_ARRAY_CTORS = [ |
| 11 | + Uint8Array, Int8Array, Uint8ClampedArray, |
| 12 | + Uint16Array, Int16Array, |
| 13 | + Uint32Array, Int32Array, |
| 14 | + Float16Array, Float32Array, Float64Array, |
| 15 | + BigInt64Array, BigUint64Array, |
| 16 | +]; |
| 17 | + |
| 18 | +test('BufferSource accepts ArrayBuffer', () => { |
| 19 | + const ab = new ArrayBuffer(8); |
| 20 | + assert.strictEqual(converters.BufferSource(ab), ab); |
| 21 | +}); |
| 22 | + |
| 23 | +test('BufferSource accepts all TypedArray kinds', () => { |
| 24 | + for (const Ctor of TYPED_ARRAY_CTORS) { |
| 25 | + const ta = new Ctor(4); |
| 26 | + assert.strictEqual(converters.BufferSource(ta), ta); |
| 27 | + } |
| 28 | +}); |
| 29 | + |
| 30 | +test('BufferSource accepts Buffer', () => { |
| 31 | + const buf = Buffer.alloc(8); |
| 32 | + assert.strictEqual(converters.BufferSource(buf), buf); |
| 33 | +}); |
| 34 | + |
| 35 | +test('BufferSource accepts DataView', () => { |
| 36 | + const dv = new DataView(new ArrayBuffer(8)); |
| 37 | + assert.strictEqual(converters.BufferSource(dv), dv); |
| 38 | +}); |
| 39 | + |
| 40 | +test('BufferSource accepts ArrayBuffer subclass instance', () => { |
| 41 | + class MyAB extends ArrayBuffer {} |
| 42 | + const sub = new MyAB(8); |
| 43 | + assert.strictEqual(converters.BufferSource(sub), sub); |
| 44 | +}); |
| 45 | + |
| 46 | +test('BufferSource accepts TypedArray with null prototype', () => { |
| 47 | + const ta = new Uint8Array(4); |
| 48 | + Object.setPrototypeOf(ta, null); |
| 49 | + assert.strictEqual(converters.BufferSource(ta), ta); |
| 50 | +}); |
| 51 | + |
| 52 | +test('BufferSource accepts DataView with null prototype', () => { |
| 53 | + const dv = new DataView(new ArrayBuffer(4)); |
| 54 | + Object.setPrototypeOf(dv, null); |
| 55 | + assert.strictEqual(converters.BufferSource(dv), dv); |
| 56 | +}); |
| 57 | + |
| 58 | +test('BufferSource accepts ArrayBuffer with null prototype', () => { |
| 59 | + const ab = new ArrayBuffer(4); |
| 60 | + Object.setPrototypeOf(ab, null); |
| 61 | + assert.strictEqual(converters.BufferSource(ab), ab); |
| 62 | +}); |
| 63 | + |
| 64 | +test('BufferSource rejects SharedArrayBuffer', () => { |
| 65 | + assert.throws( |
| 66 | + () => converters.BufferSource(new SharedArrayBuffer(4)), |
| 67 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 68 | + ); |
| 69 | +}); |
| 70 | + |
| 71 | +test('BufferSource rejects SAB-backed TypedArray', () => { |
| 72 | + const view = new Uint8Array(new SharedArrayBuffer(4)); |
| 73 | + assert.throws( |
| 74 | + () => converters.BufferSource(view), |
| 75 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 76 | + ); |
| 77 | +}); |
| 78 | + |
| 79 | +test('BufferSource rejects SAB-backed DataView', () => { |
| 80 | + const dv = new DataView(new SharedArrayBuffer(4)); |
| 81 | + assert.throws( |
| 82 | + () => converters.BufferSource(dv), |
| 83 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | +test('BufferSource accepts a detached ArrayBuffer', () => { |
| 88 | + const ab = new ArrayBuffer(8); |
| 89 | + structuredClone(ab, { transfer: [ab] }); |
| 90 | + assert.strictEqual(ab.byteLength, 0); |
| 91 | + assert.strictEqual(converters.BufferSource(ab), ab); |
| 92 | +}); |
| 93 | + |
| 94 | +test('BufferSource rejects objects with a forged @@toStringTag', () => { |
| 95 | + const fake = { [Symbol.toStringTag]: 'Uint8Array' }; |
| 96 | + assert.throws( |
| 97 | + () => converters.BufferSource(fake), |
| 98 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 99 | + ); |
| 100 | +}); |
| 101 | + |
| 102 | +for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [], |
| 103 | + {}, () => {}]) { |
| 104 | + test(`BufferSource rejects ${typeof value} ${String(value)}`, () => { |
| 105 | + assert.throws( |
| 106 | + () => converters.BufferSource(value), |
| 107 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 108 | + ); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +test('ArrayBufferView accepts all TypedArray kinds', () => { |
| 113 | + for (const Ctor of TYPED_ARRAY_CTORS) { |
| 114 | + const ta = new Ctor(4); |
| 115 | + assert.strictEqual(converters.ArrayBufferView(ta), ta); |
| 116 | + } |
| 117 | +}); |
| 118 | + |
| 119 | +test('ArrayBufferView accepts DataView', () => { |
| 120 | + const dv = new DataView(new ArrayBuffer(8)); |
| 121 | + assert.strictEqual(converters.ArrayBufferView(dv), dv); |
| 122 | +}); |
| 123 | + |
| 124 | +test('ArrayBufferView accepts TypedArray subclass instance', () => { |
| 125 | + class MyU8 extends Uint8Array {} |
| 126 | + const sub = new MyU8(4); |
| 127 | + assert.strictEqual(converters.ArrayBufferView(sub), sub); |
| 128 | +}); |
| 129 | + |
| 130 | +test('ArrayBufferView accepts TypedArray with null prototype', () => { |
| 131 | + const ta = new Uint8Array(4); |
| 132 | + Object.setPrototypeOf(ta, null); |
| 133 | + assert.strictEqual(converters.ArrayBufferView(ta), ta); |
| 134 | +}); |
| 135 | + |
| 136 | +test('ArrayBufferView accepts DataView with null prototype', () => { |
| 137 | + const dv = new DataView(new ArrayBuffer(4)); |
| 138 | + Object.setPrototypeOf(dv, null); |
| 139 | + assert.strictEqual(converters.ArrayBufferView(dv), dv); |
| 140 | +}); |
| 141 | + |
| 142 | +test('ArrayBufferView rejects raw ArrayBuffer', () => { |
| 143 | + assert.throws( |
| 144 | + () => converters.ArrayBufferView(new ArrayBuffer(4)), |
| 145 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 146 | + ); |
| 147 | +}); |
| 148 | + |
| 149 | +test('ArrayBufferView rejects raw SharedArrayBuffer', () => { |
| 150 | + assert.throws( |
| 151 | + () => converters.ArrayBufferView(new SharedArrayBuffer(4)), |
| 152 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 153 | + ); |
| 154 | +}); |
| 155 | + |
| 156 | +test('ArrayBufferView rejects SAB-backed TypedArray', () => { |
| 157 | + const view = new Uint8Array(new SharedArrayBuffer(4)); |
| 158 | + assert.throws( |
| 159 | + () => converters.ArrayBufferView(view), |
| 160 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 161 | + ); |
| 162 | +}); |
| 163 | + |
| 164 | +test('ArrayBufferView rejects SAB-backed DataView', () => { |
| 165 | + const dv = new DataView(new SharedArrayBuffer(4)); |
| 166 | + assert.throws( |
| 167 | + () => converters.ArrayBufferView(dv), |
| 168 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 169 | + ); |
| 170 | +}); |
| 171 | + |
| 172 | +test('ArrayBufferView rejects objects with a forged @@toStringTag', () => { |
| 173 | + const fake = { [Symbol.toStringTag]: 'Uint8Array' }; |
| 174 | + assert.throws( |
| 175 | + () => converters.ArrayBufferView(fake), |
| 176 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 177 | + ); |
| 178 | +}); |
| 179 | + |
| 180 | +for (const value of [null, undefined, 0, 1, 1n, '', 'x', true, Symbol('s'), [], |
| 181 | + {}, () => {}]) { |
| 182 | + test(`ArrayBufferView rejects ${typeof value} ${String(value)}`, () => { |
| 183 | + assert.throws( |
| 184 | + () => converters.ArrayBufferView(value), |
| 185 | + { code: 'ERR_INVALID_ARG_TYPE' }, |
| 186 | + ); |
| 187 | + }); |
| 188 | +} |
0 commit comments