-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows scripts to be used as values.
- Loading branch information
1 parent
8a96656
commit f1fd9e5
Showing
15 changed files
with
299 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/org/cyclops/integratedscripting/api/network/IScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.cyclops.integratedscripting.api.network; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A script that contains executable members. | ||
* @author rubensworks | ||
*/ | ||
public interface IScript { | ||
|
||
/** | ||
* Get the given script member. | ||
* @param memberName A member name. | ||
* @return The member, or null if it does not exist. | ||
*/ | ||
@Nullable | ||
public IScriptMember getMember(String memberName); | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/org/cyclops/integratedscripting/api/network/IScriptFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.cyclops.integratedscripting.api.network; | ||
|
||
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException; | ||
|
||
import javax.annotation.Nullable; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Instantiates scripts for paths in disks. | ||
* @author rubensworks | ||
*/ | ||
public interface IScriptFactory { | ||
|
||
/** | ||
* Get the script by the given path in the given disk. | ||
* @param disk A disk id. | ||
* @param path A script path. | ||
* @return The script or null. | ||
*/ | ||
@Nullable | ||
public IScript getScript(int disk, Path path) throws EvaluationException; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/cyclops/integratedscripting/api/network/IScriptMember.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package org.cyclops.integratedscripting.api.network; | ||
|
||
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException; | ||
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue; | ||
|
||
/** | ||
* A script member that contains a value. | ||
* @author rubensworks | ||
*/ | ||
public interface IScriptMember { | ||
|
||
/** | ||
* @return The member value as Integrated Dynamics value. | ||
* @throws EvaluationException If an error occurred. | ||
*/ | ||
public IValue getValue() throws EvaluationException; | ||
|
||
} |
4 changes: 3 additions & 1 deletion
4
src/main/java/org/cyclops/integratedscripting/api/network/IScriptingData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/org/cyclops/integratedscripting/core/network/GraalScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.cyclops.integratedscripting.core.network; | ||
|
||
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException; | ||
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue; | ||
import org.cyclops.integratedscripting.api.network.IScript; | ||
import org.cyclops.integratedscripting.api.network.IScriptMember; | ||
import org.cyclops.integratedscripting.evaluate.translation.ValueTranslators; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Value; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* A script referencing a Graal value. | ||
* @author rubensworks | ||
*/ | ||
public class GraalScript implements IScript, IScriptMember { | ||
|
||
private final Context graalContext; | ||
private final Value graalValue; | ||
|
||
public GraalScript(Context graalContext, Value graalValue) { | ||
this.graalContext = graalContext; | ||
this.graalValue = graalValue; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public IScriptMember getMember(String memberName) { | ||
Value member = this.graalValue.getMember(memberName); | ||
return member == null ? null : new GraalScript(this.graalContext, member); | ||
} | ||
|
||
@Override | ||
public IValue getValue() throws EvaluationException { | ||
return ValueTranslators.REGISTRY.translateFromGraal(this.graalContext, graalValue); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/org/cyclops/integratedscripting/core/network/GraalScriptFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.cyclops.integratedscripting.core.network; | ||
|
||
import net.minecraft.network.chat.Component; | ||
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException; | ||
import org.cyclops.integratedscripting.api.network.IScript; | ||
import org.cyclops.integratedscripting.api.network.IScriptFactory; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Source; | ||
import org.graalvm.polyglot.Value; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* Instantiates scripts using the GraalVM. | ||
* @author rubensworks | ||
*/ | ||
public class GraalScriptFactory implements IScriptFactory { | ||
|
||
private final Context graalContext; | ||
private final Value languageBinding; | ||
private final String languageId; | ||
|
||
public GraalScriptFactory(Context graalContext, Value languageBinding, String languageId) { | ||
this.graalContext = graalContext; | ||
this.languageBinding = languageBinding; | ||
this.languageId = languageId; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public IScript getScript(int disk, Path path) throws EvaluationException { | ||
try { | ||
Source source = Source.newBuilder(this.languageId, ScriptingNetworkHelpers.getScriptingData().getScripts(disk).get(path), path.toString()).build(); | ||
this.graalContext.eval(source); | ||
return new GraalScript(this.graalContext, this.languageBinding); | ||
} catch (IOException e) { | ||
throw new EvaluationException(Component.literal(e.getMessage())); | ||
} | ||
} | ||
} |
Oops, something went wrong.