-
-
Notifications
You must be signed in to change notification settings - Fork 378
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
Merged
Moderocky
merged 47 commits into
SkriptLang:feature/script-reflection
from
Moderocky:dynamic-function-calling
Dec 29, 2024
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
106c555
Create Script type.
Moderocky fe9a1fb
Support script string/name conversion.
Moderocky da4d4b1
Script expression.
Moderocky ef742cc
Add script lang entry.
Moderocky 571d302
Tests for script expression & names.
Moderocky 5da3884
Support all scripts expression.
Moderocky deb484c
Script effects & tests.
Moderocky f1c0c72
Create dummy Script handle for disabled scripts.
Moderocky 6c5a55a
Script reflection feature flag.
Moderocky e0debc8
Restrict literal parsing to commands & parse.
Moderocky 5a692e3
Test feature flag for resolving name.
Moderocky e9c7b74
Split ExprScripts by feature to support disabled scripts.
Moderocky 1b54e08
Fix ExprName tests for new & old behaviour.
Moderocky 064f1ce
Add tests for disabled script handles.
Moderocky c46a5c9
Apply suggestions from code review
Moderocky 1c95947
Improve script loading/unloading safety.
Moderocky 43b245d
Add feature check for script hotswapping.
Moderocky c102949
Use expression stream.
Moderocky 40e92b1
Conformity for file names and proper loading safety.
Moderocky 803b34c
Document validity & add condition support.
Moderocky 3ec719c
Add script is loaded condition + tests.
Moderocky a82c3cd
Dynamic function calling + tests.
Moderocky e35f9d2
Add language entry for types.
Moderocky c19648d
Single-encounter input bootstrapping.
Moderocky 1eddf56
Apply suggestions from code review
Moderocky ab861c9
Fix inspection.
Moderocky 19a99d2
Update src/main/java/ch/njol/skript/expressions/ExprFunction.java
Moderocky 97f0356
Update src/main/java/ch/njol/skript/expressions/ExprResult.java
Moderocky ab096cc
Changes from review.
Moderocky ca3d648
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky d543809
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky 58dd1b4
Fix merge problems.
Moderocky 9e81543
Remove script command method usage.
Moderocky 5bf9856
Fix branch muck.
Moderocky 0143f2b
Fix more branch muck.
Moderocky 9772529
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky 757ab53
Fix up ExprName.
Moderocky 820c9c6
Add docs.
Moderocky 4ce50d6
Add docs.
Moderocky fb240c5
Add docs.
Moderocky 36a9afd
Fix bits.
Moderocky 3e4d05c
Apply suggestions from code review
Moderocky bd0845a
Fix some bits.
Moderocky 52a35fc
Function parsing by name.
Moderocky ba4af38
Fix up some bits for Walrus.
Moderocky e1ba77a
Merge branch 'feature/script-reflection' into dynamic-function-calling
Moderocky c1802b5
Fix merge error.
Moderocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,90 @@ | ||
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) { | ||
Executable task = executable.getSingle(event); | ||
if (task == null) | ||
return; | ||
Object[] arguments; | ||
if (task instanceof DynamicFunctionReference<?> reference) { | ||
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); | ||
Moderocky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} |
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
129 changes: 129 additions & 0 deletions
129
src/main/java/ch/njol/skript/expressions/ExprFunction.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,129 @@ | ||
package ch.njol.skript.expressions; | ||
|
||
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.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.COMBINED, | ||
"[the|a] function [named] %string% [(in|from) %-script%]", | ||
"[the] functions [named] %strings% [(in|from) %-script%]", | ||
"[all [[of] the]|the] functions (in|from) %script%" | ||
); | ||
} | ||
|
||
private Expression<String> name; | ||
APickledWalrus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private Expression<Script> script; | ||
private int mode; | ||
private boolean local; | ||
private Script here; | ||
|
||
@Override | ||
@SuppressWarnings("null") | ||
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 = mode == 2 || expressions[1] != null; | ||
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 = DynamicFunctionReference.resolveFunction(name, script); | ||
Moderocky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (reference == null) | ||
yield CollectionUtils.array(); | ||
yield CollectionUtils.array(reference); | ||
} | ||
case 1 -> this.name.stream(event).map(string -> DynamicFunctionReference.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); | ||
}; | ||
} | ||
|
||
@Override | ||
public boolean isSingle() { | ||
return mode != 2 && 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); | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be a named inner class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you mean by this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like
private static class DynamicFunctionReferenceParser extends Parser...
. I did this in the JavaClasses rework to make the registration section easier to navigate, however its not that big of a deal as most parsers are just an anonymous classThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I'm not sure this would add a huge amount of value, since it would just be moved to another place, not made any smaller.