Skip to content

Commit

Permalink
[Refactor] move into static methods to prototype and rename
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Feb 8, 2024
1 parent 698a83e commit 3a52985
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 158 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ If `Uint8Array` is not present, the `shim` functions and `auto` entrypoints will

## Supported things

- [`Uint8Array.prototype.toBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64)
- [`Uint8Array.prototype.toHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tohex)
- [`Uint8Array.fromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.frombase64)
- [`Uint8Array.fromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.fromhex)
- [`Uint8Array.prototype.toBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tobase64)
- [`Uint8Array.prototype.toHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.tohex)
- [`Uint8Array.prototype.setFromBase64`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfrombase64)
- [`Uint8Array.prototype.setFromHex`](https://tc39.es/proposal-arraybuffer-base64/spec/#sec-uint8array.prototype.setfromhex)

## Getting started

Expand Down
7 changes: 0 additions & 7 deletions Uint8Array.fromBase64Into/polyfill.js

This file was deleted.

7 changes: 0 additions & 7 deletions Uint8Array.fromHexInto/polyfill.js

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,89 +19,91 @@ var ValidateUint8Array = require('../aos/ValidateUint8Array');
var typedArrayByteOffset = require('typed-array-byte-offset');
var typedArrayBuffer = require('typed-array-buffer');

module.exports = function fromBase64Into(string, into) {
module.exports = function setFromBase64(string) {
if (!$Uint8Array) {
throw new $SyntaxError('This environment does not support Uint8Array');
}

if (typeof string !== 'string') {
throw new $TypeError('`string` is not a string: ' + string); // step 1
}
var into = this; // step 1

ValidateUint8Array(into); // step 2

var opts = GetOptionsObject(arguments.length > 2 ? arguments[2] : void undefined); // step 3
if (typeof string !== 'string') {
throw new $TypeError('`string` is not a string: ' + string); // step 3
}

var opts = GetOptionsObject(arguments.length > 1 ? arguments[1] : void undefined); // step 4

var alphabet = Get(opts, 'alphabet'); // step 4
var alphabet = Get(opts, 'alphabet'); // step 5

if (typeof alphabet === 'undefined') {
alphabet = 'base64'; // step 5
alphabet = 'base64'; // step 6
}

if (typeof alphabet !== 'string') {
throw new $TypeError('`alphabet` is not a string: ' + alphabet); // step 6
throw new $TypeError('`alphabet` is not a string: ' + alphabet); // step 7
}

if (alphabet !== 'base64' && alphabet !== 'base64url') {
throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 7
throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + alphabet); // step 8
}

var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 8
var lastChunkHandling = Get(opts, 'lastChunkHandling'); // step 9

if (typeof lastChunkHandling === 'undefined') {
lastChunkHandling = 'loose'; // step 9
lastChunkHandling = 'loose'; // step 10
}

if (lastChunkHandling !== 'loose' && lastChunkHandling !== 'strict' && lastChunkHandling !== 'stop-before-partial') {
throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 10
throw new $TypeError('`lastChunkHandling` must be `\'loose\'`, `\'strict\'`, or `\'stop-before-partial\'`'); // step 11
}

var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 11
var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 12

if (IsTypedArrayOutOfBounds(taRecord)) {
throw new $TypeError('typed array is out of bounds'); // step 12
throw new $TypeError('typed array is out of bounds'); // step 13
}

var byteLength = TypedArrayByteLength(taRecord); // step 13
var byteLength = TypedArrayByteLength(taRecord); // step 14

var maxLength = byteLength; // step 14
var maxLength = byteLength; // step 15

var result = FromBase64(string, alphabet, lastChunkHandling, maxLength); // step 15
var result = FromBase64(string, alphabet, lastChunkHandling, maxLength); // step 16

var bytes = result['[[Bytes]]']; // step 16
var bytes = result['[[Bytes]]']; // step 17

var written = bytes.length; // step 17
var written = bytes.length; // step 18

// 18. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk.
// 19. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk.

if (written > byteLength) {
throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 19
throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 20
}

SetUint8ArrayBytes(into, bytes); // step 20
SetUint8ArrayBytes(into, bytes); // step 21

var offset = typedArrayByteOffset(into); // step 20
var offset = typedArrayByteOffset(into); // step 22

var index = 0; // step 21
var index = 0; // step 23

var intoBuffer = typedArrayBuffer(into);

while (index < written) { // step 22
var byte = bytes[index]; // step 22.a
while (index < written) { // step 24
var byte = bytes[index]; // step 24.a

var byteIndexInBuffer = index + offset; // step 22.b
var byteIndexInBuffer = index + offset; // step 24.b

SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'Uint8', byte, true, 'Unordered'); // step 22.c
SetValueInBuffer(intoBuffer, byteIndexInBuffer, 'Uint8', byte, true, 'Unordered'); // step 24.c

index += 1; // step 22.d
index += 1; // step 24.d
}

// 23. Let resultObject be OrdinaryObjectCreate(%Object.prototype%).
// 24. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])).
// 25. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)).
// 26. Return resultObject.
// 25. Let resultObject be OrdinaryObjectCreate(%Object.prototype%).
// 26. Perform ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]])).
// 27. Perform ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written)).
// 28. Return resultObject.

return { // steps 23 - 26
return { // steps 25 - 28
read: result['[[Read]]'],
written: written
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

var bound = callBind(getPolyfill(), null);
var bound = callBind(getPolyfill());

define(bound, {
getPolyfill: getPolyfill,
Expand Down
7 changes: 7 additions & 0 deletions Uint8Array.prototype.setFromBase64/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return typeof Uint8Array === 'function' && typeof Uint8Array.prototype.setFromBase64 === 'function' ? Uint8Array.prototype.setFromBase64 : implementation;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
var getPolyfill = require('./polyfill');
var define = require('define-properties');

module.exports = function shimUint8ArrayFromBase64Into() {
module.exports = function shimUint8ArraySetFromBase64() {
var polyfill = getPolyfill();

if (typeof Uint8Array === 'function') {
define(
Uint8Array,
{ fromBase64Into: polyfill },
{ fromBase64Into: function () { return Uint8Array.fromBase64Into !== polyfill; } }
Uint8Array.prototype,
{ setFromBase64: polyfill },
{ setFromBase64: function () { return Uint8Array.prototype.setFromBase64 !== polyfill; } }
);
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,49 @@ var TypedArrayByteLength = require('../aos/TypedArrayByteLength');
var ValidateUint8Array = require('../aos/ValidateUint8Array');
var SetUint8ArrayBytes = require('../aos/SetUint8ArrayBytes');

module.exports = function fromHexInto(string, into) {
module.exports = function setFromHex(string) {
if (!$Uint8Array) {
throw new $SyntaxError('This environment does not support Uint8Array'); // step 1
}

if (typeof string !== 'string') {
throw new $TypeError('`string` is not a string: ' + string); // step 1
}
var into = this; // step 1

ValidateUint8Array(into); // step 2

var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 3
if (typeof string !== 'string') {
throw new $TypeError('`string` is not a string: ' + string); // step 3
}

var taRecord = MakeTypedArrayWithBufferWitnessRecord(into, 'SEQ-CST'); // step 4

if (IsTypedArrayOutOfBounds(taRecord)) {
throw new $TypeError('fromHexInto called on Typed Array backed by detached buffer'); // step 4
throw new $TypeError('fromHexInto called on Typed Array backed by detached buffer'); // step 5
}

var byteLength = TypedArrayByteLength(taRecord); // step 5
var byteLength = TypedArrayByteLength(taRecord); // step 6

var maxLength = byteLength; // step 6
var maxLength = byteLength; // step 7

var result = FromHex(string, maxLength); // step 7
var result = FromHex(string, maxLength); // step 8

var bytes = result['[[Bytes]]']; // step 8
var bytes = result['[[Bytes]]']; // step 9

var written = bytes.length; // step 9
var written = bytes.length; // step 10

// 10. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk.
// 11. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing into cannot have been detached or shrunk.

if (written > byteLength) {
throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 11
throw new $TypeError('Assertion failed: written is not <= byteLength'); // step 12
}

SetUint8ArrayBytes(into, bytes); // step 12
SetUint8ArrayBytes(into, bytes); // step 13

// var resultObject = {}; // step 13 // OrdinaryObjectCreate(%Object.prototype%)
// CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 14
// CreateDataPropertyOrThrow(resultObject, 'written', written); // step 15
// return resultObject; // step 16
// var resultObject = {}; // step 14 // OrdinaryObjectCreate(%Object.prototype%)
// CreateDataPropertyOrThrow(resultObject, 'read', result['[[Read]]']); // step 15
// CreateDataPropertyOrThrow(resultObject, 'written', written); // step 16
// return resultObject; // step 17

return { // steps 13 - 16
return { // steps 14 - 17
read: result['[[Read]]'],
written: written
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');

var bound = callBind(getPolyfill(), null);
var bound = callBind(getPolyfill());

define(bound, {
getPolyfill: getPolyfill,
Expand Down
7 changes: 7 additions & 0 deletions Uint8Array.prototype.setFromHex/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return typeof Uint8Array === 'function' && typeof Uint8Array.prototype.setFromHex === 'function' ? Uint8Array.prototype.setFromHex : implementation;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
var getPolyfill = require('./polyfill');
var define = require('define-properties');

module.exports = function shimUint8ArrayFromHexInto() {
module.exports = function shimUint8ArraySetFromHex() {
var polyfill = getPolyfill();

if (typeof Uint8Array === 'function') {
define(
Uint8Array,
{ fromHexInto: polyfill },
{ fromHexInto: function () { return Uint8Array.fromHexInto !== polyfill; } }
Uint8Array.prototype,
{ setFromHex: polyfill },
{ setFromHex: function () { return Uint8Array.prototype.setFromHex !== polyfill; } }
);
}

Expand Down
24 changes: 17 additions & 7 deletions aos/IsFixedLengthArrayBuffer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
'use strict';

// var $TypeError = require('es-errors/type');
var $TypeError = require('es-errors/type');

var IsResizableArrayBuffer = require('./IsResizableArrayBuffer');
var callBound = require('call-bind/callBound');

var $arrayBufferResizable = callBound('%ArrayBuffer.prototype.resizable%', true);
var $sharedArrayGrowable = callBound('%SharedArrayBuffer.prototype.growable%', true);

var isArrayBuffer = require('is-array-buffer');
var isSharedArrayBuffer = require('is-shared-array-buffer');

// https://tc39.es/ecma262/#sec-isfixedlengtharraybuffer
// https://262.ecma-international.org/15.0/#sec-isfixedlengtharraybuffer

module.exports = function IsFixedLengthArrayBuffer(arrayBuffer) {
if (!isArrayBuffer(arrayBuffer) && !isSharedArrayBuffer(arrayBuffer)) {
// checked inside IsResizableArrayBuffer
// throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or a SharedArrayBuffer');
var isAB = isArrayBuffer(arrayBuffer);
var isSAB = isSharedArrayBuffer(arrayBuffer);
if (!isAB && !isSAB) {
throw new $TypeError('Assertion failed: `arrayBuffer` must be an ArrayBuffer or SharedArrayBuffer');
}

return !IsResizableArrayBuffer(arrayBuffer);
if (isAB && $arrayBufferResizable) {
return !$arrayBufferResizable(arrayBuffer); // step 1
}
if (isSAB && $sharedArrayGrowable) {
return !$sharedArrayGrowable(arrayBuffer); // step 1
}
return true; // step 2
};
38 changes: 0 additions & 38 deletions aos/IsResizableArrayBuffer.js

This file was deleted.

8 changes: 4 additions & 4 deletions index.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[
"Uint8Array.prototype.toBase64",
"Uint8Array.prototype.toHex",
"Uint8Array.fromBase64",
"Uint8Array.fromBase64Into",
"Uint8Array.fromHex",
"Uint8Array.fromHexInto"
"Uint8Array.prototype.setFromBase64",
"Uint8Array.prototype.setFromHex",
"Uint8Array.prototype.toBase64",
"Uint8Array.prototype.toHex"
]
Loading

0 comments on commit 3a52985

Please sign in to comment.