Skip to content
Draft
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
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ engine.eval("function add(a, b) { return a + b; }");

See the full [JSR-223 scripting documentation](scripting/README.md) for details on bindings, `Compilable`, `Invocable` (`invokeFunction`, `invokeMethod`, `getInterface`), output redirection, and more.

## Native Execution (Experimental)

By default QuickJs4J runs on JVM bytecode. To run it on native code instead, compiled with the experimental [Endive Redline](https://endive.run/docs/experimental/redline) compiler, add:

```xml
<dependency>
<groupId>io.roastedroot</groupId>
<artifactId>quickjs4j-redline-experimental</artifactId>
</dependency>
```

No code changes are needed. Native code is used on Linux, macOS and Windows (x86_64 and aarch64), with Panama FFM on Java 25+ and jffi on older JDKs. On other platforms, or when a custom memory factory is set with `Engine.builder().withMemoryFactory(...)`, the default bytecode is used.

## Building a JS/TS Library

To build your JavaScript/TypeScript library, refer to [this example](it/src/it/apicurio-example/src/main/resources/library).
Expand Down
12 changes: 12 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<!-- the tests are re-run by quickjs4j-redline-experimental -->
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
25 changes: 19 additions & 6 deletions core/src/main/java/io/roastedroot/quickjs4j/core/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;
import run.endive.log.Logger;
import run.endive.log.SystemLogger;
Expand Down Expand Up @@ -100,9 +101,7 @@ private Engine(
});
this.invokables = invokables;
instance =
Instance.builder(JavyPluginModule.load())
.withMemoryFactory(memoryFactory)
.withMachineFactory(JavyPluginModule::create)
instanceBuilder(memoryFactory)
.withImportValues(
ImportValues.builder()
.addFunction(wasi.toHostFunctions())
Expand All @@ -113,6 +112,23 @@ private Engine(
exports.initializeRuntime();
}

// a custom memory factory can only be honored by the default instance
private static Instance.Builder instanceBuilder(Function<MemoryLimits, Memory> memoryFactory) {
if (memoryFactory == null) {
for (var provider :
ServiceLoader.load(
InstanceBuilderProvider.class, Engine.class.getClassLoader())) {
var builder = provider.builder();
if (builder.isPresent()) {
return builder.get();
}
}
}
return Instance.builder(JavyPluginModule.load())
.withMemoryFactory(memoryFactory == null ? ByteArrayMemory::new : memoryFactory)
.withMachineFactory(JavyPluginModule::create);
}

private String readJavyString(int ptr, int len) {
var bytes = instance.memory().readBytes(ptr, len);
return new String(bytes, UTF_8);
Expand Down Expand Up @@ -598,9 +614,6 @@ public Engine build() {
if (mapper == null) {
mapper = DEFAULT_OBJECT_MAPPER;
}
if (memoryFactory == null) {
memoryFactory = ByteArrayMemory::new;
}
Map<String, Builtins> finalBuiltins = new HashMap<>();
// TODO: any validation to be done here?
for (var builtin : builtins) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.roastedroot.quickjs4j.core;

import java.util.Optional;
import run.endive.runtime.Instance;

/**
* Service loaded by {@link Engine} to replace the default, build time compiled, javy plugin
* instance (e.g. with a native one from quickjs4j-redline-experimental).
*/
public interface InstanceBuilderProvider {
/**
* @return a builder for the javy plugin module, or empty when not supported on this platform
*/
Optional<Instance.Builder> builder();
}
Loading
Loading