mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-27 16:02:16 +00:00
more
This commit is contained in:
parent
36d54d63d5
commit
e78c4c7f54
@ -40,6 +40,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
this.length_ = length;
|
||||
}
|
||||
|
||||
@unsafe get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.length_;
|
||||
}
|
||||
@ -49,18 +53,18 @@ export class Array<T> extends ArrayBufferView {
|
||||
this.length_ = length;
|
||||
}
|
||||
|
||||
resize(length: i32): void {
|
||||
var buffer = this.buffer;
|
||||
var oldCapacity = buffer.byteLength >>> alignof<T>();
|
||||
private resize(length: i32): void {
|
||||
var oldData = this.data;
|
||||
var oldCapacity = oldData.byteLength >>> alignof<T>();
|
||||
if (<u32>length > <u32>oldCapacity) {
|
||||
const MAX_LENGTH = ArrayBuffer.MAX_BYTELENGTH >>> alignof<T>();
|
||||
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
|
||||
let newCapacity = length << alignof<T>();
|
||||
let newBuffer = REALLOC(changetype<usize>(buffer), newCapacity);
|
||||
if (newBuffer !== changetype<usize>(buffer)) {
|
||||
this.buffer = changetype<ArrayBuffer>(newBuffer); // links
|
||||
this.dataStart = newBuffer;
|
||||
this.dataEnd = newBuffer + newCapacity;
|
||||
let newCapacity = <usize>length << alignof<T>();
|
||||
let newData = REALLOC(changetype<usize>(oldData), newCapacity); // registers on move
|
||||
if (newData !== changetype<usize>(oldData)) {
|
||||
this.data = changetype<ArrayBuffer>(newData); // links
|
||||
this.dataStart = newData;
|
||||
this.dataEnd = newData + newCapacity;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -114,23 +118,21 @@ export class Array<T> extends ArrayBufferView {
|
||||
// }
|
||||
|
||||
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
|
||||
var base = this.dataStart;
|
||||
var dataStart = this.dataStart;
|
||||
var length = this.length_;
|
||||
|
||||
start = start < 0 ? max(length + start, 0) : min(start, length);
|
||||
end = end < 0 ? max(length + end, 0) : min(end, length);
|
||||
|
||||
if (sizeof<T>() == 1) {
|
||||
if (start < end) {
|
||||
memory.fill(
|
||||
base + <usize>start,
|
||||
dataStart + <usize>start,
|
||||
<u8>value,
|
||||
<usize>(end - start)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
for (; start < end; ++start) {
|
||||
store<T>(base + (<usize>start << alignof<T>()), value);
|
||||
store<T>(dataStart + (<usize>start << alignof<T>()), value);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
@ -144,9 +146,9 @@ export class Array<T> extends ArrayBufferView {
|
||||
var length = this.length_;
|
||||
if (length == 0 || fromIndex >= length) return -1;
|
||||
if (fromIndex < 0) fromIndex = max(length + fromIndex, 0);
|
||||
var base = this.dataStart;
|
||||
var dataStart = this.dataStart;
|
||||
while (fromIndex < length) {
|
||||
if (load<T>(base + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
|
||||
if (load<T>(dataStart + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
|
||||
++fromIndex;
|
||||
}
|
||||
return -1;
|
||||
@ -155,11 +157,11 @@ export class Array<T> extends ArrayBufferView {
|
||||
lastIndexOf(searchElement: T, fromIndex: i32 = this.length_): i32 {
|
||||
var length = this.length_;
|
||||
if (length == 0) return -1;
|
||||
if (fromIndex < 0) fromIndex = length + fromIndex; // no need to clamp
|
||||
if (fromIndex < 0) fromIndex = length + fromIndex;
|
||||
else if (fromIndex >= length) fromIndex = length - 1;
|
||||
var base = this.dataStart;
|
||||
while (fromIndex >= 0) { // ^
|
||||
if (load<T>(base + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
|
||||
var dataStart = this.dataStart;
|
||||
while (fromIndex >= 0) {
|
||||
if (load<T>(dataStart + (<usize>fromIndex << alignof<T>())) == searchElement) return fromIndex;
|
||||
--fromIndex;
|
||||
}
|
||||
return -1;
|
||||
@ -177,19 +179,32 @@ export class Array<T> extends ArrayBufferView {
|
||||
concat(other: Array<T>): Array<T> {
|
||||
var thisLen = this.length_;
|
||||
var otherLen = select(0, other.length_, other === null);
|
||||
var outLen = thisLen + otherLen;
|
||||
var out = new Array<T>(outLen);
|
||||
if (thisLen) {
|
||||
memory.copy(out.dataStart, this.dataStart, <usize>thisLen << alignof<T>());
|
||||
var out = new Array<T>(thisLen + otherLen);
|
||||
var outStart = out.dataStart;
|
||||
var thisSize = <usize>thisLen << alignof<T>();
|
||||
if (isManaged<T>()) {
|
||||
let thisStart = this.dataStart;
|
||||
for (let offset: usize = 0; offset < thisSize; offset += sizeof<T>()) {
|
||||
let element = load<T>(thisStart + offset);
|
||||
store<T>(outStart + offset, element);
|
||||
LINK(changetype<usize>(element), changetype<usize>(out));
|
||||
}
|
||||
if (otherLen) {
|
||||
memory.copy(out.dataStart + (<usize>thisLen << alignof<T>()), other.dataStart, <usize>otherLen << alignof<T>());
|
||||
let otherStart = other.dataStart;
|
||||
let otherSize = <usize>otherLen << alignof<T>();
|
||||
for (let offset: usize = 0; offset < otherSize; offset += sizeof<T>()) {
|
||||
let element = load<T>(otherStart + offset);
|
||||
store<T>(outStart + thisSize + offset, element);
|
||||
LINK(changetype<usize>(element), changetype<usize>(out));
|
||||
}
|
||||
} else {
|
||||
memory.copy(outStart, this.dataStart, thisSize);
|
||||
memory.copy(outStart + thisSize, other.dataStart, <usize>otherLen << alignof<T>());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
|
||||
var buffer = this.buffer_;
|
||||
var dataStart = this.dataStart;
|
||||
var len = this.length_;
|
||||
|
||||
end = min<i32>(end, len);
|
||||
@ -202,13 +217,13 @@ export class Array<T> extends ArrayBufferView {
|
||||
from += count - 1;
|
||||
to += count - 1;
|
||||
while (count) {
|
||||
STORE<T>(buffer, to, LOAD<T>(buffer, from));
|
||||
store<T>(dataStart + (<usize>to << alignof<T>()), load<T>(dataStart + (<usize>from << alignof<T>())));
|
||||
--from, --to, --count;
|
||||
}
|
||||
} else {
|
||||
memory.copy(
|
||||
changetype<usize>(buffer) + HEADER_SIZE + (<usize>to << alignof<T>()),
|
||||
changetype<usize>(buffer) + HEADER_SIZE + (<usize>from << alignof<T>()),
|
||||
dataStart + (<usize>to << alignof<T>()),
|
||||
dataStart + (<usize>from << alignof<T>()),
|
||||
<usize>count << alignof<T>()
|
||||
);
|
||||
}
|
||||
@ -231,14 +246,15 @@ export class Array<T> extends ArrayBufferView {
|
||||
|
||||
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U> {
|
||||
var length = this.length_;
|
||||
var result = new Array<U>(length);
|
||||
var resultStart = result.dataStart;
|
||||
var out = new Array<U>(length);
|
||||
var outStart = out.dataStart;
|
||||
for (let index = 0; index < min(length, this.length_); ++index) {
|
||||
let element = load<T>(this.dataStart + (<usize>index << alignof<T>()));
|
||||
store<U>(resultStart + (<usize>index << alignof<T>()), element);
|
||||
if (isManaged<U>()) LINK(changetype<usize>(element), changetype<usize>(result));
|
||||
let value = load<T>(this.dataStart + (<usize>index << alignof<T>()));
|
||||
let result = callbackfn(value, index, this);
|
||||
store<U>(outStart + (<usize>index << alignof<U>()), result);
|
||||
if (isManaged<U>()) LINK(changetype<usize>(result), changetype<usize>(out));
|
||||
}
|
||||
return result;
|
||||
return out;
|
||||
}
|
||||
|
||||
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
|
||||
@ -360,9 +376,10 @@ export class Array<T> extends ArrayBufferView {
|
||||
reverse(): Array<T> {
|
||||
var base = this.dataStart;
|
||||
for (let front = 0, back = this.length_ - 1; front < back; ++front, --back) {
|
||||
let temp: T = load<T>(base, front);
|
||||
store<T>(base + (<usize>front << alignof<T>()), load<T>(base + (<usize>back << alignof<T>())));
|
||||
store<T>(base + (<usize>back << alignof<T>()), temp);
|
||||
let temp = load<T>(base, front);
|
||||
let dest = base + (<usize>back << alignof<T>());
|
||||
store<T>(base + (<usize>front << alignof<T>()), load<T>(dest));
|
||||
store<T>(dest, temp);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
@ -1,7 +1,4 @@
|
||||
import {
|
||||
AL_MASK,
|
||||
MAX_SIZE_32
|
||||
} from "./internal/allocator";
|
||||
import { AL_MASK, MAX_SIZE_32 } from "./util/allocator";
|
||||
|
||||
@builtin export declare const HEAP_BASE: usize;
|
||||
|
||||
@ -151,9 +148,9 @@ export abstract class ArrayBufferBase {
|
||||
export abstract class ArrayBufferView {
|
||||
[key: number]: number;
|
||||
|
||||
protected data: ArrayBuffer;
|
||||
protected dataStart: usize;
|
||||
protected dataEnd: usize;
|
||||
@unsafe data: ArrayBuffer;
|
||||
@unsafe dataStart: usize;
|
||||
@unsafe dataEnd: usize;
|
||||
|
||||
constructor(length: i32, alignLog2: i32) {
|
||||
if (<u32>length > <u32>ArrayBufferBase.MAX_BYTELENGTH >>> alignLog2) throw new RangeError("Invalid length");
|
||||
@ -161,11 +158,7 @@ export abstract class ArrayBufferView {
|
||||
var buffer = new ArrayBuffer(byteLength);
|
||||
this.data = buffer;
|
||||
this.dataStart = changetype<usize>(buffer);
|
||||
this.dataEnd = changetype<usize>(buffer) + length;
|
||||
}
|
||||
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
this.dataEnd = changetype<usize>(buffer) + <usize>length;
|
||||
}
|
||||
|
||||
get byteOffset(): i32 {
|
||||
@ -175,6 +168,11 @@ export abstract class ArrayBufferView {
|
||||
get byteLength(): i32 {
|
||||
return <i32>(this.dataEnd - this.dataStart);
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
ERROR("not implemented");
|
||||
return unreachable();
|
||||
}
|
||||
}
|
||||
|
||||
export abstract class StringBase {
|
||||
@ -185,7 +183,7 @@ export abstract class StringBase {
|
||||
}
|
||||
}
|
||||
|
||||
import { memcmp, memmove, memset } from "./internal/memory";
|
||||
import { memcmp, memmove, memset } from "./util/memory";
|
||||
|
||||
export namespace memory {
|
||||
@builtin export declare function size(): i32;
|
||||
|
@ -9,7 +9,7 @@ import {
|
||||
parse,
|
||||
CharCode,
|
||||
isWhiteSpaceOrLineTerminator
|
||||
} from "./internal/string";
|
||||
} from "./util/string";
|
||||
|
||||
@sealed export class String extends StringBase {
|
||||
|
||||
@ -431,8 +431,8 @@ import {
|
||||
return len;
|
||||
}
|
||||
|
||||
static fromUTF8(ptr: usize, len: usize): String {
|
||||
if (len < 1) return changetype<String>("");
|
||||
static fromUTF8(ptr: usize, len: usize): string {
|
||||
if (len < 1) return changetype<string>("");
|
||||
var ptrPos = <usize>0;
|
||||
var buf = memory.allocate(<usize>len << 1);
|
||||
var bufPos = <usize>0;
|
||||
@ -468,10 +468,10 @@ import {
|
||||
}
|
||||
}
|
||||
assert(ptrPos == len);
|
||||
var out = ALLOC(<u32>(bufPos >> 1));
|
||||
var out = ALLOC(bufPos);
|
||||
memory.copy(changetype<usize>(out), buf, bufPos);
|
||||
memory.free(buf);
|
||||
return REGISTER<String>(out);
|
||||
return REGISTER<string>(out);
|
||||
}
|
||||
|
||||
toUTF8(): usize {
|
||||
|
@ -1,14 +1,5 @@
|
||||
import {
|
||||
ALLOC,
|
||||
REGISTER,
|
||||
LINK,
|
||||
ArrayBufferView
|
||||
} from "./runtime";
|
||||
|
||||
import {
|
||||
COMPARATOR,
|
||||
SORT as SORT_IMPL
|
||||
} from "./internal/sort";
|
||||
import { ALLOC, REGISTER, LINK, ArrayBufferView } from "./runtime";
|
||||
import { COMPARATOR, SORT as SORT_IMPL } from "./util/sort";
|
||||
|
||||
function clampToByte(value: i32): i32 {
|
||||
return ~(value >> 31) & (((255 - value) >> 31) | value); // & 255
|
||||
@ -18,10 +9,16 @@ export class Int8Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 0);
|
||||
super(length, alignof<i8>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength;
|
||||
}
|
||||
|
||||
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int8Array {
|
||||
return FILL<Int8Array, i8>(this, value, start, end);
|
||||
@ -74,7 +71,7 @@ export class Uint8Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u8>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 0);
|
||||
super(length, alignof<u8>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength; }
|
||||
@ -180,10 +177,16 @@ export class Int16Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i16>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 1);
|
||||
super(length, alignof<i16>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 1; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 1;
|
||||
}
|
||||
|
||||
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int16Array {
|
||||
return FILL<Int16Array, i16>(this, value, start, end);
|
||||
@ -236,10 +239,16 @@ export class Uint16Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u16>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 1);
|
||||
super(length, alignof<u16>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 1; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 1;
|
||||
}
|
||||
|
||||
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint16Array {
|
||||
return FILL<Uint16Array, u16>(this, value, start, end);
|
||||
@ -292,10 +301,16 @@ export class Int32Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i32>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 2);
|
||||
super(length, alignof<i32>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 2; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 2;
|
||||
}
|
||||
|
||||
fill(value: i32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int32Array {
|
||||
return FILL<Int32Array, i32>(this, value, start, end);
|
||||
@ -348,10 +363,16 @@ export class Uint32Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u32>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 2);
|
||||
super(length, alignof<u32>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 2; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 2;
|
||||
}
|
||||
|
||||
fill(value: u32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint32Array {
|
||||
return FILL<Uint32Array, u32>(this, value, start, end);
|
||||
@ -404,10 +425,16 @@ export class Int64Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<i64>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 3);
|
||||
super(length, alignof<i64>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 3; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 3;
|
||||
}
|
||||
|
||||
fill(value: i64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Int64Array {
|
||||
return FILL<Int64Array, i64>(this, value, start, end);
|
||||
@ -460,10 +487,16 @@ export class Uint64Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<u64>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 3);
|
||||
super(length, alignof<u64>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 3; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 3;
|
||||
}
|
||||
|
||||
fill(value: u64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Uint64Array {
|
||||
return FILL<Uint64Array, u64>(this, value, start, end);
|
||||
@ -516,10 +549,16 @@ export class Float32Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f32>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 2);
|
||||
super(length, alignof<f32>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 2; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 2;
|
||||
}
|
||||
|
||||
fill(value: f32, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float32Array {
|
||||
return FILL<Float32Array, f32>(this, value, start, end);
|
||||
@ -572,10 +611,16 @@ export class Float64Array extends ArrayBufferView {
|
||||
@lazy static readonly BYTES_PER_ELEMENT: usize = sizeof<f64>();
|
||||
|
||||
constructor(length: i32) {
|
||||
super(length, 3);
|
||||
super(length, alignof<f64>());
|
||||
}
|
||||
|
||||
get length(): i32 { return this.byteLength >>> 3; }
|
||||
get buffer(): ArrayBuffer {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
get length(): i32 {
|
||||
return this.byteLength >>> 3;
|
||||
}
|
||||
|
||||
fill(value: f64, start: i32 = 0, end: i32 = i32.MAX_VALUE): Float64Array {
|
||||
return FILL<Float64Array, f64>(this, value, start, end);
|
||||
|
@ -1,3 +1,2 @@
|
||||
@sealed
|
||||
export abstract class V128 {
|
||||
@sealed export class V128 {
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user