Skip to content

[Implement] Naive Buffer.from, and friends - #6

Open
jtenner wants to merge 15 commits into
mainfrom
from
Open

[Implement] Naive Buffer.from, and friends#6
jtenner wants to merge 15 commits into
mainfrom
from

Conversation

@jtenner

@jtennerjtenner commented Jul 19, 2019

Copy link
Copy Markdown
Contributor

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.

// utf-16 has a work around in AssemblyScriptletbuff=Buffer.from(String.UTF16.encode(value));// using Buffer.fromString(value, "utf16le");

Things to note about this function:

  • String[] objects do something complicated: string -> f64 -> u8 to remain compatible with node.
>Buffer.from(["3","257.3","15","Infinity","NaN"])<Buffer03010F0000>
  • String objects need to be converted to UTF8
  • Buffer objects share their view of the same ArrayBuffer
  • ArrayBuffer objects are simply attached to a new Buffer
  • ArrayBufferView objects need to convert their values to u8.

@jtenner
jtenner requested a review from dcodeIOJuly 19, 2019 18:24
@jtennerjtenner self-assigned this Jul 19, 2019
@jtennerjtenner added the enhancement New feature or request label Jul 19, 2019
@jtennerjtenner changed the title [Implement] Naive Buffer.from[Implement] Naive Buffer.from, and friendsAug 22, 2019
@jtenner

Copy link
Copy Markdown
ContributorAuthor

@RedDwarfian and @dcodeIO thoughts on this pull request?

@RedDwarfian

Copy link
Copy Markdown
Contributor

It is not clear in the Naive Buffer.from code where non-string arrays such as Array<u8> are handled. I would suggest adding a comment to clarify that.

@jtenner

Copy link
Copy Markdown
ContributorAuthor

We might like to pull the ASCII encoding pull request first. #27

Otherwise, this is ready for review.

Comment threadassembly/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);

Copy link
Copy Markdown
ContributorAuthor

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 {

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown
ContributorAuthor

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?

@jtenner
jtenner requested a review from MaxGraeyMarch 13, 2020 04:23
Comment threadassembly/buffer/index.ts Outdated
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancementNew feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jtenner@RedDwarfian@MaxGraey