Skip to content

Commit 9b63a64

Browse files
committed
feat: new docs
1 parent 2f2bbef commit 9b63a64

20 files changed

Lines changed: 1377 additions & 234 deletions

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ Scriptify is a library to evaluate scripts written in JavaScript with ability to
55
This library is designed to execute JavaScript scripts and has the ability to register global functions and constants.
66
It also allows you to configure security for executing scripts.
77

8+
## Quick Start
9+
10+
```java
11+
import org.densy.scriptify.api.exception.ScriptException;
12+
import org.densy.scriptify.js.graalvm.script.JsScript;
13+
14+
public class Main {
15+
public static void main(String[] args) throws ScriptException {
16+
JsScript script = new JsScript();
17+
Object result = script.evalOneShot("1 + 2 + 3");
18+
System.out.println(result);
19+
}
20+
}
21+
```
22+
23+
For Rhino use `org.densy.scriptify.js.rhino.script.JsScript` instead.
24+
25+
## Documentation
26+
27+
Full documentation is available in [docs/index.md](docs/index.md).
28+
829
## Other scripts support
930
- [TypeScript](https://github.com/DensyDev/Scriptify-TypeScript) - TS support using swc4j
1031
- [TypeScript Declaration Generator](https://github.com/DensyDev/Scriptify-DTS-Generator) - Declaration generator for JS or TS
@@ -61,4 +82,4 @@ For adding a library with JS for Rhino or GraalVM:
6182
```groovy
6283
implementation "org.densy.scriptify:script-js-rhino:1.6.0-SNAPSHOT"
6384
implementation "org.densy.scriptify:script-js-graalvm:1.6.0-SNAPSHOT"
64-
```
85+
```

docs/constants.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Constants
2+
3+
Constants expose named Java values to scripts.
4+
5+
## Constant Interface
6+
7+
```java
8+
org.densy.scriptify.api.script.constant.ScriptConstant
9+
```
10+
11+
Create a constant:
12+
13+
```java
14+
ScriptConstant constant = ScriptConstant.of("appName", "Scriptify");
15+
```
16+
17+
Or implement it:
18+
19+
```java
20+
public final class AppNameConstant implements ScriptConstant {
21+
@Override
22+
public String getName() {
23+
return "appName";
24+
}
25+
26+
@Override
27+
public Object getValue() {
28+
return "Scriptify";
29+
}
30+
}
31+
```
32+
33+
## Register Globally
34+
35+
```java
36+
script.getConstantManager().register(ScriptConstant.of("appName", "Scriptify"));
37+
script.evalOneShot("appName");
38+
```
39+
40+
Global constants are exported to the global module during compilation.
41+
42+
## Export From a Module
43+
44+
```java
45+
SimpleScriptInternalModule app = new SimpleScriptInternalModule("app");
46+
app.export(new ScriptConstantExport(ScriptConstant.of("name", "Scriptify")));
47+
script.getModuleManager().addModule(app);
48+
```
49+
50+
```js
51+
import { name } from "app";
52+
name;
53+
```
54+
55+
## Constant Manager
56+
57+
`ScriptConstantManager` provides:
58+
59+
```java
60+
Map<String, ScriptConstant> getConstants();
61+
ScriptConstant getConstant(String name);
62+
void register(ScriptConstant constant);
63+
void remove(String name);
64+
```
65+
66+
Default implementation:
67+
68+
```java
69+
org.densy.scriptify.core.script.constant.StandardConstantManager
70+
```
71+
72+
## Built-In Core Constants
73+
74+
| Constant | Value |
75+
| --- | --- |
76+
| `baseDir` | Current JVM working directory as an absolute string. |
77+
| `osName` | `System.getProperty("os.name")`. |
78+
79+
These are exported by `StandardScriptModule`.
80+
81+
## Deprecated Common Manager
82+
83+
`CommonConstantManager` is deprecated. Prefer module exports.

docs/custom-runtime.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Custom Runtime
2+
3+
Scriptify can support additional scripting runtimes by implementing `Script<T>`.
4+
5+
## Required Contract
6+
7+
```java
8+
public final class MyScript implements Script<Object> {
9+
private final ScriptSecurityManager securityManager = new StandardSecurityManager();
10+
private final ScriptFunctionManager functionManager = new StandardFunctionManager();
11+
private final ScriptConstantManager constantManager = new StandardConstantManager();
12+
private final ScriptModuleManager moduleManager = createModuleManager();
13+
14+
@Override
15+
public ScriptSecurityManager getSecurityManager() {
16+
return securityManager;
17+
}
18+
19+
@Override
20+
public ScriptModuleManager getModuleManager() {
21+
return moduleManager;
22+
}
23+
24+
@Override
25+
public ScriptFunctionManager getFunctionManager() {
26+
return functionManager;
27+
}
28+
29+
@Override
30+
public ScriptConstantManager getConstantManager() {
31+
return constantManager;
32+
}
33+
34+
@Override
35+
public CompiledScript<Object> compile(String source) throws ScriptException {
36+
throw new UnsupportedOperationException();
37+
}
38+
}
39+
```
40+
41+
## Runtime Responsibilities
42+
43+
A runtime implementation should:
44+
45+
- create the engine context;
46+
- configure security before script execution;
47+
- expose global functions;
48+
- expose global constants;
49+
- expose internal modules;
50+
- load external modules;
51+
- apply `ScriptAccess.ALL` and `ScriptAccess.EXPLICIT`;
52+
- convert script values into Java values before invoking `ScriptFunction`;
53+
- convert Java return values back into runtime values;
54+
- wrap runtime failures in `ScriptException`;
55+
- close engine resources in `CompiledScript.close`.
56+
57+
## Function Bridge
58+
59+
Your runtime needs an engine-specific callable wrapper for `ScriptFunctionDefinition`.
60+
61+
That wrapper should:
62+
63+
- receive script arguments;
64+
- convert them to Java values;
65+
- find a matching `ScriptFunctionExecutor`;
66+
- call `executor.execute(script, args...)`;
67+
- convert the result back to the script engine.
68+
69+
## Module Export Resolver
70+
71+
Implement:
72+
73+
```java
74+
ScriptModuleExportResolverFactory
75+
ScriptModuleExportResolver
76+
```
77+
78+
The resolver maps Scriptify exports to runtime values:
79+
80+
- `ScriptFunctionExport`;
81+
- `ScriptFunctionDefinitionExport`;
82+
- `ScriptConstantExport`;
83+
- `ScriptValueExport`;
84+
- custom `ScriptExport` implementations if you add them.
85+
86+
Throw `ScriptModuleWrongContextException` if the factory receives a context object from another runtime.
87+
88+
## Security Integration
89+
90+
If the engine supports host class lookup restrictions, wire it to `ScriptSecurityManager.getExcludes()`.
91+
92+
If the engine supports file system hooks, route file paths through:
93+
94+
```java
95+
script.getSecurityManager().getPathAccessor()
96+
```
97+
98+
If the engine exposes Java objects/classes, implement `ScriptAccess.EXPLICIT`.

docs/examples/custom_script_runtime.md

Lines changed: 0 additions & 117 deletions
This file was deleted.

docs/examples/functions_and_constants.md

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)