Skip to content

Commit 420555a

Browse files
committed
docs for debug mode changes
1 parent ac63996 commit 420555a

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

README.md

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,9 @@ build variant of quickjs-emscripten, and also the [DEBUG_SYNC] build variant.
561561
The debug sync build variant has extra instrumentation code for detecting memory
562562
leaks.
563563

564-
The class [TestQuickJSWASMModule] exposes the memory leak detection API, although
565-
this API is only accurate when using `DEBUG_SYNC` variant.
564+
The class [TestQuickJSWASMModule] exposes the memory leak detection API,
565+
although this API is only accurate when using `DEBUG_SYNC` variant. You can also
566+
enable [debug logging](#debugging) to help diagnose failures.
566567

567568
```typescript
568569
// Define your test suite in a function, so that you can test against
@@ -744,40 +745,51 @@ You can use quickjs-emscripten directly from an HTML file in two ways:
744745

745746
### Debugging
746747

747-
- Switch to a DEBUG build variant of the WebAssembly module to see debug log messages from the C part of this library:
748+
Debug logging can be enabled globally, or for specific runtimes. You need to use a DEBUG build variant of the WebAssembly module to see debug log messages from the C part of this library.
748749

749-
```typescript
750-
import { newQuickJSWASMModule, DEBUG_SYNC } from "quickjs-emscripten"
750+
```typescript
751+
import { newQuickJSWASMModule, DEBUG_SYNC } from "quickjs-emscripten"
752+
753+
const QuickJS = await newQuickJSWASMModule(DEBUG_SYNC)
754+
```
755+
756+
With quickjs-emscripten-core:
751757

752-
const QuickJS = await newQuickJSWASMModule(DEBUG_SYNC)
753-
```
758+
```typescript
759+
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
760+
import DEBUG_SYNC from "@jitl/quickjs-wasmfile-debug-sync"
761+
762+
const QuickJS = await newQuickJSWASMModuleFromVariant(DEBUG_SYNC)
763+
```
754764

755-
With quickjs-emscripten-core:
765+
To enable debug logging globally, call [setDebugMode][setDebugMode]. This affects global Javascript parts of the library, like the module loader and asyncify internals, and is inherited by runtimes created after the call.
756766

757-
```typescript
758-
import { newQuickJSWASMModuleFromVariant } from "quickjs-emscripten-core"
759-
import DEBUG_SYNC from "@jitl/quickjs-wasmfile-debug-sync"
767+
```typescript
768+
import { setDebugMode } from "quickjs-emscripten"
760769

761-
const QuickJS = await newQuickJSWASMModuleFromVariant(DEBUG_SYNC)
762-
```
770+
setDebugMode(true)
771+
```
763772

764-
- Enable debug log messages from the Javascript part of this library with [setDebugMode][setDebugMode]:
773+
With quickjs-emscripten-core:
765774

766-
```typescript
767-
import { setDebugMode } from "quickjs-emscripten"
775+
```typescript
776+
import { setDebugMode } from "quickjs-emscripten-core"
768777

769-
setDebugMode(true)
770-
```
778+
setDebugMode(true)
779+
```
771780

772-
With quickjs-emscripten-core:
781+
To enable debug logging for a specific runtime, call [setDebugModeRt][setDebugModeRt]. This affects only the runtime and its associated contexts.
773782

774-
```typescript
775-
import { setDebugMode } from "quickjs-emscripten-core"
783+
```typescript
784+
const runtime = QuickJS.newRuntime()
785+
runtime.setDebugMode(true)
776786

777-
setDebugMode(true)
778-
```
787+
const context = QuickJS.newContext()
788+
context.runtime.setDebugMode(true)
789+
```
779790

780791
[setDebugMode]: doc/quickjs-emscripten/exports.md#setdebugmode
792+
[setDebugModeRt]: https://github.com/justjake/quickjs-emscripten/blob/main/doc/quickjs-emscripten-core/classes/QuickJSRuntime.md#setdebugmode
781793

782794
### Supported Platforms
783795

packages/quickjs-emscripten-core/src/debug.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
export let QTS_DEBUG = false
66

77
/**
8-
* Enable (or disable) debug logging and object creation tracking in the Javascript API.
8+
* Enable (or disable) debug logging and object creation tracking globally.
9+
* This setting is inherited by newly created QuickJSRuntime instances.
910
* To get debug logging in the WebAssembly module, you need to use a debug build variant.
11+
* See [the quickjs-emscripten-core README](https://github.com/justjake/quickjs-emscripten/tree/main/doc/quickjs-emscripten-core) for more about build variants.
1012
*/
1113
export function setDebugMode(enabled: boolean = true) {
1214
QTS_DEBUG = enabled
1315
}
1416

17+
export function isDebugMode() {
18+
return QTS_DEBUG
19+
}
20+
1521
/**
1622
* @private
1723
*/

packages/quickjs-emscripten-core/src/runtime.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
} from "@jitl/quickjs-ffi-types"
99
import { maybeAsyncFn } from "./asyncify-helpers"
1010
import { QuickJSContext } from "./context"
11-
import { debugLog } from "./debug"
11+
import { QTS_DEBUG, debugLog } from "./debug"
1212
import { QuickJSWrongOwner } from "./errors"
1313
import type { Disposable } from "./lifetime"
1414
import { DisposableResult, Lifetime, Scope, UsingDisposable } from "./lifetime"
@@ -116,6 +116,10 @@ export class QuickJSRuntime extends UsingDisposable implements Disposable {
116116
this.callbacks.setRuntimeCallbacks(this.rt.value, this.cToHostCallbacks)
117117

118118
this.executePendingJobs = this.executePendingJobs.bind(this)
119+
120+
if (QTS_DEBUG) {
121+
this.setDebugMode(true)
122+
}
119123
}
120124

121125
get alive() {

0 commit comments

Comments
 (0)