# GraalExtension

`com.jsmacrosce.jsmacros.graal.GraalExtension`

Provides Graal language support and the `JavaWrapper` library through the extension loader; it is not a script global.  
 
 The language definition and its engine are shared by all cores that load this class. The engine
 uses the configuration of the core that first builds it, whether during warmup or script
 selection. This extension assumes one core per process: a second core would use the profile and
 libraries of the core that first requested the language definition.  
 ```js
const extension = context.getCtx().runner.extensions.getExtensionForName("graal");  
if (extension !== null) {
  Chat.log(extension.getExtensionName() + ": " + extension.defaultFileExtension());  
}
 ```

## Constructors

### new GraalExtension {#com.jsmacrosce.jsmacros.graal.GraalExtension_constructor-}

```js
new GraalExtension()
```

## Methods

### defaultFileExtension {#com.jsmacrosce.jsmacros.graal.GraalExtension_defaultFileExtension-}

```js
defaultFileExtension(): String
```

Returns the default file suffix, without a leading period.

**Returns:** `"js"`

### extensionMatch {#com.jsmacrosce.jsmacros.graal.GraalExtension_extensionMatch-File-}

```js
extensionMatch(File fname): LanguageExtension.ExtMatch
```

Tests a file using the initialized runner's Graal engine and language detection; a detected language gets a name-preferred match when its basename contains `"graal"`.  
 An `IOException` from language detection is printed and results in `NOT_MATCH`.

**Parameters:**
- `fname`: candidate file to test

**Returns:** `MATCH_WITH_NAME` for a detected language with `"graal"` in its basename, `MATCH` for another detected language, or `NOT_MATCH` when the engine lacks the detected language or detection throws `IOException`

### getExtensionName {#com.jsmacrosce.jsmacros.graal.GraalExtension_getExtensionName-}

```js
getExtensionName(): String
```

Returns the loader registration identifier `"graal"`.

**Returns:** this extension's registration identifier

### getLanguage {#com.jsmacrosce.jsmacros.graal.GraalExtension_getLanguage-Core-}

```js
getLanguage(Core<?, ?> runner): BaseLanguage<?, ?>
```

Returns the cached Graal language definition, creating it on the first call.  
 The definition is stored statically across extension instances and uses the core supplied when it is created;  
 a call finding the cache populated ignores its core argument. Creation is unsynchronized, so concurrent calls
 can construct competing definitions. During creation, the calling thread's context class loader is changed to
 this extension's loader and restored on the normal path, not in a failure-protected block.

**Parameters:**
- `runner`: core used if the shared definition has not yet been created

**Returns:** the cached Graal language definition

### getLibraries {#com.jsmacrosce.jsmacros.graal.GraalExtension_getLibraries-}

```js
getLibraries(): Set<Class<? extends BaseLibrary>>
```

Returns this extension's library classes for loader registration.  
 Each call returns a fresh mutable set containing `FWrapper.class`.

**Returns:** fresh set of contributed library classes

### getPriority {#com.jsmacrosce.jsmacros.graal.GraalExtension_getPriority-}

```js
getPriority(): int
```

Returns this language extension's selection priority.

**Returns:** priority value `1000`; higher priorities are normally preferred, but this does not guarantee selection

### init {#com.jsmacrosce.jsmacros.graal.GraalExtension_init-Core-}

```js
init(Core<?, ?> runner): void
```

Stores the core and registers this extension's `graal` configuration options.  
 The runner is stored before registration; a registration failure is wrapped in `RuntimeException` and does not undo that assignment.

**Parameters:**
- `runner`: core to store and use for configuration registration

### isGuestObject {#com.jsmacrosce.jsmacros.graal.GraalExtension_isGuestObject-Object-}

```js
isGuestObject(Object o): boolean
```

Tests whether the object is a Graal `Value` instance.  
 This type check does not establish that the value originated in guest code.

**Parameters:**
- `o`: value to inspect

**Returns:** `true` for a `Value` instance; `false` otherwise

### wrapException {#com.jsmacrosce.jsmacros.graal.GraalExtension_wrapException-Throwable-}

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

Builds a diagnostic wrapper for a Graal polyglot exception; other throwable types are not recognized.  
 Host exceptions use their host class name and optional message; other polyglot exceptions use
 `GuestExceptionSimplifier`, falling back to `"UnknownGuestException"` if its message is null.  
 The wrapper retains the original throwable and may include a source position and linked guest/host frames;  
 the frame chain stops at the first host `org.graalvm.polyglot.Context.eval` frame.

**Parameters:**
- `ex`: throwable to inspect

**Returns:** diagnostic wrapper for a `PolyglotException`, or `null` otherwise
