mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 11:11:43 +00:00
Initial new rt integration
This commit is contained in:
@ -1,19 +0,0 @@
|
||||
/** Number of alignment bits. */
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
export const AL_BITS: u32 = 3;
|
||||
|
||||
/** Number of possible alignment values. */
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
export const AL_SIZE: usize = 1 << <usize>AL_BITS;
|
||||
|
||||
/** Mask to obtain just the alignment bits. */
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
export const AL_MASK: usize = AL_SIZE - 1;
|
||||
|
||||
/** Maximum 32-bit allocation size. */
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
export const MAX_SIZE_32: usize = 1 << 30; // 1GB
|
@ -1,6 +1,7 @@
|
||||
import { allocate, register, discard } from "./runtime";
|
||||
/// <reference path="../rt/index.d.ts" />
|
||||
|
||||
import { idof } from "../builtins";
|
||||
import { CharCode } from "./string";
|
||||
import { __runtime_id } from "../runtime";
|
||||
import { ArrayBufferView } from "../arraybuffer";
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@ -264,10 +265,10 @@ export function utoa32(value: u32): String {
|
||||
if (!value) return "0";
|
||||
|
||||
var decimals = decimalCount32(value);
|
||||
var out = allocate(decimals << 1);
|
||||
var out = __alloc(decimals << 1, idof<String>());
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
return changetype<String>(out); // retains
|
||||
}
|
||||
|
||||
export function itoa32(value: i32): String {
|
||||
@ -277,12 +278,12 @@ export function itoa32(value: i32): String {
|
||||
if (sign) value = -value;
|
||||
|
||||
var decimals = decimalCount32(value) + u32(sign);
|
||||
var out = allocate(decimals << 1);
|
||||
var out = __alloc(decimals << 1, idof<String>());
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
return changetype<String>(out); // retains
|
||||
}
|
||||
|
||||
export function utoa64(value: u64): String {
|
||||
@ -292,14 +293,14 @@ export function utoa64(value: u64): String {
|
||||
if (value <= u32.MAX_VALUE) {
|
||||
let val32 = <u32>value;
|
||||
let decimals = decimalCount32(val32);
|
||||
out = allocate(decimals << 1);
|
||||
out = __alloc(decimals << 1, idof<String>());
|
||||
utoa32_core(out, val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value);
|
||||
out = allocate(decimals << 1);
|
||||
out = __alloc(decimals << 1, idof<String>());
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
return changetype<String>(out); // retains
|
||||
}
|
||||
|
||||
export function itoa64(value: i64): String {
|
||||
@ -312,16 +313,16 @@ export function itoa64(value: i64): String {
|
||||
if (<u64>value <= <u64>u32.MAX_VALUE) {
|
||||
let val32 = <u32>value;
|
||||
let decimals = decimalCount32(val32) + u32(sign);
|
||||
out = allocate(decimals << 1);
|
||||
out = __alloc(decimals << 1, idof<String>());
|
||||
utoa32_core(changetype<usize>(out), val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value) + u32(sign);
|
||||
out = allocate(decimals << 1);
|
||||
out = __alloc(decimals << 1, idof<String>());
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
return changetype<String>(out); // retains
|
||||
}
|
||||
|
||||
export function itoa<T extends number>(value: T): String {
|
||||
@ -626,11 +627,12 @@ export function dtoa(value: f64): String {
|
||||
if (isNaN<f64>(value)) return "NaN";
|
||||
return select<String>("-Infinity", "Infinity", value < 0);
|
||||
}
|
||||
var temp = allocate(MAX_DOUBLE_LENGTH << 1);
|
||||
var temp = __alloc(MAX_DOUBLE_LENGTH << 1, idof<String>());
|
||||
var length = dtoa_core(temp, value);
|
||||
var result = changetype<String>(temp).substring(0, length); // registers
|
||||
discard(temp);
|
||||
return result;
|
||||
if (length < MAX_DOUBLE_LENGTH) {
|
||||
return changetype<String>(temp).substring(0, length); // retains/releases `temp`, retains return
|
||||
}
|
||||
return changetype<String>(temp); // retains
|
||||
}
|
||||
|
||||
export function itoa_stream<T extends number>(buffer: usize, offset: usize, value: T): u32 {
|
||||
@ -692,8 +694,7 @@ export function dtoa_stream(buffer: usize, offset: usize, value: f64): u32 {
|
||||
} else {
|
||||
let sign = i32(value < 0);
|
||||
let len = 8 + sign;
|
||||
let source = changetype<usize>(select<String>("-Infinity", "Infinity", sign));
|
||||
memory.copy(buffer, source, len << 1);
|
||||
memory.copy(buffer, changetype<usize>(select<String>("-Infinity", "Infinity", sign)), len << 1);
|
||||
return len;
|
||||
}
|
||||
}
|
||||
|
@ -1,158 +0,0 @@
|
||||
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
|
||||
import { __runtime_id } from "../runtime";
|
||||
import { Array } from "../array";
|
||||
import { ArrayBufferView } from "../arraybuffer";
|
||||
|
||||
/**
|
||||
* The common runtime object header prepended to all managed objects. Has a size of 16 bytes in
|
||||
* WASM32 and contains a classId (e.g. for instanceof checks), the allocation size (e.g. for
|
||||
* .byteLength and .length computation) and additional reserved fields to be used by GC. If no
|
||||
* GC is present, the HEADER is cut into half excluding the reserved fields, as indicated by
|
||||
* HEADER_SIZE.
|
||||
*/
|
||||
@unmanaged export class HEADER {
|
||||
/** Unique id of the respective class or a magic value if not yet registered.*/
|
||||
classId: u32;
|
||||
/** Size of the allocated payload. */
|
||||
payloadSize: u32;
|
||||
/** Reserved field for use by GC. */
|
||||
reserved1: usize; // itcm: tagged next
|
||||
/** Reserved field for use by GC. */
|
||||
reserved2: usize; // itcm: prev
|
||||
}
|
||||
|
||||
/** Common runtime header size. */
|
||||
// @ts-ignore: decorator
|
||||
@lazy
|
||||
export const HEADER_SIZE: usize = (offsetof<HEADER>() + AL_MASK) & ~AL_MASK;
|
||||
|
||||
/** Common runtime header magic. Used to assert registered/unregistered status. */
|
||||
// @ts-ignore: decorator
|
||||
@lazy
|
||||
export const HEADER_MAGIC: u32 = 0xA55E4B17;
|
||||
|
||||
/** Maximum byte length of any buffer-like object. */
|
||||
// @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));
|
||||
}
|
||||
|
||||
/** Allocates the memory necessary to represent a managed object of the specified size. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function allocate(payloadSize: usize): usize {
|
||||
var header = changetype<HEADER>(memory.allocate(adjust(payloadSize)));
|
||||
header.classId = HEADER_MAGIC;
|
||||
header.payloadSize = payloadSize;
|
||||
if (isDefined(__ref_collect)) {
|
||||
header.reserved1 = 0;
|
||||
header.reserved2 = 0;
|
||||
}
|
||||
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 {
|
||||
// Background: When managed objects are allocated these aren't immediately registered with GC
|
||||
// but can be used as scratch objects while unregistered. This is useful in situations where
|
||||
// the object must be reallocated multiple times because its final size isn't known beforehand,
|
||||
// e.g. in Array#filter, with only the final object making it into GC'ed userland.
|
||||
var header = changetype<HEADER>(ref - HEADER_SIZE);
|
||||
var payloadSize = header.payloadSize;
|
||||
if (payloadSize < newPayloadSize) {
|
||||
let newAdjustedSize = adjust(newPayloadSize);
|
||||
if (select(adjust(payloadSize), 0, ref > HEAP_BASE) < newAdjustedSize) {
|
||||
// move if the allocation isn't large enough or not a heap object
|
||||
let newHeader = changetype<HEADER>(memory.allocate(newAdjustedSize));
|
||||
newHeader.classId = header.classId;
|
||||
if (isDefined(__ref_collect)) {
|
||||
newHeader.reserved1 = 0;
|
||||
newHeader.reserved2 = 0;
|
||||
}
|
||||
let newRef = changetype<usize>(newHeader) + HEADER_SIZE;
|
||||
memory.copy(newRef, ref, payloadSize);
|
||||
memory.fill(newRef + payloadSize, 0, newPayloadSize - payloadSize);
|
||||
if (header.classId == HEADER_MAGIC) {
|
||||
// free right away if not registered yet
|
||||
assert(ref > HEAP_BASE); // static objects aren't scratch objects
|
||||
memory.free(changetype<usize>(header));
|
||||
} else if (isDefined(__ref_collect)) {
|
||||
// if previously registered, register again
|
||||
// @ts-ignore: stub
|
||||
__ref_register(ref);
|
||||
}
|
||||
header = newHeader;
|
||||
ref = newRef;
|
||||
} else {
|
||||
// otherwise just clear additional memory within this block
|
||||
memory.fill(ref + payloadSize, 0, newPayloadSize - payloadSize);
|
||||
}
|
||||
} else {
|
||||
// if the size is the same or less, just update the header accordingly.
|
||||
// unused space is cleared when grown, so no need to do this here.
|
||||
}
|
||||
header.payloadSize = newPayloadSize;
|
||||
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 {
|
||||
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);
|
||||
memory.free(changetype<usize>(header));
|
||||
} else {
|
||||
memory.free(changetype<usize>(ref - HEADER_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
/** Registers a managed object of the kind represented by the specified runtime id. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
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 = id;
|
||||
} else {
|
||||
changetype<HEADER>(ref - HEADER_SIZE).classId = id;
|
||||
}
|
||||
if (isDefined(__ref_register)) __ref_register(ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
export function makeArray(length: i32, alignLog2: usize, id: u32, data: usize = 0): usize {
|
||||
var array = register(allocate(offsetof<i32[]>()), id);
|
||||
var bufferSize = <usize>length << alignLog2;
|
||||
var buffer = register(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), length, offsetof<i32[]>("length_"));
|
||||
if (data) memory.copy(buffer, data, bufferSize);
|
||||
return array;
|
||||
}
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
export function NEWARRAY<T>(length: i32): Array<T> {
|
||||
return changetype<Array<T>>(makeArray(length, alignof<T>(), __runtime_id<Array<T>>(), 0));
|
||||
}
|
Reference in New Issue
Block a user