Skip to content

Commit

Permalink
Finally remove LibFunction.opcode
Browse files Browse the repository at this point in the history
 - Change unpack to be indentical to table.unpack. Lua 5.1's unpack
   doesn't __len/__index (and so only works on tables, rather than
   userdata), but I don't think will break any existing code.

 - Remove several of our platform abstractions, and always just use the
   underlying OS API calls. This code only exists for passing the Lua
   tests (it's not used in CC at all), so the additional flexibility is
   not really useful.

 - Similarly, split BaseLib into a safe subset (as used by CC) and the
   full version as needed for tests.

 - Move the compiler interface to only generate functions from
   prototypes, rather than starting from scratch. We could remove
   it (luaj.luajc is long dead), I do have plans to have another go at
   using it.

 - Some small bits of cleanup while I was in the area.
  • Loading branch information
SquidDev committed Feb 25, 2023
1 parent 0ffcdad commit 89adc08
Show file tree
Hide file tree
Showing 81 changed files with 1,655 additions and 3,064 deletions.
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 @@ -241,6 +241,11 @@ public class Constants {
*/
public static final LuaString EMPTYSTRING = valueOf("");

/**
* The global loaded package table.
*/
public static final LuaString LOADED = valueOf("_LOADED");

/**
* Constant limiting metatag loop processing
*/
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/squiddev/cobalt/GlobalRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.squiddev.cobalt;

/**
* The global registry, a store of Lua values
*/
public final class GlobalRegistry {
private final LuaTable table = new LuaTable();

GlobalRegistry() {
}

/**
* Get the underlying registry table.
*
* @return The global debug registry.
*/
public LuaTable get() {
return table;
}

/**
* Get a subtable in the global {@linkplain #get()} registry table}. If the key exists but is not a table, then
* it will be overridden.
*
* @param name The name of the registry table.
* @return The subentry.
*/
public LuaTable getSubTable(LuaString name) {
LuaValue value = table.rawget(name);
if (value.isTable()) return (LuaTable) value;

LuaTable newValue = new LuaTable();
table.rawset(name, newValue);
return newValue;
}
}
185 changes: 15 additions & 170 deletions src/main/java/org/squiddev/cobalt/LuaState.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
import org.squiddev.cobalt.compiler.LuaC;
import org.squiddev.cobalt.debug.DebugHandler;
import org.squiddev.cobalt.debug.DebugHelpers;
import org.squiddev.cobalt.lib.platform.FileResourceManipulator;
import org.squiddev.cobalt.lib.platform.ResourceManipulator;

import java.io.InputStream;
import java.io.PrintStream;
import java.util.TimeZone;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -44,16 +39,6 @@
* Global lua state
*/
public final class LuaState {
/**
* The active input stream
*/
public InputStream stdin;

/**
* The active output stream
*/
public PrintStream stdout;

/**
* The metatable for all strings
*/
Expand Down Expand Up @@ -84,31 +69,16 @@ public final class LuaState {
*/
public LuaTable threadMetatable;

/**
* Lookup of loaded packages
*/
public final LuaTable loadedPackages = new LuaTable();

/**
* The active resource manipulator
*/
public final ResourceManipulator resourceManipulator;

/**
* The compiler for this threstate
*/
public final LoadState.LuaCompiler compiler;
public final LoadState.FunctionFactory compiler;

/**
* The handler for the debugger. Override this for custom debug actions.
*/
public final DebugHandler debug;

/**
* The timezone for this state, as used by {@code os}.
*/
public final TimeZone timezone;

/**
* The currently executing thread
*/
Expand Down Expand Up @@ -136,29 +106,31 @@ public final class LuaState {
*/
private final ErrorReporter reportError;

private final GlobalRegistry registry = new GlobalRegistry();

public LuaState() {
this(new LuaState.Builder());
}

private LuaState(Builder builder) {
stdin = builder.stdin;
stdout = builder.stdout;
stringMetatable = builder.stringMetatable;
booleanMetatable = builder.booleanMetatable;
numberMetatable = builder.numberMetatable;
nilMetatable = builder.nilMetatable;
functionMetatable = builder.functionMetatable;
threadMetatable = builder.threadMetatable;
resourceManipulator = builder.resourceManipulator;
compiler = builder.compiler;
debug = builder.debug;
timezone = builder.timezone;
threader = new YieldThreader(builder.coroutineExecutor);
reportError = builder.reportError;

mainThread = currentThread = new LuaThread(this, new LuaTable());
}

/**
* Get the global registry, a Lua table used to store Lua values.
*
* @return The global debug registry.
*/
public GlobalRegistry registry() {
return registry;
}


/**
* Abandon this state, instructing any pending thread to terminate.
*/
Expand Down Expand Up @@ -247,18 +219,8 @@ public static class Builder {
return thread;
});

private InputStream stdin = System.in;
private PrintStream stdout = System.out;
private LuaTable stringMetatable;
private LuaTable booleanMetatable;
private LuaTable numberMetatable;
private LuaTable nilMetatable;
private LuaTable functionMetatable;
private LuaTable threadMetatable;
private ResourceManipulator resourceManipulator = new FileResourceManipulator();
private LoadState.LuaCompiler compiler = LuaC.INSTANCE;
private LoadState.FunctionFactory compiler = LoadState::interpretedFunction;
private DebugHandler debug = DebugHandler.INSTANCE;
private TimeZone timezone = TimeZone.getDefault();
private Executor coroutineExecutor = defaultCoroutineExecutor;
private ErrorReporter reportError;

Expand All @@ -271,118 +233,13 @@ public LuaState build() {
return new LuaState(this);
}

/**
* Set the initial standard input for this Lua state. This defaults to {@link System#in}.
*
* @param stdin The new standard input
* @return This builder
* @see LuaState#stdin
*/
public Builder stdin(InputStream stdin) {
if (stdin == null) throw new NullPointerException("stdin cannot be null");
this.stdin = stdin;
return this;
}

/**
* Set the initial standard output for this Lua state. This defaults to {@link System#out}.
*
* @param stdout The new standard output
* @return This builder
* @see LuaState#stdout
*/
public Builder stdout(PrintStream stdout) {
if (stdout == null) throw new NullPointerException("stdout cannot be null");
this.stdout = stdout;
return this;
}

/**
* Set the initial metatable for string values within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder stringMetatable(LuaTable metatable) {
stringMetatable = metatable;
return this;
}

/**
* Set the initial metatable for boolean values within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder booleanMetatable(LuaTable metatable) {
booleanMetatable = metatable;
return this;
}

/**
* Set the initial metatable for numeric values within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder numberMetatable(LuaTable metatable) {
numberMetatable = metatable;
return this;
}

/**
* Set the initial metatable for nil values within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder nilMetatable(LuaTable metatable) {
nilMetatable = metatable;
return this;
}

/**
* Set the initial metatable for functions within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder functionMetatable(LuaTable metatable) {
functionMetatable = metatable;
return this;
}

/**
* Set the initial metatable for threads within this Lua State. This defaults to {@code null}.
*
* @param metatable The initial metatable
* @return This builder
*/
public Builder threadMetatable(LuaTable metatable) {
threadMetatable = metatable;
return this;
}

/**
* Set the resource manipulator that the {@code os} and {@code io} libraries will use. This defaults to a
* {@link FileResourceManipulator}, which uses the default file system.
*
* @param resourceManipulator The new resource manipulator
* @return This builder
*/
public Builder resourceManipulator(ResourceManipulator resourceManipulator) {
if (this.resourceManipulator == null) throw new NullPointerException("resourceManipulator cannot be null");
this.resourceManipulator = resourceManipulator;
return this;
}

/**
* Set the compiler for this Lua state. This defaults to using the {@link LuaC} compiler.
*
* @param compiler The new compiler to use
* @return This builder
*/
public Builder compiler(LoadState.LuaCompiler compiler) {
public Builder compiler(LoadState.FunctionFactory compiler) {
if (compiler == null) throw new NullPointerException("compiler cannot be null");
this.compiler = compiler;
return this;
Expand All @@ -400,18 +257,6 @@ public Builder debug(DebugHandler debug) {
return this;
}

/**
* Set the timezone for this Lua state.
*
* @param zone The new timezone
* @return This builder
*/
public Builder timezone(TimeZone zone) {
if (zone == null) throw new NullPointerException("zone cannot be null");
timezone = zone;
return this;
}

/**
* Set the coroutine executor for this state.
*
Expand Down
16 changes: 0 additions & 16 deletions src/main/java/org/squiddev/cobalt/LuaTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
*/
package org.squiddev.cobalt;

import org.squiddev.cobalt.function.LuaFunction;
import org.squiddev.cobalt.lib.LuaLibrary;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -716,19 +713,6 @@ public LuaValue[] keys() throws LuaError {
return l.toArray(new LuaValue[l.size()]);
}

/**
* Load a library instance by setting its environment to {@code this}
* and calling it, which should initialize the library instance and
* install itself into this instance.
*
* @param state The current lua state
* @param library The callable {@link LuaFunction} to load into {@code this}
* @return {@link LuaValue} containing the result of the initialization call.
*/
public LuaValue load(LuaState state, LuaLibrary library) {
return library.add(state, this);
}

//region Resizing

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/squiddev/cobalt/LuaThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.squiddev.cobalt.debug.DebugState;
import org.squiddev.cobalt.function.LuaFunction;
import org.squiddev.cobalt.lib.CoroutineLib;
import org.squiddev.cobalt.lib.jse.JsePlatform;
import org.squiddev.cobalt.lib.CoreLibraries;

import java.lang.ref.WeakReference;
import java.util.Objects;
Expand All @@ -45,7 +45,7 @@
* A LuaThread is typically created in response to a scripted call to
* {@code coroutine.create()}
* <p>
* The utility class {@link JsePlatform}
* The utility class {@link CoreLibraries}
* sees to it that this initialization is done properly.
* For this reason it is highly recommended to use one of these classes
* when initializing globals.
Expand All @@ -55,7 +55,7 @@
* to manage call state, it is possible to yield from anywhere in luaj.
*
* @see LuaValue
* @see JsePlatform
* @see CoreLibraries
* @see CoroutineLib
*/
public class LuaThread extends LuaValue {
Expand Down
Loading

0 comments on commit 89adc08

Please sign in to comment.