[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/script-host](https://jsprismarine.dev/api/@jsprismarine/script-host/index.md) / ScriptEngine

# ScriptEngine

## Classes

- [ScriptEngine](https://jsprismarine.dev/api/@jsprismarine/script-host/ScriptEngine/classes/ScriptEngine.md)
- [ScriptHostLibrary](https://jsprismarine.dev/api/@jsprismarine/script-host/ScriptEngine/classes/ScriptHostLibrary.md)

## Interfaces

### DebuggerOptions

Defined in: [script-host/src/ScriptEngine.ts:120](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L120)

How [ScriptEngine.attachDebugger](https://jsprismarine.dev/api/@jsprismarine/script-host/ScriptEngine/classes/ScriptEngine.md#attachdebugger) attaches a debugger.

#### Properties

##### context?

> `readonly` `optional` **context?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:126](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L126)

The context the debugger watches, [DEFAULT\_CONTEXT](#default-context) when absent. Only its scripts stop
and poll; the other contexts' scripts run as if no debugger were attached, even in a file
with a breakpoint. A stop's frames still include every context's, each queried in its own.

##### pollLines?

> `readonly` `optional` **pollLines?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:132](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L132)

How many lines a running script runs between [DebuggerListener.poll](https://jsprismarine.dev/api/@jsprismarine/script-host/debugger/EngineDebugger/index.md#poll) calls, which let a
debugger pause a script that never ends. A count of lines, not a time, so a script polls at
the same lines every run. No polling when 0.

***

### ScriptEngineOptions

Defined in: [script-host/src/ScriptEngine.ts:106](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L106)

How [ScriptHostLibrary.createEngine](https://jsprismarine.dev/api/@jsprismarine/script-host/ScriptEngine/classes/ScriptHostLibrary.md#createengine) sets up an engine.

#### Properties

##### bufferSize?

> `readonly` `optional` **bufferSize?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:112](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L112)

The transfer buffer's first size in bytes.

##### loadModule?

> `readonly` `optional` **loadModule?**: [`ModuleLoader`](#moduleloader)

Defined in: [script-host/src/ScriptEngine.ts:114](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L114)

Supplies the source of each module a script imports.

##### memoryLimit?

> `readonly` `optional` **memoryLimit?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:108](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L108)

The bytes the engine may allocate; going over stops the script. Unlimited when absent.

##### resolveModule?

> `readonly` `optional` **resolveModule?**: [`ModuleResolver`](#moduleresolver)

Defined in: [script-host/src/ScriptEngine.ts:116](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L116)

Names the module each import reaches; [defaultModuleName](#defaultmodulename) when absent.

##### stackSize?

> `readonly` `optional` **stackSize?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:110](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L110)

The native stack, in bytes, past which a script gets `InternalError: stack overflow`.

***

### StepBudget

Defined in: [script-host/src/ScriptEngine.ts:139](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L139)

How many steps one entry from the host, and one tick, may take. Unlimited when absent.

#### Properties

##### perCallback?

> `readonly` `optional` **perCallback?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:140](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L140)

##### perTick?

> `readonly` `optional` **perTick?**: `number`

Defined in: [script-host/src/ScriptEngine.ts:141](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L141)

## Type Aliases

### EngineStats

> **EngineStats** = `{ -readonly [Key in keyof typeof Stat]: number }`

Defined in: [script-host/src/ScriptEngine.ts:40](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L40)

An engine's counters. A step is one call of QuickJS's interrupt handler, which QuickJS makes once
every 10,000 of its polls at backward jumps and calls, so the same script takes the same steps on
every machine.

***

### HostFunction

> **HostFunction** = (...`args`) => [`ScriptValue`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/index.md#scriptvalue)

Defined in: [script-host/src/ScriptEngine.ts:52](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L52)

A function scripts call to reach the host. It runs synchronously and must not return a promise.

#### Parameters

##### args

...[`ScriptValue`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/index.md#scriptvalue)[]

#### Returns

[`ScriptValue`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/index.md#scriptvalue)

***

### ModuleLoader

> **ModuleLoader** = (`name`, `context`) => `string` \| [`ScriptHandle`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/classes/ScriptHandle.md) \| `undefined`

Defined in: [script-host/src/ScriptEngine.ts:97](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L97)

Returns a module for its normalized name: its source, or an object whose own enumerable
properties become its exports. Undefined when there is none.

#### Parameters

##### name

`string`

The normalized module name.

##### context

[`ScriptContextId`](#scriptcontextid)

The id of the context that imports it.

#### Returns

`string` \| [`ScriptHandle`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/classes/ScriptHandle.md) \| `undefined`

***

### ModuleResolver

> **ModuleResolver** = (`base`, `specifier`, `context`) => `string`

Defined in: [script-host/src/ScriptEngine.ts:62](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L62)

Names the module a specifier reaches from the module that imports it. An engine loads each name
once per context, so two specifiers given the same name share one module.

#### Parameters

##### base

`string`

The name of the importing module.

##### specifier

`string`

The specifier as the import wrote it.

##### context

[`ScriptContextId`](#scriptcontextid)

The id of the context that imports it.

#### Returns

`string`

The module's name, which the [ModuleLoader](#moduleloader) is then asked for.

***

### ScriptContextId

> **ScriptContextId** = `number`

Defined in: [script-host/src/ScriptEngine.ts:100](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L100)

A context's id in its engine. The context an engine is created with is [DEFAULT\_CONTEXT](#default-context).

## Variables

### ABI\_VERSION

> `const` **ABI\_VERSION**: `4` = `4`

Defined in: [script-host/src/ScriptEngine.ts:13](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L13)

The C interface version this binding speaks, `JSH_ABI_VERSION` in the header.

***

### DEFAULT\_BUFFER\_SIZE

> `const` **DEFAULT\_BUFFER\_SIZE**: `number`

Defined in: [script-host/src/ScriptEngine.ts:43](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L43)

The bytes a new engine's transfer buffer starts with. It grows when a value needs more.

***

### DEFAULT\_CONTEXT

> `const` **DEFAULT\_CONTEXT**: [`ScriptContextId`](#scriptcontextid) = `0`

Defined in: [script-host/src/ScriptEngine.ts:103](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L103)

The context an engine is created with.

***

### DEFAULT\_POLL\_LINES

> `const` **DEFAULT\_POLL\_LINES**: `10000` = `10_000`

Defined in: [script-host/src/ScriptEngine.ts:136](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L136)

The lines between polls when [DebuggerOptions.pollLines](#polllines) is absent.

***

### DEFAULT\_STACK\_SIZE

> `const` **DEFAULT\_STACK\_SIZE**: `number`

Defined in: [script-host/src/ScriptEngine.ts:49](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L49)

The native stack a new engine's scripts may use, in bytes. A host callback runs on the same
stack, so this leaves room for Node.js's own frames under QuickJS's.

***

### Stat

> `const` **Stat**: `object`

Defined in: [script-host/src/ScriptEngine.ts:22](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L22)

The `jsh_stat` counters, by the field of [EngineStats](#enginestats) each fills.

#### Type Declaration

##### bytesFromHost

> `readonly` **bytesFromHost**: `5` = `5`

##### bytesToHost

> `readonly` **bytesToHost**: `4` = `4`

##### callbackSteps

> `readonly` **callbackSteps**: `1` = `1`

##### handles

> `readonly` **handles**: `9` = `9`

##### hostCalls

> `readonly` **hostCalls**: `3` = `3`

##### memoryLimitHits

> `readonly` **memoryLimitHits**: `7` = `7`

##### memoryUsed

> `readonly` **memoryUsed**: `8` = `8`

##### stackOverflows

> `readonly` **stackOverflows**: `6` = `6`

##### steps

> `readonly` **steps**: `0` = `0`

##### tickSteps

> `readonly` **tickSteps**: `2` = `2`

## Functions

### defaultModuleName()

> **defaultModuleName**(`base`, `specifier`): `string`

Defined in: [script-host/src/ScriptEngine.ts:71](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/ScriptEngine.ts#L71)

QuickJS's own module names: a specifier that starts with `.` is joined to the importing
module's folder, its leading `./` and `../` segments resolved, and any other is its own name.

#### Parameters

##### base

`string`

The name of the importing module.

##### specifier

`string`

The specifier as the import wrote it.

#### Returns

`string`

The module's name.
