Skip to content

Commit

Permalink
chore: debug build
Browse files Browse the repository at this point in the history
- add debug build
- update tests to check for memory leaks in debug mode
  • Loading branch information
skostyrko committed Aug 3, 2024
1 parent 840a219 commit 88f3469
Show file tree
Hide file tree
Showing 45 changed files with 4,279 additions and 421 deletions.
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
.prettierignore
.prettierrc.json
compile-wasm.sh
jest.config.js
src/**
test/**
7 changes: 6 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import eslintConfigPrettier from 'eslint-config-prettier';

export default tseslint.config(
{
ignores: ['src/qsplib/public/qsp-engine.js', 'dist/*', 'src/qsplib/build/*'],
ignores: [
'src/qsplib/public/qsp-engine.js',
'src/qsplib/public/qsp-engine-debug.js',
'dist/*',
'src/qsplib/build/*',
],
},
eslint.configs.recommended,
...tseslint.configs.recommended,
Expand Down
12 changes: 0 additions & 12 deletions jest.config.js

This file was deleted.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
"unpkg": "./dist/wasm-engine.umd.js",
"exports": {
".": "./dist/wasm-engine.modern.js",
"./qsp-engine.wasm": "./dist/qsp-engine.wasm"
"./wasm-engine-debug": "./dist/wasm-engine-debug.modern.js",
"./qsp-engine.wasm": "./dist/qsp-engine.wasm",
"./qsp-engine-debug.wasm": "./dist/qsp-engine-debug.wasm"
},
"types": "./dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"prebuild": "rimraf ./dist && mkdir dist && cp ./src/qsplib/public/qsp-engine.wasm ./dist/qsp-engine.wasm",
"prebuild": "rimraf ./dist && mkdir dist && cp ./src/qsplib/public/qsp-engine.wasm ./dist/qsp-engine.wasm && cp ./src/qsplib/public/qsp-engine-debug.wasm ./dist/qsp-engine-debug.wasm",
"build": "microbundle",
"test": "vitest run",
"test:watch": "vitest",
"test:debug": "DEBUG=1 vitest run",
"prepublish": "npm run build",
"release": "np",
"lint": "eslint . && tsc --noEmit"
Expand Down
4 changes: 4 additions & 0 deletions src/contracts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface QspAPI {
readVariableByKey<Name extends string>(name: Name, key: string): QspVariableType<Name>;
readVariableSize(name: string): number;
execCode(code: string): void;
execExpression(expr: string): void;
execCounter(): void;
updateUserInput(code: string): void;
execLoc(name: string): void;
Expand All @@ -39,4 +40,7 @@ export interface QspAPI {
getLocationsList(): void;
getLocationCode(name: string): string[];
getActionCode(name: string, index: number): string[];

_cleanup(): void;
_run_checks(): void;
}
2 changes: 2 additions & 0 deletions src/contracts/wasm-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,6 @@ export interface QspWasmModule extends EmscriptenModule {
_getLocationsList( count: Ptr): Ptr;
_getLocationCode(name: CharsPtr, count: Ptr): Ptr;
_getActionCode(name: CharsPtr, index: number, count: Ptr): Ptr;

__run_checks(): void;
}
27 changes: 23 additions & 4 deletions src/lib/qsp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export class QspAPIImpl implements QspAPI {
}

openGame(data: ArrayBuffer, isNewGame: boolean): void {
if (isNewGame) this._cleanup();
withBufferWrite(this.module, data, (ptr, size) =>
this.module._loadGameData(ptr, size, isNewGame ? 1 : 0),
);
Expand Down Expand Up @@ -278,7 +279,9 @@ export class QspAPIImpl implements QspAPI {
}
getActionCode(location: string, index: number): string[] {
const namePtr = this.getStaticStringPointer(location);
return withStringListRead(this.module, (ptr: Ptr) => this.module._getActionCode(namePtr, index, ptr));
return withStringListRead(this.module, (ptr: Ptr) =>
this.module._getActionCode(namePtr, index, ptr),
);
}

private init(): void {
Expand Down Expand Up @@ -318,7 +321,11 @@ export class QspAPIImpl implements QspAPI {
}
}

private registerCallback(type: QspCallType, callback: (...args: never) => unknown, signature: string): void {
private registerCallback(
type: QspCallType,
callback: (...args: never) => unknown,
signature: string,
): void {
this.module._setCallBack(type, this.module.addFunction(callback, signature));
}

Expand Down Expand Up @@ -375,7 +382,6 @@ export class QspAPIImpl implements QspAPI {
};

onMsg = (textPtr: StringPtr): void => {
this.onRefresh(false);
const text = readString(this.module, textPtr);
return asAsync(this.module, (done) => {
this.emit('msg', text, () => {
Expand All @@ -385,7 +391,6 @@ export class QspAPIImpl implements QspAPI {
};

onInput = (textPtr: StringPtr, retPtr: Ptr, maxSize: number): void => {
this.onRefresh(false);
const text = readString(this.module, textPtr);
return asAsync(this.module, (done) => {
const onInput = (inputText: string): void => {
Expand Down Expand Up @@ -520,6 +525,20 @@ export class QspAPIImpl implements QspAPI {
this.expressionValues.clear();
}

_run_checks() {
this.module.__run_checks();
}

_cleanup() {
this.clearCache();
this.variableWatchers.clear();
this.expressionWatchers.clear();
for (const ptr of this.staticStrings.values()) {
this.module._free(ptr);
}
this.staticStrings.clear();
}

private reportWatched() {
this.clearCache();
for (const updater of this.variableWatchers.values()) {
Expand Down
14 changes: 14 additions & 0 deletions src/lib/qsp-engine-debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { QspAPI } from '../contracts/api';
import createQspModule from '../qsplib/public/qsp-engine-debug';

import { QspAPIImpl } from './qsp-api';

export function initDebugQspEngine(wasmBinary: ArrayBufferView | ArrayBuffer): Promise<QspAPI> {
return new Promise((resolve) => {
createQspModule({
wasmBinary,
}).then((moduleWasm) => {
resolve(new QspAPIImpl(moduleWasm));
});
});
}
34 changes: 34 additions & 0 deletions src/qsplib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,38 @@ set_target_properties(qsp-engine PROPERTIES LINK_FLAGS "-Wno-parentheses \
-s ALLOW_MEMORY_GROWTH=1 \
-s FILESYSTEM=0 \
-s EXPORT_NAME=createQspModule \
-s DYNAMIC_EXECUTION=0")

add_executable(qsp-engine-debug ${SOURCES})
target_include_directories(qsp-engine-debug PRIVATE "${CMAKE_BINARY_DIR}/qsp-src" "${CMAKE_BINARY_DIR}/oniguruma-src" "${CMAKE_BINARY_DIR}/qsp-build")
target_link_libraries(qsp-engine-debug PRIVATE qsp onig)

target_compile_options(qsp-engine-debug PRIVATE -fsanitize=address)
target_link_options(qsp-engine-debug PRIVATE -fsanitize=address)

set_target_properties(qsp-engine-debug PROPERTIES LINK_FLAGS "-Wno-parentheses \
-Wno-empty-body \
-Wno-unsequenced \
-Wcast-align \
-Wover-aligned \
-fsanitize=address \
-gsource-map \
-O1 \
-sASSERTIONS \
-s EXPORT_ES6=1 \
-s WASM=1 \
-s RESERVED_FUNCTION_POINTERS=20 \
-s ASYNCIFY \
-s EXPORTED_RUNTIME_METHODS='[addFunction, Asyncify]' \
-s EXPORTED_FUNCTIONS='[_malloc, _free]' \
-s ENVIRONMENT='web,worker,webview,node' \
-s STACK_SIZE=5242880 \
-s ASYNCIFY_STACK_SIZE=16384 \
-s MODULARIZE=1 \
-s INVOKE_RUN=0 \
-s WARN_UNALIGNED=1 \
-s STACK_OVERFLOW_CHECK=2 \
-s ALLOW_MEMORY_GROWTH=1 \
-s FILESYSTEM=0 \
-s EXPORT_NAME=createQspModule \
-s DYNAMIC_EXECUTION=0")
5 changes: 5 additions & 0 deletions src/qsplib/public/qsp-engine-debug.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="emscripten" />
import { QspWasmModule } from '../../contracts/wasm-module';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default function Module(emscriptenArgs: any): Promise<QspWasmModule>;
Loading

0 comments on commit 88f3469

Please sign in to comment.