# BaseScriptContext

`com.jsmacrosce.jsmacros.core.language.BaseScriptContext`

**Since:** 1.4.0

Holds a language runtime context and tracks threads and event containers associated with its execution.  
 Closing releases bound event locks, interrupts the threads the script owns, and removes this context
 from its runner. Engine threads that only call into the script for a callback are excluded from that
 interrupt wave, because interrupting a game or network thread mid-work corrupts state outside the script.

## Type Parameters

- `T`: language-specific runtime context type

## Nested types

- [ScriptAssertionError](BaseScriptContext.ScriptAssertionError.md)
- [SleepRunnable](BaseScriptContext.SleepRunnable.md)

## Constructors

### new BaseScriptContext {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_constructor-Core-BaseEvent-File-}

```js
new BaseScriptContext(Core<?, ?> runner, BaseEvent event, File file)
```

Initializes a context with its runner and optional triggering event and script file.

**Parameters:**
- `runner`: owning runner
- `event`: triggering event, if any
- `file`: script file, if any

## Fields

### eventListeners {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_eventListeners}

```js
eventListeners: WeakHashMap<IEventListener, String>
```

Live listener-to-event-name map used by event registration; its listener keys are weakly held.

### hasMethodWrapperBeenInvoked {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_hasMethodWrapperBeenInvoked}

```js
hasMethodWrapperBeenInvoked: boolean
```

Whether a context-associated `MethodWrapper` has been constructed; this starts `false` and is used by `shouldKeepAlive`.

### runner {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_runner}

```js
runner: Core<?, ?>
```

The `Core` that owns this context; the reference is fixed, but the runner itself is not immutable.

### startTime {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_startTime}

```js
startTime: long
```

Construction timestamp in milliseconds since the Unix epoch, not the language-execution start time.

### syncObject {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_syncObject}

```js
syncObject: WeakReference<Object>
```

Weak reference to this context's synchronization object; clearing its private strong reference permits collection.

### triggeringEvent {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_triggeringEvent}

```js
triggeringEvent: BaseEvent
```

Event supplied when this context was triggered, or `null` when none was supplied.

## Methods

### bindEvent {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_bindEvent-Thread-EventContainer-}

```js
bindEvent(Thread th, EventContainer<BaseScriptContext<T>> event): void
```

Associates an event container with a thread, replacing any existing association.

**Parameters:**
- `th`: thread to associate
- `event`: event container to associate

**Since:** 1.6.0

### bindThread {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_bindThread-Thread-}

```js
bindThread(Thread t): boolean
```

Adds a thread to this context's bound-thread set as a thread the script owns. Owned threads
 are interrupted when the context closes.

**Parameters:**
- `t`: thread to bind; `null` and binding after closure are rejected

**Returns:** `true` if the thread was not already bound

**Since:** 1.6.0

### clearSyncObject {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_clearSyncObject-}

```js
clearSyncObject(): void
```

Drops the context's strong reference to its sync object.

### closeContext {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_closeContext-boolean-}

```js
closeContext(boolean preventLog): void
```

Closes this context and sets whether the execution wrapper suppresses its cancellation log.  
 A call made after the context already closed still records the flag but performs no teardown.

**Parameters:**
- `preventLog`: whether to suppress the cancellation log

### closeContext {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_closeContext-}

```js
closeContext(): void
```

Closes this context once: releases bound event locks, interrupts the threads the script
owns, gives the language a chance to close its runtime, and unregisters the context from
the runner. Later calls do nothing.  

Threads bound only for callback dispatch through [bindCallerThread()](#) are not
interrupted here; a language runtime may still apply its own cancellation while
[doSubclassClose()](#) runs. The context monitor is released before event locks are
released and before the language runtime closes, because an interrupted thread may need to
unbind itself and closing a runtime may wait for those threads. Owned threads are selected
and interrupted under the monitor so an unbind cannot finish between those two steps.

### getBoundEvents {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getBoundEvents-}

```js
getBoundEvents(): Map<Thread, EventContainer<? extends BaseScriptContext<T>>>
```

Returns the mutable map of threads to their bound event containers.

**Returns:** the backing map

**Since:** 1.6.0

### getBoundThreads {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getBoundThreads-}

```js
getBoundThreads(): Set<Thread>
```

Returns the mutable backing set of threads bound to this context.

**Returns:** the live set of bound threads

**Since:** 1.6.0

### getContainedFolder {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getContainedFolder-}

```js
getContainedFolder(): File
```

Returns the absolute containing folder for the script file, or the absolute configured macro folder if no file was supplied.

**Returns:** the absolute folder; throws [NullPointerException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/NullPointerException.html) if the script file has no parent

**Since:** 1.6.0

### getContext {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getContext-}

```js
getContext(): T
```

Returns the language-specific runtime context.

**Returns:** runtime context, or `null` if unset

### getFile {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getFile-}

```js
getFile(): File
```

Returns the script file supplied when this context was created, if any.

**Returns:** the script file, or `null` if none was supplied

**Since:** 1.6.0

### getMainThread {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getMainThread-}

```js
getMainThread(): Thread
```

Returns the main execution thread, or `null` before it is assigned.

**Returns:** the main thread, or `null` before assignment

**Since:** 1.5.0

### getSyncObject {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getSyncObject-}

```js
getSyncObject(): Object
```

Returns the sync object while it remains strongly referenced elsewhere, or `null` after collection.

**Returns:** sync object, or `null` if collected

### getTriggeringEvent {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_getTriggeringEvent-}

```js
getTriggeringEvent(): BaseEvent
```

Returns the event that triggered this script, or `null` if none was supplied.

**Returns:** triggering event, or `null`

**Since:** 1.5.0

### isContextClosed {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_isContextClosed-}

```js
isContextClosed(): boolean
```

Returns whether the context is closed; also closes it if its weakly held sync object was cleared.  
 Closing is done outside this context's monitor so a language runtime close can wait for
 threads that need to unbind themselves.

**Returns:** `true` if closed

### isMultiThreaded {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_isMultiThreaded-}

```js
isMultiThreaded(): boolean
```

Returns whether this language context supports concurrent execution on multiple threads.

**Returns:** `true` if concurrent execution on multiple threads is supported

### releaseBoundEventIfPresent {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_releaseBoundEventIfPresent-Thread-}

```js
releaseBoundEventIfPresent(Thread thread): boolean
```

Releases the event lock for the thread's bound event container, if present.  
 The event is located under the context monitor but released after it is dropped, so this
 path and [closeContext()](#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_closeContext-) acquire the event and context monitors in the same order.

**Parameters:**
- `thread`: thread whose event lock is released

**Returns:** `true` if an event container was present; otherwise `false`

**Since:** 1.6.0

### setContext {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_setContext-T-}

```js
setContext(T context): void
```

Sets the language runtime context once; a second assignment is rejected.  
 If this context closed while the runtime was being built, the runtime is closed instead of
 published, so it cannot run after a stop.

**Parameters:**
- `context`: runtime context to store

### setMainThread {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_setMainThread-Thread-}

```js
setMainThread(Thread t): void
```

Assigns and binds the main thread; a second assignment is rejected.

**Parameters:**
- `t`: thread to assign

**Since:** 1.5.0

### shouldKeepAlive {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_shouldKeepAlive-}

```js
shouldKeepAlive(): boolean
```

Returns whether the method-wrapper flag is set or a service requires this context to remain alive.

**Returns:** `true` if either condition holds

### unbindThread {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_unbindThread-Thread-}

```js
unbindThread(Thread t): void
```

Removes a bound thread and releases its associated event lock, if present.  
 The event is located under the context monitor but released after it is dropped, so this
 path and [closeContext()](#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_closeContext-) acquire the event and context monitors in the same order.

**Parameters:**
- `t`: bound thread; throws [ScriptAssertionError](BaseScriptContext.ScriptAssertionError.md) if it is not bound

**Since:** 1.6.0

### wrapSleep {#com.jsmacrosce.jsmacros.core.language.BaseScriptContext_wrapSleep-BaseScriptContext.SleepRunnable-}

```js
wrapSleep(BaseScriptContext.SleepRunnable sleep): void
```

Runs an operation that may block; the default implementation runs it directly.

**Parameters:**
- `sleep`: operation to run
