Skip to content

[Feature] Implement Buffer Class #708

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions std/assembly/arraybuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export abstract class ArrayBufferView {
if (value instanceof Float32Array) return true;
if (value instanceof Float64Array) return true;
if (value instanceof DataView) return true;
if (value instanceof Buffer) return true;
}
return false;
}
Expand Down
59 changes: 59 additions & 0 deletions std/assembly/buffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Uint8Array } from "typedarray";
import { ArrayBufferView } from "arraybuffer";
import { TypeinfoFlags } from "shared/typeinfo";

export class Buffer extends Uint8Array {

// TODO: Add more
private static NO_ENCODING: i32 = 0;
private static UTF8_ENCODING: i32 = 1;
private static UTF16_ENCODING: i32 = 2;
private encoding: i32 = 0;

constructor(size: i32) {
super(size);
}

public static isBuffer<T>(obj: T): bool {
return obj instanceof Buffer && obj != null;
}

public static from<T>(source: T, encoding: string = "utf8"): Buffer {
if (source instanceof ArrayBuffer) {
let buffer = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
store<usize>(changetype<usize>(buffer), __retain(changetype<usize>(source)), offsetof<Buffer>("data"));
store<usize>(changetype<usize>(buffer), changetype<usize>(source), offsetof<Buffer>("dataStart"));
store<u32>(changetype<usize>(buffer), source.byteLength, offsetof<Buffer>("dataLength"));
return buffer;
} else if (source instanceof string) {
if (encoding == null) encoding = "utf8";

if (encoding == "utf8" || encoding == "utf16") {
let encoded = encoding == "utf8"
? String.UTF8.encode(source)
: String.UTF16.encode(source);
let buffer = changetype<Buffer>(__alloc(offsetof<Buffer>(), idof<Buffer>()));
store<usize>(changetype<usize>(buffer), __retain(changetype<usize>(encoded)), offsetof<Buffer>("data"));
store<usize>(changetype<usize>(buffer), changetype<usize>(encoded), offsetof<Buffer>("dataStart"));
store<u32>(changetype<usize>(buffer), encoded.byteLength, offsetof<Buffer>("dataLength"));

return buffer;
}
assert(false);
return null;
} else if (source instanceof ArrayBufferView) {
var sourceInfo = __typeinfo(idof<T>());
var sourceIsManaged = (sourceInfo & TypeinfoFlags.VALUE_MANAGED) != 0; // disallow this
assert(!sourceIsManaged);
let length = source.length;
let buffer = new Buffer(length);
for (let i = 0; i < length; i++) {
buffer[i] = <u8>source[i];
}
return buffer;
} else {
assert(false);
return null;
}
}
}
5 changes: 4 additions & 1 deletion std/assembly/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Uint8Array } from "typedarray";

/**
* Environment definitions for compiling AssemblyScript to WebAssembly using asc.
* @module std/assembly
Expand Down Expand Up @@ -1150,7 +1152,8 @@ declare abstract class TypedArray<T> implements ArrayBufferView<T> {
/** The reverse() method reverses a typed array in place. The first typed array element becomes the last and the last becomes the first. This method has the same algorithm as Array.prototype.reverse(). */
reverse(): this;
}

/** A node buffer. */
declare class Buffer extends Uint8Array {}
/** An array of twos-complement 8-bit signed integers. */
declare class Int8Array extends TypedArray<i8> {}
/** An array of 8-bit unsigned integers. */
Expand Down
24 changes: 12 additions & 12 deletions tests/compiler/std/arraybuffer.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(data (i32.const 376) "\14\00\00\00\01\00\00\00\01\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s")
(data (i32.const 416) "\08\00\00\00\01\00\00\00\00\00\00\00\08\00\00\00\01\00\00\00\02")
(data (i32.const 440) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00~\00l\00i\00b\00/\00d\00a\00t\00a\00v\00i\00e\00w\00.\00t\00s")
(data (i32.const 488) "\10\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\93\04\00\00\02\00\00\001\04\00\00\02\00\00\001\00\00\00\02\00\00\001\00\00\00\02\00\00\00Q\04\00\00\02\00\00\00Q\00\00\00\02\00\00\00\91\04\00\00\02\00\00\00\91\00\00\00\02\00\00\00\11\05\00\00\02\00\00\00\11\01\00\00\02\00\00\00\91\0c\00\00\02\00\00\00\11\0d\00\00\02\00\00\00\10")
(data (i32.const 488) "\11\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\93\04\00\00\02\00\00\001\04\00\00\02\00\00\001\00\00\00\02\00\00\001\00\00\00\02\00\00\00Q\04\00\00\02\00\00\00Q\00\00\00\02\00\00\00\91\04\00\00\02\00\00\00\91\00\00\00\02\00\00\00\11\05\00\00\02\00\00\00\11\01\00\00\02\00\00\00\91\0c\00\00\02\00\00\00\11\0d\00\00\02\00\00\00\10\00\00\00\00\00\00\001\00\00\00\05")
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
(global $~lib/rt/pure/CUR (mut i32) (i32.const 0))
(global $~lib/rt/pure/END (mut i32) (i32.const 0))
Expand Down Expand Up @@ -617,10 +617,10 @@
if
unreachable
end
i32.const 624
i32.const 640
i32.const 0
i32.store
i32.const 2192
i32.const 2208
i32.const 0
i32.store
i32.const 0
Expand All @@ -634,7 +634,7 @@
local.get $0
i32.const 2
i32.shl
i32.const 624
i32.const 640
i32.add
i32.const 0
i32.store offset=4
Expand All @@ -653,7 +653,7 @@
i32.add
i32.const 2
i32.shl
i32.const 624
i32.const 640
i32.add
i32.const 0
i32.store offset=96
Expand All @@ -671,13 +671,13 @@
br $loop|0
end
end
i32.const 624
i32.const 2208
i32.const 640
i32.const 2224
memory.size
i32.const 16
i32.shl
call $~lib/rt/tlsf/addMemory
i32.const 624
i32.const 640
global.set $~lib/rt/tlsf/ROOT
)
(func $~lib/rt/tlsf/prepareSize (; 9 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
Expand Down Expand Up @@ -1278,7 +1278,7 @@
)
(func $~lib/rt/pure/__retain (; 17 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
i32.const 620
i32.const 628
i32.gt_u
if
local.get $0
Expand Down Expand Up @@ -1776,7 +1776,7 @@
)
(func $~lib/rt/pure/__release (; 27 ;) (type $FUNCSIG$vi) (param $0 i32)
local.get $0
i32.const 620
i32.const 628
i32.gt_u
if
local.get $0
Expand Down Expand Up @@ -2406,7 +2406,7 @@
)
(func $~lib/rt/pure/__visit (; 39 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
local.get $0
i32.const 620
i32.const 628
i32.lt_u
if
return
Expand Down Expand Up @@ -2522,7 +2522,7 @@
i32.const 8
i32.sub
i32.load
br_table $switch$1$case$2 $switch$1$case$2 $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $switch$1$default
br_table $switch$1$case$2 $switch$1$case$2 $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $block$4$break $switch$1$default
end
return
end
Expand Down
8 changes: 4 additions & 4 deletions tests/compiler/std/arraybuffer.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(data (i32.const 376) "\14\00\00\00\01\00\00\00\01\00\00\00\14\00\00\00~\00l\00i\00b\00/\00r\00t\00.\00t\00s\00")
(data (i32.const 416) "\08\00\00\00\01\00\00\00\00\00\00\00\08\00\00\00\01\00\00\00\02\00\00\00")
(data (i32.const 440) " \00\00\00\01\00\00\00\01\00\00\00 \00\00\00~\00l\00i\00b\00/\00d\00a\00t\00a\00v\00i\00e\00w\00.\00t\00s\00")
(data (i32.const 488) "\10\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\93\04\00\00\02\00\00\001\04\00\00\02\00\00\001\00\00\00\02\00\00\001\00\00\00\02\00\00\00Q\04\00\00\02\00\00\00Q\00\00\00\02\00\00\00\91\04\00\00\02\00\00\00\91\00\00\00\02\00\00\00\11\05\00\00\02\00\00\00\11\01\00\00\02\00\00\00\91\0c\00\00\02\00\00\00\11\0d\00\00\02\00\00\00\10\00\00\00\00\00\00\00")
(data (i32.const 488) "\11\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\10\00\00\00\00\00\00\00\93\04\00\00\02\00\00\001\04\00\00\02\00\00\001\00\00\00\02\00\00\001\00\00\00\02\00\00\00Q\04\00\00\02\00\00\00Q\00\00\00\02\00\00\00\91\04\00\00\02\00\00\00\91\00\00\00\02\00\00\00\11\05\00\00\02\00\00\00\11\01\00\00\02\00\00\00\91\0c\00\00\02\00\00\00\11\0d\00\00\02\00\00\00\10\00\00\00\00\00\00\001\00\00\00\05\00\00\00")
(table $0 1 funcref)
(elem (i32.const 0) $null)
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
Expand All @@ -34,7 +34,7 @@
(global $~lib/rt/pure/ROOTS (mut i32) (i32.const 0))
(global $~lib/argc (mut i32) (i32.const 0))
(global $~lib/rt/__rtti_base i32 (i32.const 488))
(global $~lib/heap/__heap_base i32 (i32.const 620))
(global $~lib/heap/__heap_base i32 (i32.const 628))
(export "memory" (memory $0))
(start $start)
(func $~lib/rt/tlsf/removeBlock (; 5 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
Expand Down Expand Up @@ -1665,7 +1665,7 @@
if
i32.const 24
i32.const 72
i32.const 57
i32.const 58
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4265,7 +4265,7 @@
i32.const 8
i32.sub
i32.load
br_table $switch$1$case$2 $switch$1$case$2 $switch$1$case$4 $switch$1$case$5 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$default
br_table $switch$1$case$2 $switch$1$case$2 $switch$1$case$4 $switch$1$case$5 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$case$4 $switch$1$default
end
return
end
Expand Down
6 changes: 6 additions & 0 deletions tests/compiler/std/buffer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"asc_flags": [
"--runtime half",
"--use ASC_RTRACE=1"
]
}
Loading