-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
runtime/src/main/java/ortus/boxlang/runtime/testing/Phase2Lambda$greet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* [BoxLang] | ||
* | ||
* Copyright [2023] [Ortus Solutions, Corp] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ortus.boxlang.runtime.testing; | ||
|
||
import ortus.boxlang.runtime.context.FunctionBoxContext; | ||
import ortus.boxlang.runtime.operators.Concat; | ||
import ortus.boxlang.runtime.scopes.Key; | ||
import ortus.boxlang.runtime.scopes.LocalScope; | ||
import ortus.boxlang.runtime.types.Lambda; | ||
|
||
/** | ||
* Phase 2 BoxLang | ||
* Example of UDF delcaration and execution | ||
*/ | ||
public class Phase2Lambda$greet extends Lambda { | ||
|
||
private static Phase2Lambda$greet instance; | ||
|
||
private Phase2Lambda$greet() { | ||
super( | ||
new Argument[] { | ||
new Argument( true, "String", Key.of( "name" ), "Brad", "" ) | ||
} | ||
); | ||
} | ||
|
||
public static synchronized Phase2Lambda$greet getInstance() { | ||
if ( instance == null ) { | ||
instance = new Phase2Lambda$greet(); | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* <pre> | ||
( required string name='Brad' ) -> { | ||
var greeting = "Hello " & name; | ||
return greeting; | ||
} | ||
* </pre> | ||
*/ | ||
@Override | ||
public Object invoke( FunctionBoxContext context ) { | ||
|
||
context.getScopeNearby( LocalScope.name ).assign( | ||
Key.of( "Greeting" ), | ||
Concat.invoke( | ||
"Hello ", | ||
context.scopeFindNearby( Key.of( "name" ), null ).value() | ||
) | ||
); | ||
|
||
return context.scopeFindNearby( Key.of( "greeting" ), null ).value(); | ||
} | ||
|
||
} |
142 changes: 142 additions & 0 deletions
142
runtime/src/main/java/ortus/boxlang/runtime/testing/Phase2Lambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* [BoxLang] | ||
* | ||
* Copyright [2023] [Ortus Solutions, Corp] | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package ortus.boxlang.runtime.testing; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import ortus.boxlang.runtime.BoxRuntime; | ||
import ortus.boxlang.runtime.context.IBoxContext; | ||
// BoxLang Auto Imports | ||
import ortus.boxlang.runtime.dynamic.BaseTemplate; | ||
import ortus.boxlang.runtime.dynamic.Referencer; | ||
import ortus.boxlang.runtime.loader.ClassLocator; | ||
import ortus.boxlang.runtime.loader.ImportDefinition; | ||
import ortus.boxlang.runtime.scopes.IScope; | ||
import ortus.boxlang.runtime.scopes.Key; | ||
|
||
/** | ||
* Phase 2 BoxLang | ||
* Example of UDF delcaration and execution | ||
*/ | ||
public class Phase2Lambda extends BaseTemplate { | ||
|
||
private static Phase2Lambda instance; | ||
|
||
private final static List<ImportDefinition> imports = List.of(); | ||
|
||
private Phase2Lambda() { | ||
} | ||
|
||
public static synchronized Phase2Lambda getInstance() { | ||
if ( instance == null ) { | ||
instance = new Phase2Lambda(); | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* <pre> | ||
<cfscript> | ||
variables.greet = ( required string name='Brad' ) -> { | ||
var greeting = "Hello " & name; | ||
return greeting; | ||
} | ||
variables.out = (create java.lang.System).out; | ||
// Positional args | ||
variables.out.println( greet( 'John' ) ); | ||
// named args | ||
variables.out.println( greet( name='John' ) ); | ||
</cfscript> | ||
* </pre> | ||
*/ | ||
|
||
@Override | ||
public void _invoke( IBoxContext context ) throws Throwable { | ||
ClassLocator classLocator = ClassLocator.getInstance(); | ||
IScope variablesScope = context.getScopeNearby( Key.of( "variables" ) ); | ||
|
||
// Create instance of Lambda and set in the variables scope | ||
variablesScope.put( | ||
Key.of( "greet" ), | ||
Phase2Lambda$greet.getInstance() | ||
); | ||
|
||
variablesScope.put( | ||
Key.of( "out" ), | ||
Referencer.get( | ||
classLocator.load( context, "java:java.lang.System", imports ), | ||
Key.of( "out" ), | ||
false ) | ||
); | ||
|
||
// Positional args | ||
Referencer.getAndInvoke( | ||
context, | ||
// Object | ||
variablesScope.get( Key.of( "out" ) ), | ||
// Method | ||
Key.of( "println" ), | ||
// Arguments | ||
new Object[] { | ||
context.invokeFunction( Key.of( "greet" ), new Object[] { "John" } ) | ||
}, | ||
false | ||
); | ||
|
||
// named args | ||
Referencer.getAndInvoke( | ||
context, | ||
// Object | ||
variablesScope.get( Key.of( "out" ) ), | ||
// Method | ||
Key.of( "println" ), | ||
// Arguments | ||
new Object[] { | ||
context.invokeFunction( Key.of( "greet" ), Map.of( Key.of( "name" ), "Bob" ) ) | ||
}, | ||
false | ||
); | ||
|
||
} | ||
|
||
/** | ||
* Main method | ||
* | ||
* @param args | ||
*/ | ||
public static void main( String[] args ) { | ||
// This is the main method, it will be invoked when the template is executed | ||
// You can use this | ||
// Get a runtime going | ||
BoxRuntime boxRuntime = BoxRuntime.getInstance( true ); | ||
|
||
try { | ||
boxRuntime.executeTemplate( Phase2Lambda.getInstance() ); | ||
} catch ( Throwable e ) { | ||
e.printStackTrace(); | ||
System.exit( 1 ); | ||
} | ||
|
||
// Bye bye! Ciao Bella! | ||
boxRuntime.shutdown(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters