Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor index #409

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions packages/ast/src/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,17 @@ defineType("Elem", {
unionType: ["Node"],
fields: {
table: {
type: "Index"
type: "Index",
assertNodeType: true,
},
offset: {
array: true,
type: "Instruction"
},
funcs: {
array: true,
type: "Index"
type: "Index",
assertNodeType: true,
}
}
});
Expand All @@ -289,7 +291,8 @@ defineType("IndexInFuncSection", {
unionType: ["Node"],
fields: {
index: {
type: "Index"
type: "Index",
assertNodeType: true,
}
}
});
Expand All @@ -308,7 +311,8 @@ defineType("TypeInstruction", {
fields: {
id: {
maybe: true,
type: "Index"
type: "Index",
assertNodeType: true,
},
functype: {
type: "Signature"
Expand All @@ -320,7 +324,9 @@ defineType("Start", {
unionType: ["Node"],
fields: {
index: {
type: "Index"
assertNodeType: true,
type: "Index",
assertNodeType: true,
}
}
});
Expand Down Expand Up @@ -404,7 +410,8 @@ defineType("Table", {
elements: {
array: true,
optional: true,
type: "Index"
type: "Index",
assertNodeType: true,
}
}
});
Expand All @@ -417,7 +424,8 @@ defineType("Memory", {
},
id: {
maybe: true,
type: "Index"
type: "Index",
assertNodeType: true,
}
}
});
Expand Down Expand Up @@ -456,7 +464,8 @@ defineType("ModuleExportDescr", {
type: "ExportDescrType"
},
id: {
type: "Index"
type: "Index",
assertNodeType: true,
}
}
});
Expand Down Expand Up @@ -555,6 +564,7 @@ defineType("CallInstruction", {
value: "call"
},
index: {
assertNodeType: true,
type: "Index"
},
instrArgs: {
Expand Down Expand Up @@ -599,7 +609,8 @@ defineType("Func", {
fields: {
name: {
maybe: true,
type: "Index"
type: "Index",
assertNodeType: true,
},
signature: {
type: "SignatureOrTypeRef"
Expand All @@ -620,4 +631,19 @@ defineType("Func", {
}
});

defineType("Index", {
unionType: ["Node"],
fields: {
index: {
maybe: true,
type: "Idx"
},
identifier: {
assertNodeType: true,
maybe: true,
type: "Identifier"
}
}
});

module.exports = definitions;
4 changes: 3 additions & 1 deletion packages/ast/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export {
indexLiteral,
memIndexLiteral,
instruction,
objectInstruction
objectInstruction,
indexOfIndex,
indexOfIdentifier
} from "./node-helpers.js";

export { traverse } from "./traverse";
Expand Down
26 changes: 25 additions & 1 deletion packages/ast/src/node-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow

import { assert } from "mamacro";

const {
parse32F,
parse64F,
Expand All @@ -10,7 +12,15 @@ const {
isInfLiteral
} = require("@webassemblyjs/wast-parser/lib/number-literals");

import { longNumberLiteral, floatLiteral, numberLiteral, instr } from "./nodes";
import {
assertNumberLiteral,
assertIdentifier,
longNumberLiteral,
floatLiteral,
numberLiteral,
instr,
index
} from "./nodes";

export function numberLiteralFromRaw(
rawValue: number | string,
Expand Down Expand Up @@ -113,3 +123,17 @@ export function memIndexLiteral(value: number): Memidx {
const x: U32Literal = numberLiteralFromRaw(value, "u32");
return x;
}

export function indexOfIndex(n: NumberLiteral) {
console.log(n.type);
assertNumberLiteral(n);

return index(n, null);
}

export function indexOfIdentifier(n: Identifer) {
console.log(n.type);
assertIdentifier(n);

return index(null, n);
}
74 changes: 72 additions & 2 deletions packages/ast/src/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,10 @@ export function elem(
offset: Array<Instruction>,
funcs: Array<Index>
): Elem {
assert(
table.type === "Index",
"Argument table must be of type Index, given: " + table.type
);
assert(typeof offset === "object" && typeof offset.length !== "undefined");

assert(typeof funcs === "object" && typeof funcs.length !== "undefined");
Expand All @@ -399,6 +403,11 @@ export function elem(
}

export function indexInFuncSection(index: Index): IndexInFuncSection {
assert(
index.type === "Index",
"Argument index must be of type Index, given: " + index.type
);

const node: IndexInFuncSection = {
type: "IndexInFuncSection",
index
Expand All @@ -420,6 +429,13 @@ export function typeInstruction(
id: ?Index,
functype: Signature
): TypeInstruction {
if (id !== null && id !== undefined) {
assert(
id.type === "Index",
"Argument id must be of type Index, given: " + id.type
);
}

const node: TypeInstruction = {
type: "TypeInstruction",
id,
Expand All @@ -430,6 +446,11 @@ export function typeInstruction(
}

export function start(index: Index): Start {
assert(
index.type === "Index",
"Argument index must be of type Index, given: " + index.type
);

const node: Start = {
type: "Start",
index
Expand Down Expand Up @@ -543,6 +564,13 @@ export function table(
}

export function memory(limits: Limit, id: ?Index): Memory {
if (id !== null && id !== undefined) {
assert(
id.type === "Index",
"Argument id must be of type Index, given: " + id.type
);
}

const node: Memory = {
type: "Memory",
limits,
Expand Down Expand Up @@ -593,6 +621,11 @@ export function moduleExportDescr(
exportType: ExportDescrType,
id: Index
): ModuleExportDescr {
assert(
id.type === "Index",
"Argument id must be of type Index, given: " + id.type
);

const node: ModuleExportDescr = {
type: "ModuleExportDescr",
exportType,
Expand Down Expand Up @@ -720,6 +753,11 @@ export function callInstruction(
index: Index,
instrArgs?: Array<Expression>
): CallInstruction {
assert(
index.type === "Index",
"Argument index must be of type Index, given: " + index.type
);

if (instrArgs !== null && instrArgs !== undefined) {
assert(
typeof instrArgs === "object" && typeof instrArgs.length !== "undefined"
Expand Down Expand Up @@ -778,6 +816,13 @@ export function func(
isExternal?: boolean,
metadata?: FuncMetadata
): Func {
if (name !== null && name !== undefined) {
assert(
name.type === "Index",
"Argument name must be of type Index, given: " + name.type
);
}

assert(typeof body === "object" && typeof body.length !== "undefined");

if (isExternal !== null && isExternal !== undefined) {
Expand Down Expand Up @@ -805,6 +850,24 @@ export function func(
return node;
}

export function index(index: ?Idx, identifier: ?Identifier): Index {
if (identifier !== null && identifier !== undefined) {
assert(
identifier.type === "Identifier",
"Argument identifier must be of type Identifier, given: " +
identifier.type
);
}

const node: Index = {
type: "Index",
index,
identifier
};

return node;
}

export const isModule = isTypeOf("Module");

export const isModuleMetadata = isTypeOf("ModuleMetadata");
Expand Down Expand Up @@ -885,6 +948,8 @@ export const isByteArray = isTypeOf("ByteArray");

export const isFunc = isTypeOf("Func");

export const isIndex = isTypeOf("Index");

export const isNode = (node: Node) =>
isModule(node) ||
isModuleMetadata(node) ||
Expand Down Expand Up @@ -925,7 +990,8 @@ export const isNode = (node: Node) =>
isCallInstruction(node) ||
isCallIndirectInstruction(node) ||
isByteArray(node) ||
isFunc(node);
isFunc(node) ||
isIndex(node);

export const isBlock = (node: Node) =>
isLoopInstruction(node) || isBlockInstruction(node) || isFunc(node);
Expand Down Expand Up @@ -1039,6 +1105,8 @@ export const assertByteArray = assertTypeOf("ByteArray");

export const assertFunc = assertTypeOf("Func");

export const assertIndex = assertTypeOf("Index");

export const unionTypesMap = {
Module: ["Node"],
ModuleMetadata: ["Node"],
Expand Down Expand Up @@ -1079,7 +1147,8 @@ export const unionTypesMap = {
CallInstruction: ["Node", "Instruction"],
CallIndirectInstruction: ["Node", "Instruction"],
ByteArray: ["Node"],
Func: ["Node", "Block"]
Func: ["Node", "Block"],
Index: ["Node"]
};

export const nodeAndUnionTypes = [
Expand Down Expand Up @@ -1123,6 +1192,7 @@ export const nodeAndUnionTypes = [
"CallIndirectInstruction",
"ByteArray",
"Func",
"Index",
"Node",
"Block",
"Instruction",
Expand Down
6 changes: 2 additions & 4 deletions packages/ast/src/types/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ type Memidx = U32Literal;
type Globalidx = U32Literal;
type Localidx = U32Literal;
type Labelidx = U32Literal;

type Index =
type Idx =
| Typeidx
| Funcidx
| Tableidx
| Memidx
| Globalidx
| Localidx
| Labelidx
| Identifier; // WAST shorthand
| Labelidx;

type SignatureOrTypeRef = Index | Signature;

Expand Down
10 changes: 9 additions & 1 deletion packages/ast/src/types/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ type Node =
| CallInstruction
| CallIndirectInstruction
| ByteArray
| Func;
| Func
| Index;

type Block = LoopInstruction | BlockInstruction | Func;

Expand Down Expand Up @@ -367,3 +368,10 @@ type Func = {
isExternal?: boolean,
metadata?: FuncMetadata
};

type Index = {
...BaseNode,
type: "Index",
index: ?Idx,
identifier: ?Identifier
};
2 changes: 1 addition & 1 deletion packages/ast/test/traverse.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe("AST traverse", () => {

it("should remove export in module", () => {
const root = t.module("test", [
t.moduleExport("a", t.moduleExportDescr("Func", t.indexLiteral(0)))
t.moduleExport("a", t.moduleExportDescr("Func", t.indexOfIndex(t.indexLiteral(0))))
]);

traverse(root, {
Expand Down
Loading