-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pass-style,marshal): ByteArray, a binary Passable
- Loading branch information
Showing
16 changed files
with
245 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { X } from '@endo/errors'; | ||
import { | ||
transferBufferToImmutable, | ||
isBufferImmutable, | ||
} from '@endo/immutable-arraybuffer'; | ||
import { assertChecker } from './passStyle-helpers.js'; | ||
|
||
const { getPrototypeOf, getOwnPropertyDescriptor } = Object; | ||
const { ownKeys, apply } = Reflect; | ||
|
||
const AnImmutableArrayBuffer = transferBufferToImmutable(new ArrayBuffer(0)); | ||
|
||
/** | ||
* As proposed, this will be the same as `ArrayBuffer.prototype`. As shimmed, | ||
* this will be a hidden intrinsic that inherits from `ArrayBuffer.prototype`. | ||
* Either way, get this in a way that we can trust it after lockdown, and | ||
* require that all immutable ArrayBuffers directly inherit from it. | ||
*/ | ||
const ImmutableArrayBufferPrototype = getPrototypeOf(AnImmutableArrayBuffer); | ||
|
||
// @ts-expect-error ok to implicitly assert the access is found | ||
const immutableGetter = getOwnPropertyDescriptor( | ||
ImmutableArrayBufferPrototype, | ||
'immutable', | ||
).get; | ||
|
||
/** | ||
* @param {unknown} candidate | ||
* @param {import('./types.js').Checker} [check] | ||
* @returns {boolean} | ||
*/ | ||
const canBeValid = (candidate, check = undefined) => | ||
(candidate instanceof ArrayBuffer && isBufferImmutable(candidate)) || | ||
(!!check && check(false, X`Immutable ArrayBuffer expected: ${candidate}`)); | ||
|
||
/** | ||
* @type {import('./internal-types.js').PassStyleHelper} | ||
*/ | ||
export const ByteArrayHelper = harden({ | ||
styleName: 'byteArray', | ||
|
||
canBeValid, | ||
|
||
assertValid: (candidate, _passStyleOfRecur) => { | ||
canBeValid(candidate, assertChecker); | ||
getPrototypeOf(candidate) === ImmutableArrayBufferPrototype || | ||
assert.fail(X`Malformed ByteArray ${candidate}`, TypeError); | ||
// @ts-expect-error assume immutableGetter was found | ||
apply(immutableGetter, candidate, []) || | ||
assert.fail(X`Must be an immutable ArrayBuffer: ${candidate}`); | ||
ownKeys(candidate).length === 0 || | ||
assert.fail( | ||
X`ByteArrays must not have own properties: ${candidate}`, | ||
TypeError, | ||
); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { Fail, q } from '@endo/errors'; | ||
import { passStyleOf } from './passStyleOf.js'; | ||
|
||
/** @import {CopyArray, CopyRecord, Passable, RemotableObject} from './types.js' */ | ||
/** | ||
* @import {CopyArray, CopyRecord, Passable, RemotableObject, ByteArray} from './types.js' | ||
*/ | ||
|
||
/** | ||
* Check whether the argument is a pass-by-copy array, AKA a "copyArray" | ||
|
@@ -13,6 +15,16 @@ import { passStyleOf } from './passStyleOf.js'; | |
const isCopyArray = arr => passStyleOf(arr) === 'copyArray'; | ||
harden(isCopyArray); | ||
|
||
/** | ||
* Check whether the argument is a pass-by-copy binary data, AKA a "byteArray" | ||
* in @endo/marshal terms | ||
* | ||
* @param {Passable} arr | ||
* @returns {arr is ByteArray} | ||
*/ | ||
const isByteArray = arr => passStyleOf(arr) === 'byteArray'; | ||
harden(isByteArray); | ||
|
||
/** | ||
* Check whether the argument is a pass-by-copy record, AKA a | ||
* "copyRecord" in @endo/marshal terms | ||
|
@@ -47,6 +59,24 @@ const assertCopyArray = (array, optNameOfArray = 'Alleged array') => { | |
harden(assertCopyArray); | ||
|
||
/** | ||
* @callback AssertByteArray | ||
* @param {Passable} array | ||
* @param {string=} optNameOfArray | ||
* @returns {asserts array is ByteArray} | ||
Check warning on line 65 in packages/pass-style/src/typeGuards.js GitHub Actions / lint
|
||
*/ | ||
|
||
/** @type {AssertByteArray} */ | ||
const assertByteArray = (array, optNameOfArray = 'Alleged byteArray') => { | ||
const passStyle = passStyleOf(array); | ||
passStyle === 'byteArray' || | ||
Fail`${q( | ||
optNameOfArray, | ||
)} ${array} must be a pass-by-copy binary data, not ${q(passStyle)}`; | ||
}; | ||
harden(assertByteArray); | ||
|
||
/** | ||
* @callback AssertRecord | ||
* @param {any} record | ||
* @param {string=} optNameOfRecord | ||
* @returns {asserts record is CopyRecord<any>} | ||
|
@@ -80,8 +110,10 @@ harden(assertRemotable); | |
export { | ||
assertRecord, | ||
assertCopyArray, | ||
assertByteArray, | ||
assertRemotable, | ||
isRemotable, | ||
isRecord, | ||
isCopyArray, | ||
isByteArray, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.