From e99c3e4453980026a4a4efe97f5931bf812ee100 Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Wed, 11 Oct 2023 15:04:49 -0400 Subject: [PATCH] fix(emscripten): save and restore the stack with a run Addresses issues with multiple invocations. #760 Co-authored-by: Forrest Li --- src/pipeline/PipelineEmscriptenModule.ts | 2 ++ src/pipeline/internal/runPipelineEmscripten.ts | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/pipeline/PipelineEmscriptenModule.ts b/src/pipeline/PipelineEmscriptenModule.ts index efbde812e..a151334b9 100644 --- a/src/pipeline/PipelineEmscriptenModule.ts +++ b/src/pipeline/PipelineEmscriptenModule.ts @@ -12,6 +12,8 @@ interface PipelineEmscriptenModule extends EmscriptenModule { writeArrayToMemory: typeof writeArrayToMemory; writeAsciiToMemory: typeof writeAsciiToMemory; AsciiToString: (ptr: number) => string; + stackSave: () => number; + stackRestore: (ptr: number) => void; resetModuleStdout: () => void; resetModuleStderr: () => void; diff --git a/src/pipeline/internal/runPipelineEmscripten.ts b/src/pipeline/internal/runPipelineEmscripten.ts index d797138af..fcaad3484 100644 --- a/src/pipeline/internal/runPipelineEmscripten.ts +++ b/src/pipeline/internal/runPipelineEmscripten.ts @@ -291,6 +291,7 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args: pipelineModule.resetModuleStdout() pipelineModule.resetModuleStderr() + const stackPtr = pipelineModule.stackSave() let returnValue = 0 try { returnValue = pipelineModule.callMain(args.slice()) @@ -308,6 +309,8 @@ function runPipelineEmscripten (pipelineModule: PipelineEmscriptenModule, args: } } throw exception + } finally { + pipelineModule.stackRestore(stackPtr) } const stdout = pipelineModule.getModuleStdout() const stderr = pipelineModule.getModuleStderr()