Skip to content

Commit

Permalink
Change timeout system to occur on instructions instead of API calls
Browse files Browse the repository at this point in the history
This means loops which do not touch CC specific methods will still
produce an error, rather than terminating the computer.
  • Loading branch information
SquidDev committed May 7, 2017
1 parent 0f2c1d9 commit 40b27c5
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/main/java/dan200/computercraft/core/lua/CobaltLuaMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public InputStream findResource( String filename )
state.debug = new DebugHandler( state )
{
private int count = 0;
private boolean hasSoftAbort;

@Override
public void onInstruction( DebugState ds, DebugFrame di, int pc, Varargs extras, int top ) throws LuaError
Expand All @@ -76,6 +77,10 @@ public void onInstruction( DebugState ds, DebugFrame di, int pc, Varargs extras,
if( m_hardAbortMessage != null ) LuaThread.yield( state, NONE );
this.count = 0;
}
else
{
handleSoftAbort();
}

super.onInstruction( ds, di, pc, extras, top );
}
Expand All @@ -84,6 +89,24 @@ public void onInstruction( DebugState ds, DebugFrame di, int pc, Varargs extras,
public void poll() throws LuaError
{
if( m_hardAbortMessage != null ) LuaThread.yield( state, NONE );
handleSoftAbort();
}

private void handleSoftAbort() throws LuaError {
// If the soft abort has been cleared then we can reset our flags and continue.
String message = m_softAbortMessage;
if (message == null) {
hasSoftAbort = false;
return;
}

if (hasSoftAbort && m_hardAbortMessage == null) {
// If we have fired our soft abort, but we haven't been hard aborted then everything is OK.
return;
}

hasSoftAbort = true;
throw new LuaError(message);
}
};

Expand Down Expand Up @@ -267,17 +290,6 @@ public void unload()
}
}

private void tryAbort() throws LuaError
{
String abortMessage = m_softAbortMessage;
if( abortMessage != null )
{
m_softAbortMessage = null;
m_hardAbortMessage = null;
throw new LuaError( abortMessage );
}
}

private LuaTable wrapLuaObject( ILuaObject object )
{
LuaTable table = new LuaTable();
Expand All @@ -293,7 +305,6 @@ private LuaTable wrapLuaObject( ILuaObject object )
@Override
public Varargs invoke( final LuaState state, Varargs _args ) throws LuaError
{
tryAbort();
Object[] arguments = toObjects( _args, 1 );
Object[] results;
try
Expand Down Expand Up @@ -570,7 +581,7 @@ else if( objects.containsKey( value ) )
LuaValue k = Constants.NIL;
while( true )
{
Varargs keyValue = null;
Varargs keyValue;
try
{
keyValue = luaTable.next( k );
Expand Down

0 comments on commit 40b27c5

Please sign in to comment.