Skip to content

Commit

Permalink
Update to Lua 5.2
Browse files Browse the repository at this point in the history
Yes, I could do this in multiple commits. Or I could chuck it in one
horrifying mess!
  • Loading branch information
SquidDev committed Nov 1, 2023
1 parent dc245ed commit 3b9e158
Show file tree
Hide file tree
Showing 93 changed files with 1,534 additions and 1,159 deletions.
28 changes: 19 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
id("com.github.hierynomus.license") version "0.16.1"
}

group = "org.squiddev"
version = "0.7.3"
group = "cc.tweaked"
version = "0.8.0-SNAPSHOT"

java {
toolchain {
Expand All @@ -18,6 +18,10 @@ java {
sourceSets {
// Put double conversion in a separate library, so we can run the signedness checker on it.
register("doubles")
register("moduleInfo") {
compileClasspath = main.get().compileClasspath
runtimeClasspath = files()
}
}

repositories {
Expand Down Expand Up @@ -53,10 +57,8 @@ fun configureChecker(sourceSet: SourceSet, arguments: () -> List<String>) {

tasks.named(sourceSet.compileJavaTaskName, JavaCompile::class) {
options.isFork = true
options.compilerArgumentProviders.add {
arguments()
}
options.forkOptions.jvmArgumentProviders.add {
options.compilerArgs.addAll(arguments())
options.forkOptions.jvmArgs!!.addAll(
listOf(
"--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
Expand All @@ -67,8 +69,8 @@ fun configureChecker(sourceSet: SourceSet, arguments: () -> List<String>) {
"--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
"--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
"--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
)
}
),
)
}
}

Expand Down Expand Up @@ -105,12 +107,20 @@ tasks.compileJava {
finalizedBy(instrumentJava)
}

// Due to splitting doubles into a separate library, we can't put a module-info into the main source set (as the doubles
// classes wouldn't be visible). Instead, we compile module-info.java on its own.
tasks.named<JavaCompile>("compileModuleInfoJava") {
dependsOn(tasks.compileJava)
options.compilerArgs.addAll(listOf("--patch-module", "cc.tweaked.cobalt=${untransformedClasses.get().asFile.absolutePath}"))
}

license {
include("*.java")
}

tasks.jar {
from(sourceSets["doubles"].output)
from(sourceSets["moduleInfo"].output)
}

publishing {
Expand All @@ -120,7 +130,7 @@ publishing {

pom {
name.set("Cobalt")
description.set("A reentrant fork of LuaJ for Lua 5.1")
description.set("A reentrant fork of LuaJ for Lua 5.2")
url.set("https://github.com/SquidDev/Cobalt")

scm {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = "Cobalt"
rootProject.name = "cobalt"

include("build-tools")
55 changes: 55 additions & 0 deletions src/main/java/cc/tweaked/cobalt/internal/LegacyEnv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cc.tweaked.cobalt.internal;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.squiddev.cobalt.Constants;
import org.squiddev.cobalt.LuaTable;
import org.squiddev.cobalt.LuaValue;
import org.squiddev.cobalt.Prototype;
import org.squiddev.cobalt.debug.Upvalue;
import org.squiddev.cobalt.function.LuaClosure;

import java.util.Objects;

/**
* Utilities for working with Lua 5.1-style {@code getfenv}/{@code setfenv}.
* <p>
* These simply search for an {@link Constants#ENV _ENV} upvalue and set it.
*/
public final class LegacyEnv {
private LegacyEnv() {
}

private static int findEnv(Prototype prototype) {
for (int i = 0; i < prototype.upvalues(); i++) {
if (Objects.equals(prototype.getUpvalueName(i), Constants.ENV)) return i;
}

return -1;
}

public static @Nullable LuaTable getEnv(LuaClosure closure) {
int index = findEnv(closure.getPrototype());
return index >= 0 && closure.getUpvalue(index).getValue() instanceof LuaTable t ? t : null;
}

public static @Nullable LuaTable getEnv(LuaValue value) {
return value instanceof LuaClosure c ? getEnv(c) : null;
}

public static void setEnv(LuaClosure closure, LuaTable env) {
int index = findEnv(closure.getPrototype());
if (index >= 0) {
// Slightly odd to create a new upvalue here, but ensures that it only affects this function.
closure.setUpvalue(index, new Upvalue(env));
}
}

public static boolean setEnv(LuaValue value, LuaTable env) {
if (!(value instanceof LuaClosure c)) return false;

setEnv(c, env);
// We always return true on Lua closures, even if technically this won't do anything, as it ensures somewhat
// consistent behaviour.
return true;
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/squiddev/cobalt/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ public class Constants {
*/
public static final LuaString LOADED = valueOf("_LOADED");

/**
* LuaString constant with value "_ENV" for use as metatag
*/
public static final LuaString ENV = valueOf("_ENV");

/**
* Constant limiting metatag loop processing
*/
Expand Down
Loading

0 comments on commit 3b9e158

Please sign in to comment.