Skip to content

Commit fb18dcb

Browse files
committed
Stop passing globals around everywhere
And fix tests. I ran the mcfly tests, but nothing else D:.
1 parent dbd1752 commit fb18dcb

File tree

23 files changed

+82
-90
lines changed

23 files changed

+82
-90
lines changed

src/main/java/org/squiddev/cobalt/function/LibFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ public String debugName() {
259259
*/
260260
protected abstract Varargs invoke(LuaState state, Varargs args) throws LuaError, UnwindThrowable;
261261

262-
public static void setGlobalLibrary(LuaState state, LuaTable env, String name, LuaValue library) throws LuaError {
263-
env.rawset(name, library);
262+
public static void setGlobalLibrary(LuaState state, String name, LuaValue library) throws LuaError {
263+
state.globals().rawset(name, library);
264264
state.registry().getSubTable(Constants.LOADED).rawset(name, library);
265265
}
266266

src/main/java/org/squiddev/cobalt/lib/BaseLib.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public final class BaseLib {
5959
private BaseLib() {
6060
}
6161

62-
public static void add(LuaTable env) {
62+
public static void add(LuaState state) {
63+
var env = state.globals();
6364
var self = new BaseLib();
6465
env.rawset("_G", env);
6566
env.rawset("_VERSION", valueOf("Lua 5.2"));

src/main/java/org/squiddev/cobalt/lib/Bit32Lib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public final class Bit32Lib {
3838
private Bit32Lib() {
3939
}
4040

41-
public static void add(LuaState state, LuaTable env) throws LuaError {
42-
LibFunction.setGlobalLibrary(state, env, "bit32", RegisteredFunction.bind(new RegisteredFunction[]{
41+
public static void add(LuaState state) throws LuaError {
42+
LibFunction.setGlobalLibrary(state, "bit32", RegisteredFunction.bind(new RegisteredFunction[]{
4343
RegisteredFunction.ofV("band", Bit32Lib::band),
4444
RegisteredFunction.of("bnot", Bit32Lib::bnot),
4545
RegisteredFunction.ofV("bor", Bit32Lib::bor),

src/main/java/org/squiddev/cobalt/lib/CoreLibraries.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import org.squiddev.cobalt.LuaError;
2828
import org.squiddev.cobalt.LuaState;
29-
import org.squiddev.cobalt.LuaTable;
3029

3130
/**
3231
* Set up an environment with all core/safe globals installed.
@@ -39,33 +38,28 @@ private CoreLibraries() {
3938
* Create a standard set of globals and setup a thread
4039
*
4140
* @param state The current lua state
42-
* @return Table of globals initialized with the standard JSE libraries
4341
* @see #debugGlobals(LuaState)
4442
* @see CoreLibraries
4543
*/
46-
public static LuaTable standardGlobals(LuaState state) throws LuaError {
47-
LuaTable globals = state.globals();
48-
BaseLib.add(globals);
49-
TableLib.add(state, globals);
50-
StringLib.add(state, globals);
51-
CoroutineLib.add(state, globals);
52-
MathLib.add(state, globals);
53-
Utf8Lib.add(state, globals);
54-
return globals;
44+
public static void standardGlobals(LuaState state) throws LuaError {
45+
BaseLib.add(state);
46+
TableLib.add(state);
47+
StringLib.add(state);
48+
CoroutineLib.add(state);
49+
MathLib.add(state);
50+
Utf8Lib.add(state);
5551
}
5652

5753
/**
5854
* Create standard globals including the {@link DebugLib} library.
5955
*
6056
* @param state The current lua state
61-
* @return Table of globals initialized with the standard JSE and debug libraries
6257
* @see #standardGlobals(LuaState)
6358
* @see CoreLibraries
6459
* @see DebugLib
6560
*/
66-
public static LuaTable debugGlobals(LuaState state) throws LuaError {
67-
LuaTable _G = standardGlobals(state);
68-
DebugLib.add(state, _G);
69-
return _G;
61+
public static void debugGlobals(LuaState state) throws LuaError {
62+
standardGlobals(state);
63+
DebugLib.add(state);
7064
}
7165
}

src/main/java/org/squiddev/cobalt/lib/CoroutineLib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ public final class CoroutineLib {
5555
private CoroutineLib() {
5656
}
5757

58-
public static void add(LuaState state, LuaTable env) throws LuaError {
59-
LibFunction.setGlobalLibrary(state, env, "coroutine", RegisteredFunction.bind(new RegisteredFunction[]{
58+
public static void add(LuaState state) throws LuaError {
59+
LibFunction.setGlobalLibrary(state, "coroutine", RegisteredFunction.bind(new RegisteredFunction[]{
6060
RegisteredFunction.of("create", CoroutineLib::create),
6161
RegisteredFunction.ofV("running", CoroutineLib::running),
6262
RegisteredFunction.of("status", CoroutineLib::status),

src/main/java/org/squiddev/cobalt/lib/DebugLib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ public final class DebugLib {
7474
private DebugLib() {
7575
}
7676

77-
public static void add(LuaState state, LuaTable env) throws LuaError {
78-
LibFunction.setGlobalLibrary(state, env, "debug", RegisteredFunction.bind(new RegisteredFunction[]{
77+
public static void add(LuaState state) throws LuaError {
78+
LibFunction.setGlobalLibrary(state, "debug", RegisteredFunction.bind(new RegisteredFunction[]{
7979
RegisteredFunction.ofV("debug", DebugLib::debug),
8080
RegisteredFunction.ofV("getfenv", DebugLib::getfenv),
8181
RegisteredFunction.ofV("gethook", DebugLib::gethook),

src/main/java/org/squiddev/cobalt/lib/MathLib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class MathLib {
5050
private MathLib() {
5151
}
5252

53-
public static void add(LuaState state, LuaTable env) throws LuaError {
53+
public static void add(LuaState state) throws LuaError {
5454
var self = new MathLib();
5555
final RegisteredFunction[] functions = new RegisteredFunction[]{
5656
RegisteredFunction.of("abs", (s, arg) -> valueOf(Math.abs(arg.checkDouble()))),
@@ -90,7 +90,7 @@ public static void add(LuaState state, LuaTable env) throws LuaError {
9090
t.rawset("huge", LuaDouble.POSINF);
9191
t.rawset("mod", t.rawget("fmod"));
9292

93-
LibFunction.setGlobalLibrary(state, env, "math", t);
93+
LibFunction.setGlobalLibrary(state, "math", t);
9494
}
9595

9696
private static LuaValue fmod(LuaState state, LuaValue arg1, LuaValue arg2) throws LuaError {

src/main/java/org/squiddev/cobalt/lib/StringLib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class StringLib {
5353
private StringLib() {
5454
}
5555

56-
public static void add(LuaState state, LuaTable env) throws LuaError {
56+
public static void add(LuaState state) throws LuaError {
5757
LuaTable t = RegisteredFunction.bind(new RegisteredFunction[]{
5858
RegisteredFunction.of("len", StringLib::len),
5959
RegisteredFunction.of("lower", StringLib::lower),
@@ -76,7 +76,7 @@ public static void add(LuaState state, LuaTable env) throws LuaError {
7676

7777
t.rawset("gfind", t.rawget("gmatch"));
7878

79-
LibFunction.setGlobalLibrary(state, env, "string", t);
79+
LibFunction.setGlobalLibrary(state, "string", t);
8080
state.stringMetatable = tableOf(INDEX, t);
8181
}
8282

src/main/java/org/squiddev/cobalt/lib/TableLib.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public final class TableLib {
5858
private TableLib() {
5959
}
6060

61-
public static void add(LuaState state, LuaTable env) throws LuaError {
61+
public static void add(LuaState state) throws LuaError {
6262
LuaTable t = RegisteredFunction.bind(new RegisteredFunction[]{
6363
RegisteredFunction.of("getn", TableLib::getn),
6464
RegisteredFunction.of("maxn", TableLib::maxn),
@@ -73,9 +73,9 @@ public static void add(LuaState state, LuaTable env) throws LuaError {
7373
RegisteredFunction.ofS("unpack", TableLib::unpack),
7474
});
7575

76-
env.rawset("unpack", t.rawget("unpack"));
76+
state.globals().rawset("unpack", t.rawget("unpack"));
7777

78-
LibFunction.setGlobalLibrary(state, env, "table", t);
78+
LibFunction.setGlobalLibrary(state, "table", t);
7979
}
8080

8181
private static LuaValue checkTableLike(LuaState state, Varargs args, int index, int flags) throws LuaError {

src/main/java/org/squiddev/cobalt/lib/Utf8Lib.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class Utf8Lib {
2929
private Utf8Lib() {
3030
}
3131

32-
public static void add(LuaState state, LuaTable env) throws LuaError {
32+
public static void add(LuaState state) throws LuaError {
3333
var self = new Utf8Lib();
3434
self.codesIter = RegisteredFunction.ofV("utf8.codesIter", Utf8Lib::codesIter).create();
3535

@@ -43,7 +43,7 @@ public static void add(LuaState state, LuaTable env) throws LuaError {
4343
});
4444
t.rawset("charpattern", PATTERN);
4545

46-
LibFunction.setGlobalLibrary(state, env, "utf8", t);
46+
LibFunction.setGlobalLibrary(state, "utf8", t);
4747
}
4848

4949
public static int buildCharacter(byte[] buffer, long codepoint) {

0 commit comments

Comments
 (0)