-
-
Notifications
You must be signed in to change notification settings - Fork 669
Add DataView to standard library #316
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
Conversation
Don't worry about that. Binaryen has |
I will rebase and order/squash the commits in a good way when I'm done, just pushing now so that you can see the changes more easily 👍 |
std/assembly/dataview.ts
Outdated
|
||
@inline | ||
function get<T>(buffer: ArrayBuffer, byteOffset: i32, littleEndian: boolean): T { | ||
const result = load<T>(changetype<usize>(buffer) + byteOffset, HEADER_SIZE); |
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.
in AS better use var
instead const
if this variable can't compute in compile time or store in data section
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.
Good to know, will fix 👍
Do you mind explaining why? let
?
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.
It better ask to @dcodeIO
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.
As a rule of thumb, const
currently behaves pretty much like static const
in other languages, indicating storage in static memory. I'm considering changing that, though.
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.
@dcodeIO Should we use let
(unless const
changes) then instead of var
?
About alignment. wasm can read/store unalignment memory without caveat: |
std/assembly/dataview.ts
Outdated
@@ -16,9 +16,11 @@ export class DataView { | |||
constructor( | |||
readonly buffer: ArrayBuffer, | |||
readonly byteOffset: i32 = 0, | |||
readonly byteLength: i32 = i32.MAX_VALUE, | |||
readonly byteLength: i32 = buffer.byteLength - byteOffset, |
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.
I don't think this support yet
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.
alas, it is not
ERROR TS2304: Cannot find name 'a'.
function test (a: ArrayBuffer, l: i32 = a.byteLength): i32 {
~
Would it be hard to fix? 😁 maybe a bit out of scope for this PR though 😂
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.
diff --git a/std/assembly/dataview.ts b/std/assembly/dataview.ts
index f1a8ebe..ef5df88 100644
--- a/std/assembly/dataview.ts
+++ b/std/assembly/dataview.ts
@@ -16,8 +16,10 @@ export class DataView {
constructor(
readonly buffer: ArrayBuffer,
readonly byteOffset: i32 = 0,
- readonly byteLength: i32 = buffer.byteLength - byteOffset,
+ readonly byteLength: i32 = i32.MIN_VALUE,
) {
+ if (byteLength === i32.MIN_VALUE) byteLength = buffer.byteLength - byteOffset
+
if (byteOffset < 0) throw new RangeError("byteOffset cannot be negative");
if (byteLength < 0) throw new RangeError("byteLength cannot be negative");
if (byteOffset + byteLength > buffer.byteLength) throw new RangeError("Length out of range of buffer");
Is this an acceptable compromise?
It won't give a proper error if someone passes in i32.MIN_VALUE
as the byteLength
, but I think we have to choose one i32 value to mean "nothing was passed"
(btw. creating a DataView
with a byteLength
of 0
is totally valid)
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.
What about this?
byteLength = min(buffer.byteLength - byteOffset, byteLength);
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.
I was thinking that it would be preferable to throw if a too long byteLength was passed, rather than silently truncating the length of the DataView
. That is how the spec says as well:
https://www.ecma-international.org/ecma-262/6.0/#sec-dataview-buffer-byteoffset-bytelength
12.c. If offset+viewByteLength > bufferByteLength, throw a RangeError exception.
Hmm, without fb34590 I'm getting the following error, would love some insight 🤔
|
Also plz add tests to new file |
I think that everything should be good to go now 🙌 I've rebased on master and squashed everything into one clean commit 😎 |
Also need declare |
Added entry to |
Looking good to me, except that it somewhat feels like the |
Yeah, they are currently only used for getting and setting integers, since floats require special handling. I'd be happy to either 1) remove the functions and just have the code at each callsite, 2) update them to also work for floats, so that they are used all the time or 3) rename them |
Regarding 2) I think that these could have been wrapped in a |
Done 🚀 |
Thank you! :) |
Opening this for early feedback, as soon as I know I'm on the right track it should be very easy for me to add all the required methods.
Some questions:
Should I just
throw new RangeError('Out of bounds access')
when trying to access outside the bounds?Is there any way to overload the
get*
functions for whenlittleEndian
is known at compile time, or will the compiler just figure it out anyway?e.g. something like this:
Do I have to register
DataView
as a global variable somewhere?Where do I put the tests? I'm assuming somewhere under☺️
tests/
, but then I'm a bit lostThank you for your time, looking forward to working a lot more on this project, it rocks! 🙌
fixes #315
FIXME:
dist/*
filesNOTICE
file