Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix up concat
  • Loading branch information
jtenner committed Mar 12, 2020
commit cd34165c6d3a871298e09eab91dd9f304ced8cb8
51 changes: 29 additions & 22 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { Uint8Array } from "typedarray";
import { Array } from "array";

export class Buffer extends Uint8Array {
[key: number]: u8;

constructor(size: i32) {
super(size);
}
Expand All @@ -22,43 +20,52 @@ export class Buffer extends Uint8Array {
let itemCount = <usize>items.length;
let itemsDataStart = items.dataStart;

// loop over each item and increase the size of the resulting buffer
for (let i: usize = 0; i < itemCount; i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
// check for nulls
if (item == 0) continue;
size += <usize>load<u32>(item, offsetof<Uint8Array>("dataLength"));
size += <usize>load<u32>(item, offsetof<Buffer>("byteLength"));
}

// account for passed concat buffer length
size = min<usize>(<usize>length, size);

let buffer = __alloc(size, idof<ArrayBuffer>());
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
let arrayBuffer = __alloc(size, idof<ArrayBuffer>());
let result = __alloc(offsetof<Buffer>(), idof<Buffer>());

// assemble the Buffer
store<usize>(result, __retain(arrayBuffer), offsetof<Buffer>("buffer"));
store<usize>(result, arrayBuffer, offsetof<Buffer>("byteLength"));

result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = buffer;
let start: usize = result.dataStart;
for (let i: usize = 0; i < itemCount && size > 0; i++) {
let start = arrayBuffer;
for (let i: usize = 0; i32(i < itemCount) & i32(size > 0); i++) {
let item = load<usize>(itemsDataStart + (i << alignof<usize>()));
if (item == null) continue;
let count = min<u32>(size, <usize>load<u32>(item, offsetof<Uint8Array>("dataLength")));
memory.copy(start, load<usize>(item, offsetof<Uint8Array>("dataStart")), count);
// if Buffer is null, continue
if (item == 0) continue;
let count = min<usize>(size, <usize>load<u32>(item, offsetof<Buffer>("byteLength")));
memory.copy(start, load<usize>(item, offsetof<Buffer>("dataStart")), count);
start += count;
size -= count;
}

return result;
// return and retain
return changetype<Buffer>(result);
}

@unsafe public static allocUnsafe(size: i32): Buffer {
// Node throws an error if size is less than 0
if (u32(size) > BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
@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>());
// This retains the pointer to the result Buffer.
let result = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
result.data = changetype<ArrayBuffer>(buffer);
result.dataStart = buffer;
result.dataLength = size;
return result;
let result = __alloc(offsetof<Buffer>(), idof<Buffer>());

// 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"));

// return and retain
return changetype<Buffer>(result);
}

static isBuffer<T>(value: T): bool {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"devDependencies": {
"@as-pect/core": "^3.1.0-beta.3",
"assemblyscript": "0.9.3",
"assemblyscript": "0.9.4",
"glob": "^7.1.6"
},
"scripts": {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.