|
10 | 10 | - ["quickjs:engine".defineBuiltinModule (exported function)](#quickjsenginedefinebuiltinmodule-exported-function) |
11 | 11 | - ["quickjs:engine".ModuleDelegate (exported ModuleDelegate)](#quickjsenginemoduledelegate-exported-moduledelegate) |
12 | 12 | - ["quickjs:engine".gc (exported function)](#quickjsenginegc-exported-function) |
| 13 | + - ["quickjs:engine".StackFrameMapper (exported type)](#quickjsenginestackframemapper-exported-type) |
| 14 | + - ["quickjs:engine".setStackFrameMapper (exported function)](#quickjsenginesetstackframemapper-exported-function) |
| 15 | + - ["quickjs:engine".getStackFrameMapper (exported function)](#quickjsenginegetstackframemapper-exported-function) |
13 | 16 | - ["quickjs:engine".formatValue (exported function)](#quickjsengineformatvalue-exported-function) |
14 | 17 | - ["quickjs:engine".\_\_printObject (exported function)](#quickjsengine__printobject-exported-function) |
15 | 18 |
|
@@ -54,6 +57,22 @@ declare module "quickjs:engine" { |
54 | 57 | ): void; |
55 | 58 | export const ModuleDelegate: ModuleDelegate; |
56 | 59 | export function gc(): void; |
| 60 | + export type StackFrameMapper = ( |
| 61 | + filename: string, |
| 62 | + line: number, |
| 63 | + column: number, |
| 64 | + ) => |
| 65 | + | { |
| 66 | + filename: string; |
| 67 | + line: number; |
| 68 | + column: number; |
| 69 | + } |
| 70 | + | null |
| 71 | + | undefined; |
| 72 | + export function setStackFrameMapper( |
| 73 | + mapper: StackFrameMapper | null | undefined, |
| 74 | + ): void; |
| 75 | + export function getStackFrameMapper(): StackFrameMapper | null; |
57 | 76 | export function formatValue( |
58 | 77 | value: any, |
59 | 78 | options?: { |
@@ -221,6 +240,94 @@ function is useful in case of specific memory constraints or for testing. |
221 | 240 | export function gc(): void; |
222 | 241 | ``` |
223 | 242 |
|
| 243 | +## "quickjs:engine".StackFrameMapper (exported type) |
| 244 | + |
| 245 | +A callback that translates the location of a stack frame as an error's |
| 246 | +backtrace is built. See [setStackFrameMapper](/meta/generated-docs/engine.md#quickjsenginesetstackframemapper-exported-function) for details. |
| 247 | + |
| 248 | +`line` and `column` are 1-based, both for the values passed in and for the |
| 249 | +values returned. To change the frame's location, return an object |
| 250 | +containing all three of `filename`, `line`, and `column`. Return `null` or |
| 251 | +`undefined` (or an object missing any field) to leave the location |
| 252 | +unchanged. |
| 253 | + |
| 254 | +```ts |
| 255 | +type StackFrameMapper = ( |
| 256 | + filename: string, |
| 257 | + line: number, |
| 258 | + column: number, |
| 259 | +) => |
| 260 | + | { |
| 261 | + filename: string; |
| 262 | + line: number; |
| 263 | + column: number; |
| 264 | + } |
| 265 | + | null |
| 266 | + | undefined; |
| 267 | +``` |
| 268 | + |
| 269 | +## "quickjs:engine".setStackFrameMapper (exported function) |
| 270 | + |
| 271 | +Register a callback that translates the location of each stack frame as an |
| 272 | +error's backtrace is built. This is the hook to use for source-map support: |
| 273 | +the engine itself knows nothing about source maps, so the callback is where |
| 274 | +you map a compiled `(filename, line, column)` back to its original source |
| 275 | +location. |
| 276 | + |
| 277 | +The callback is invoked once per frame while the backtrace string is being |
| 278 | +assembled. The location it returns is used both in the human-readable |
| 279 | +`error.stack` string AND in the `fileName` / `lineNumber` / `columnNumber` |
| 280 | +own properties set on the error object, so they stay consistent. |
| 281 | + |
| 282 | +`line` and `column` are 1-based, both for the values passed to the callback |
| 283 | +and for the values it returns. |
| 284 | + |
| 285 | +To take effect, the callback must return an object containing all three of |
| 286 | +`filename`, `line`, and `column`. If it instead returns `null` or |
| 287 | +`undefined`, returns an object missing any of those fields, returns a |
| 288 | +non-object, or throws, the frame's original location is kept unchanged (a |
| 289 | +thrown error is swallowed rather than propagated into backtrace |
| 290 | +construction). |
| 291 | + |
| 292 | +Only one mapper can be registered at a time; registering a new one replaces |
| 293 | +the previous one. Pass `null` or `undefined` to unregister, restoring the |
| 294 | +default behavior of reporting compiled locations. |
| 295 | + |
| 296 | +While the mapper is running, it is temporarily disabled for any error thrown |
| 297 | +from within it, so an error thrown inside the mapper will not recurse |
| 298 | +infinitely; that nested error's backtrace simply reports its original |
| 299 | +(unmapped) locations. |
| 300 | + |
| 301 | +- `@param` _mapper_ — The translation callback, or `null`/`undefined` to unregister. |
| 302 | + |
| 303 | +```ts |
| 304 | +export function setStackFrameMapper( |
| 305 | + mapper: StackFrameMapper | null | undefined, |
| 306 | +): void; |
| 307 | +``` |
| 308 | + |
| 309 | +## "quickjs:engine".getStackFrameMapper (exported function) |
| 310 | + |
| 311 | +Return the stack frame mapper currently registered via |
| 312 | +[setStackFrameMapper](/meta/generated-docs/engine.md#quickjsenginesetstackframemapper-exported-function), or `null` if none is registered. |
| 313 | + |
| 314 | +This is useful for composing mappers: read the existing one, then register |
| 315 | +a new mapper that adds your own behavior and delegates to the previous one. |
| 316 | + |
| 317 | +```js |
| 318 | +const previous = getStackFrameMapper(); |
| 319 | +setStackFrameMapper((filename, line, column) => { |
| 320 | + const mapped = previous ? previous(filename, line, column) : null; |
| 321 | + const location = mapped ?? { filename, line, column }; |
| 322 | + // ...apply your own additional adjustments to `location`... |
| 323 | + return location; |
| 324 | +}); |
| 325 | +``` |
| 326 | +
|
| 327 | +```ts |
| 328 | +export function getStackFrameMapper(): StackFrameMapper | null; |
| 329 | +``` |
| 330 | + |
224 | 331 | ## "quickjs:engine".formatValue (exported function) |
225 | 332 |
|
226 | 333 | Format a value for debugging using QuickJS's built-in C-level printer. |
|
0 commit comments