Skip to content

Commit

Permalink
Update Cobalt to 0.7
Browse files Browse the repository at this point in the history
 - Timeouts are now driven by an interrupt system, rather than polling.
   While we do not impose memory limits, this should close #1333.

 - Update the table library to largely match Lua 5.4:
    - Add table.move
    - Table methods (with the exception of foreach/foreachi) now use
      metamethods (closes #1088).
   There's still some remaining quirks (for instance, table.insert
   accepts values out-of-bounds), but I think that's fine.

 - Cobalt's threaded-coroutine system is gone (load now supports
   yielding), so we no longer track coroutine metrics.

 - Type errors now use __name. See #1355, though this does not apply to
   CC methods (either on the Java or CraftOS side), so is not enough to
   resolve it.

See cc-tweaked/Cobalt@v0.6.0...v0.7.0 for the
full delta.
  • Loading branch information
SquidDev committed Mar 26, 2023
1 parent 0046b09 commit 5bb2e8e
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 227 deletions.
16 changes: 8 additions & 8 deletions doc/reference/feature_compat.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CC: Tweaked is based off of the Cobalt Lua runtime, which uses Lua 5.1. However,
| Hex literal fractional/exponent parts || |
| Empty statements || |
| `__len` metamethod || |
| `__ipairs` metamethod || |
| `__ipairs` metamethod || Deprecated in Lua 5.3. `ipairs` uses `__len`/`__index` instead. |
| `__pairs` metamethod || |
| `bit32` library || |
| `collectgarbage` isrunning, generational, incremental options || `collectgarbage` does not exist in CC:T. |
Expand All @@ -32,16 +32,16 @@ CC: Tweaked is based off of the Cobalt Lua runtime, which uses Lua 5.1. However,
| `rawlen` function || |
| Negative index to `select` || |
| Removed `unpack` | 🔶 | Only if `disable_lua51_features` is enabled in the configuration. |
| Arguments to `xpcall` | | |
| Second return value from `coroutine.running` | | |
| Arguments to `xpcall` | | |
| Second return value from `coroutine.running` | | |
| Removed `module` || |
| `package.loaders` -> `package.searchers` || |
| Second argument to loader functions || |
| `package.config` || |
| `package.searchpath` || |
| Removed `package.seeall` || |
| `string.dump` on functions with upvalues (blanks them out) || |
| `string.rep` separator | | |
| `string.rep` separator | | |
| `%g` match group || |
| Removal of `%z` match group || |
| Removed `table.maxn` | 🔶 | Only if `disable_lua51_features` is enabled in the configuration. |
Expand All @@ -64,7 +64,7 @@ CC: Tweaked is based off of the Cobalt Lua runtime, which uses Lua 5.1. However,
| Removal of ambiguity error || |
| Identifiers may no longer use locale-dependent letters || |
| Ephemeron tables || |
| Identical functions may be reused || |
| Identical functions may be reused || Removed in Lua 5.4 |
| Generational garbage collector || Cobalt uses the built-in Java garbage collector. |

## Lua 5.3
Expand All @@ -75,10 +75,10 @@ CC: Tweaked is based off of the Cobalt Lua runtime, which uses Lua 5.1. However,
| `\u{XXX}` escape sequence || |
| `utf8` library || |
| removed `__ipairs` metamethod || |
| `coroutine.isyieldable` | | |
| `coroutine.isyieldable` | | |
| `string.dump` strip argument || |
| `string.pack`/`string.unpack`/`string.packsize` || |
| `table.move` | | |
| `table.move` | | |
| `math.atan2` -> `math.atan` || |
| Removed `math.frexp`, `math.ldexp`, `math.pow`, `math.cosh`, `math.sinh`, `math.tanh` || |
| `math.maxinteger`/`math.mininteger` || |
Expand All @@ -87,7 +87,7 @@ CC: Tweaked is based off of the Cobalt Lua runtime, which uses Lua 5.1. However,
| `math.ult` || |
| Removed `bit32` library || |
| Remove `*` from `file:read` modes || |
| Metamethods respected in `table.*`, `ipairs` | 🔶 | Only `__lt` is respected. |
| Metamethods respected in `table.*`, `ipairs` | | |

## Lua 5.0
| Feature | Supported? | Notes |
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ parchmentMc = "1.19.3"
asm = "9.3"
autoService = "1.0.1"
checkerFramework = "3.32.0"
cobalt = "0.6.0"
cobalt = "0.7.0"
fastutil = "8.5.9"
guava = "31.1-jre"
jetbrainsAnnotations = "24.0.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,6 @@ private void addTranslations() {
add(Metrics.HTTP_DOWNLOAD, "HTTP download");
add(Metrics.WEBSOCKET_INCOMING, "Websocket incoming");
add(Metrics.WEBSOCKET_OUTGOING, "Websocket outgoing");
add(Metrics.COROUTINES_CREATED, "Coroutines created");
add(Metrics.COROUTINES_DISPOSED, "Coroutines disposed");
add(Metrics.TURTLE_OPS, "Turtle operations");

add(AggregatedMetric.TRANSLATION_PREFIX + Aggregate.MAX.id(), "%s (max)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

package dan200.computercraft.core.computer;

import com.google.errorprone.annotations.concurrent.GuardedBy;
import dan200.computercraft.core.lua.ILuaMachine;
import dan200.computercraft.core.lua.MachineResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -48,6 +52,8 @@ public final class TimeoutState {
public static final String ABORT_MESSAGE = "Too long without yielding";

private final ComputerThread scheduler;
@GuardedBy("this")
private final List<Runnable> listeners = new ArrayList<>(0);

private boolean paused;
private boolean softAbort;
Expand Down Expand Up @@ -92,8 +98,15 @@ public synchronized void refresh() {
// Important: The weird arithmetic here is important, as nanoTime may return negative values, and so we
// need to handle overflow.
var now = System.nanoTime();
if (!paused) paused = currentDeadline - now <= 0 && scheduler.hasPendingWork(); // now >= currentDeadline
if (!softAbort) softAbort = now - cumulativeStart - TIMEOUT >= 0; // now - cumulativeStart >= TIMEOUT
var changed = false;
if (!paused && (paused = currentDeadline - now <= 0 && scheduler.hasPendingWork())) { // now >= currentDeadline
changed = true;
}
if (!softAbort && (softAbort = now - cumulativeStart - TIMEOUT >= 0)) { // now - cumulativeStart >= TIMEOUT
changed = true;
}

if (changed) updateListeners();
}

/**
Expand Down Expand Up @@ -131,6 +144,9 @@ public boolean isHardAborted() {
*/
void hardAbort() {
softAbort = hardAbort = true;
synchronized (this) {
updateListeners();
}
}

/**
Expand All @@ -153,6 +169,7 @@ synchronized void pauseTimer() {
// We set the cumulative time to difference between current time and "nominal start time".
cumulativeElapsed = System.nanoTime() - cumulativeStart;
paused = false;
updateListeners();
}

/**
Expand All @@ -161,5 +178,22 @@ synchronized void pauseTimer() {
synchronized void stopTimer() {
cumulativeElapsed = 0;
paused = softAbort = hardAbort = false;
updateListeners();
}

@GuardedBy("this")
private void updateListeners() {
for (var listener : listeners) listener.run();
}

public synchronized void addListener(Runnable listener) {
Objects.requireNonNull(listener, "listener cannot be null");
listeners.add(listener);
listener.run();
}

public synchronized void removeListener(Runnable listener) {
Objects.requireNonNull(listener, "listener cannot be null");
listeners.remove(listener);
}
}
Loading

0 comments on commit 5bb2e8e

Please sign in to comment.