avoid wasm symbol conflicts

This commit is contained in:
nora 2023-07-31 13:49:43 +02:00
parent 6bdbf14ecb
commit e951cd5ee1
8 changed files with 188 additions and 99 deletions

View file

@ -39,7 +39,7 @@ function resolveModItem(
case "inline": {
const contents = new Map(
mod.modKind.contents.map((item) => [item.node.name, item.id])
);
);
cx.modContentsCache.set(modId, contents);
return contents.get(name);
}
@ -52,11 +52,15 @@ function resolveModItem(
export function resolve(ast: Ast): Ast {
const cx: Context = { ast, modContentsCache: new Map() };
const rootItems = resolveModule(cx, ast.rootItems);
const rootItems = resolveModule(cx, [ast.packageName], ast.rootItems);
return { ...ast, rootItems };
}
function resolveModule(cx: Context, contents: Item[]): Item[] {
function resolveModule(
cx: Context,
modName: string[],
contents: Item[]
): Item[] {
const items = new Map<string, number>();
contents.forEach((item) => {
@ -117,6 +121,8 @@ function resolveModule(cx: Context, contents: Item[]): Item[] {
return cx.ast;
},
itemInner(item) {
const defPath = [...modName, item.node.name];
switch (item.kind) {
case "function": {
const params = item.node.params.map(({ name, span, type }) => ({
@ -143,22 +149,28 @@ function resolveModule(cx: Context, contents: Item[]): Item[] {
body,
},
id: item.id,
defPath,
};
}
case "mod": {
if (item.node.modKind.kind === "inline") {
const contents = resolveModule(cx, item.node.modKind.contents);
const contents = resolveModule(
cx,
defPath,
item.node.modKind.contents
);
return {
...item,
kind: "mod",
node: { ...item.node, modKind: { kind: "inline", contents } },
defPath,
};
}
break;
}
}
return superFoldItem(item, this);
return { ...superFoldItem(item, this), defPath };
},
expr(expr) {
switch (expr.kind) {
@ -195,9 +207,16 @@ function resolveModule(cx: Context, contents: Item[]): Item[] {
};
}
case "fieldAccess": {
if (expr.lhs.kind === "ident") {
// If the lhs is a module we need to convert this into a path.
const res = resolveIdent(expr.lhs.value);
// We convert field accesses to paths if the lhs refers to a module.
const lhs = this.expr(expr.lhs);
if (lhs.kind === "ident" || lhs.kind === "path") {
const res =
lhs.kind === "ident" ? resolveIdent(lhs.value) : lhs.res;
const segments =
lhs.kind === "ident" ? [lhs.value.name] : lhs.segments;
if (res.kind === "item") {
const module = unwrap(cx.ast.itemsById.get(res.id));
if (module.kind === "mod") {
@ -225,9 +244,9 @@ function resolveModule(cx: Context, contents: Item[]): Item[] {
return {
kind: "path",
segments: [expr.lhs.value.name, expr.field.value],
segments: [...segments, expr.field.value],
res: pathRes,
span: spanMerge(expr.lhs.span, expr.field.span),
span: spanMerge(lhs.span, expr.field.span),
};
}
}