1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// @flow
/* eslint no-unused-vars: off */
type Bytes = number;
type Functype = [Array<Valtype>, Array<Valtype>];
type Addr = {
index: Bytes,
size: Bytes
};
type FuncAddr = Addr;
type TableAddr = Addr;
type MemAddr = Addr;
type GlobalAddr = Addr;
interface ExternalVal {
type: string;
addr: Addr;
}
type FuncExternalVal = ExternalVal & {
type: "Func"
};
type ExportInstance = {
name: string,
value: ExternalVal
};
type FuncInstance = {
type: Functype,
module: ?ModuleInstance, // its originating module
// TODO(sven): according to the spec the code property is a string
// see https://webassembly.github.io/spec/core/exec/runtime.html#function-instances
// but in the context of an interpreter it make no sense to me.
// I'll store the instructions from the function body here.
code: Array<Instruction> | Function,
isExternal: boolean
};
type GlobalInstance = {
type: Valtype,
mutability: Mutability,
value: ?NumericOperations<*>
};
type ModuleInstance = {
types: any,
funcaddrs: Array<FuncAddr>,
tableaddrs: Array<TableAddr>,
memaddrs: Array<MemAddr>,
globaladdrs: Array<GlobalAddr>,
exports: Array<ExportInstance>
};
/**
* Stack
*/
// https://webassembly.github.io/spec/core/exec/runtime.html#syntax-frame
type StackFrame = {
values: Array<any>,
globals: Array<any>,
locals: Array<StackLocal>,
labels: Array<Label>,
originatingModule: ModuleInstance,
allocator: Allocator,
trace?: (number, number, Instruction, StackFrame) => void,
returnAddress: number
};
type StackLocal = {
type: Valtype,
value: any
};
type i32 = any;
interface IntegerValue<T> extends NumericOperations<T> {
div_s(operand: T): T;
div_u(operand: T): T;
rem_s(operand: T): T;
rem_u(operand: T): T;
shl(operand: T): T;
shr_s(operand: T): T;
shr_u(operand: T): T;
rotl(operand: T): T;
rotr(operand: T): T;
and(operand: T): T;
or(operand: T): T;
xor(operand: T): T;
eq(operand: T): i32;
ne(operand: T): i32;
lt_s(operand: T): i32;
lt_u(operand: T): i32;
le_s(operand: T): i32;
le_u(operand: T): i32;
gt_s(operand: T): i32;
gt_u(operand: T): i32;
ge_s(operand: T): i32;
ge_u(operand: T): i32;
clz(): T;
popcnt(): T;
eqz(): i32;
}
interface FloatingPointValue<T, U> extends NumericOperations<T> {
min(operand: T): T;
max(operand: T): T;
copysign(operand: T): T;
neg(): T;
abs(): T;
reinterpret(): U;
eq(operand: T): i32;
}
interface NumericOperations<T> {
add(operand: T): T;
sub(operand: T): T;
mul(operand: T): T;
div(operand: T): T;
equals(operand: T): boolean;
toNumber(): number;
toString(): string;
isTrue(): boolean;
toString(): string;
// converts the number into an array of bytes - for integers
// this is in little-endian order
toByteArray(): Array<number>;
}
type Label = {
arity: number,
value: any,
id: ?Identifier
};
interface Allocator {
malloc(Bytes): Addr;
get(Addr): any;
set(Addr, any): void;
free(Addr): void;
}
interface TableInstance {
get(number): ?Hostfunc;
push(Hostfunc): void;
}
type SignatureMap = { [string]: string } | [string, string];
type IRFuncTable = {
name: string,
startAt: number
};
type IR = {
funcTable: Array<IRFuncTable>,
program: {
[number]: Instruction
}
};
|