Interface MochaEngine<T>
- Since:
- 3.0.0
-
Method Summary
Modifier and TypeMethodDescriptionvoidBinds the givenclazzstatic fields and methods.<B> voidbindInstance(@NotNull Class<? super B> clazz, B instance, @NotNull String name, @NotNull String @NotNull ... aliases) Binds the giveninstancenon-static fields and methods.@NotNull javassist.ClassPoolReturns the internalClassPoolused by the compiler.default @NotNull MochaFunctionCompiles the given code into a Molang function that takes no arguments.<F extends MochaCompiledFunction>
FCompiles the given code into a Molang function that can take arguments.default @NotNull MochaFunctionCompiles the given code into a Molang function that takes no arguments.default <F extends MochaCompiledFunction>
FCompiles the given code into a Molang function that can take arguments.static MochaEngine<?>create()static <T> MochaEngine<T>create(T entity) static <T> MochaEngine<T>create(T entity, Consumer<Scope.Builder> scopeBuilder) static @NotNull MochaEngine<?>Creates a new, clean and emptyMochaEngineinstance with the standard, default bindings.static <T> @NotNull MochaEngine<T>createStandard(T entity) Creates a new, clean and emptyMochaEngineinstance with the standard, default bindings.doubleParses and evaluates the given Molang source.default doubleParses and evaluates the given Molang source.doubleeval(@NotNull List<Expression> expressions) Evaluates the givenexpressions, these expressions are already parsed and are interpreted as fast as possible.@NotNull MochaEngine<T>handleParseExceptions(@Nullable Consumer<@NotNull ParseException> exceptionHandler) Sets theParseExceptionhandler.@NotNull List<Expression>Parses the data from the givenreaderto aListofExpressionNote that this method won't close the givenreaderdefault @NotNull List<Expression>Parses the givenstringto a list ofExpression@NotNull MochaEngine<T>postCompile(@Nullable Consumer<byte @NotNull []> bytecodeConsumer) Sets the post-compile function, which is called after a script is compiled to a new class, and before it is loaded.@NotNull MochaFunctionprepareEval(@NotNull Reader reader) Parses the data from the givenreaderand returns a cached, interpretableMochaFunction.default @NotNull MochaFunctionprepareEval(@NotNull String string) Parses the givenstringand returns a cached, interpretableMochaFunction.@NotNull Scopescope()Returns the bindings for this Molang engine instance.@NotNull MochaEngine<T>warnOnReflectiveFunctionUsage(boolean warnOnReflectiveFunctionUsage) Sets the boolean value for the "warn on reflective function usage" option.
-
Method Details
-
create
-
create
-
create
-
createStandard
Creates a new, clean and emptyMochaEngineinstance with the standard, default bindings.- Returns:
- The created
MochaEngineinstance. - Since:
- 3.0.0
-
createStandard
Creates a new, clean and emptyMochaEngineinstance with the standard, default bindings.- Returns:
- The created
MochaEngineinstance. - Since:
- 3.0.0
-
parse
Parses the data from the givenreaderto aListofExpressionNote that this method won't close the givenreader- Parameters:
reader- The reader to read the data from- Returns:
- The list of parsed expressions
- Throws:
ParseException- If read failed or there are syntax errors in the scriptIOException- Since:
- 3.0.0
-
parse
@NotNull default @NotNull List<Expression> parse(@NotNull @NotNull String string) throws ParseException Parses the givenstringto a list ofExpression- Parameters:
string- The MoLang string- Returns:
- The list of parsed expressions
- Throws:
ParseException- If parsing fails
-
eval
Evaluates the givenexpressions, these expressions are already parsed and are interpreted as fast as possible.- Parameters:
expressions- The expressions to evaluate.- Returns:
- The result of the evaluation.
- Since:
- 3.0.0
-
eval
Parses and evaluates the given Molang source.Note that the engine instance is not responsible for caching parsed expressions, so if you want to re-use parsed expressions, you should use the
parse(Reader)andeval(List)methods.- Parameters:
source- The source to evaluate.- Returns:
- The result of the evaluation.
- Since:
- 3.0.0
- See Also:
-
eval
Parses and evaluates the given Molang source.Note that the engine instance is not responsible for caching parsed expressions, so if you want to re-use parsed expressions, you should use the
parse(String)andeval(List)methods.- Parameters:
source- The source to evaluate.- Returns:
- The result of the evaluation.
- Since:
- 3.0.0
- See Also:
-
prepareEval
Parses the data from the givenreaderand returns a cached, interpretableMochaFunction.Note that this method won't close the given
readerThis approach is the same as parsing to a List of expressions, caching them and then evaluating using
eval(List), but easier, since it already keeps thisMochaEngineinstance.- Parameters:
reader- The reader to read the data from- Returns:
- The cached, interpretable function
- Since:
- 3.0.0
-
prepareEval
Parses the givenstringand returns a cached, interpretableMochaFunction.This approach is the same as parsing to a List of expressions, caching them and then evaluating using
eval(List), but easier, since it already keeps thisMochaEngineinstance.- Parameters:
string- The MoLang string- Returns:
- The cached, interpretable function
- Since:
- 3.0.0
-
compile
@NotNull <F extends MochaCompiledFunction> F compile(@NotNull @NotNull Reader reader, @NotNull @NotNull Class<F> interfaceType) Compiles the given code into a Molang function that can take arguments.- Parameters:
reader- The code to compile.interfaceType- The interface to implement, must have a single method.- Returns:
- The compiled function.
- Since:
- 3.0.0
-
compile
@NotNull default <F extends MochaCompiledFunction> F compile(@NotNull @NotNull String code, @NotNull @NotNull Class<F> interfaceType) Compiles the given code into a Molang function that can take arguments.- Parameters:
code- The code to compile.interfaceType- The interface to implement, must have a single method.- Returns:
- The compiled function.
- Since:
- 3.0.0
-
compile
Compiles the given code into a Molang function that takes no arguments.- Parameters:
reader- The code to compile.- Returns:
- The compiled function.
- Since:
- 3.0.0
-
compile
Compiles the given code into a Molang function that takes no arguments.- Parameters:
code- The code to compile.- Returns:
- The compiled function.
- Since:
- 3.0.0
-
classPool
@Internal @NotNull @NotNull javassist.ClassPool classPool()Returns the internalClassPoolused by the compiler.- Returns:
- The compiler class pool
- Since:
- 3.0.0
-
bind
Binds the givenclazzstatic fields and methods.Fields and methods are bound in the following format:
namespace.field namespace.method() namespace.method(arg1, arg2)Where
namespaceis given by the given class'Bindingannotation, and the field and method names are also given byBindingannotations.- Parameters:
clazz- The class to bind.- Since:
- 3.0.0
- See Also:
-
bindInstance
<B> void bindInstance(@NotNull @NotNull Class<? super B> clazz, @NotNull B instance, @NotNull @NotNull String name, @NotNull @NotNull String @NotNull ... aliases) Binds the giveninstancenon-static fields and methods.Fields and methods are bound in the following format:
name.field name.method() name.method(arg1, arg2)Where
nameis given by thenameparameter, and the field and method names are given byBindingannotations.- Type Parameters:
B- The instance's type.- Parameters:
clazz- The instance's class (or interface) to use.instance- The instance to bind.name- The name to bind the instance to.aliases- The aliases to bind the instance to.- Since:
- 3.0.0
-
warnOnReflectiveFunctionUsage
@Contract("_ -> this") @NotNull @NotNull MochaEngine<T> warnOnReflectiveFunctionUsage(boolean warnOnReflectiveFunctionUsage) Sets the boolean value for the "warn on reflective function usage" option.When set to true,
eval(java.util.List<team.unnamed.mocha.parser.ast.Expression>)may log a warning when evaluating code that includes a call to a function that was registered using only annotations, and for so, has to be called using Reflection, taking some extra time.Note that this behavior can be avoided by setting a
ObjectValuewhen binding static or non-static methods and fieldsBy default this is false.
- Parameters:
warnOnReflectiveFunctionUsage- The new value for the option- Returns:
- This engine instance
- Since:
- 3.0.0
-
handleParseExceptions
@Contract("_ -> this") @NotNull @NotNull MochaEngine<T> handleParseExceptions(@Nullable @Nullable Consumer<@NotNull ParseException> exceptionHandler) Sets theParseExceptionhandler. This handler will be called whenever an internal call to theparse(java.io.Reader)method fails. These internal calls commonly include interpreting or compiling code.Usually useful for logging/debugging purposes.
By default this is null.
- Parameters:
exceptionHandler- The new parse exception handler- Returns:
- This engine instance
- Since:
- 3.0.0
-
postCompile
@Contract("_ -> this") @NotNull @NotNull MochaEngine<T> postCompile(@Nullable @Nullable Consumer<byte @NotNull []> bytecodeConsumer) Sets the post-compile function, which is called after a script is compiled to a new class, and before it is loaded. The received argument is the class bytecode, which can be written to a file for debugging purposes.By default this is set to null.
- Parameters:
bytecodeConsumer- The new post-compile function- Returns:
- This engine instance
- Since:
- 3.0.0
-
scope
Returns the bindings for this Molang engine instance.- Returns:
- This engine's bindings
- Since:
- 3.0.0
-