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

[WIP] Async #48

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
4 changes: 2 additions & 2 deletions packages/compiler/src/__tests__/errorsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ const testCases = fs.readdirSync(DIR);
testCases.forEach((filename: string) => {
const testFunc = filename.startsWith("only.") ? test.only : test;
const testName = filename.replace(/^only\./, "");
testFunc(`${testName}`, () => {
testFunc(`${testName}`, async () => {
const eel = fs.readFileSync(path.join(DIR, filename), { encoding: "utf8" });
let compilerError = null;
try {
compileModule({
await compileModule({
pools: { main: new Set() },
functions: { run: { pool: "main", code: eel } },
});
Expand Down
11 changes: 6 additions & 5 deletions packages/compiler/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ type CompilerOptions = {
preParsed?: boolean;
};

export function compileModule({
export async function compileModule({
pools,
functions: funcs,
eelVersion = 2,
preParsed = false,
}: CompilerOptions) {
}: CompilerOptions): Promise<Uint8Array> {
if (Object.keys(pools).includes("shims")) {
throw new Error(
'You may not name a pool "shims". "shims" is reserved for injected JavaScript functions.'
Expand Down Expand Up @@ -81,7 +81,7 @@ export function compileModule({
localVariables: number[];
}[] = [];

Object.entries(funcs).forEach(([name, { pool, code }]) => {
for (const [name, { pool, code }] of Object.entries(funcs)) {
if (pools[pool] == null) {
const poolsList = Object.keys(pools);
if (poolsList.length === 0) {
Expand All @@ -108,7 +108,7 @@ export function compileModule({
throw new Error("Invalid AST");
}
if (ast.body.length === 0) {
return;
continue;
}
const localVariables: number[] = [];
const context: CompilerContext = {
Expand Down Expand Up @@ -160,7 +160,8 @@ export function compileModule({
returns: [],
localVariables,
});
});
await Promise.resolve();
}

const localFuncs = localFuncOrder.map(name => {
const func = localFuncMap[name];
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function loadModule({
Object.entries(pools).forEach(([key, globals]) => {
compilerPools[key] = new Set(Object.keys(globals));
});
const buffer = compileModule({
const buffer = await compileModule({
pools: compilerPools,
functions,
eelVersion,
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/tools/__tests__/binary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test("Can execute hand crafted binary Wasm", async () => {
shims,
};

const buffer = compileModule({
const buffer = await compileModule({
pools: {
main: new Set(Object.keys(importObject.main)),
},
Expand Down
14 changes: 7 additions & 7 deletions packages/compiler/tools/parseMilk.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ const BAD = new Set([
"fixtures/mega/Telek & EMPR - Hell Cave (Fiddle A Bit (Flicker @xis) Mix).milk", // vnum_increment = ;
]);

function validate(milkPath, context) {
async function validate(milkPath, context) {
if (BAD.has(milkPath)) {
return;
}
const presetIni = fs.readFileSync(milkPath, { encoding: "utf8" });
const eels = getEels(presetIni);

Object.entries(eels).forEach(([name, eel]) => {
for (const [name, eel] of Object.entries(eels)) {
try {
const root = parse(eel);
compileModule({
await compileModule({
globals: new Set(),
functions: { run: root },
shims,
Expand All @@ -155,7 +155,7 @@ function validate(milkPath, context) {
}
throw e;
}
});
}
}

let milkFiles;
Expand Down Expand Up @@ -184,10 +184,10 @@ const context = {
const errors = {};
let good = 0;
let bad = 0;
milkFiles.forEach(milk => {
for (const milk of milkFiles) {
// console.log(`Validating eel in "${milk}"...`);
try {
validate(milk, context);
await validate(milk, context);
good++;
} catch (e) {
const messageLines = e.message.split("\n");
Expand All @@ -209,7 +209,7 @@ milkFiles.forEach(milk => {
}
bad++;
}
});
}

if (bad === 0) {
console.log("No errors found!");
Expand Down
25 changes: 13 additions & 12 deletions packages/playground/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,20 @@ export function useWasm(code, globals, eelVersion) {
if (code == null) {
return;
}
try {
const wasm = compileModule({
functions: {
main: { pool: "main", code }
},
pools: { main: new Set(Object.keys(globals)) },
eelVersion
compileModule({
functions: {
main: { pool: "main", code }
},
pools: { main: new Set(Object.keys(globals)) },
eelVersion
})
.then(wasm => {
setWasm(wasm);
setWasmError(null);
})
.catch(e => {
setWasmError(e);
});
setWasm(wasm);
setWasmError(null);
} catch (e) {
setWasmError(e);
}
}, [code, eelVersion, globals]);

return [wasm, wasmError, eelVersion];
Expand Down