# Core

`com.jsmacrosce.jsmacros.core.Core`

Connects a runtime's profile, configuration, event and library registries, extensions, services, and script execution facilities.  
```js
const Client = Java.type('com.jsmacrosce.jsmacros.client.JsMacrosClient');  
const core = Client.clientCore;  
Chat.log(core.profile.getCurrentProfileName());  
```

## Type Parameters

- `T`: profile implementation type
- `U`: event registry factory result type

## Constructors

### new Core {#com.jsmacrosce.jsmacros.core.Core_constructor-Function-BiFunction-File-File-Logger-}

```js
new Core(Function<Core<T extends BaseProfile, U extends BaseEventRegistry>, U extends BaseEventRegistry> eventRegistryFunction, BiFunction<Core<T extends BaseProfile, U extends BaseEventRegistry>, Logger, T extends BaseProfile> profileFunction, File configFolder, File macroFolder, Logger logger)
```

Creates the event registry, loads configuration, creates the profile and managers, then calls profile initialization with the configured default name and loads configured services, starting enabled entries.  
The event-registry factory runs first and receives this partially initialized core; the profile factory runs after configuration setup but before the extension loader and service manager are assigned.

**Parameters:**
- `eventRegistryFunction`: factory that creates the event registry from this core
- `profileFunction`: factory that creates the profile from this core and logger
- `configFolder`: configuration directory
- `macroFolder`: script directory
- `logger`: diagnostics logger

## Fields

### config {#com.jsmacrosce.jsmacros.core.Core_config}

```js
config: ConfigManager
```

Configuration manager associated with this core.

### eventRegistry {#com.jsmacrosce.jsmacros.core.Core_eventRegistry}

```js
eventRegistry: BaseEventRegistry
```

Event registry supplied by the event-registry factory.

### extensions {#com.jsmacrosce.jsmacros.core.Core_extensions}

```js
extensions: ExtensionLoader
```

Extension loader associated with this core; extensions are not necessarily loaded during construction.

### helperRegistry {#com.jsmacrosce.jsmacros.core.Core_helperRegistry}

```js
helperRegistry: ClassWrapperTree<Object, BaseHelper<?>>
```

Maps Java object types to helper wrappers; initially, unmatched objects use an `Object` fallback returning `null`.

### libraryRegistry {#com.jsmacrosce.jsmacros.core.Core_libraryRegistry}

```js
libraryRegistry: LibraryRegistry
```

Registry of scripting libraries associated with this core.

### profile {#com.jsmacrosce.jsmacros.core.Core_profile}

```js
profile: T extends BaseProfile
```

Profile instance returned by the profile factory.

### services {#com.jsmacrosce.jsmacros.core.Core_services}

```js
services: ServiceManager
```

Service manager associated with this core.

### threadPool {#com.jsmacrosce.jsmacros.core.Core_threadPool}

```js
threadPool: JsMacrosThreadPool
```

Worker pool used for scripts associated with this core.

## Methods

### addContext {#com.jsmacrosce.jsmacros.core.Core_addContext-EventContainer-}

```js
addContext(EventContainer<?> container): void
```

Adds the event container's context to the weakly tracked set.

**Parameters:**
- `container`: event container whose context is tracked

### exec {#com.jsmacrosce.jsmacros.core.Core_exec-ScriptTrigger-BaseEvent-}

```js
exec(ScriptTrigger macro, BaseEvent event): EventContainer<?>
```

Starts the trigger's script for an event and returns its event container.

**Parameters:**
- `macro`: script trigger to execute
- `event`: event supplied to the script

**Returns:** the event container for the script execution

### exec {#com.jsmacrosce.jsmacros.core.Core_exec-ScriptTrigger-BaseEvent-Runnable-Consumer-}

```js
exec(ScriptTrigger macro, BaseEvent event, Runnable then, Consumer<Throwable> catcher): EventContainer<?>
```

Starts the trigger's script and returns its event container, with optional completion and failure callbacks.

**Parameters:**
- `macro`: script trigger to execute
- `event`: event supplied to the script
- `then`: callback run after successful script execution
- `catcher`: callback given an execution failure

**Returns:** the event container for the script execution

### exec {#com.jsmacrosce.jsmacros.core.Core_exec-String-String-File-BaseEvent-Runnable-Consumer-}

```js
exec(String lang, String script, File fakeFile, BaseEvent event, Runnable then, Consumer<Throwable> catcher): EventContainer<?>
```

Starts source execution, selecting its language from `fakeFile`'s extension when supplied, or from `lang` otherwise.

**Parameters:**
- `lang`: language identifier or extension hint
- `script`: source text
- `fakeFile`: optional file used to select a language extension and as the script file; may be `null`
- `event`: event supplied to the script
- `then`: callback run after successful script execution
- `catcher`: callback given an execution failure

**Returns:** the event container for the script execution

**Since:** 1.7.0

### getContexts {#com.jsmacrosce.jsmacros.core.Core_getContexts-}

```js
getContexts(): Set<BaseScriptContext<?>>
```

Returns the tracked script contexts.

**Returns:** the live set, which holds contexts weakly and does not keep them alive

### registerHelper {#com.jsmacrosce.jsmacros.core.Core_registerHelper-Class-Class-}

```js
registerHelper(Class<E> type, Class<R extends BaseHelper<E>> wrapper): void
```

Registers a helper wrapper for objects of the specified Java type; the wrapper is constructed when the registered mapping is used. A missing or inaccessible matching constructor causes `RuntimeException` during registration. Failures invoking the constructor are wrapped in `RuntimeException` when the mapping is used.

**Parameters:**
- `type`: exact constructor parameter type accepted by the wrapper
- `wrapper`: helper class with an accessible constructor taking `type`

**Type Parameters:**
- `E`: input Java object type
- `R`: helper type for `E`

### wrapException {#com.jsmacrosce.jsmacros.core.Core_wrapException-Throwable-}

```js
wrapException(Throwable ex): BaseWrappedException<?>
```

Wraps an exception with the first language-specific wrapper available, or as a host-stack exception otherwise.

**Parameters:**
- `ex`: exception to wrap

**Returns:** the wrapped exception, or `null` when `ex` is `null`
