# 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 bound threads, and removes this context from its runner.

## 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.

**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.

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

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

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

Closes this context, releases bound event locks, interrupts bound threads, and unregisters it from the runner.

### 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.

**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.

**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.

**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.

**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
