Skip to content

Commit

Permalink
Handle output from PseudoConstructor
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jan 9, 2024
1 parent 0dee3d0 commit e2340a0
Show file tree
Hide file tree
Showing 11 changed files with 83 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/java/ortus/boxlang/runtime/BoxRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ public void executeTemplate( BoxTemplate template, IBoxContext context ) {
// Fire!!!
template.invoke( scriptingContext );
} finally {
scriptingContext.flushBuffer();
scriptingContext.flushBuffer( false );

// Debugging Timer
instance.logger.atDebug().log(
Expand Down Expand Up @@ -547,7 +547,7 @@ public Object executeStatement( String source, IBoxContext context ) {
// Fire!!!
return scriptRunnable.invoke( scriptingContext );
} finally {
scriptingContext.flushBuffer();
scriptingContext.flushBuffer( false );
// Debugging Timer
instance.logger.atDebug().log(
"Executed source [{}] ms",
Expand Down Expand Up @@ -585,7 +585,7 @@ public void executeSource( String source, IBoxContext context ) {
// Fire!!!
scriptRunnable.invoke( scriptingContext );
} finally {
scriptingContext.flushBuffer();
scriptingContext.flushBuffer( false );

// Debugging Timer
instance.logger.atDebug().log(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,11 @@ public IBoxContext writeToBuffer( Object o ) {
/**
* Flush the buffer to the output stream and then clears the local buffers
*
* @param force true, flush even if output is disabled
*
* @return This context
*/
public IBoxContext flushBuffer() {
public IBoxContext flushBuffer( boolean force ) {
if ( hasParent() ) {
synchronized ( buffer ) {
getParent().writeToBuffer( buffer.toString() );
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/ortus/boxlang/runtime/context/ClassBoxContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public ClassBoxContext( IBoxContext parent, IClassRunnable thisClass ) {
super( parent );
this.variablesScope = thisClass.getVariablesScope();
this.thisScope = thisClass.getThisScope();
this.thisClass = thisClass;

if ( parent == null ) {
throw new BoxRuntimeException( "Parent context cannot be null for ClassBoxContext" );
Expand Down Expand Up @@ -147,7 +148,6 @@ public IScope getScopeNearby( Key name, boolean shallow ) throws ScopeNotFoundEx
*/
@Override
public IScope getDefaultAssignmentScope() {
// DIFFERENT FROM CFML ENGINES! Same as Lucee's "local mode"
return variablesScope;
}

Expand All @@ -171,4 +171,19 @@ public void registerUDF( UDF udf ) {
public IClassRunnable getThisClass() {
return thisClass;
}

/**
* Flush the buffer to the output stream and then clears the local buffers
*
* @param force true, flush even if output is disabled
*
* @return This context
*/
public IBoxContext flushBuffer( boolean force ) {
// direct flushing ignored if we can't output
if ( force || getThisClass().canOutput() ) {
super.flushBuffer( force );
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,18 @@ public IClassRunnable getThisClass() {
return ( IClassRunnable ) templates.peek();
}

/**
* Flush the buffer to the output stream and then clears the local buffers
*
* @param force true, flush even if output is disabled
*
* @return This context
*/
public IBoxContext flushBuffer( boolean force ) {
// direct flushing ignored if we can't output
if ( force || getFunction().canOutput( this ) ) {
super.flushBuffer( force );
}
return this;
}
}
4 changes: 3 additions & 1 deletion src/main/java/ortus/boxlang/runtime/context/IBoxContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,11 @@ public record ScopeSearchResult( IScope scope, Object value ) {
* to its parent context. Different "top level" buffers can decide what they want to do with the buffer.
* i.e. Scripting sends to the console, Web sends to HTTP response stream, etc.
*
* @param force true, flush even if output is disabled
*
* @return This context
*/
public IBoxContext flushBuffer();
public IBoxContext flushBuffer( boolean force );

/**
* Clear the buffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,11 @@ public IScope getDefaultAssignmentScope() {
/**
* Flush the buffer to the output stream
*
* @param force true, flush even if output is disabled
*
* @return This context
*/
public IBoxContext flushBuffer() {
public IBoxContext flushBuffer( boolean force ) {
if ( hasParent() ) {
getParent().writeToBuffer( buffer.toString() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ private static <T> T bootstrapBLClass( IBoxContext context, IClassRunnable cfc,
IBoxContext classContext = new ClassBoxContext( context, cfc );

// Bootstrap the pseudoConstructor
classContext.pushTemplate( cfc );
cfc.pseudoConstructor( classContext );

// Call constructor
Expand Down Expand Up @@ -343,6 +344,9 @@ private static <T> T bootstrapBLClass( IBoxContext context, IClassRunnable cfc,
}
}
}
// This is for any output written in the pseudoconstructor that needs to be flushed
classContext.flushBuffer( false );
classContext.popTemplate();
return ( T ) cfc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ortus/boxlang/runtime/types/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public Object invoke( FunctionBoxContext context ) {

// If output=true, then flush any content in buffer
if ( canOutput( context ) ) {
context.flushBuffer();
context.flushBuffer( false );
}

return data.get( "result" );
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/TestCases/phase3/ClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,34 @@ public void testInitMethod() {

}

@DisplayName( "PseudoConstructor can output" )
@Test
public void testPseudoConstructorOutput() {

instance.executeStatement(
"""
cfc = new src.test.java.TestCases.phase3.PseudoConstructorOutput();
result = getBoxContext().getBuffer().toString()
""", context );

assertThat( variables.get( Key.of( "result" ) ) ).isEqualTo( "PseudoConstructorOutput" );

}

@DisplayName( "PseudoConstructor will not output" )
@Test
public void testPseudoConstructorNoOutput() {

instance.executeStatement(
"""
cfc = new src.test.java.TestCases.phase3.PseudoConstructorNoOutput();
result = getBoxContext().getBuffer().toString()
""", context );

assertThat( variables.get( Key.of( "result" ) ) ).isEqualTo( "" );

}

}
3 changes: 3 additions & 0 deletions src/test/java/TestCases/phase3/PseudoConstructorNoOutput.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
component output=false {
echo( "PseudoConstructorOutput" );
}
3 changes: 3 additions & 0 deletions src/test/java/TestCases/phase3/PseudoConstructorOutput.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
component output=true {
echo( "PseudoConstructorOutput" );
}

0 comments on commit e2340a0

Please sign in to comment.