-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(pass-style,marshal): ByteArray, a binary Passable #2414
Draft
erights
wants to merge
1
commit into
master
Choose a base branch
from
markm-binary-direct
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | |
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 | ||
|
@@ -35,7 +47,7 @@ | |
/** | ||
* @param {any} array | ||
* @param {string=} optNameOfArray | ||
* @returns {asserts array is CopyArray<any>} | ||
Check warning on line 50 in packages/pass-style/src/typeGuards.js GitHub Actions / lint
|
||
*/ | ||
const assertCopyArray = (array, optNameOfArray = 'Alleged array') => { | ||
const passStyle = passStyleOf(array); | ||
|
@@ -47,6 +59,24 @@ | |
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 @@ | |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should switch to shortlex. See https://en.wikipedia.org/wiki/Shortlex_order
That way, encodePassable could encode as prefix followed by the unescaped content.