More emulator stuff

- Fix link
 - Slight tweaks to how we generate LuaMethods, to avoid generating as
   much reflection code.
 - Allow turning off minification via Gradle properties.
This commit is contained in:
Jonathan Coates 2023-10-01 11:45:52 +01:00
parent 5016ef594f
commit 01d775e2a5
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 41 additions and 42 deletions

View File

@ -38,5 +38,6 @@ ## Static content
highlighting of non-Lua code blocks, and replaces special `<mc-recipe>` tags with a rendered view of a given
Minecraft recipe.
[cct-javadoc]: https://github.com/cc-tweaked/cct-javadoc
[illuaminate]: https://github.com/Squiddev/illuaminate
[TeaVM]: https://github.com/konsoletyper/teavm "TeaVM - Compiler of Java bytecode to JavaScript"
[cct-javadoc]: https://github.com/cc-tweaked/cct-javadoc: "cct-javadoc - A Javadoc doclet to extract documentation from @LuaFunction methods."
[illuaminate]: https://github.com/Squiddev/illuaminate: "illuaminate - Very WIP static analysis for Lua"

View File

@ -41,8 +41,10 @@ val compileTeaVM by tasks.registering(JavaExec::class) {
description = "Generate our classes and resources files"
val output = layout.buildDirectory.dir("teaVM")
val minify = !project.hasProperty("noMinify")
inputs.property("version", "modVersion")
inputs.property("version", modVersion)
inputs.property("minify", minify)
inputs.files(sourceSets.main.get().runtimeClasspath).withPropertyName("inputClasspath")
outputs.dir(output).withPropertyName("output")
@ -55,6 +57,7 @@ val compileTeaVM by tasks.registering(JavaExec::class) {
"-Dcct.version=$modVersion",
"-Dcct.classpath=${main.runtimeClasspath.asPath}",
"-Dcct.output=${output.getAbsolutePath()}",
"-Dcct.minify=$minify",
)
},
)
@ -66,6 +69,9 @@ val rollup by tasks.registering(cc.tweaked.gradle.NpxExecToDir::class) {
group = LifecycleBasePlugin.BUILD_GROUP
description = "Bundles JS into rollup"
val minify = !project.hasProperty("noMinify")
inputs.property("minify", minify)
// Sources
inputs.files(fileTree("src/frontend")).withPropertyName("sources")
inputs.files(compileTeaVM)
@ -76,7 +82,7 @@ val rollup by tasks.registering(cc.tweaked.gradle.NpxExecToDir::class) {
// Output directory. Also defined in illuaminate.sexp and rollup.config.js
output.set(layout.buildDirectory.dir("rollup"))
args = listOf("rollup", "--config", "rollup.config.js")
args = listOf("rollup", "--config", "rollup.config.js") + if (minify) emptyList() else listOf("--configDebug")
}
val illuaminateDocs by tasks.registering(cc.tweaked.gradle.IlluaminateExecToDir::class) {

View File

@ -12,10 +12,10 @@ import postcss from "rollup-plugin-postcss";
const input = "src/frontend";
const minify = true;
const minify = args => !args.configDebug;
/** @type import("rollup").RollupOptions */
export default {
/** @type import("rollup").RollupOptionsFunction */
export default args => ({
input: [`${input}/index.tsx`],
output: {
// Also defined in build.gradle.kts
@ -26,9 +26,6 @@ export default {
preset: "es2015",
constBindings: true,
},
amd: {
define: "require",
}
},
context: "window",
@ -45,7 +42,7 @@ export default {
postcss({
namedExports: true,
minimize: minify,
minimize: minify(args),
extract: true,
}),
@ -66,6 +63,6 @@ export default {
},
},
minify && terser(),
minify(args) && terser(),
],
};
});

View File

@ -36,15 +36,16 @@ public static void main(String[] args) throws Exception {
var classpath = getPath(scope, "cct.classpath");
var output = getFile("cct.output");
var version = System.getProperty("cct.version");
var minify = Boolean.parseBoolean(System.getProperty("cct.minify", "true"));
buildClasses(input, classpath, output);
buildClasses(input, classpath, output, minify);
buildResources(version, input, classpath, output);
} catch (UncheckedIOException e) {
throw e.getCause();
}
}
private static void buildClasses(List<Path> input, List<Path> classpath, Path output) throws Exception {
private static void buildClasses(List<Path> input, List<Path> classpath, Path output, boolean minify) throws Exception {
var remapper = new TransformingClassLoader(classpath);
// Remap several classes to our stubs. Really we should add all of these to TeaVM, but our current
// implementations are a bit of a hack.
@ -75,7 +76,7 @@ private static void buildClasses(List<Path> input, List<Path> classpath, Path ou
tool.setMainClass("cc.tweaked.web.Main");
tool.setOptimizationLevel(TeaVMOptimizationLevel.ADVANCED);
tool.setObfuscated(true);
tool.setObfuscated(minify);
tool.generate();
TeaVMProblemRenderer.describeProblems(tool.getDependencyInfo().getCallGraph(), tool.getProblemProvider(), new ConsoleTeaVMToolLog(false));

View File

@ -22,6 +22,7 @@
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Consumer;
/**
* Compile-time generation of {@link LuaMethod} methods.
@ -31,20 +32,22 @@
*/
@CompileTime
public class MethodReflection {
public static List<NamedMethod<LuaMethod>> getMethods(Class<?> klass) {
List<NamedMethod<LuaMethod>> out = new ArrayList<>();
getMethodsImpl(klass, (name, method, nonYielding) -> out.add(new NamedMethod<>(name, method, nonYielding, null)));
return out;
@Meta
public static native boolean getMethods(Class<?> type, Consumer<NamedMethod<LuaMethod>> make);
private static void getMethods(ReflectClass<?> klass, Value<Consumer<NamedMethod<LuaMethod>>> make) {
var result = getMethodsImpl(klass, make);
// Using "unsupportedCase" here causes us to skip generating any code and just return null. While null isn't
// a boolean, it's still false-y and thus has the same effect in the generated JS!
if (!result) Metaprogramming.unsupportedCase();
Metaprogramming.exit(() -> result);
}
@Meta
private static native void getMethodsImpl(Class<?> type, MakeMethod make);
private static void getMethodsImpl(ReflectClass<Object> klass, Value<MakeMethod> make) {
private static boolean getMethodsImpl(ReflectClass<?> klass, Value<Consumer<NamedMethod<LuaMethod>>> make) {
if (!klass.getName().startsWith("dan200.computercraft.") && !klass.getName().startsWith("cc.tweaked.web.peripheral")) {
return;
return false;
}
if (klass.getName().contains("lambda")) return;
if (klass.getName().contains("lambda")) return false;
Class<?> actualClass;
try {
@ -53,17 +56,16 @@ private static void getMethodsImpl(ReflectClass<Object> klass, Value<MakeMethod>
throw new RuntimeException(e);
}
for (var method : Internal.getMethods(actualClass)) {
var methods = Internal.getMethods(actualClass);
for (var method : methods) {
var name = method.name();
var nonYielding = method.nonYielding();
var actualField = method.method().getField("INSTANCE");
Metaprogramming.emit(() -> make.get().make(name, (LuaMethod) actualField.get(null), nonYielding));
Metaprogramming.emit(() -> make.get().accept(new NamedMethod<>(name, (LuaMethod) actualField.get(null), nonYielding, null)));
}
}
public interface MakeMethod {
void make(String name, LuaMethod method, boolean nonYielding);
return !methods.isEmpty();
}
private static final class Internal {

View File

@ -21,24 +21,16 @@ private TLuaMethodSupplier() {
@Override
public boolean forEachSelfMethod(Object object, UntargetedConsumer<LuaMethod> consumer) {
var methods = MethodReflection.getMethods(object.getClass());
for (var method : methods) consumer.accept(method.name(), method.method(), method);
return !methods.isEmpty();
return MethodReflection.getMethods(object.getClass(), method -> consumer.accept(method.name(), method.method(), method));
}
@Override
public boolean forEachMethod(Object object, TargetedConsumer<LuaMethod> consumer) {
var methods = MethodReflection.getMethods(object.getClass());
for (var method : methods) consumer.accept(object, method.name(), method.method(), method);
var hasMethods = !methods.isEmpty();
var hasMethods = MethodReflection.getMethods(object.getClass(), method -> consumer.accept(object, method.name(), method.method(), method));
if (object instanceof ObjectSource source) {
for (var extra : source.getExtra()) {
var extraMethods = MethodReflection.getMethods(extra.getClass());
if (!extraMethods.isEmpty()) hasMethods = true;
for (var method : extraMethods) consumer.accept(extra, method.name(), method.method(), method);
hasMethods |= MethodReflection.getMethods(extra.getClass(), method -> consumer.accept(extra, method.name(), method.method(), method));
}
}