This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
ScriptCraft/docs/BRIDGES.md
TheBrokenRail 37f11a92f0
All checks were successful
ScriptCraft/pipeline/head This commit looks good
Add Documentation
2020-05-24 14:09:25 -04:00

1.8 KiB

Bridges

To communicate between Java and JS code, ScriptCraft utilizes a system called bridges.

JS-To-Java Bridges

Define The Bridge

import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;
import com.thebrokenrail.scriptcraft.core.ScriptCraftEntryPoint;

public class ExampleBridges implements ScriptCraftEntryPoint {
    @Override
    public String getEntryPoint() {
        return "example-bridges";
    }

    @Override
    public void registerBridges() {
        ScriptCraftCore.addBridge("ExampleBridges.helloWorld", args -> {
            System.out.println("Hello World! " + args[0]);
            return "Return Value";
        });
    }
}

Use The Bridge

import { useBridge } from 'scriptcraft-core';

console.log(useBridge('ExampleBridges.helloWorld', 'Argument'));

Java-To-JS Bridges

Define The Bridge

import { addBridge } from 'scriptcraft-core';

addBridge('ExampleBridges.helloWorld', arg => {
    console.log('Hello World! ' + args[0]);
    return 'Return Value';
});

Use The Bridge

import com.thebrokenrail.scriptcraft.core.ScriptCraftCore;

public class ExampleBridges {
    public static void run() {
        System.out.println((String) ScriptCraftCore.useBridge("ExampleBridges.helloWorld", "Argument"));
    }
}

Supported Types

JS Type Java Type
Number Double (object-form)
Boolean Boolean (object-form)
String String
Array Object[]
Object null
JavaObject Object

JavaObject

A JavaObject is a normal JS object with an internal reference to a Java object, the Java object reference cannot be accessed directly from JS, but when it is passed back to Java using a bridge, it will become the referenced Java object.