Skip to content

Commit

Permalink
Merge branch 'development' of github.com:ortus-boxlang/BoxLang into d…
Browse files Browse the repository at this point in the history
…evelopment
  • Loading branch information
michaelborn committed May 30, 2024
2 parents c04bce9 + 70538fb commit 2f4dc87
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public Invoke() {
*
* @argument.instance Instance of a Box Class or name of one to instantiate. If an empty string is provided, the method will be invoked within the
* same template or Box Class.
*
*
* @argument.methodname The name of the method to invoke
*
*
* @argument.arguments An array of positional arguments or a struct of named arguments to pass into the method.
*/
public Object _invoke( IBoxContext context, ArgumentsScope arguments ) {
Expand Down
28 changes: 23 additions & 5 deletions src/main/java/ortus/boxlang/runtime/runnables/BoxClassSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static BoxMeta getBoxMeta( IClassRunnable thisClass ) {

/**
* Represent as string, or throw exception if not possible
*
*
* @return The string representation
*/
public static String asString( IClassRunnable thisClass ) {
Expand Down Expand Up @@ -388,17 +388,35 @@ public static Object dereferenceAndInvoke( IClassRunnable thisClass, IBoxContext
// Check for generated accessors
Object hasAccessors = thisClass.getAnnotations().get( Key.accessors );
if ( hasAccessors != null && BooleanCaster.cast( hasAccessors ) ) {

// Getter Call and Return
Property getterProperty = thisClass.getGetterLookup().get( name );
if ( getterProperty != null ) {
return thisClass.getBottomClass().getVariablesScope().dereference( context, getterProperty.name(), safe );
}

// Setter Call and Return
Property setterProperty = thisClass.getSetterLookup().get( name );
if ( setterProperty != null ) {
Key thisName = setterProperty.name();
if ( !namedArguments.containsKey( thisName ) ) {
throw new BoxRuntimeException( "Missing argument for setter '" + name.getName() + "'" );
Key thisName = setterProperty.name();
Object thisValue = namedArguments.containsKey( thisName ) ? namedArguments.get( thisName ) : null;

// If we are still null, check an argument collection
if ( thisValue == null && namedArguments.containsKey( Function.ARGUMENT_COLLECTION ) ) {
Object argCollection = namedArguments.get( Function.ARGUMENT_COLLECTION );
if ( argCollection instanceof IStruct castedArgCollection ) {
thisValue = castedArgCollection.getOrDefault( thisName, null );
} else if ( argCollection instanceof List castedArgCollection && !castedArgCollection.isEmpty() ) {
thisValue = castedArgCollection.get( 0 );
}
}

if ( thisValue == null ) {
throw new BoxRuntimeException(
"Missing argument value for setter '" + name.getName() + "'. The passed arguments are [" + namedArguments.toString() + "]" );
}
thisClass.getBottomClass().getVariablesScope().assign( context, thisName, namedArguments.get( thisName ) );

thisClass.getBottomClass().getVariablesScope().assign( context, thisName, thisValue );
return thisClass;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/bx/Task.bx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class{
class accessors="true"{

property foo;
property firstName;
property lastName;
property numeric age default=1;
Expand Down
6 changes: 5 additions & 1 deletion src/test/bx/Test.bxs
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
println( "In Script running" )
task = new Task();
invoke( task, "setFoo", { foo : "bar" } );
result = invoke( task, "getFoo" );

println( result );
9 changes: 5 additions & 4 deletions src/test/java/TestCases/ScratchPad.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ void testIt() {
// @formatter:off
instance.executeSource(
"""
test = [ 1, 2 ,3 ]
for( item, index in test ) {
println( item )
}
task = new src.test.bx.Task();
invoke( task, "setFoo", { foo : "bar" } );
result = invoke( task, "getFoo" );
println( result );
""", context);
// @formatter:on

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,31 @@ function meh( a ) {

}

@DisplayName( "Test invoke dynamic setters" )
@Test
void testInvokeDynamicSetters() {
// @formatter:off
instance.executeSource(
"""
task = new src.test.bx.Task();
invoke( task, "setFoo", { foo : "bar" } );
result = invoke( task, "getFoo" );
""", context);
// @formatter:on

assertThat( variables.get( result ) ).isEqualTo( "bar" );

// @formatter:off
instance.executeSource(
"""
task = new src.test.bx.Task();
invoke( task, "setFoo", [ "bar" ] );
result = invoke( task, "getFoo" );
""", context);
// @formatter:on

assertThat( variables.get( result ) ).isEqualTo( "bar" );

}

}

0 comments on commit 2f4dc87

Please sign in to comment.