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

Test global var if ONGSAVE calls GOTO #8

Merged
merged 2 commits into from
Sep 29, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/lib/qsp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ export class QspAPIImpl implements QspAPI {
const ptr = allocErrorInfoPointer(this.module);
this.module._getLastError(ptr);
const error = readError(this.module, ptr);
this.emit('error', error);
if (error.errorCode > 0)
this.emit('error', error);
this.module._free(ptr);
};

Expand Down
Binary file modified src/qsplib/public/qsp-engine-debug.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/qsplib/public/qsp-engine-debug.wasm.map

Large diffs are not rendered by default.

Binary file modified src/qsplib/public/qsp-engine.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion src/qsplib/src/qsp_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void *saveGameData(int *realSize)
if (!fileSize)
{
free(fileData);
return fileData;
return 0;
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/variables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,44 @@ $glob_test = $test
expect(api.readVariable('$glob_test')).toBe("value");
});

test('global variables get restored if ONGSAVE calls goto', () => {
const onSaveGame = vi.fn((_, callback) => { api.saveGame(); callback(); });
api.on('save_game', onSaveGame);

runTestFile(api,
`
$ongsave = 'other'
$test='value'
local $test='value 1'
if 1:
local $test='value 2'
$last_loc_test1 = $test
savegame 'test.sav'
$last_loc_test2 = $test
end
---
# other
$glob_test1 = $test
gt 'new'
---
# new
$glob_test2 = $test
act 'test value':
$glob_test3 = $test
end
`);

api.selectAction(0);
api.execSelectedAction();

expect(api.readVariable('$last_loc_test1')).toBe("value 2");
expect(onSaveGame).toHaveBeenCalledWith('test.sav', expect.any(Function));
expect(api.readVariable('$last_loc_test2')).toBe("");
expect(api.readVariable('$glob_test1')).toBe("value");
expect(api.readVariable('$glob_test2')).toBe("value");
expect(api.readVariable('$glob_test3')).toBe("value");
});

test('local variables in nested calls are preserved (shadowing global)', () => {
runTestFile(
api,
Expand Down