Skip to content

Commit

Permalink
Fix broken evaluate behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeers committed Mar 27, 2024
1 parent 0cd0123 commit 8dc5eff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
15 changes: 8 additions & 7 deletions src/main/java/ortus/boxlang/debugger/BoxLangDebugger.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import com.sun.jdi.AbsentInformationException;
Expand Down Expand Up @@ -271,17 +272,16 @@ public WrappedValue getContextForStackFrame( StackFrameTuple tuple ) {
return findVariableyName( tuple, "context" );
}

public WrappedValue evaluateInContext( String expression, int frameId )
public CompletableFuture<WrappedValue> evaluateInContext( String expression, int frameId )
throws InvalidTypeException, ClassNotLoadedException, IncompatibleThreadStateException, InvocationException {
// get current stackframe of breakpoint thread
StackFrameTuple sf = getSeenStack( frameId );
StackFrameTuple sf = getSeenStack( frameId );
// get the context
WrappedValue context = findVariableyName( sf, "context" );
WrappedValue context = findVariableyName( sf, "context" );
// get the runtime
WrappedValue runtime = getRuntime();
WrappedValue runtime = getRuntime();
// execute the expression

return runtime.invokeByNameAndArgsWithError(
return runtime.invokeAsync(
"executeStatement",
Arrays.asList( "java.lang.String", "ortus.boxlang.runtime.context.IBoxContext" ),
Arrays.asList(
Expand Down Expand Up @@ -577,9 +577,10 @@ private void handleClassPrepareEvent( ClassPrepareEvent event ) {

vmClasses.add( event.referenceType() );
SourceMap map = boxpiler.getSourceMapFromFQN( event.referenceType().name() );
if ( map == null ) {
if ( map == null || map.source == null ) {
return;
}

this.sourceMaps.put( map.source.toLowerCase(), map );
this.sourceMapsFromFQN.put( event.referenceType().name(), map );

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/ortus/boxlang/debugger/DebugAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,11 @@ public void visit( PauseRequest debugRequest ) {
* @param debugRequest
*/
public void visit( EvaluateRequest debugRequest ) {

try {
WrappedValue value = this.debugger.evaluateInContext( debugRequest.arguments.expression, debugRequest.arguments.frameId );
Variable varValue = JDITools.getVariable( "evaluated", value );
new EvaluateResponse( debugRequest, varValue ).send( this.outputStream );
this.debugger.evaluateInContext( debugRequest.arguments.expression, debugRequest.arguments.frameId ).thenAcceptAsync( ( wrappedValue ) -> {
Variable varValue = JDITools.getVariable( "evaluated", wrappedValue );
new EvaluateResponse( debugRequest, varValue ).send( this.outputStream );
} );
} catch ( InvocationException e ) {
String message = JDITools.wrap( this.debugger.bpe.thread(), e.exception() ).invoke( "getMessage" ).asStringReference().value();
new EvaluateResponse( debugRequest, message ).send( this.outputStream );
Expand Down Expand Up @@ -475,13 +475,15 @@ public void visit( StackTraceRequest debugRequest ) {
sf.source = new Source();
} else if ( blType == BoxLangType.CLOSURE ) {
// TODO figure out how to get the name of the closure from a parent context
String calledName = this.debugger.getContextForStackFrame( tuple ).invoke( "findClosestFunctionName" ).invoke( "getOriginalValue" )
String calledName = this.debugger.getContextForStackFrame( tuple )
.invoke( "findClosestFunctionName" ).invoke( "getOriginalValue" )
.asStringReference().value();

sf.name = calledName + " (closure)";
sf.source = new Source();
} else if ( blType == BoxLangType.LAMBDA ) {
String calledName = this.debugger.getContextForStackFrame( tuple ).invoke( "findClosestFunctionName" ).invoke( "getOriginalValue" )
String calledName = this.debugger.getContextForStackFrame( tuple )
.invoke( "findClosestFunctionName" ).invoke( "getOriginalValue" )
.asStringReference().value();
sf.name = calledName + " (lambda)";
sf.source = new Source();
Expand All @@ -499,7 +501,6 @@ public void visit( StackTraceRequest debugRequest ) {
return sf;
} )
.collect( Collectors.toList() );

new StackTraceResponse( debugRequest, stackFrames ).send( this.outputStream );
} catch ( IncompatibleThreadStateException e ) {
// TODO Auto-generated catch block
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/ortus/boxlang/debugger/JDITools.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;
import java.util.concurrent.CompletableFuture;

import com.sun.jdi.AbsentInformationException;
import com.sun.jdi.ArrayReference;
Expand Down Expand Up @@ -260,6 +261,28 @@ public VirtualMachine vm() {
return thread.virtualMachine();
}

public CompletableFuture<WrappedValue> invokeAsync( String methodName, List<String> argTypes, List<Value> args ) {
return CompletableFuture.supplyAsync( () -> {
try {
return this.invokeByNameAndArgsWithError( methodName, argTypes, args );
} catch ( InvalidTypeException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( ClassNotLoadedException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IncompatibleThreadStateException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( InvocationException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
} );
}

/**
* Invoke a method on the contained Value within the debugee VM. Uses the provided argTypes to find the correct method signature to call.
*
Expand Down

0 comments on commit 8dc5eff

Please sign in to comment.