1
0
mirror of https://github.com/fluencelabs/assemblyscript synced 2025-06-27 13:41:48 +00:00

PSON decoder example using namespaced imports

This commit is contained in:
dcodeIO
2017-12-30 05:11:58 +01:00
parent c67f87a988
commit 2888ba14ad
41 changed files with 1283 additions and 336 deletions

@ -2182,7 +2182,7 @@ function builderEndsWith(sb: string[], code: CharCode): bool {
}
/** Converts a string to its literal representation including quotes. */
export function stringToLiteral(str: string): string {
export function stringToLiteral(str: string, singleQuoted: bool = false): string {
var ret = new Array<string>();
var off = 0;
for (var i = 0, k = str.length; i < k;) {
@ -2263,11 +2263,12 @@ export function stringToLiteral(str: string): string {
break;
}
}
var quote = singleQuoted ? "'" : "\"";
if (off == 0) {
assert(ret.length == 0);
return "\"" + str + "\"";
return quote + str + quote;
}
if (i > off)
ret.push(str.substring(off, i));
return "\"" + ret.join("") + "\"";
return quote + ret.join("") + quote;
}

@ -840,8 +840,8 @@ export class Compiler extends DiagnosticEmitter {
compileDoStatement(statement: DoStatement): ExpressionRef {
var label = this.currentFunction.enterBreakContext();
var condition = this.compileExpression(statement.condition, Type.i32);
var body = this.compileStatement(statement.statement);
var condition = this.compileExpression(statement.condition, Type.i32);
this.currentFunction.leaveBreakContext();
var breakLabel = "break|" + label;
var continueLabel = "continue|" + label;
@ -1494,8 +1494,11 @@ export class Compiler extends DiagnosticEmitter {
case Token.PERCENT:
left = this.compileExpression(expression.left, contextualType, ConversionKind.NONE);
right = this.compileExpression(expression.right, this.currentType);
if (this.currentType.isAnyFloat)
throw new Error("not implemented"); // TODO: internal fmod, possibly simply imported from JS
if (this.currentType.isAnyFloat) {
// TODO: internal fmod, possibly simply imported from JS
this.error(DiagnosticCode.Operation_not_supported, expression.range);
return this.module.createUnreachable();
}
op = this.currentType.isSignedInteger
? this.currentType.isLongInteger
? BinaryOp.RemI64

@ -15,7 +15,7 @@ export type ImportRef = usize;
export type ExportRef = usize;
export type Index = u32;
export enum NativeType {
export enum NativeType {
None = _BinaryenTypeNone(),
I32 = _BinaryenTypeInt32(),
I64 = _BinaryenTypeInt64(),
@ -796,7 +796,7 @@ export class Module {
}
// currently supports side effect free expressions only
cloneExpression(expr: ExpressionRef, noSideEffects: bool = false, maxDepth: i32 = 0x7fffffff): ExpressionRef {
cloneExpression(expr: ExpressionRef, noSideEffects: bool = false, maxDepth: i32 = i32.MAX_VALUE): ExpressionRef {
if (this.noEmit || maxDepth < 0) return 0;
var nested1: ExpressionRef,
@ -900,28 +900,24 @@ export class Relooper {
}
}
export function setAPITracing(on: bool): void {
_BinaryenSetAPITracing(on ? 1 : 0);
}
// helpers
// can't do stack allocation here: STACKTOP is a global in WASM but a hidden variable in asm.js
function allocU8Array(u8s: Uint8Array | null): usize {
if (!u8s) return 0;
var ptr = Heap.allocate((<Uint8Array>u8s).length);
var ptr = Heap.allocate(u8s.length);
var idx = ptr;
for (var i = 0, k = (<Uint8Array>u8s).length; i < k; ++i)
store<u8>(idx++, (<Uint8Array>u8s)[i]);
for (var i = 0, k = u8s.length; i < k; ++i)
store<u8>(idx++, u8s[i]);
return ptr;
}
function allocI32Array(i32s: i32[] | null): usize {
if (!i32s) return 0;
var ptr = Heap.allocate((<i32[]>i32s).length << 2);
var ptr = Heap.allocate(i32s.length << 2);
var idx = ptr;
for (var i = 0, k = (<i32[]>i32s).length; i < k; ++i) {
var val = (<i32[]>i32s)[i];
for (var i = 0, k = i32s.length; i < k; ++i) {
var val = i32s[i];
// store<i32>(idx, val) is not portable
store<u8>(idx , ( val & 0xff) as u8);
store<u8>(idx + 1, ((val >> 8) & 0xff) as u8);
@ -956,12 +952,12 @@ function stringLengthUTF8(str: string): usize {
function allocString(str: string | null): usize {
if (str == null) return 0;
var ptr = Heap.allocate(stringLengthUTF8((<string>str)) + 1);
var ptr = Heap.allocate(stringLengthUTF8(str) + 1);
var idx = ptr;
for (var i = 0, k = (<string>str).length; i < k; ++i) {
var u = (<string>str).charCodeAt(i);
for (var i = 0, k = str.length; i < k; ++i) {
var u = str.charCodeAt(i);
if (u >= 0xD800 && u <= 0xDFFF && i + 1 < k)
u = 0x10000 + ((u & 0x3FF) << 10) | ((<string>str).charCodeAt(++i) & 0x3FF);
u = 0x10000 + ((u & 0x3FF) << 10) | (str.charCodeAt(++i) & 0x3FF);
if (u <= 0x7F)
store<u8>(idx++, u as u8);
else if (u <= 0x7FF) {