mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-23 19:51:47 +00:00
Fix unsigned ops; Fix parenthesized conversion; Minor restructure
This commit is contained in:
36
std/assembly/array.ts
Normal file
36
std/assembly/array.ts
Normal file
@ -0,0 +1,36 @@
|
||||
// Multiple options:
|
||||
// 1. C-like with no 'length' or 'push'
|
||||
// 2. Descriptors that can be constructed from lower level arrays
|
||||
|
||||
class Array<T> {
|
||||
|
||||
readonly capacity: i32;
|
||||
length: i32;
|
||||
ptr: usize;
|
||||
|
||||
static fromPtr<T>(ptr: usize, capacity: i32): Array<T> {
|
||||
assert(capacity >= 0);
|
||||
const arr: Array<T> = new Array(0);
|
||||
store<i32>(changetype<Array<T>, usize>(arr), capacity);
|
||||
arr.length = ptr;
|
||||
arr.ptr = ptr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
constructor(capacity: i32 = 0) {
|
||||
assert(capacity >= 0);
|
||||
this.capacity = this.length = capacity;
|
||||
if (capacity > 0) {
|
||||
this.ptr = Heap.allocate(<usize>capacity);
|
||||
} else {
|
||||
this.ptr = 0;
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
store<i64>(changetype<this,usize>(this), 0);
|
||||
Heap.dispose(this.ptr);
|
||||
this.ptr = 0;
|
||||
Heap.dispose(changetype<this,usize>(this));
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
var globalScope = typeof window !== "undefined" && window || typeof global !== "undefined" && global || self;
|
||||
|
||||
var HEAP = new Uint8Array(65536);
|
||||
var HEAP = new Uint8Array(0);
|
||||
var HEAP_OFFSET = 0;
|
||||
|
||||
Object.defineProperties(globalScope["Heap"] = {
|
||||
@ -8,7 +8,7 @@ Object.defineProperties(globalScope["Heap"] = {
|
||||
if (!size) return 0;
|
||||
if (HEAP_OFFSET + size > HEAP.length) {
|
||||
var oldHeap = HEAP;
|
||||
HEAP = new Uint8Array(Math.max(HEAP.length + size, HEAP.length * 2));
|
||||
HEAP = new Uint8Array(Math.max(65536, HEAP.length + size, HEAP.length * 2));
|
||||
HEAP.set(oldHeap);
|
||||
}
|
||||
var ptr = HEAP_OFFSET;
|
||||
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"extends": "../../portable-assembly.json",
|
||||
"extends": "../../portable.json",
|
||||
"compilerOptions": {
|
||||
"diagnostics": true
|
||||
},
|
||||
|
Reference in New Issue
Block a user