BaseScriptContext
com.jsmacrosce.jsmacros.core.language.BaseScriptContext
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
Constructors
new BaseScriptContext
Fields
eventListeners
Live listener-to-event-name map used by event registration; its listener keys are weakly held.
hasMethodWrapperBeenInvoked
hasMethodWrapperBeenInvoked: boolean#Whether a context-associated
MethodWrapper has been constructed; this starts false and is used by shouldKeepAlive.runner
startTime
Construction timestamp in milliseconds since the Unix epoch, not the language-execution start time.
syncObject
Weak reference to this context's synchronization object; clearing its private strong reference permits collection.
triggeringEvent
Methods
bindEvent
Associates an event container with a thread, replacing any existing association.
Parameters:
th: thread to associateevent: event container to associate
bindThread
clearSyncObject
clearSyncObject(): void#Drops the context's strong reference to its sync object.
closeContext
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(): 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
Returns the mutable map of threads to their bound event containers.
Returns: the backing map
getBoundThreads
getContainedFolder
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 if the script file has no parent
getContext
getContext(): T#Returns the language-specific runtime context.
Returns: runtime context, or
null if unsetgetFile
getMainThread
getSyncObject
getTriggeringEvent
isContextClosed
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 closedisMultiThreaded
isMultiThreaded(): boolean#Returns whether this language context supports concurrent execution on multiple threads.
Returns:
true if concurrent execution on multiple threads is supportedreleaseBoundEventIfPresent
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() 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 falsesetContext
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
shouldKeepAlive
shouldKeepAlive(): boolean#Returns whether the method-wrapper flag is set or a service requires this context to remain alive.
Returns:
true if either condition holdsunbindThread
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() acquire the event and context monitors in the same order.
Parameters:
t: bound thread; throws ScriptAssertionError if it is not bound
wrapSleep
wrapSleep(BaseScriptContext.SleepRunnable sleep): void#Runs an operation that may block; the default implementation runs it directly.
Parameters:
sleep: operation to run