public final class QuickJs extends Object implements Closeable
This class is NOT thread safe. If multiple threads access an instance concurrently it must be externally.
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Release the native resources associated with this object.
|
byte[] |
compile(String sourceCode,
String fileName)
Compile
sourceCode and return the bytecode. |
static QuickJs |
create()
Create a new interpreter instance.
|
Object |
evaluate(String script)
Evaluate
script and return a result. |
Object |
evaluate(String script,
String fileName)
Evaluate
script and return any result. |
Object |
execute(byte[] bytecode)
Load and execute
bytecode and return the result. |
protected void |
finalize() |
<T> T |
get(String name,
Class<T> type)
Attaches to a global JavaScript object called
name that implements type. |
<T> void |
set(String name,
Class<T> type,
T object)
Provides
object to JavaScript as a global object called name. |
@NonNull public static QuickJs create()
close() on the returned instance to avoid leaking native memory.@Nullable public Object evaluate(@NonNull String script, @NonNull String fileName)
script and return any result. fileName will be used in error
reporting.QuickJsException - if there is an error evaluating the script.@Nullable public Object evaluate(@NonNull String script)
script and return a result.QuickJsException - if there is an error evaluating the script.public <T> void set(@NonNull
String name,
@NonNull
Class<T> type,
@NonNull
T object)
object to JavaScript as a global object called name. type
defines the interface implemented by object that will be accessible to JavaScript.
type must be an interface that does not extend any other interfaces, and cannot define
any overloaded methods.
Methods of the interface may return void or any of the following supported argument
types: boolean, Boolean, int, Integer, double,
Double, String.
@NonNull
public <T> T get(@NonNull
String name,
@NonNull
Class<T> type)
name that implements type.
type defines the interface implemented in JavaScript that will be accessible to Java.
type must be an interface that does not extend any other interfaces, and cannot define
any overloaded methods.
Methods of the interface may return void or any of the following supported argument
types: boolean, Boolean, int, Integer, double,
Double, String.
@NonNull
public byte[] compile(@NonNull
String sourceCode,
@NonNull
String fileName)
sourceCode and return the bytecode. fileName will be used in error
reporting.QuickJsException - if the sourceCode could not be compiled.@Nullable public Object execute(@NonNull byte[] bytecode)
bytecode and return the result.QuickJsException - if there is an error loading or executing the code.public void close()
close in interface Closeableclose in interface AutoCloseable