1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
[quickjs-emscripten](../README.md) / [Exports](../modules.md) / QuickJSRuntime
# Class: QuickJSRuntime
A runtime represents a Javascript runtime corresponding to an object heap.
Several runtimes can exist at the same time but they cannot exchange objects.
Inside a given runtime, no multi-threading is supported.
You can think of separate runtimes like different domains in a browser, and
the contexts within a runtime like the different windows open to the same
domain.
Create a runtime via [QuickJSWASMModule.newRuntime](QuickJSWASMModule.md#newruntime).
You should create separate runtime instances for untrusted code from
different sources for isolation. However, stronger isolation is also
available (at the cost of memory usage), by creating separate WebAssembly
modules to further isolate untrusted code.
See [newQuickJSWASMModule](../modules.md#newquickjswasmmodule).
Implement memory and CPU constraints with [setInterruptHandler](QuickJSRuntime.md#setinterrupthandler)
(called regularly while the interpreter runs), [setMemoryLimit](QuickJSRuntime.md#setmemorylimit), and
[setMaxStackSize](QuickJSRuntime.md#setmaxstacksize).
Use [computeMemoryUsage](QuickJSRuntime.md#computememoryusage) or [dumpMemoryUsage](QuickJSRuntime.md#dumpmemoryusage) to guide memory limit
tuning.
Configure ES module loading with [setModuleLoader](QuickJSRuntime.md#setmoduleloader).
## Hierarchy
- **`QuickJSRuntime`**
↳ [`QuickJSAsyncRuntime`](QuickJSAsyncRuntime.md)
## Implements
- [`Disposable`](../interfaces/Disposable.md)
## Table of contents
### Properties
- [context](QuickJSRuntime.md#context)
### Accessors
- [alive](QuickJSRuntime.md#alive)
### Methods
- [assertOwned](QuickJSRuntime.md#assertowned)
- [computeMemoryUsage](QuickJSRuntime.md#computememoryusage)
- [dispose](QuickJSRuntime.md#dispose)
- [dumpMemoryUsage](QuickJSRuntime.md#dumpmemoryusage)
- [executePendingJobs](QuickJSRuntime.md#executependingjobs)
- [hasPendingJob](QuickJSRuntime.md#haspendingjob)
- [newContext](QuickJSRuntime.md#newcontext)
- [removeInterruptHandler](QuickJSRuntime.md#removeinterrupthandler)
- [removeModuleLoader](QuickJSRuntime.md#removemoduleloader)
- [setInterruptHandler](QuickJSRuntime.md#setinterrupthandler)
- [setMaxStackSize](QuickJSRuntime.md#setmaxstacksize)
- [setMemoryLimit](QuickJSRuntime.md#setmemorylimit)
- [setModuleLoader](QuickJSRuntime.md#setmoduleloader)
## Properties
### context
• **context**: `undefined` \| [`QuickJSContext`](QuickJSContext.md)
If this runtime was created as as part of a context, points to the context
associated with the runtime.
If this runtime was created stand-alone, this may or may not contain a context.
A context here may be allocated if one is needed by the runtime, eg for [computeMemoryUsage](QuickJSRuntime.md#computememoryusage).
#### Defined in
[ts/runtime.ts:84](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L84)
## Accessors
### alive
• `get` **alive**(): `boolean`
#### Returns
`boolean`
#### Implementation of
[Disposable](../interfaces/Disposable.md).[alive](../interfaces/Disposable.md#alive)
#### Defined in
[ts/runtime.ts:126](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L126)
## Methods
### assertOwned
▸ **assertOwned**(`handle`): `void`
Assert that `handle` is owned by this runtime.
**`throws`** QuickJSWrongOwner if owned by a different runtime.
#### Parameters
| Name | Type |
| :------ | :------ |
| `handle` | [`QuickJSHandle`](../modules.md#quickjshandle) |
#### Returns
`void`
#### Defined in
[ts/runtime.ts:326](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L326)
___
### computeMemoryUsage
▸ **computeMemoryUsage**(): [`QuickJSHandle`](../modules.md#quickjshandle)
Compute memory usage for this runtime. Returns the result as a handle to a
JSValue object. Use [QuickJSContext.dump](QuickJSContext.md#dump) to convert to a native object.
Calling this method will allocate more memory inside the runtime. The information
is accurate as of just before the call to `computeMemoryUsage`.
For a human-digestible representation, see [dumpMemoryUsage](QuickJSRuntime.md#dumpmemoryusage).
#### Returns
[`QuickJSHandle`](../modules.md#quickjshandle)
#### Defined in
[ts/runtime.ts:295](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L295)
___
### dispose
▸ **dispose**(): `void`
Dispose of the underlying resources used by this object.
#### Returns
`void`
#### Implementation of
[Disposable](../interfaces/Disposable.md).[dispose](../interfaces/Disposable.md#dispose)
#### Defined in
[ts/runtime.ts:130](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L130)
___
### dumpMemoryUsage
▸ **dumpMemoryUsage**(): `string`
#### Returns
`string`
a human-readable description of memory usage in this runtime.
For programmatic access to this information, see [computeMemoryUsage](QuickJSRuntime.md#computememoryusage).
#### Defined in
[ts/runtime.ts:306](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L306)
___
### executePendingJobs
▸ **executePendingJobs**(`maxJobsToExecute?`): [`ExecutePendingJobsResult`](../modules.md#executependingjobsresult)
Execute pendingJobs on the runtime until `maxJobsToExecute` jobs are
executed (default all pendingJobs), the queue is exhausted, or the runtime
encounters an exception.
In QuickJS, promises and async functions *inside the runtime* create
pendingJobs. These do not execute immediately and need to triggered to run.
#### Parameters
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| `maxJobsToExecute` | `number` \| `void` | `-1` | When negative, run all pending jobs. Otherwise execute at most `maxJobsToExecute` before returning. |
#### Returns
[`ExecutePendingJobsResult`](../modules.md#executependingjobsresult)
On success, the number of executed jobs. On error, the exception
that stopped execution, and the context it occurred in. Note that
executePendingJobs will not normally return errors thrown inside async
functions or rejected promises. Those errors are available by calling
[resolvePromise](QuickJSContext.md#resolvepromise) on the promise handle returned by the async function.
#### Defined in
[ts/runtime.ts:240](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L240)
___
### hasPendingJob
▸ **hasPendingJob**(): `boolean`
In QuickJS, promises and async functions create pendingJobs. These do not execute
immediately and need to be run by calling [executePendingJobs](QuickJSRuntime.md#executependingjobs).
#### Returns
`boolean`
true if there is at least one pendingJob queued up.
#### Defined in
[ts/runtime.ts:191](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L191)
___
### newContext
▸ **newContext**(`options?`): [`QuickJSContext`](QuickJSContext.md)
#### Parameters
| Name | Type |
| :------ | :------ |
| `options` | [`ContextOptions`](../interfaces/ContextOptions.md) |
#### Returns
[`QuickJSContext`](QuickJSContext.md)
#### Defined in
[ts/runtime.ts:134](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L134)
___
### removeInterruptHandler
▸ **removeInterruptHandler**(): `void`
Remove the interrupt handler, if any.
See [setInterruptHandler](QuickJSRuntime.md#setinterrupthandler).
#### Returns
`void`
#### Defined in
[ts/runtime.ts:216](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L216)
___
### removeModuleLoader
▸ **removeModuleLoader**(): `void`
Remove the the loader set by [setModuleLoader](QuickJSRuntime.md#setmoduleloader). This disables module loading.
#### Returns
`void`
#### Defined in
[ts/runtime.ts:178](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L178)
___
### setInterruptHandler
▸ **setInterruptHandler**(`cb`): `void`
Set a callback which is regularly called by the QuickJS engine when it is
executing code. This callback can be used to implement an execution
timeout.
The interrupt handler can be removed with [removeInterruptHandler](QuickJSRuntime.md#removeinterrupthandler).
#### Parameters
| Name | Type |
| :------ | :------ |
| `cb` | [`InterruptHandler`](../modules.md#interrupthandler) |
#### Returns
`void`
#### Defined in
[ts/runtime.ts:204](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L204)
___
### setMaxStackSize
▸ **setMaxStackSize**(`stackSize`): `void`
Set the max stack size for this runtime, in bytes.
To remove the limit, set to `0`.
#### Parameters
| Name | Type |
| :------ | :------ |
| `stackSize` | `number` |
#### Returns
`void`
#### Defined in
[ts/runtime.ts:314](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L314)
___
### setMemoryLimit
▸ **setMemoryLimit**(`limitBytes`): `void`
Set the max memory this runtime can allocate.
To remove the limit, set to `-1`.
#### Parameters
| Name | Type |
| :------ | :------ |
| `limitBytes` | `number` |
#### Returns
`void`
#### Defined in
[ts/runtime.ts:280](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L280)
___
### setModuleLoader
▸ **setModuleLoader**(`moduleLoader`, `moduleNormalizer?`): `void`
Set the loader for EcmaScript modules requested by any context in this
runtime.
The loader can be removed with [removeModuleLoader](QuickJSRuntime.md#removemoduleloader).
#### Parameters
| Name | Type |
| :------ | :------ |
| `moduleLoader` | [`JSModuleLoader`](../interfaces/JSModuleLoader.md) |
| `moduleNormalizer?` | [`JSModuleNormalizer`](../interfaces/JSModuleNormalizer.md) |
#### Returns
`void`
#### Defined in
[ts/runtime.ts:169](https://github.com/justjake/quickjs-emscripten/blob/main/ts/runtime.ts#L169)
|