-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
5,036 additions
and
5,140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,202 +1,63 @@ | ||
import { dtoa_buffered, itoa_buffered } from "util/number"; | ||
import { OBJECT, TOTAL_OVERHEAD } from "rt/common"; | ||
// @ts-ignore | ||
@inline const MAX_LEN: usize = 1024; | ||
const STORE: usize[] = []; | ||
let STORE_LEN: usize = 0; | ||
const CACHE = memory.data(i32(MAX_LEN)); | ||
// Configurable amount of referenceable strings | ||
let POINTER = changetype<usize>(CACHE); | ||
// @ts-ignore | ||
@inline const MAX_CACHE = CACHE + MAX_LEN; | ||
import { bytes } from "../util/bytes"; | ||
import { nextPowerOf2 } from "../util/nextPowerOf2"; | ||
|
||
export namespace bs { | ||
// @ts-ignore | ||
@inline export function write_int<T extends number>(num: T): void { | ||
POINTER += itoa_buffered(POINTER, num) << 1; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_int_u<T extends number>(num: T): void { | ||
POINTER += itoa_buffered(POINTER, num) << 1; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_fl<T extends number>(num: T): void { | ||
POINTER += dtoa_buffered(POINTER, num) << 1; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_fl_u<T extends number>(num: T): void { | ||
POINTER += dtoa_buffered(POINTER, num) << 1; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_b(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void { | ||
memory.copy(POINTER, buf, bytes); | ||
POINTER += bytes; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_b_u(buf: usize, bytes: usize = changetype<OBJECT>(buf - TOTAL_OVERHEAD).rtSize): void { | ||
memory.copy(POINTER, buf, bytes); | ||
POINTER += bytes; | ||
} | ||
let maxOffset: usize = 0; | ||
|
||
// @ts-ignore | ||
@inline export function write_s(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void { | ||
memory.copy(POINTER, changetype<usize>(str), bytes); | ||
POINTER += bytes; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_s_u(str: string, bytes: usize = changetype<OBJECT>(changetype<usize>(str) - TOTAL_OVERHEAD).rtSize): void { | ||
memory.copy(POINTER, changetype<usize>(str), bytes); | ||
POINTER += bytes; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_s_se(str: string, start: usize, end: usize): void { | ||
const bytes = end - start; | ||
memory.copy(POINTER, changetype<usize>(str) + start, bytes); | ||
POINTER += bytes; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_s_se_u(str: string, start: usize, end: usize): void { | ||
const bytes = end - start; | ||
memory.copy(POINTER, changetype<usize>(str) + start, bytes); | ||
POINTER += bytes; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_8(char: i32): void { | ||
store<u8>(POINTER, char); | ||
POINTER += 2; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_8_u(char: i32): void { | ||
store<u8>(POINTER, char); | ||
//POINTER += 2; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_16(char: i32): void { | ||
store<u16>(POINTER, char); | ||
POINTER += 2; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_16_u(char: i32): void { | ||
store<u16>(POINTER, char); | ||
//POINTER += 2; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_32(chars: i32): void { | ||
store<u32>(POINTER, chars); | ||
POINTER += 4; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_32_u(chars: i32): void { | ||
store<u32>(POINTER, chars); | ||
//POINTER += 4; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_64(chars: i64): void { | ||
store<u64>(POINTER, chars); | ||
POINTER += 8; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_64_u(chars: i64): void { | ||
store<u64>(POINTER, chars); | ||
POINTER += 8; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_128(chars: v128): void { | ||
store<v128>(POINTER, chars); | ||
POINTER += 16; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function write_128_n(chars: v128, n: usize): void { | ||
store<v128>(POINTER, chars); | ||
POINTER += n; | ||
if (MAX_CACHE <= POINTER) bs.shrink(); | ||
} | ||
/** | ||
* This serves as the central buffer | ||
*/ | ||
export namespace bs { | ||
export let buffer: usize = 0; | ||
export let offset: usize = 0; | ||
export let byteLength: usize = 0; | ||
|
||
// @ts-ignore | ||
@inline export function write_128_u(chars: v128): void { | ||
store<v128>(POINTER, chars); | ||
//POINTER += 16; | ||
//if (MAX_CACHE <= POINTER) bs.shrink(); | ||
@inline export function setBuffer<T>(data: T): void { | ||
buffer = changetype<usize>(data); | ||
offset = changetype<usize>(data); | ||
byteLength = bytes(data); | ||
maxOffset = byteLength + buffer; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function shrink(): void { | ||
const len = POINTER - CACHE; | ||
STORE_LEN += len; | ||
const out = __new(len, idof<ArrayBuffer>()); | ||
memory.copy(out, CACHE, len); | ||
bs.reset(); | ||
STORE.push(out); | ||
@inline export function ensureCapacity(size: u32): void { | ||
const newSize = offset + size; | ||
if (newSize > maxOffset) { | ||
const newPtr = __renew(buffer, (byteLength = nextPowerOf2(newSize - buffer))); | ||
offset = offset - buffer + newPtr; | ||
maxOffset = newPtr + byteLength; | ||
buffer = newPtr; | ||
} | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function out<T>(): T { | ||
const len = POINTER - CACHE; | ||
let out = __new(len + STORE_LEN, idof<T>()); | ||
|
||
memory.copy(out, CACHE, len); | ||
if (STORE_LEN) { | ||
out += len; | ||
for (let i = 0; i < STORE.length; i++) { | ||
const ptr = changetype<usize>(unchecked(STORE[i])); | ||
const storeLen = changetype<OBJECT>(ptr - TOTAL_OVERHEAD).rtSize; | ||
memory.copy(out, ptr, storeLen); | ||
//__unpin(ptr); | ||
out += storeLen; | ||
} | ||
STORE_LEN = 0; | ||
@inline export function ensureSize(size: u32): void { | ||
const newSize = offset + size; | ||
if (newSize > maxOffset) { | ||
const newPtr = __renew(buffer, (byteLength = newSize - buffer)); | ||
offset = offset - buffer + newPtr; | ||
maxOffset = newPtr + byteLength; | ||
buffer = newPtr; | ||
} | ||
bs.reset(); | ||
|
||
return changetype<T>(out); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function out_u<T>(): T { | ||
const len = POINTER - CACHE; | ||
const out = __new(len + STORE_LEN, idof<T>()); | ||
|
||
memory.copy(out, CACHE, len); | ||
bs.reset(); | ||
|
||
return changetype<T>(out); | ||
@inline export function shrink(): void { | ||
byteLength = offset - buffer; | ||
buffer = __renew(buffer, byteLength); | ||
maxOffset = byteLength + buffer; | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function _out(out: usize): void { | ||
memory.copy(out, CACHE, POINTER - CACHE); | ||
@inline export function shrinkTo<T>(): T { | ||
shrink(); | ||
offset = buffer; | ||
return changetype<T>(buffer); | ||
} | ||
|
||
// @ts-ignore | ||
@inline export function reset(): void { | ||
POINTER = CACHE; | ||
@inline export function to<T>(): T { | ||
offset = buffer; | ||
return changetype<T>(buffer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.