1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-07-01 01:23:30 +00:00

Update Cobalt version for latest bugfixes/changes

- Fix load not unwinding stack
 - Allow transferring control between coroutines when unwinding is
   disabled

Fixes #128
This commit is contained in:
SquidDev 2019-02-25 14:17:12 +00:00
parent 162fb37421
commit fb9c125ab8

View File

@ -117,7 +117,7 @@ private void handleSoftAbort() throws LuaError
throw new LuaError( message ); throw new LuaError( message );
} }
} ) } )
.yieldThreader( command -> { .coroutineExecutor( command -> {
Tracking.addValue( m_computer, TrackingField.COROUTINES_CREATED, 1 ); Tracking.addValue( m_computer, TrackingField.COROUTINES_CREATED, 1 );
coroutines.execute( () -> { coroutines.execute( () -> {
try try
@ -145,7 +145,8 @@ private void handleSoftAbort() throws LuaError
if( ComputerCraft.debug_enable ) m_globals.load( state, new DebugLib() ); if( ComputerCraft.debug_enable ) m_globals.load( state, new DebugLib() );
// Register custom load/loadstring provider which automatically adds prefixes. // Register custom load/loadstring provider which automatically adds prefixes.
LibFunction.bind( m_globals, PrefixLoader.class, new String[] { "load", "loadstring" } ); m_globals.rawset( "load", new PrefixWrapperFunction( m_globals.rawget( "load" ), 0 )) ;
m_globals.rawset( "loadstring", new PrefixWrapperFunction( m_globals.rawget( "loadstring" ), 1 ) );
// Remove globals we don't want to expose // Remove globals we don't want to expose
m_globals.rawset( "collectgarbage", Constants.NIL ); m_globals.rawset( "collectgarbage", Constants.NIL );
@ -230,7 +231,7 @@ public void handleEvent( String eventName, Object[] arguments )
if( m_mainRoutine.getStatus().equals( "dead" ) ) unload(); if( m_mainRoutine.getStatus().equals( "dead" ) ) unload();
} }
catch( LuaError | HardAbortError e ) catch( LuaError | HardAbortError | InterruptedException e )
{ {
unload(); unload();
ComputerCraft.log.warn( "Top level coroutine errored", e ); ComputerCraft.log.warn( "Top level coroutine errored", e );
@ -630,13 +631,24 @@ private static Object[] toObjects( Varargs values, int startIdx )
return objects; return objects;
} }
private static class PrefixLoader extends VarArgFunction private static class PrefixWrapperFunction extends VarArgFunction
{ {
private static final LuaString FUNCTION_STR = valueOf( "function" ); private static final LuaString FUNCTION_STR = valueOf( "function" );
private static final LuaString EQ_STR = valueOf( "=" ); private static final LuaString EQ_STR = valueOf( "=" );
private final LibFunction underlying;
public PrefixWrapperFunction(LuaValue wrap, int opcode) {
LibFunction underlying = (LibFunction)wrap;
this.underlying = underlying;
this.opcode = opcode;
this.name = underlying.debugName();
this.env = underlying.getfenv();
}
@Override @Override
public Varargs invoke( LuaState state, Varargs args ) throws LuaError public Varargs invoke( LuaState state, Varargs args ) throws LuaError, UnwindThrowable
{ {
switch( opcode ) switch( opcode )
{ {
@ -648,7 +660,7 @@ public Varargs invoke( LuaState state, Varargs args ) throws LuaError
{ {
chunkname = OperationHelper.concat( EQ_STR, chunkname ); chunkname = OperationHelper.concat( EQ_STR, chunkname );
} }
return BaseLib.loadStream( state, new StringInputStream( state, func ), chunkname ); return underlying.invoke(state, varargsOf(func, chunkname));
} }
case 1: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg case 1: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
{ {
@ -658,7 +670,7 @@ public Varargs invoke( LuaState state, Varargs args ) throws LuaError
{ {
chunkname = OperationHelper.concat( EQ_STR, chunkname ); chunkname = OperationHelper.concat( EQ_STR, chunkname );
} }
return BaseLib.loadStream( state, script.toInputStream(), chunkname ); return underlying.invoke(state, varargsOf(script, chunkname));
} }
} }
@ -666,60 +678,6 @@ public Varargs invoke( LuaState state, Varargs args ) throws LuaError
} }
} }
private static class StringInputStream extends InputStream
{
private final LuaState state;
private final LuaValue func;
private byte[] bytes;
private int offset, remaining = 0;
StringInputStream( LuaState state, LuaValue func )
{
this.state = state;
this.func = func;
}
@Override
public int read() throws IOException
{
if( remaining <= 0 )
{
LuaValue s;
try
{
s = OperationHelper.noYield( state, () -> OperationHelper.call( state, func ) );
}
catch( LuaError e )
{
throw new IOException( e );
}
if( s.isNil() )
{
return -1;
}
LuaString ls;
try
{
ls = s.strvalue();
}
catch( LuaError e )
{
throw new IOException( e );
}
bytes = ls.bytes;
offset = ls.offset;
remaining = ls.length;
if( remaining <= 0 )
{
return -1;
}
}
--remaining;
return bytes[offset++];
}
}
private static class HardAbortError extends Error private static class HardAbortError extends Error
{ {
private static final long serialVersionUID = 7954092008586367501L; private static final long serialVersionUID = 7954092008586367501L;