-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Yes, I could do this in multiple commits. Or I could chuck it in one horrifying mess!
- Loading branch information
Showing
93 changed files
with
1,534 additions
and
1,159 deletions.
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
rootProject.name = "Cobalt" | ||
rootProject.name = "cobalt" | ||
|
||
include("build-tools") |
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,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; | ||
} | ||
} |
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
Oops, something went wrong.