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

# Class: ScriptApiContext

Defined in: [script-host/src/shape/binding.ts:144](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L144)

One pack's view of the Script API in an engine. It installs each module the pack resolves into
a namespace object, with every class, enum, constant and error class of the served version, and
dispatches every member to its registered implementation. A member with none throws
`NotImplementedError`, and the context logs it the first time the pack uses it.

## Constructors

### Constructor

> **new ScriptApiContext**(`engine`, `options`): `ScriptApiContext`

Defined in: [script-host/src/shape/binding.ts:167](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L167)

Evaluates the binding's runtime in the engine and gives it this context's host function.

#### Parameters

##### engine

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

##### options

[`ScriptApiContextOptions`](https://jsprismarine.dev/api/@jsprismarine/script-host/shape/binding/index.md#scriptapicontextoptions)

#### Returns

`ScriptApiContext`

#### Throws

When the engine cannot run the runtime.

## Methods

### bind()

> **bind**(`shape`): [`ScriptHandle`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/classes/ScriptHandle.md)

Defined in: [script-host/src/shape/binding.ts:197](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L197)

Installs a served version of a module, after the modules it depends on, and returns its
namespace. The context owns the handle and frees it in [destroy](#destroy).

#### Parameters

##### shape

[`ModuleShape`](https://jsprismarine.dev/api/@jsprismarine/script-host/shape/history/index.md#moduleshape)

#### Returns

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

#### Throws

When the pack already has another version of the module.

***

### create()

> **create**(`module`, `className`, `tag?`, `state?`): [`ScriptHandle`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/classes/ScriptHandle.md)

Defined in: [script-host/src/shape/binding.ts:233](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L233)

Makes an instance of a bound class without running its constructor, for an implementation to
return. The caller frees the handle.

#### Parameters

##### module

`string`

##### className

`string`

##### tag?

`number` = `0`

What the instance stands for, which every call of its members receives as
  `ScriptCall.tag`, such as an index into the host's own table.

##### state?

`string` = `''`

What the instance holds, which every call of its members receives as
  `ScriptCall.state`, such as an entity's id. It lives as long as the instance does.

#### Returns

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

#### Throws

When the module or the class is not bound.

***

### destroy()

> **destroy**(): `void`

Defined in: [script-host/src/shape/binding.ts:282](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L282)

Frees the context's handles. The engine keeps what scripts still refer to.

#### Returns

`void`

***

### identify()

> **identify**(`value`): [`HostInstance`](https://jsprismarine.dev/api/@jsprismarine/script-host/shape/binding/index.md#hostinstance) \| `undefined`

Defined in: [script-host/src/shape/binding.ts:246](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L246)

The tag and the state the host gave an instance when it made it, such as the entity a
`Player` passed to a member carries.

#### Parameters

##### value

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

#### Returns

[`HostInstance`](https://jsprismarine.dev/api/@jsprismarine/script-host/shape/binding/index.md#hostinstance) \| `undefined`

Undefined for a value the host did not make, which a script made with
  `Object.create` or a constructor the host did not run.

***

### makeError()

> **makeError**(`error`): [`ScriptHandle`](https://jsprismarine.dev/api/@jsprismarine/script-host/values/classes/ScriptHandle.md)

Defined in: [script-host/src/shape/binding.ts:267](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L267)

Makes an instance of one of the API's error classes without throwing it, such as the error
a promise rejects with. The caller frees the handle.

#### Parameters

##### error

[`ScriptApiError`](https://jsprismarine.dev/api/@jsprismarine/script-host/errors/classes/ScriptApiError.md)

#### Returns

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

#### Throws

When the engine cannot make it.

***

### namespace()

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

Defined in: [script-host/src/shape/binding.ts:220](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L220)

The namespace of a bound module.

#### Parameters

##### module

`string`

#### Returns

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

***

### setState()

> **setState**(`value`, `state`): `void`

Defined in: [script-host/src/shape/binding.ts:258](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/script-host/src/shape/binding.ts#L258)

Replaces the state of an instance the host made, outside a call into one of its members,
such as a drawn debug shape whose `timeLeft` the tick counts down. Leaves any other value alone.

#### Parameters

##### value

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

##### state

`string`

#### Returns

`void`
