-
Notifications
You must be signed in to change notification settings - Fork 10
[Implement] Naive Buffer.from, and friends #6
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
Open
jtenner
wants to merge
15
commits into
main
Choose a base branch
from
from
base: main
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.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
62b4dc9
[Implement] Naive Buffer.from
jtenner 3b67754
[Cleanup] remove log function
jtenner c2a6432
[Cleanup] add T extends ArrayBufferView to squelch intellisense
jtenner daf9dc1
[Chore] merge master
jtenner ba78bc9
Merge branch 'from' of https://github.com/AssemblyScript/node into from
jtenner 66af315
Merge branch 'master' of https://github.com/AssemblyScript/node into …
jtenner cc536a0
[merge] from master
jtenner c6997e1
[Fix] array of strings implementation
jtenner a333399
Merge branch 'master' of https://github.com/assemblyscript/node into …
jtenner 45a61c5
[Implement] rest of buffer.from functions per dcode
jtenner a05852d
merge master, add helper assembleBuffer function, fix tests
jtenner 7f69832
remove type info
jtenner 92c303d
Merge branch 'master' of https://github.com/assemblyscript/node into …
jtenner 11be548
add ascii encoding
jtenner 7f3e8c2
rename to allocBuffer
jtenner 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 hidden or 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,10 +1,20 @@ | ||
import { BLOCK_MAXSIZE, BLOCK, BLOCK_OVERHEAD } from "rt/common"; | ||
import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error"; | ||
import { Uint8Array } from "typedarray"; | ||
import { ArrayBufferView } from "arraybuffer"; | ||
import { Array } from "array"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline | ||
function allocBuffer(arrayBuffer: usize, offset: usize, length: u32): Buffer { | ||
let pointer = __alloc(offsetof<Buffer>(), idof<Buffer>()); | ||
store<usize>(pointer, __retain(arrayBuffer), offsetof<Buffer>("buffer")); | ||
store<usize>(pointer, arrayBuffer + offset, offsetof<Buffer>("dataStart")); | ||
store<u32>(pointer, length, offsetof<Buffer>("byteLength")); | ||
return changetype<Buffer>(pointer); | ||
} | ||
|
||
export class Buffer extends Uint8Array { | ||
[key: number]: u8; | ||
|
||
constructor(size: i32) { | ||
super(size); | ||
} | ||
|
@@ -16,19 +26,89 @@ export class Buffer extends Uint8Array { | |
@unsafe static allocUnsafe(size: i32): Buffer { | ||
// range must be valid | ||
if (<usize>size > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH); | ||
let buffer = __alloc(size, idof<ArrayBuffer>()); | ||
let result = __alloc(offsetof<Buffer>(), idof<Buffer>()); | ||
return allocBuffer(__alloc(size, idof<ArrayBuffer>()), 0, size); | ||
} | ||
|
||
public static fromArrayBuffer(buffer: ArrayBuffer, byteOffset: i32 = 0, length: i32 = -1): Buffer { | ||
length = select(buffer.byteLength, length, length < 0); | ||
if (i32(byteOffset < 0) | i32(byteOffset > buffer.byteLength - length)) throw new RangeError(E_INDEXOUTOFRANGE); | ||
if (length == 0) return new Buffer(0); | ||
|
||
return allocBuffer(changetype<usize>(buffer), <usize>byteOffset, <u32>length); | ||
} | ||
|
||
public static fromString(value: string, encoding: string = "utf8"): Buffer { | ||
let buffer: ArrayBuffer; | ||
if (encoding == "utf8" || encoding == "utf-8") { | ||
buffer = String.UTF8.encode(value); | ||
} else if (encoding == "utf16le") { | ||
buffer = String.UTF16.encode(value); | ||
} else if (encoding == "hex") { | ||
buffer = Buffer.HEX.encode(value); | ||
} else if (encoding == "ascii") { | ||
buffer = Buffer.ASCII.encode(value); | ||
} else { | ||
throw new TypeError("Invalid string encoding."); | ||
} | ||
|
||
// assemble the buffer | ||
return allocBuffer(changetype<usize>(buffer), 0, buffer.byteLength); | ||
} | ||
|
||
public static fromArray<T extends ArrayBufferView>(value: T, offset: i32 = 0, length: i32 = -1): Buffer { | ||
length = select(value.length, length, length < 0); | ||
if (i32(offset < 0) | i32(offset > value.length)) throw new RangeError(E_INDEXOUTOFRANGE); | ||
if (length > value.length - offset) throw new RangeError(E_INVALIDLENGTH); | ||
if (length == 0) return new Buffer(0); | ||
let arrayBuffer = __alloc(length, idof<ArrayBuffer>()); | ||
if (value instanceof Array<string>) { | ||
for (let i = 0; i < length; i++) { | ||
let index = i + offset; | ||
let byteValue = parseFloat(unchecked(value[index])); | ||
store<u8>(arrayBuffer + <usize>i, <u8>(isFinite(byteValue) ? <u8>byteValue : 0)); | ||
} | ||
} else { | ||
for (let i = 0; i < length; i++) { | ||
let index = i + offset; | ||
let element = isDefined(unchecked(value[index])) | ||
? unchecked(value[index]) | ||
: value[index]; | ||
|
||
if (isFloat(element)) { | ||
store<u8>(arrayBuffer + <usize>i, <u8>(isFinite(element) ? <u8>element : 0)); | ||
} else { | ||
store<u8>(arrayBuffer + <usize>i, <u8>element); | ||
} | ||
} | ||
} | ||
|
||
return allocBuffer(arrayBuffer, 0, length); | ||
} | ||
|
||
// set the properties | ||
store<usize>(result, __retain(buffer), offsetof<Buffer>("buffer")); | ||
store<usize>(result, buffer, offsetof<Buffer>("dataStart")); | ||
store<i32>(result, size, offsetof<Buffer>("byteLength")); | ||
public static fromBuffer(source: Buffer): Buffer { | ||
let length = source.byteLength; | ||
let data = __alloc(length, idof<ArrayBuffer>()); // retains | ||
memory.copy(data, source.dataStart, length); | ||
return allocBuffer(data, 0, length); | ||
} | ||
|
||
// 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 commentThe reason will be displayed to describe this comment to others. Learn more. We can suppress this error by returning |
||
public static from<T>(value: T): Buffer { | ||
if (value instanceof ArrayBuffer) { | ||
return allocBuffer(changetype<usize>(value), 0, value.byteLength); | ||
} else if (value instanceof String) { | ||
// @ts-ignore value not instance of `string` does changetype<string>(value) work here? | ||
let buffer = String.UTF8.encode(value); | ||
return allocBuffer(changetype<usize>(buffer), 0, buffer.byteLength); | ||
} else if (value instanceof Buffer) { | ||
return Buffer.fromBuffer(value); | ||
} else if (value instanceof ArrayBufferView) { | ||
return Buffer.fromArray(value); | ||
} | ||
ERROR("Cannot call Buffer.from<T>() where T is not a string, Buffer, ArrayBuffer, Array, or Array-like Object."); | ||
} | ||
|
||
static isBuffer<T>(value: T): bool { | ||
public static isBuffer<T>(value: T): bool { | ||
return value instanceof Buffer; | ||
} | ||
|
||
|
@@ -39,13 +119,7 @@ export class Buffer extends Uint8Array { | |
end = end < 0 ? max(len + end, 0) : min(end, len); | ||
end = max(end, begin); | ||
|
||
var out = __alloc(offsetof<Buffer>(), idof<Buffer>()); // retains | ||
store<usize>(out, __retain(changetype<usize>(this.buffer)), offsetof<Buffer>("buffer")); | ||
store<usize>(out, this.dataStart + <usize>begin, offsetof<Buffer>("dataStart")); | ||
store<i32>(out, end - begin, offsetof<Buffer>("byteLength")); | ||
|
||
// retains | ||
return changetype<Buffer>(out); | ||
return allocBuffer(changetype<usize>(this.buffer), <usize>this.byteOffset + <usize>begin, <u32>end - <u32>begin); | ||
} | ||
|
||
readInt8(offset: i32 = 0): i8 { | ||
|
This file contains hidden or 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 hidden or 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
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.
@dcodeIO what is going to happen to this
extends
clause? Are we going to detachArray
fromArrayBufferView
?