Skip to content

Commit

Permalink
Merge pull request #8 from QSPFoundation/tests/ExtraTestForGotoOnSpec…
Browse files Browse the repository at this point in the history
…ialHandler

Test global var if ONGSAVE calls GOTO
  • Loading branch information
srg-kostyrko authored Sep 29, 2024
2 parents 2b93122 + bc1c220 commit da4f9e7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
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

0 comments on commit da4f9e7

Please sign in to comment.