Skip to content
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
6 changes: 3 additions & 3 deletions apps/micropolis/cli/wasm/sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createNoopJsCallback } from '../../src/lib/wasm/callbacks';
import { loadMicropolisMainModule } from '../../src/lib/wasm/node';
import { normalizeStructuredFormat, stringifyStructured } from '../lib/format.js';

type Flags = { city?: string; ticks?: number; format?: string };
type Flags = { city?: string; ticks?: number; format?: string; verbose?: boolean };

function stringFlag(v: unknown, fallback: string): string {
return typeof v === 'string' && v.length > 0 ? v : fallback;
Expand Down Expand Up @@ -53,7 +53,7 @@ function summarize(
async function runSmoke(flags: Flags) {
const ticks = numberFlag(flags.ticks, 10);
const city = stringFlag(flags.city, '/cities/haight.cty');
const engine = await loadMicropolisMainModule();
const engine = await loadMicropolisMainModule(flags.verbose ?? false);
const micropolis = new engine.Micropolis();
const cb = createNoopJsCallback(engine);
micropolis.setCallback(cb, {});
Expand All @@ -69,7 +69,7 @@ async function runSmoke(flags: Flags) {

async function runInfo(flags: Flags) {
const city = stringFlag(flags.city, '/cities/haight.cty');
const engine = await loadMicropolisMainModule();
const engine = await loadMicropolisMainModule(flags.verbose ?? false);
const micropolis = new engine.Micropolis();
const cb = createNoopJsCallback(engine);
micropolis.setCallback(cb, {});
Expand Down
15 changes: 7 additions & 8 deletions apps/micropolis/src/lib/wasm/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ function isBrowserOnlyEngineError(error: unknown): boolean {
);
}

export async function loadMicropolisMainModule(): Promise<MainModule> {
export async function loadMicropolisMainModule(verbose = false): Promise<MainModule> {
assertWasmArtifactsPresent();
const wasmBinary = toArrayBuffer(readFileSync(wasmPath));
const dataBinary = toArrayBuffer(readFileSync(dataPath));
const print = verbose ? (message: string) => console.log(message) : () => {};
const printErr = verbose ? (message: string) => console.error(message) : () => {};

try {
return (await initModule({
print: () => {},
printErr: () => {},
print,
printErr,
setStatus: () => {},
locateFile: (filename: string) => path.join(projectLib, filename),
wasmBinary,
getPreloadedPackage: () => dataBinary
})) as MainModule;
} catch (nodeError) {
Expand All @@ -66,11 +66,10 @@ export async function loadMicropolisMainModule(): Promise<MainModule> {
(globalThis as { process?: unknown }).process = undefined;

return (await initModule({
print: () => {},
printErr: () => {},
print,
printErr,
setStatus: () => {},
locateFile: (filename: string) => path.join(projectLib, filename),
wasmBinary,
getPreloadedPackage: () => dataBinary
})) as MainModule;
} finally {
Expand Down