mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 03:01:55 +00:00
more static array
This commit is contained in:
@ -40,7 +40,7 @@ export declare function CLASSID<T>(): u32;
|
||||
export declare function ITERATEROOTS(fn: (ref: usize) => void): void;
|
||||
|
||||
/** Adjusts an allocation to actual block size. Primarily targets TLSF. */
|
||||
export function ADJUST(payloadSize: usize): usize {
|
||||
function adjustToBlock(payloadSize: usize): usize {
|
||||
// round up to power of 2, e.g. with HEADER_SIZE=8:
|
||||
// 0 -> 2^3 = 8
|
||||
// 1..8 -> 2^4 = 16
|
||||
@ -52,9 +52,13 @@ export function ADJUST(payloadSize: usize): usize {
|
||||
|
||||
/** Allocates a new object and returns a pointer to its payload. Does not fill. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
@unsafe @inline
|
||||
export function ALLOCATE(payloadSize: usize): usize {
|
||||
var header = changetype<HEADER>(memory.allocate(ADJUST(payloadSize)));
|
||||
return doAllocate(payloadSize);
|
||||
}
|
||||
|
||||
function doAllocate(payloadSize: usize): usize {
|
||||
var header = changetype<HEADER>(memory.allocate(adjustToBlock(payloadSize)));
|
||||
header.classId = HEADER_MAGIC;
|
||||
header.payloadSize = payloadSize;
|
||||
if (GC_IMPLEMENTED) {
|
||||
@ -66,8 +70,12 @@ export function ALLOCATE(payloadSize: usize): usize {
|
||||
|
||||
/** Reallocates an object if necessary. Returns a pointer to its (moved) payload. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe
|
||||
@unsafe @inline
|
||||
export function REALLOCATE(ref: usize, newPayloadSize: usize): usize {
|
||||
return doReallocate(ref, newPayloadSize);
|
||||
}
|
||||
|
||||
function doReallocate(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,
|
||||
@ -75,8 +83,8 @@ export function REALLOCATE(ref: usize, newPayloadSize: usize): usize {
|
||||
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) {
|
||||
let newAdjustedSize = adjustToBlock(newPayloadSize);
|
||||
if (select(adjustToBlock(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;
|
||||
@ -114,27 +122,42 @@ export function REALLOCATE(ref: usize, newPayloadSize: usize): usize {
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @inline
|
||||
export function REGISTER<T>(ref: usize): T {
|
||||
ASSERT_UNREGISTERED(ref);
|
||||
changetype<HEADER>(ref - HEADER_SIZE).classId = CLASSID<T>();
|
||||
return changetype<T>(doRegister(ref, CLASSID<T>()));
|
||||
}
|
||||
|
||||
function doRegister(ref: usize, classId: u32): usize {
|
||||
if (!ASC_NO_ASSERT) assertUnregistered(ref);
|
||||
changetype<HEADER>(ref - HEADER_SIZE).classId = classId;
|
||||
// @ts-ignore: stub
|
||||
if (GC_IMPLEMENTED) __gc_register(ref);
|
||||
return changetype<T>(ref);
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** Links a registered object with the (registered) object now referencing it. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @inline
|
||||
export function LINK<T,TParent>(ref: T, parentRef: TParent): void {
|
||||
ASSERT_REGISTERED(changetype<usize>(ref));
|
||||
ASSERT_REGISTERED(changetype<usize>(parentRef));
|
||||
doLink(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||
}
|
||||
|
||||
function doLink(ref: usize, parentRef: usize): void {
|
||||
if (!ASC_NO_ASSERT) {
|
||||
assertRegistered(ref);
|
||||
assertRegistered(parentRef);
|
||||
}
|
||||
// @ts-ignore: stub
|
||||
if (GC_IMPLEMENTED) __gc_link(changetype<usize>(ref), changetype<usize>(parentRef));
|
||||
}
|
||||
|
||||
/** Discards an unregistered object that turned out to be unnecessary. */
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @inline
|
||||
export function DISCARD(ref: usize): void {
|
||||
ASSERT_UNREGISTERED(ref);
|
||||
doDiscard(ref);
|
||||
}
|
||||
|
||||
function doDiscard(ref: usize): void {
|
||||
if (!ASC_NO_ASSERT) assertUnregistered(ref);
|
||||
memory.free(changetype<usize>(ref - HEADER_SIZE));
|
||||
}
|
||||
|
||||
@ -142,39 +165,33 @@ export function DISCARD(ref: usize): void {
|
||||
// @ts-ignore: decorator
|
||||
@unsafe @inline
|
||||
export function WRAPARRAY<T>(buffer: ArrayBuffer): T[] {
|
||||
// TODO: this is quite a lot to compile inline
|
||||
var array = REGISTER<T[]>(ALLOCATE(offsetof<T[]>()));
|
||||
return changetype<T[]>(doWrapArray(buffer, CLASSID<T[]>(), alignof<T>()));
|
||||
}
|
||||
|
||||
function doWrapArray(buffer: ArrayBuffer, classId: u32, alignLog2: usize): usize {
|
||||
var array = doRegister(doAllocate(offsetof<i32[]>()), classId);
|
||||
var bufferSize = <usize>buffer.byteLength;
|
||||
var newBuffer = REGISTER<ArrayBuffer>(ALLOCATE(bufferSize));
|
||||
changetype<ArrayBufferView>(array).data = newBuffer; // links
|
||||
var newBuffer = doRegister(doAllocate(bufferSize), classId);
|
||||
changetype<ArrayBufferView>(array).data = changetype<ArrayBuffer>(newBuffer); // links
|
||||
changetype<ArrayBufferView>(array).dataStart = changetype<usize>(newBuffer);
|
||||
changetype<ArrayBufferView>(array).dataEnd = changetype<usize>(newBuffer) + bufferSize;
|
||||
store<i32>(changetype<usize>(array), <i32>(bufferSize >>> alignof<T>()), offsetof<T[]>("length_"));
|
||||
if (isManaged<T>()) {
|
||||
ERROR("unexpected managed type"); // not used currently
|
||||
let dataOffset: usize = 0;
|
||||
while (dataOffset < bufferSize) {
|
||||
let element: T = load<T>(changetype<usize>(buffer) + dataOffset);
|
||||
store<T>(changetype<usize>(newBuffer) + dataOffset, element);
|
||||
LINK(element, array);
|
||||
dataOffset += sizeof<T>();
|
||||
}
|
||||
} else {
|
||||
memory.copy(changetype<usize>(newBuffer), changetype<usize>(buffer), bufferSize);
|
||||
}
|
||||
return array;
|
||||
store<i32>(changetype<usize>(array), <i32>(bufferSize >>> alignLog2), offsetof<i32[]>("length_"));
|
||||
memory.copy(changetype<usize>(newBuffer), changetype<usize>(buffer), bufferSize);
|
||||
return changetype<usize>(array);
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
/** Asserts that a managed object is still unregistered. */
|
||||
function ASSERT_UNREGISTERED(ref: usize): void {
|
||||
// @ts-ignore: decorator
|
||||
function assertUnregistered(ref: usize): void {
|
||||
assert(ref > HEAP_BASE); // must be a heap object
|
||||
assert(changetype<HEADER>(ref - HEADER_SIZE).classId == HEADER_MAGIC);
|
||||
}
|
||||
|
||||
/** Asserts that a managed object has already been registered. */
|
||||
function ASSERT_REGISTERED(ref: usize): void {
|
||||
// @ts-ignore: decorator
|
||||
function assertRegistered(ref: usize): void {
|
||||
assert(ref > HEAP_BASE); // must be a heap object
|
||||
assert(changetype<HEADER>(ref - HEADER_SIZE).classId != HEADER_MAGIC);
|
||||
}
|
||||
|
22
std/portable/index.d.ts
vendored
22
std/portable/index.d.ts
vendored
@ -130,7 +130,7 @@ declare function fmod(x: f64, y: f64): f64;
|
||||
declare function fmodf(x: f32, y: f32): f32;
|
||||
|
||||
/** Converts any other numeric value to an 8-bit signed integer. */
|
||||
declare function i8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i8;
|
||||
declare function i8(value: any): i8;
|
||||
declare namespace i8 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: i8;
|
||||
@ -142,7 +142,7 @@ declare namespace i8 {
|
||||
export function parseInt(string: string, radix?: i32): i8;
|
||||
}
|
||||
/** Converts any other numeric value to a 16-bit signed integer. */
|
||||
declare function i16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i16;
|
||||
declare function i16(value: any): i16;
|
||||
declare namespace i16 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: i16;
|
||||
@ -154,7 +154,7 @@ declare namespace i16 {
|
||||
export function parseInt(string: string, radix?: i32): i16;
|
||||
}
|
||||
/** Converts any other numeric value to a 32-bit signed integer. */
|
||||
declare function i32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): i32;
|
||||
declare function i32(value: any): i32;
|
||||
declare namespace i32 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: i32;
|
||||
@ -166,7 +166,7 @@ declare namespace i32 {
|
||||
export function parseInt(string: string, radix?: i32): i32;
|
||||
}
|
||||
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) signed integer. */
|
||||
declare function isize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
|
||||
declare function isize(value: any): isize;
|
||||
declare namespace isize {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: isize;
|
||||
@ -178,7 +178,7 @@ declare namespace isize {
|
||||
export function parseInt(string: string, radix?: i32): isize;
|
||||
}
|
||||
/** Converts any other numeric value to an 8-bit unsigned integer. */
|
||||
declare function u8(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u8;
|
||||
declare function u8(value: any): u8;
|
||||
declare namespace u8 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: u8;
|
||||
@ -190,7 +190,7 @@ declare namespace u8 {
|
||||
export function parseInt(string: string, radix?: i32): u8;
|
||||
}
|
||||
/** Converts any other numeric value to a 16-bit unsigned integer. */
|
||||
declare function u16(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u16;
|
||||
declare function u16(value: any): u16;
|
||||
declare namespace u16 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: u16;
|
||||
@ -202,7 +202,7 @@ declare namespace u16 {
|
||||
export function parseInt(string: string, radix?: i32): u16;
|
||||
}
|
||||
/** Converts any other numeric value to a 32-bit unsigned integer. */
|
||||
declare function u32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): u32;
|
||||
declare function u32(value: any): u32;
|
||||
declare namespace u32 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: u32;
|
||||
@ -214,7 +214,7 @@ declare namespace u32 {
|
||||
export function parseInt(string: string, radix?: i32): u32;
|
||||
}
|
||||
/** Converts any other numeric value to a 32-bit (in WASM32) respectivel 64-bit (in WASM64) unsigned integer. */
|
||||
declare function usize(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): isize;
|
||||
declare function usize(value: any): isize;
|
||||
declare namespace usize {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: usize;
|
||||
@ -226,7 +226,7 @@ declare namespace usize {
|
||||
export function parseInt(string: string, radix?: i32): usize;
|
||||
}
|
||||
/** Converts any other numeric value to a 1-bit unsigned integer. */
|
||||
declare function bool(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): bool;
|
||||
declare function bool(value: any): bool;
|
||||
declare namespace bool {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: bool;
|
||||
@ -234,7 +234,7 @@ declare namespace bool {
|
||||
export const MAX_VALUE: bool;
|
||||
}
|
||||
/** Converts any other numeric value to a 32-bit float. */
|
||||
declare function f32(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): f32;
|
||||
declare function f32(value: any): f32;
|
||||
declare namespace f32 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: f32;
|
||||
@ -262,7 +262,7 @@ declare namespace f32 {
|
||||
export function parseInt(string: string, radix?: i32): f32;
|
||||
}
|
||||
/** Converts any other numeric value to a 64-bit float. */
|
||||
declare function f64(value: i8 | i16 | i32 | isize | u8 | u16 | u32 | usize | bool | f32 | f64): f64;
|
||||
declare function f64(value: any): f64;
|
||||
declare namespace f64 {
|
||||
/** Smallest representable value. */
|
||||
export const MIN_VALUE: f64;
|
||||
|
Reference in New Issue
Block a user