# LibraryRegistry

`com.jsmacrosce.jsmacros.core.library.LibraryRegistry`

Registers library classes and supplies applicable instances as globals for script runs.  
 Instances are shared, created per language, per execution, or per execution and language,  
 according to the library superclass.

## Constructors

### new LibraryRegistry {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_constructor-Core-}

```js
new LibraryRegistry(Core<?, ?> runner)
```

Stores the core used to instantiate shared and per-language libraries during registration.

**Parameters:**
- `runner`: core used for subsequent library instantiation

## Fields

### libraries {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_libraries}

```js
libraries: Map<Library, BaseLibrary>
```

Libraries instantiated once when registered and then shared, keyed by their
 [Library](Library.md) annotation.

### perExec {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_perExec}

```js
perExec: Map<Library, Class<? extends PerExecLibrary>>
```

Per-execution library classes; each applicable class is instantiated for a script run
 using a [BaseScriptContext](../language/BaseScriptContext.md).

### perExecLanguage {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_perExecLanguage}

```js
perExecLanguage: Map<Class<? extends BaseLanguage<?, ?>>, Map<Library, Class<? extends PerExecLanguageLibrary<?, ?>>>>
```

Per-execution, per-language library classes, keyed by their registered language class.  
 Applicable classes are instantiated for each run with its context and the language class.

### perLanguage {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_perLanguage}

```js
perLanguage: Map<Class<? extends BaseLanguage<?, ?>>, Map<Library, PerLanguageLibrary>>
```

Per-language instances, keyed by the language class used at registration. A language class
 also receives entries registered for its superclasses.

## Methods

### addLibraries {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_addLibraries-Iterable-}

```js
addLibraries(Iterable<Class<? extends BaseLibrary>> classes): void
```

Registers several libraries as one batch. If any registration fails, every registry map is
 restored to its state before the batch and the failure is rethrown, so an extension whose
 later library fails does not leave its earlier globals usable.

**Parameters:**
- `classes`: library classes to register in order

### addLibrary {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_addLibrary-Class-}

```js
addLibrary(Class<? extends BaseLibrary> clazz): void
```

Registers a library class, instantiating shared and per-language kinds immediately and
 storing per-execution kinds for later construction. The superclass determines the kind.  
 Registration requires a [Library](Library.md) annotation; per-language construction uses a
 [Core](../Core.md) and the registered language class, while shared construction uses a [Core](../Core.md).  
 This method is synchronized.

**Parameters:**
- `clazz`: the library class to register

### addLibrary {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_addLibrary-Class-List-}

```js
addLibrary(Class<? extends BaseLibrary> clazz, List<Class<? extends BaseLanguage<?, ?>>> languages): void
```

Registers a library class that is restricted to the given languages.  
 
 The languages are supplied separately rather than only through the [Library](Library.md)
 annotation because a generated library class is defined by the core's class loader, while
 the language it is restricted to may live in an extension's loader. A class reference in
 an annotation is resolved against the loader that defined the annotated class, so such a
 reference could not be read back. The supplied languages take precedence over the ones in
 the annotation. For shared and per-execution libraries they restrict which languages
 receive the stored instance; for per-language libraries they select the registration keys.

**Parameters:**
- `clazz`: the library class to register
- `languages`: the languages the library applies to; empty means the annotation decides

### getLibraries {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_getLibraries-BaseLanguage-BaseScriptContext-}

```js
getLibraries(BaseLanguage<?, ?> language, BaseScriptContext<?> context): Map<String, BaseLibrary>
```

Returns applicable shared and per-execution libraries. Per-execution instances are
 constructed by this call; shared instances are reused.

**Parameters:**
- `language`: the language the script is being run in
- `context`: the run context used to construct per-execution libraries

**Returns:** libraries keyed by their [Library](Library.md) global names

### getOnceLibraries {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_getOnceLibraries-BaseLanguage-}

```js
getOnceLibraries(BaseLanguage<?, ?> language): Map<String, BaseLibrary>
```

Returns shared libraries registered for all languages or this exact language, plus
 per-language instances registered for this language or one of its superclasses.

**Parameters:**
- `language`: the language the libraries are being asked for

**Returns:** libraries keyed by their [Library](Library.md) global names

### getPerExecLibraries {#com.jsmacrosce.jsmacros.core.library.LibraryRegistry_getPerExecLibraries-BaseLanguage-BaseScriptContext-}

```js
getPerExecLibraries(BaseLanguage<?, ?> language, BaseScriptContext<?> context): Map<String, BaseLibrary>
```

Constructs and returns applicable per-execution libraries. Construction failures are
 wrapped in a [RuntimeException](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/RuntimeException.html).

**Parameters:**
- `language`: the language the libraries are being asked for
- `context`: the run context used to construct libraries

**Returns:** libraries keyed by their [Library](Library.md) global names
