mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-22 03:01:55 +00:00
implement __runtime_flags
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import { runtime, __runtime_id } from "../runtime";
|
||||
import { ArrayBufferView } from "../arraybuffer";
|
||||
import { allocate, register, discard } from "./runtime";
|
||||
import { CharCode } from "./string";
|
||||
import { __runtime_id } from "../runtime";
|
||||
import { ArrayBufferView } from "../arraybuffer";
|
||||
|
||||
// @ts-ignore: decorator
|
||||
@inline
|
||||
@ -263,10 +264,10 @@ export function utoa32(value: u32): String {
|
||||
if (!value) return "0";
|
||||
|
||||
var decimals = decimalCount32(value);
|
||||
var out = runtime.allocate(decimals << 1);
|
||||
var out = allocate(decimals << 1);
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
return changetype<String>(runtime.register(out, __runtime_id<String>()));
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
}
|
||||
|
||||
export function itoa32(value: i32): String {
|
||||
@ -276,12 +277,12 @@ export function itoa32(value: i32): String {
|
||||
if (sign) value = -value;
|
||||
|
||||
var decimals = decimalCount32(value) + u32(sign);
|
||||
var out = runtime.allocate(decimals << 1);
|
||||
var out = allocate(decimals << 1);
|
||||
|
||||
utoa32_core(changetype<usize>(out), value, decimals);
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return changetype<String>(runtime.register(out, __runtime_id<String>()));
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
}
|
||||
|
||||
export function utoa64(value: u64): String {
|
||||
@ -291,14 +292,14 @@ export function utoa64(value: u64): String {
|
||||
if (value <= u32.MAX_VALUE) {
|
||||
let val32 = <u32>value;
|
||||
let decimals = decimalCount32(val32);
|
||||
out = runtime.allocate(decimals << 1);
|
||||
out = allocate(decimals << 1);
|
||||
utoa32_core(out, val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value);
|
||||
out = runtime.allocate(decimals << 1);
|
||||
out = allocate(decimals << 1);
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
return changetype<String>(runtime.register(out, __runtime_id<String>()));
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
}
|
||||
|
||||
export function itoa64(value: i64): String {
|
||||
@ -311,16 +312,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 = runtime.allocate(decimals << 1);
|
||||
out = allocate(decimals << 1);
|
||||
utoa32_core(changetype<usize>(out), val32, decimals);
|
||||
} else {
|
||||
let decimals = decimalCount64(value) + u32(sign);
|
||||
out = runtime.allocate(decimals << 1);
|
||||
out = allocate(decimals << 1);
|
||||
utoa64_core(changetype<usize>(out), value, decimals);
|
||||
}
|
||||
if (sign) store<u16>(changetype<usize>(out), CharCode.MINUS);
|
||||
|
||||
return changetype<String>(runtime.register(out, __runtime_id<String>()));
|
||||
return changetype<String>(register(out, __runtime_id<String>()));
|
||||
}
|
||||
|
||||
export function itoa<T extends number>(value: T): String {
|
||||
@ -625,10 +626,10 @@ export function dtoa(value: f64): String {
|
||||
if (isNaN<f64>(value)) return "NaN";
|
||||
return select<String>("-Infinity", "Infinity", value < 0);
|
||||
}
|
||||
var temp = runtime.allocate(MAX_DOUBLE_LENGTH << 1);
|
||||
var temp = allocate(MAX_DOUBLE_LENGTH << 1);
|
||||
var length = dtoa_core(temp, value);
|
||||
var result = changetype<String>(temp).substring(0, length); // registers
|
||||
runtime.discard(temp);
|
||||
discard(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -46,6 +46,20 @@ export function adjust(payloadSize: usize): usize {
|
||||
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
|
||||
@ -91,3 +105,53 @@ export function reallocate(ref: usize, newPayloadSize: usize): usize {
|
||||
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;
|
||||
}
|
||||
|
||||
/** 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 = newObject(offsetof<i32[]>(), id);
|
||||
var bufferSize = <u32>length << alignLog2;
|
||||
var buffer = newArrayBuffer(bufferSize);
|
||||
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, <usize>bufferSize);
|
||||
return array;
|
||||
}
|
||||
|
Reference in New Issue
Block a user