Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@ Scriptify is a library to evaluate scripts written in JavaScript with ability to
This library is designed to execute JavaScript scripts and has the ability to register global functions and constants.
It also allows you to configure security for executing scripts.

## Quick Start

```java
import org.densy.scriptify.api.exception.ScriptException;
import org.densy.scriptify.js.graalvm.script.JsScript;

public class Main {
public static void main(String[] args) throws ScriptException {
JsScript script = new JsScript();
Object result = script.evalOneShot("1 + 2 + 3");
System.out.println(result);
}
}
```

For Rhino use `org.densy.scriptify.js.rhino.script.JsScript` instead.

## Documentation

Full documentation is available in [docs/index.md](docs/index.md).

## Other scripts support
- [TypeScript](https://github.com/DensyDev/Scriptify-TypeScript) - TS support using swc4j
- [TypeScript Declaration Generator](https://github.com/DensyDev/Scriptify-DTS-Generator) - Declaration generator for JS or TS
Expand Down Expand Up @@ -61,4 +82,4 @@ For adding a library with JS for Rhino or GraalVM:
```groovy
implementation "org.densy.scriptify:script-js-rhino:1.6.0-SNAPSHOT"
implementation "org.densy.scriptify:script-js-graalvm:1.6.0-SNAPSHOT"
```
```
83 changes: 83 additions & 0 deletions docs/constants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Constants

Constants expose named Java values to scripts.

## Constant Interface

```java
org.densy.scriptify.api.script.constant.ScriptConstant
```

Create a constant:

```java
ScriptConstant constant = ScriptConstant.of("appName", "Scriptify");
```

Or implement it:

```java
public final class AppNameConstant implements ScriptConstant {
@Override
public String getName() {
return "appName";
}

@Override
public Object getValue() {
return "Scriptify";
}
}
```

## Register Globally

```java
script.getConstantManager().register(ScriptConstant.of("appName", "Scriptify"));
script.evalOneShot("appName");
```

Global constants are exported to the global module during compilation.

## Export From a Module

```java
SimpleScriptInternalModule app = new SimpleScriptInternalModule("app");
app.export(new ScriptConstantExport(ScriptConstant.of("name", "Scriptify")));
script.getModuleManager().addModule(app);
```

```js
import { name } from "app";
name;
```

## Constant Manager

`ScriptConstantManager` provides:

```java
Map<String, ScriptConstant> getConstants();
ScriptConstant getConstant(String name);
void register(ScriptConstant constant);
void remove(String name);
```

Default implementation:

```java
org.densy.scriptify.core.script.constant.StandardConstantManager
```

## Built-In Core Constants

| Constant | Value |
| --- | --- |
| `baseDir` | Current JVM working directory as an absolute string. |
| `osName` | `System.getProperty("os.name")`. |

These are exported by `StandardScriptModule`.

## Deprecated Common Manager

`CommonConstantManager` is deprecated. Prefer module exports.
98 changes: 98 additions & 0 deletions docs/custom-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Custom Runtime

Scriptify can support additional scripting runtimes by implementing `Script<T>`.

## Required Contract

```java
public final class MyScript implements Script<Object> {
private final ScriptSecurityManager securityManager = new StandardSecurityManager();
private final ScriptFunctionManager functionManager = new StandardFunctionManager();
private final ScriptConstantManager constantManager = new StandardConstantManager();
private final ScriptModuleManager moduleManager = createModuleManager();

@Override
public ScriptSecurityManager getSecurityManager() {
return securityManager;
}

@Override
public ScriptModuleManager getModuleManager() {
return moduleManager;
}

@Override
public ScriptFunctionManager getFunctionManager() {
return functionManager;
}

@Override
public ScriptConstantManager getConstantManager() {
return constantManager;
}

@Override
public CompiledScript<Object> compile(String source) throws ScriptException {
throw new UnsupportedOperationException();
}
}
```

## Runtime Responsibilities

A runtime implementation should:

- create the engine context;
- configure security before script execution;
- expose global functions;
- expose global constants;
- expose internal modules;
- load external modules;
- apply `ScriptAccess.ALL` and `ScriptAccess.EXPLICIT`;
- convert script values into Java values before invoking `ScriptFunction`;
- convert Java return values back into runtime values;
- wrap runtime failures in `ScriptException`;
- close engine resources in `CompiledScript.close`.

## Function Bridge

Your runtime needs an engine-specific callable wrapper for `ScriptFunctionDefinition`.

That wrapper should:

- receive script arguments;
- convert them to Java values;
- find a matching `ScriptFunctionExecutor`;
- call `executor.execute(script, args...)`;
- convert the result back to the script engine.

## Module Export Resolver

Implement:

```java
ScriptModuleExportResolverFactory
ScriptModuleExportResolver
```

The resolver maps Scriptify exports to runtime values:

- `ScriptFunctionExport`;
- `ScriptFunctionDefinitionExport`;
- `ScriptConstantExport`;
- `ScriptValueExport`;
- custom `ScriptExport` implementations if you add them.

Throw `ScriptModuleWrongContextException` if the factory receives a context object from another runtime.

## Security Integration

If the engine supports host class lookup restrictions, wire it to `ScriptSecurityManager.getExcludes()`.

If the engine supports file system hooks, route file paths through:

```java
script.getSecurityManager().getPathAccessor()
```

If the engine exposes Java objects/classes, implement `ScriptAccess.EXPLICIT`.
117 changes: 0 additions & 117 deletions docs/examples/custom_script_runtime.md

This file was deleted.

51 changes: 0 additions & 51 deletions docs/examples/functions_and_constants.md

This file was deleted.

Loading
Loading