mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-18 09:21:35 +00:00
runtime api
This commit is contained in:
@ -43,7 +43,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
|
||||
static create<T>(capacity: i32 = 0): Array<T> {
|
||||
if (<u32>capacity > <u32>MAX_BYTELENGTH >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
|
||||
var array = changetype<Array<T>>(runtime.makeArray(capacity, __runtime_id<Array<T>>(), alignof<T>()));
|
||||
var array = changetype<Array<T>>(runtime.newArray(capacity, alignof<T>(), __runtime_id<Array<T>>()));
|
||||
memory.fill(array.dataStart, 0, <usize>array.dataLength);
|
||||
array.length_ = 0; // !
|
||||
return array;
|
||||
@ -233,7 +233,7 @@ 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 out = changetype<Array<T>>(runtime.makeArray(thisLen + otherLen, __runtime_id<Array<T>>(), alignof<T>()));
|
||||
var out = changetype<Array<T>>(runtime.newArray(thisLen + otherLen, alignof<T>(), __runtime_id<Array<T>>()));
|
||||
var outStart = out.dataStart;
|
||||
var thisSize = <usize>thisLen << alignof<T>();
|
||||
if (isManaged<T>()) {
|
||||
@ -321,7 +321,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
|
||||
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U> {
|
||||
var length = this.length_;
|
||||
var out = changetype<Array<U>>(runtime.makeArray(length, __runtime_id<Array<U>>(), alignof<U>()));
|
||||
var out = changetype<Array<U>>(runtime.newArray(length, alignof<U>(), __runtime_id<Array<U>>()));
|
||||
var outStart = out.dataStart;
|
||||
for (let index = 0; index < min(length, this.length_); ++index) {
|
||||
let value = load<T>(this.dataStart + (<usize>index << alignof<T>()));
|
||||
@ -347,7 +347,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
}
|
||||
|
||||
filter(callbackfn: (value: T, index: i32, array: Array<T>) => bool): Array<T> {
|
||||
var result = changetype<Array<T>>(runtime.makeArray(0, __runtime_id<Array<T>>(), alignof<T>()));
|
||||
var result = changetype<Array<T>>(runtime.newArray(0, alignof<T>(), __runtime_id<Array<T>>()));
|
||||
for (let index = 0, length = this.length_; index < min(length, this.length_); ++index) {
|
||||
let value = load<T>(this.dataStart + (<usize>index << alignof<T>()));
|
||||
if (callbackfn(value, index, this)) result.push(value);
|
||||
@ -435,7 +435,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
begin = begin < 0 ? max(begin + length, 0) : min(begin, length);
|
||||
end = end < 0 ? max(end + length, 0) : min(end , length);
|
||||
length = max(end - begin, 0);
|
||||
var slice = changetype<Array<T>>(runtime.makeArray(length, __runtime_id<Array<T>>(), alignof<T>()));
|
||||
var slice = changetype<Array<T>>(runtime.newArray(length, alignof<T>(), __runtime_id<Array<T>>()));
|
||||
var sliceBase = slice.dataStart;
|
||||
var thisBase = this.dataStart + (<usize>begin << alignof<T>());
|
||||
if (isManaged<T>()) {
|
||||
@ -467,7 +467,7 @@ export class Array<T> extends ArrayBufferView {
|
||||
var length = this.length_;
|
||||
start = start < 0 ? max<i32>(length + start, 0) : min<i32>(start, length);
|
||||
deleteCount = max<i32>(min<i32>(deleteCount, length - start), 0);
|
||||
var result = changetype<Array<T>>(runtime.makeArray(deleteCount, __runtime_id<Array<T>>(), alignof<T>()));
|
||||
var result = changetype<Array<T>>(runtime.newArray(deleteCount, alignof<T>(), __runtime_id<Array<T>>()));
|
||||
var resultStart = result.dataStart;
|
||||
var thisStart = this.dataStart;
|
||||
var thisBase = thisStart + (<usize>start << alignof<T>());
|
||||
|
@ -1,7 +1,7 @@
|
||||
// The runtime provides common functionality that links runtime interfaces for memory management
|
||||
// and garbage collection to the standard library, making sure it all plays well together.
|
||||
|
||||
import { HEADER, HEADER_SIZE, HEADER_MAGIC } from "./util/runtime";
|
||||
import { HEADER, HEADER_SIZE, HEADER_MAGIC, adjust } from "./util/runtime";
|
||||
import { HEAP_BASE, memory } from "./memory";
|
||||
import { ArrayBufferView } from "./arraybuffer";
|
||||
|
||||
@ -20,6 +20,7 @@ export declare function __runtime_instanceof(id: u32, superId: u32): bool;
|
||||
export class runtime {
|
||||
private constructor() { return unreachable(); }
|
||||
|
||||
/** Determines whether a managed object is considered to be an instance of the class represented by the specified runtime id. */
|
||||
@unsafe
|
||||
static instanceof(ref: usize, id: u32): bool { // keyword
|
||||
return ref
|
||||
@ -34,17 +35,7 @@ export class runtime {
|
||||
/** Runtime implementation. */
|
||||
export namespace runtime {
|
||||
|
||||
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
||||
export function adjust(payloadSize: usize): usize {
|
||||
// round up to power of 2, e.g. with HEADER_SIZE=8:
|
||||
// 0 -> 2^3 = 8
|
||||
// 1..8 -> 2^4 = 16
|
||||
// 9..24 -> 2^5 = 32
|
||||
// ...
|
||||
// MAX_LENGTH -> 2^30 = 0x40000000 (MAX_SIZE_32)
|
||||
return <usize>1 << <usize>(<u32>32 - clz<u32>(payloadSize + HEADER_SIZE - 1));
|
||||
}
|
||||
|
||||
/** Allocates the memory necessary to represent a managed object of the specified size. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function allocate(payloadSize: usize): usize {
|
||||
@ -58,6 +49,7 @@ export namespace runtime {
|
||||
return changetype<usize>(header) + HEADER_SIZE;
|
||||
}
|
||||
|
||||
/** Reallocates the memory of a managed object that turned out to be too small or too large. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function reallocate(ref: usize, newPayloadSize: usize): usize {
|
||||
@ -103,6 +95,7 @@ export namespace runtime {
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** Discards the memory of a managed object that hasn't been registered yet. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function discard(ref: usize): void {
|
||||
@ -116,32 +109,53 @@ export namespace runtime {
|
||||
}
|
||||
}
|
||||
|
||||
/** Registers a managed object of the kind represented by the specified runtime id. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function register(ref: usize, classId: u32): usize {
|
||||
export function register(ref: usize, id: u32): usize {
|
||||
if (!ASC_NO_ASSERT) {
|
||||
assert(ref > HEAP_BASE); // must be a heap object
|
||||
let header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||
assert(header.classId == HEADER_MAGIC);
|
||||
header.classId = classId;
|
||||
header.classId = id;
|
||||
} else {
|
||||
changetype<HEADER>(ref - HEADER_SIZE).classId = classId;
|
||||
changetype<HEADER>(ref - HEADER_SIZE).classId = id;
|
||||
}
|
||||
if (isDefined(__ref_register)) __ref_register(ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** Allocates and registers, but doesn't initialize the data of, a new `String` of the specified length. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function makeArray(capacity: i32, id: u32, alignLog2: usize, source: usize = 0): usize {
|
||||
export function newString(length: i32): usize {
|
||||
return runtime.register(runtime.allocate(<usize>length << 1), __runtime_id<String>());
|
||||
}
|
||||
|
||||
/** Allocates and registers, but doesn't initialize the data of, a new `ArrayBuffer` of the specified byteLength. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function newArrayBuffer(byteLength: i32): usize {
|
||||
return runtime.register(runtime.allocate(<usize>byteLength), __runtime_id<ArrayBuffer>());
|
||||
}
|
||||
|
||||
/** Allocates and registers, but doesn't initialize the data of, a new `Array` of the specified length and element alignment.*/
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function newArray(length: i32, alignLog2: usize, id: u32, data: usize = 0): usize {
|
||||
// TODO: This API isn't great, but the compiler requires it for array literals anyway,
|
||||
// that is when an array literal is encountered and its data is static, this function is
|
||||
// called and the static buffer provided as `data`. This function can also be used to
|
||||
// create typed arrays in that `Array` also implements `ArrayBufferView` but has an
|
||||
// additional `.length_` property that remains unused overhead for typed arrays.
|
||||
var array = runtime.register(runtime.allocate(offsetof<i32[]>()), id);
|
||||
var bufferSize = <usize>capacity << alignLog2;
|
||||
var buffer = runtime.register(runtime.allocate(<usize>capacity << alignLog2), __runtime_id<ArrayBuffer>());
|
||||
var bufferSize = <usize>length << alignLog2;
|
||||
var buffer = runtime.register(runtime.allocate(bufferSize), __runtime_id<ArrayBuffer>());
|
||||
changetype<ArrayBufferView>(array).data = changetype<ArrayBuffer>(buffer); // links
|
||||
changetype<ArrayBufferView>(array).dataStart = buffer;
|
||||
changetype<ArrayBufferView>(array).dataLength = bufferSize;
|
||||
store<i32>(changetype<usize>(array), capacity, offsetof<i32[]>("length_"));
|
||||
if (source) memory.copy(buffer, source, bufferSize);
|
||||
store<i32>(changetype<usize>(array), length, offsetof<i32[]>("length_"));
|
||||
if (data) memory.copy(buffer, data, bufferSize);
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
@ -356,16 +356,16 @@ import { ArrayBufferView } from "./arraybuffer";
|
||||
|
||||
split(separator: String | null = null, limit: i32 = i32.MAX_VALUE): String[] {
|
||||
assert(this !== null);
|
||||
if (!limit) return changetype<String[]>(runtime.makeArray(0, __runtime_id<String[]>(), alignof<String>()));
|
||||
if (!limit) return changetype<String[]>(runtime.newArray(0, alignof<String>(), __runtime_id<String[]>()));
|
||||
if (separator === null) return <String[]>[this];
|
||||
var length: isize = this.length;
|
||||
var sepLen: isize = separator.length;
|
||||
if (limit < 0) limit = i32.MAX_VALUE;
|
||||
if (!sepLen) {
|
||||
if (!length) return changetype<String[]>(runtime.makeArray(0, __runtime_id<String>(), alignof<String>()));
|
||||
if (!length) return changetype<String[]>(runtime.newArray(0, alignof<String>(), __runtime_id<String>()));
|
||||
// split by chars
|
||||
length = min<isize>(length, <isize>limit);
|
||||
let result = changetype<String[]>(runtime.makeArray(length, __runtime_id<String[]>(), alignof<String>()));
|
||||
let result = changetype<String[]>(runtime.newArray(length, alignof<String>(), __runtime_id<String[]>()));
|
||||
let resultStart = changetype<ArrayBufferView>(result).dataStart;
|
||||
for (let i: isize = 0; i < length; ++i) {
|
||||
let charStr = runtime.allocate(2);
|
||||
@ -379,11 +379,11 @@ import { ArrayBufferView } from "./arraybuffer";
|
||||
}
|
||||
return result;
|
||||
} else if (!length) {
|
||||
let result = changetype<String[]>(runtime.makeArray(1, __runtime_id<String[]>(), alignof<String>()));
|
||||
let result = changetype<String[]>(runtime.newArray(1, alignof<String>(), __runtime_id<String[]>()));
|
||||
store<string>(changetype<ArrayBufferView>(result).dataStart, ""); // no need to register/link
|
||||
return result;
|
||||
}
|
||||
var result = changetype<String[]>(runtime.makeArray(0, __runtime_id<String[]>(), alignof<String>()));
|
||||
var result = changetype<String[]>(runtime.newArray(0, alignof<String>(), __runtime_id<String[]>()));
|
||||
var end = 0, start = 0, i = 0;
|
||||
while ((end = this.indexOf(separator!, start)) != -1) {
|
||||
let len = end - start;
|
||||
@ -398,7 +398,7 @@ import { ArrayBufferView } from "./arraybuffer";
|
||||
start = end + sepLen;
|
||||
}
|
||||
if (!start) {
|
||||
let result = changetype<String[]>(runtime.makeArray(1, __runtime_id<String[]>(), alignof<String>()));
|
||||
let result = changetype<String[]>(runtime.newArray(1, alignof<String>(), __runtime_id<String[]>()));
|
||||
unchecked(result[0] = this);
|
||||
return result;
|
||||
}
|
||||
|
@ -34,3 +34,14 @@ export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
||||
// @ts-ignore
|
||||
@lazy
|
||||
export const MAX_BYTELENGTH: i32 = MAX_SIZE_32 - HEADER_SIZE;
|
||||
|
||||
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
||||
export function adjust(payloadSize: usize): usize {
|
||||
// round up to power of 2, e.g. with HEADER_SIZE=8:
|
||||
// 0 -> 2^3 = 8
|
||||
// 1..8 -> 2^4 = 16
|
||||
// 9..24 -> 2^5 = 32
|
||||
// ...
|
||||
// MAX_LENGTH -> 2^30 = 0x40000000 (MAX_SIZE_32)
|
||||
return <usize>1 << <usize>(<u32>32 - clz<u32>(payloadSize + HEADER_SIZE - 1));
|
||||
}
|
||||
|
Reference in New Issue
Block a user