Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic function calling. #6713

Open
wants to merge 41 commits into
base: feature/script-reflection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
106c555
Create Script type.
Moderocky May 17, 2024
fe9a1fb
Support script string/name conversion.
Moderocky May 17, 2024
da4d4b1
Script expression.
Moderocky May 17, 2024
ef742cc
Add script lang entry.
Moderocky May 17, 2024
571d302
Tests for script expression & names.
Moderocky May 17, 2024
5da3884
Support all scripts expression.
Moderocky May 17, 2024
deb484c
Script effects & tests.
Moderocky May 17, 2024
f1c0c72
Create dummy Script handle for disabled scripts.
Moderocky May 18, 2024
6c5a55a
Script reflection feature flag.
Moderocky May 18, 2024
e0debc8
Restrict literal parsing to commands & parse.
Moderocky May 18, 2024
5a692e3
Test feature flag for resolving name.
Moderocky May 18, 2024
e9c7b74
Split ExprScripts by feature to support disabled scripts.
Moderocky May 18, 2024
1b54e08
Fix ExprName tests for new & old behaviour.
Moderocky May 18, 2024
064f1ce
Add tests for disabled script handles.
Moderocky May 18, 2024
c46a5c9
Apply suggestions from code review
Moderocky May 18, 2024
1c95947
Improve script loading/unloading safety.
Moderocky May 18, 2024
43b245d
Add feature check for script hotswapping.
Moderocky May 18, 2024
c102949
Use expression stream.
Moderocky May 18, 2024
40e92b1
Conformity for file names and proper loading safety.
Moderocky May 18, 2024
803b34c
Document validity & add condition support.
Moderocky May 18, 2024
3ec719c
Add script is loaded condition + tests.
Moderocky May 19, 2024
a82c3cd
Dynamic function calling + tests.
Moderocky May 19, 2024
e35f9d2
Add language entry for types.
Moderocky May 20, 2024
c19648d
Single-encounter input bootstrapping.
Moderocky May 20, 2024
1eddf56
Apply suggestions from code review
Moderocky Aug 17, 2024
ab861c9
Fix inspection.
Moderocky Aug 17, 2024
19a99d2
Update src/main/java/ch/njol/skript/expressions/ExprFunction.java
Moderocky Sep 6, 2024
97f0356
Update src/main/java/ch/njol/skript/expressions/ExprResult.java
Moderocky Sep 6, 2024
ab096cc
Changes from review.
Moderocky Sep 7, 2024
ca3d648
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Sep 7, 2024
d543809
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Nov 21, 2024
58dd1b4
Fix merge problems.
Moderocky Nov 21, 2024
9e81543
Remove script command method usage.
Moderocky Nov 21, 2024
5bf9856
Fix branch muck.
Moderocky Nov 21, 2024
0143f2b
Fix more branch muck.
Moderocky Nov 21, 2024
9772529
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky Nov 23, 2024
757ab53
Fix up ExprName.
Moderocky Nov 23, 2024
820c9c6
Add docs.
Moderocky Nov 23, 2024
4ce50d6
Add docs.
Moderocky Nov 23, 2024
fb240c5
Add docs.
Moderocky Nov 23, 2024
36a9afd
Fix bits.
Moderocky Nov 23, 2024
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
23 changes: 23 additions & 0 deletions src/main/java/ch/njol/skript/classes/data/SkriptClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ch.njol.skript.classes.YggdrasilSerializer;
import ch.njol.skript.expressions.base.EventValueExpression;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.lang.function.DynamicFunctionReference;
import ch.njol.skript.lang.util.SimpleLiteral;
import ch.njol.skript.localization.Noun;
import ch.njol.skript.localization.RegexMessage;
Expand Down Expand Up @@ -44,6 +45,9 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.util.Executable;

import java.io.StreamCorruptedException;
import java.io.File;
Expand Down Expand Up @@ -721,6 +725,25 @@ public String toVariableNameString(final Script script) {
return path.relativize(file.toPath().toAbsolutePath()).toString();
}
}));

Classes.registerClass(new ClassInfo<>(Executable.class, "executable")
.user("executables?")
.name("Executable")
.description("Something that can be executed (run) and may accept arguments, e.g. a function.",
"This may also return a result.")
.usage("")
.examples("run {_function} with arguments 1 and true")
.since("INSERT VERSION"));

Classes.registerClass(new ClassInfo<>(DynamicFunctionReference.class, "function")
.user("functions?")
.name("Function")
.description("A function loaded by Skript.",
"This can be executed (with arguments) and may return a result.")
.usage("")
.examples("run {_function} with arguments 1 and true",
"set {_result} to the result of {_function}")
.since("INSERT VERSION"));
}

}
92 changes: 92 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffRun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.doc.*;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionList;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.function.DynamicFunctionReference;
import ch.njol.skript.registrations.Feature;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.util.Executable;

@Name("Run (Experimental)")
@Description({
"Executes a task (a function). Any returned result is discarded."
})
@Examples({
"set {_function} to the function named \"myFunction\"",
"run {_function}",
"run {_function} with arguments {_things::*}",
})
@Since("INSERT VERSION")
@Keywords({"run", "execute", "reflection", "function"})
@SuppressWarnings({"rawtypes", "unchecked"})
public class EffRun extends Effect {

static {
Skript.registerEffect(EffRun.class,
"run %executable% [arguments:with arg[ument]s %-objects%]",
"execute %executable% [arguments:with arg[ument]s %-objects%]");
}

// We don't bother with the generic type here because we have no way to verify it
// from the expression, and it makes casting more difficult to no benefit.
private Expression<Executable> executable;
private Expression<?> arguments;
private DynamicFunctionReference.Input input;
private boolean hasArguments;

@Override
public boolean init(Expression<?>[] expressions, int pattern, Kleenean isDelayed, ParseResult result) {
if (!this.getParser().hasExperiment(Feature.SCRIPT_REFLECTION))
return false;
sovdeeth marked this conversation as resolved.
Show resolved Hide resolved
this.executable = ((Expression<Executable>) expressions[0]);
this.hasArguments = result.hasTag("arguments");
if (hasArguments) {
this.arguments = LiteralUtils.defendExpression(expressions[1]);
Expression<?>[] arguments;
if (this.arguments instanceof ExpressionList<?>)
arguments = ((ExpressionList<?>) this.arguments).getExpressions();
else
arguments = new Expression[]{this.arguments};
this.input = new DynamicFunctionReference.Input(arguments);
return LiteralUtils.canInitSafely(this.arguments);
} else {
this.input = new DynamicFunctionReference.Input();
}
return true;
}

@Override
protected void execute(Event event) {
@Nullable Executable task = executable.getSingle(event);
if (task == null)
return;
Object[] arguments;
if (task instanceof DynamicFunctionReference) {
DynamicFunctionReference<?> reference = (DynamicFunctionReference) task;
Expression<?> validated = reference.validate(input);
if (validated == null)
return;
arguments = validated.getArray(event);
} else if (hasArguments) {
arguments = this.arguments.getArray(event);
} else {
arguments = new Object[0];
}
task.execute(event, arguments);
}

@Override
public String toString(@Nullable Event event, boolean debug) {
if (hasArguments)
return "run " + executable.toString(event, debug) + " with arguments " + arguments.toString(event, debug);
return "run " + executable.toString(event, debug);
}

}
1 change: 1 addition & 0 deletions src/main/java/ch/njol/skript/effects/EffScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ private void unloadScripts(File file) {
Set<Script> scripts = ScriptLoader.getScripts(file);
if (scripts.isEmpty())
return;
scripts.retainAll(loaded); // skip any that are not loaded (avoid throwing error)
ScriptLoader.unloadScripts(scripts);
} else {
Script script = ScriptLoader.getScript(file);
Expand Down
157 changes: 157 additions & 0 deletions src/main/java/ch/njol/skript/expressions/ExprFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
package ch.njol.skript.expressions;

import ch.njol.skript.ScriptLoader;
import ch.njol.skript.Skript;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.function.DynamicFunctionReference;
import ch.njol.skript.lang.function.Functions;
import ch.njol.skript.lang.function.Namespace;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.registrations.Feature;
import ch.njol.util.Kleenean;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;
import org.skriptlang.skript.lang.script.Script;

import java.io.File;
import java.util.Objects;

@Name("Function (Experimental)")
@Description("Obtain a function by name, which can be executed.")
@Examples({
"set {_function} to the function named \"myFunction\"",
"run {_function} with arguments 13 and true"
})
@Since("INSERT VERSION")
@SuppressWarnings("rawtypes")
public class ExprFunction extends SimpleExpression<DynamicFunctionReference> {

static {
Skript.registerExpression(ExprFunction.class, DynamicFunctionReference.class, ExpressionType.SIMPLE,
"[the|a] function [named] %string% [local:(in|from) %-script%]",
"[the] functions [named] %strings% [local:(in|from) %-script%]",
"[all] [the] functions (in|from) %script%"
);
}

private Expression<String> name;
private Expression<Script> script;
private int mode;
private boolean local;
private Script here;

@SuppressWarnings("null")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed,
ParseResult result) {
if (!this.getParser().hasExperiment(Feature.SCRIPT_REFLECTION))
return false;
this.mode = matchedPattern;
this.local = result.hasTag("local") || mode == 2;
switch (mode) {
Moderocky marked this conversation as resolved.
Show resolved Hide resolved
case 0, 1 -> {
//noinspection unchecked
this.name = (Expression<String>) expressions[0];
if (local)
//noinspection unchecked
this.script = (Expression<Script>) expressions[1];
}
case 2 ->
//noinspection unchecked
this.script = (Expression<Script>) expressions[0];
}
this.here = this.getParser().getCurrentScript();
return true;
}

@Override
protected DynamicFunctionReference<?>[] get(Event event) {
@Nullable Script script;
if (local)
script = this.script.getSingle(event);
else
script = here;
return switch (mode) {
case 0:
@Nullable String name = this.name.getSingle(event);
if (name == null)
yield CollectionUtils.array();
@Nullable DynamicFunctionReference reference = this.resolveFunction(name, script);
if (reference == null)
yield CollectionUtils.array();
yield CollectionUtils.array(reference);
case 1:
yield this.name.stream(event).map(string -> this.resolveFunction(string, script))
.filter(Objects::nonNull)
.toArray(DynamicFunctionReference[]::new);
case 2:
if (script == null)
yield CollectionUtils.array();
@Nullable Namespace namespace = Functions.getScriptNamespace(script.getConfig().getFileName());
if (namespace == null)
yield CollectionUtils.array();
yield namespace.getFunctions().stream()
.map(DynamicFunctionReference::new)
.toArray(DynamicFunctionReference[]::new);
default:
throw new IllegalStateException("Unexpected value: " + mode);
};
}

private @Nullable DynamicFunctionReference resolveFunction(String name, @Nullable Script script) {
// Function reference string-ifying appends a () and potentially its source,
// e.g. `myFunction() from MyScript.sk` and we should turn that into a valid function.
if (script == null && !local && name.contains(") from ")) {
// The user might be trying to resolve a local function by name only
String source = name.substring(name.lastIndexOf(" from ") + 6).trim();
script = getScript(source);
}
if (name.contains("(") && name.contains(")"))
name = name.replaceAll("\\(.*\\).*", "").trim();
// In the future, if function overloading is supported, we could even use the header
// to specify parameter types (e.g. "myFunction(text, player)"
DynamicFunctionReference<Object> reference = new DynamicFunctionReference<>(name, script);
if (!reference.valid())
return null;
return reference;
}

private @Nullable Script getScript(@Nullable String source) {
if (source == null || source.isEmpty())
return null;
@Nullable File file = ScriptLoader.getScriptFromName(source);
if (file == null || file.isDirectory())
return null;
return ScriptLoader.getScript(file);
}

@Override
public boolean isSingle() {
return mode == 0 || mode == 1 && name.isSingle();
}

@Override
public Class<? extends DynamicFunctionReference> getReturnType() {
return DynamicFunctionReference.class;
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return switch (mode) {
case 0 -> "the function named " + name.toString(event, debug)
+ (local ? " from " + script.toString(event, debug) : "");
case 1 -> "functions named " + name.toString(event, debug)
+ (local ? " from " + script.toString(event, debug) : "");
case 2 -> "the functions from " + script.toString(event, debug);
default -> throw new IllegalStateException("Unexpected value: " + mode);
};
}

}
Loading