Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const index = frame.values.slice(0).reverse().findIndex(({type}) => type === "label");
// some expression like inittializer don't have labels currently, so this is
// guarantee to fail
// assertRuntimeError(index !== -1, "POP_LABEL: label not found")
if (index !== -1) {
const initialOrderIndex = frame.values.length - 1 - index;
trace("exiting block " + frame.values[initialOrderIndex].value);
frame.values.splice(initialOrderIndex, 1);
}
`
);
define(
GOTO,
labelOffset => `
pc = offsets.indexOf(String(${labelOffset}));
`
);
define(
RETURN,
() => `
const activeFrame = getActiveStackFrame();
if (activeFrame.values.length > 0) {
return pop1(activeFrame);
} else {
return;
}
);
define(
RETURN,
() => `
const activeFrame = getActiveStackFrame();
if (activeFrame.values.length > 0) {
return pop1(activeFrame);
} else {
return;
}
`
);
define(
PUSH_NEW_STACK_FRAME,
pc => `
const stackframe = require("./stackframe");
const activeFrame = getActiveStackFrame();
const newStackFrame = stackframe.createChildStackFrame(activeFrame, ${pc});
// move active frame
framepointer++;
if (framepointer >= 300) {
throw new RuntimeError("Maximum call stack depth reached");
}
// Push the frame on top of the stack
callStack[framepointer] = newStackFrame;
// @flow
import { define } from "mamacro";
import { FSM, makeTransition } from "@webassemblyjs/helper-fsm";
import { codeFrameFromSource } from "@webassemblyjs/helper-code-frame";
declare function unexpectedCharacter(): void;
/**
* Throw an error in case the current character is invalid
*/
define(unexpectedCharacter, () =>
`throw new Error(getCodeFrame(input, line, column) + "Unexpected character " + JSON.stringify(char));`);
// eslint-disable-next-line
function getCodeFrame(source: string, line: number, column: number) {
const loc = {
start: { line, column }
};
return "\n" + codeFrameFromSource(source, loc) + "\n";
}
const WHITESPACE = /\s/;
const PARENS = /\(|\)/;
const LETTERS = /[a-z0-9_/]/i;
const idchar = /[a-z0-9!#$%&*+./:<=>?@\\[\]^_`|~-]/i;
const valtypes = ["i32", "i64", "f32", "f64"];
define(GOTO, labelOffset => `
pc = offsets.indexOf(String(${labelOffset}));
`);
define(RETURN, () => `
const activeFrame = getActiveStackFrame();
if (activeFrame.values.length > 0) {
return pop1(activeFrame);
} else {
return;
}
`);
define(PUSH_NEW_STACK_FRAME, pc => `
const stackframe = require("./stackframe");
const activeFrame = getActiveStackFrame();
const newStackFrame = stackframe.createChildStackFrame(activeFrame, ${pc});
// move active frame
framepointer++;
if (framepointer >= 300) {
throw new RuntimeError("Maximum call stack depth reached");
}
// Push the frame on top of the stack
callStack[framepointer] = newStackFrame;
`);
const index = frame.values.slice(0).reverse().findIndex(({type}) => type === "label");
// some expression like inittializer don't have labels currently, so this is
// guarantee to fail
// assertRuntimeError(index !== -1, "POP_LABEL: label not found")
if (index !== -1) {
const initialOrderIndex = frame.values.length - 1 - index;
trace("exiting block " + frame.values[initialOrderIndex].value);
frame.values.splice(initialOrderIndex, 1);
}
`);
define(GOTO, labelOffset => `
pc = offsets.indexOf(String(${labelOffset}));
`);
define(RETURN, () => `
const activeFrame = getActiveStackFrame();
if (activeFrame.values.length > 0) {
return pop1(activeFrame);
} else {
return;
}
`);
define(PUSH_NEW_STACK_FRAME, pc => `
const stackframe = require("./stackframe");
import { assertRuntimeError, define } from "mamacro";
import Long from "long";
import { Memory } from "../runtime/values/memory";
import { RuntimeError } from "../../errors";
declare function trace(msg?: string): void;
declare function GOTO(l: number): void;
declare function RETURN(): void;
declare function PUSH_NEW_STACK_FRAME(pc: number): void;
declare function POP_STACK_FRAME(): void;
declare function POP_LABEL(): void;
declare function assertNItemsOnStack(n: number): void;
define(
assertNItemsOnStack,
n => `
if (frame.values.length < ${n}) {
throw new RuntimeError(
"Assertion error: expected " + JSON.stringify(${n})
+ " on the stack, found " + frame.values.length
);
}`
);
define(
trace,
msg => `
console.log("trace " + ${msg});
`
);
trace("exiting block " + frame.values[initialOrderIndex].value);
frame.values.splice(initialOrderIndex, 1);
}
`
);
define(
GOTO,
labelOffset => `
pc = offsets.indexOf(String(${labelOffset}));
`
);
define(
RETURN,
() => `
const activeFrame = getActiveStackFrame();
if (activeFrame.values.length > 0) {
return pop1(activeFrame);
} else {
return;
}
`
);
define(
PUSH_NEW_STACK_FRAME,
pc => `
const stackframe = require("./stackframe");
isLoopInstruction,
isCallInstruction,
isFuncImportDescr,
isIdentifier,
isIfInstruction,
isNumberLiteral,
internalEndAndReturn,
traverse
} from "@webassemblyjs/ast";
export const kStart = Symbol("_start");
declare function LABEL_POP(): void;
declare function LABEL_PUSH(n: Node): void;
define(LABEL_POP, () => `
this._labels.pop();
`);
define(LABEL_PUSH, node => `
this._labels.push(${node});
`);
/**
* ModuleContext
*
* TODO(sven): refactor current implementation?
*/
type Context = {
funcs: Array<{ node?: Func, isImplemented: boolean }>
};
traverse,
getEndOfSection
} from "@webassemblyjs/ast";
import {
resizeSectionByteSize,
resizeSectionVecSize,
createEmptySection,
removeSections
} from "@webassemblyjs/helper-wasm-section";
import { overrideBytesInBuffer } from "@webassemblyjs/helper-buffer";
import { getSectionForNode } from "@webassemblyjs/helper-wasm-bytecode";
import { define } from "mamacro";
declare function CHECK_END(body: Array): void;
define(CHECK_END, body => `
const body = ${body};
if (body.length === 0 || body[body.length - 1].id !== "end") {
throw new Error("expressions must be ended");
}
`);
type State = {
uint8Buffer: Uint8Array,
deltaBytes: number,
deltaElements: number
};
function shiftLocNodeByDelta(node: Node, delta: number) {
assertHasLoc(node);
isIfInstruction,
isNumberLiteral,
internalEndAndReturn,
traverse
} from "@webassemblyjs/ast";
export const kStart = Symbol("_start");
declare function LABEL_POP(): void;
declare function LABEL_PUSH(n: Node): void;
define(LABEL_POP, () => `
this._labels.pop();
`);
define(LABEL_PUSH, node => `
this._labels.push(${node});
`);
/**
* ModuleContext
*
* TODO(sven): refactor current implementation?
*/
type Context = {
funcs: Array<{ node?: Func, isImplemented: boolean }>
};
function createContext(ast: Program): Context {
const context = {
funcs: []
};