Add table section support to lib/parse

Originally part of #288
This commit is contained in:
dcodeIO 2018-10-03 03:30:00 +02:00
parent eeb8a8fd6c
commit 59e2a63b83
8 changed files with 922 additions and 834 deletions

View File

@ -7,7 +7,7 @@ import {
ExternalKind, ExternalKind,
NameType, NameType,
MAX_PAGES, MAX_PAGES,
MAX_TABLES, MAX_ELEMS,
Opcode Opcode
} from "../src/common"; } from "../src/common";
@ -199,7 +199,7 @@ export function parse(begin: usize, end: usize): void {
let type = readVarint(7) & 0x7f; let type = readVarint(7) & 0x7f;
let flags = readVaruint(1); let flags = readVaruint(1);
let initial = readVaruint(32); let initial = readVaruint(32);
let maximum: u32 = flags & 1 ? readVaruint(32) : MAX_TABLES; let maximum = flags & 1 ? readVaruint(32) : <u32>MAX_ELEMS;
opt.onTableImport( opt.onTableImport(
tbl_space_index++, tbl_space_index++,
type, type,
@ -212,7 +212,7 @@ export function parse(begin: usize, end: usize): void {
case ExternalKind.Memory: { case ExternalKind.Memory: {
let flags = readVaruint(1); let flags = readVaruint(1);
let initial = readVaruint(32); let initial = readVaruint(32);
let maximum: u32 = flags & 1 ? readVaruint(32) : MAX_PAGES; let maximum = flags & 1 ? readVaruint(32) : <u32>MAX_PAGES;
opt.onMemoryImport( opt.onMemoryImport(
mem_space_index++, mem_space_index++,
initial, initial,
@ -247,12 +247,29 @@ export function parse(begin: usize, end: usize): void {
} }
break; break;
} }
case SectionId.Table: {
let count = readVaruint(32);
for (let index: u32 = 0; index < count; ++index) {
let type = readVaruint(7) & 0x7f;
let flags = readVaruint(1);
let initial = readVaruint(32);
let maximum = flags & 1 ? readVaruint(32) : <u32>MAX_ELEMS;
opt.onTable(
tbl_space_index++,
type,
initial,
maximum,
flags
);
}
break;
}
case SectionId.Memory: { case SectionId.Memory: {
let count = readVaruint(32); let count = readVaruint(32);
for (let index: u32 = 0; index < count; ++index) { for (let index: u32 = 0; index < count; ++index) {
let flags = readVaruint(1); let flags = readVaruint(1);
let initial = readVaruint(32); let initial = readVaruint(32);
let maximum: u32 = flags ? readVaruint(32) : MAX_PAGES; let maximum = flags & 1 ? readVaruint(32) : <u32>MAX_PAGES;
opt.onMemory( opt.onMemory(
mem_space_index++, mem_space_index++,
initial, initial,

View File

@ -10,6 +10,7 @@ export declare function onMemoryImport(index: u32, initial: u32, maximum: u32, f
export declare function onGlobalImport(index: u32, type: u32, mutability: u32): void; export declare function onGlobalImport(index: u32, type: u32, mutability: u32): void;
export declare function onMemory(index: u32, initial: u32, maximum: u32, flags: u32): void; export declare function onMemory(index: u32, initial: u32, maximum: u32, flags: u32): void;
export declare function onFunction(index: u32, typeIndex: u32): void; export declare function onFunction(index: u32, typeIndex: u32): void;
export declare function onTable(index: u32, type: u32, initial: u32, maximum: u32, flags: u32): void;
export declare function onGlobal(index: u32, type: u32, mutability: u32): void; export declare function onGlobal(index: u32, type: u32, mutability: u32): void;
export declare function onExport(index: u32, kind: u32, kindIndex: u32, nameOffset: u32, nameLength: u32): void; export declare function onExport(index: u32, kind: u32, kindIndex: u32, nameOffset: u32, nameLength: u32): void;
export declare function onStart(index: u32): void; export declare function onStart(index: u32): void;

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -42,11 +42,11 @@ export enum NameType {
Local = 2 Local = 2
} }
/** Maximum number of pages. */ /** Maximum number of memory pages. */
export const MAX_PAGES = 0xffff; export const MAX_PAGES = 0xffff;
/** Maximum number of tables. */ /** Maximum number of table elements. */
export const MAX_TABLES = 1; // MVP export const MAX_ELEMS = 0xffffffff;
/** WebAssembly opcodes. */ /** WebAssembly opcodes. */
export enum Opcode { // just a few of these are actually used export enum Opcode { // just a few of these are actually used

View File

@ -31,6 +31,8 @@ export interface ParseOptions {
onMemory?(index: number, initial: number, maximum: number, flags: number): void; onMemory?(index: number, initial: number, maximum: number, flags: number): void;
/** Called with each function if the function section is evaluated. */ /** Called with each function if the function section is evaluated. */
onFunction?(index: number, typeIndex: number): void; onFunction?(index: number, typeIndex: number): void;
/** Called with each table if the table section is evaluated.*/
onTable?(index: number, type: Type, initial: number, maximum: number, flags: number): void;
/** Called with each global if the global section is evaluated. */ /** Called with each global if the global section is evaluated. */
onGlobal?(index: number, type: Type, mutability: number): void; onGlobal?(index: number, type: Type, mutability: number): void;
/** Called with the start function index if the start section is evaluated. */ /** Called with the start function index if the start section is evaluated. */
@ -82,6 +84,7 @@ export function parse(binary: Uint8Array, options?: ParseOptions): void {
"onGlobalImport", "onGlobalImport",
"onMemory", "onMemory",
"onFunction", "onFunction",
"onTable",
"onGlobal", "onGlobal",
"onExport", "onExport",
"onStart", "onStart",

View File

@ -54,6 +54,10 @@ function onFunction(funIndex: number, typeIndex: number): void {
console.log("- Function[" + funIndex + "] -> FunctionType[" + typeIndex + "]"); console.log("- Function[" + funIndex + "] -> FunctionType[" + typeIndex + "]");
} }
function onTable(tblIndex: number, type: number, initial: number, maximum: number, flags: number): void {
console.log("- Table[" + tblIndex + "] -> " + Type[type] + ": initial=" + initial + ", maximum=" + (maximum >>> 0));
}
function onGlobal(gloIndex: number, type: Type, mutability: number): void { function onGlobal(gloIndex: number, type: Type, mutability: number): void {
console.log("- Global[" + gloIndex + "]: " + (mutability & 1 ? "mutable " : "const ") + Type[type]); console.log("- Global[" + gloIndex + "]: " + (mutability & 1 ? "mutable " : "const ") + Type[type]);
} }
@ -104,6 +108,7 @@ function onLocalName(funcIndex: number, index: number, offset: number, length: n
onGlobalImport, onGlobalImport,
onMemory, onMemory,
onFunction, onFunction,
onTable,
onGlobal, onGlobal,
onStart, onStart,
onExport, onExport,