-
Notifications
You must be signed in to change notification settings - Fork 11
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
[Implement] Naive Buffer.from, and friends #6
base: main
Are you sure you want to change the base?
Conversation
@RedDwarfian and @dcodeIO thoughts on this pull request? |
It is not clear in the Naive |
We might like to pull the ASCII encoding pull request first. #27 Otherwise, this is ready for review. |
assembly/buffer/index.ts
Outdated
if (i32(byteOffset < 0) | i32(byteOffset > buffer.byteLength - length)) throw new RangeError(E_INDEXOUTOFRANGE); | ||
if (length == 0) return new Buffer(0); | ||
|
||
return assembleBuffer(changetype<usize>(buffer), <usize>byteOffset, <u32>length); |
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.
@MaxGraey should this be min(length, buffer.byteLength - byteOffset)
?
return assembleBuffer(changetype<usize>(buffer), 0, buffer.byteLength); | ||
} | ||
|
||
public static fromArray<T extends ArrayBufferView>(value: T, offset: i32 = 0, length: i32 = -1): Buffer { |
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 what is going to happen to this extends
clause? Are we going to detach Array
from ArrayBufferView
?
|
||
// return and retain | ||
return changetype<Buffer>(result); | ||
// @ts-ignore: Buffer returns on all valid branches |
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.
We can suppress this error by returning changetype<Buffer>(null)
at the bottom here after the ERROR
. What is the right way to do this?
Implements
Buffer.from<T>(value: T)
and a few other convenience functions.There are lots of obvious problems with not having the function overloads, but there are different paths we can take.
For instance, because there are no optional parameters, we must assume that the default encoding for strings is
UTF8
.Things to note about this function:
String[]
objects do something complicated: string -> f64 -> u8 to remain compatible with node.String
objects need to be converted toUTF8
Buffer
objects share their view of the sameArrayBuffer
ArrayBuffer
objects are simply attached to a newBuffer
ArrayBufferView
objects need to convert their values tou8
.