Skip to content

Commit

Permalink
Merge branch 'development' of github.com:ortus-solutions-private/boxl…
Browse files Browse the repository at this point in the history
…ang into development
  • Loading branch information
lmajano committed Sep 21, 2023
2 parents b70dc28 + bbe56e3 commit d6df40f
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 58 deletions.
13 changes: 11 additions & 2 deletions runtime/src/main/java/ortus/boxlang/runtime/BoxRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.LoggerFactory;

import ortus.boxlang.runtime.context.IBoxContext;
import ortus.boxlang.runtime.context.RuntimeBoxContext;
import ortus.boxlang.runtime.context.ScriptingBoxContext;
import ortus.boxlang.runtime.dynamic.BaseTemplate;
import ortus.boxlang.runtime.logging.LoggingConfigurator;
Expand Down Expand Up @@ -81,6 +82,11 @@ public class BoxRuntime {
*/
private Boolean debugMode = false;

/**
* The runtime context
*/
private IBoxContext runtimeContext;

/**
* --------------------------------------------------------------------------
* Services
Expand All @@ -95,7 +101,7 @@ public class BoxRuntime {
/**
* The function service
*/
private FunctionService functionSerice;
private FunctionService functionService;

/**
* --------------------------------------------------------------------------
Expand Down Expand Up @@ -127,6 +133,9 @@ private BoxRuntime( Boolean debugMode ) {
// Create Services
this.interceptorService = InterceptorService.getInstance( RUNTIME_EVENTS );

// Create our runtime context that will be the granddaddy of all contexts that execute inside this runtime
this.runtimeContext = new RuntimeBoxContext();

// Announce Startup to Services
interceptorService.onStartup();

Expand Down Expand Up @@ -254,7 +263,7 @@ public void executeTemplate( BaseTemplate template ) throws Throwable {
instance.logger.atDebug().log( "Executing template [{}]", template.path );

// Build out the execution context for this execution and bind it to the incoming template
IBoxContext context = new ScriptingBoxContext();
IBoxContext context = new ScriptingBoxContext( runtimeContext );

// Announcements
Struct data = new Struct();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/**
* [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.context;

import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.ServerScope;
import ortus.boxlang.runtime.types.UDF;
import ortus.boxlang.runtime.types.exceptions.KeyNotFoundException;
import ortus.boxlang.runtime.types.exceptions.ScopeNotFoundException;

/**
* This context represents the context of the entire BoxLang Runtime. The runtime is persistent once
* started, and can be used to process one or more "requests" for execution. The "server" scope here is
* global and will be shared by all requests.
*/
public class RuntimeBoxContext extends BaseBoxContext {

/**
* --------------------------------------------------------------------------
* Private Properties
* --------------------------------------------------------------------------
*/

/**
* The variables scope
*/
protected IScope serverScope = new ServerScope();

/**
* --------------------------------------------------------------------------
* Constructors
* --------------------------------------------------------------------------
*/

/**
* Creates a new execution context with a bounded execution template and parent context
*
* @param template The template that this execution context is bound to
* @param parent The parent context
*/
public RuntimeBoxContext( IBoxContext parent ) {
super( parent );
}

/**
* Creates a new execution context
*/
public RuntimeBoxContext() {
this( null );
}

/**
* --------------------------------------------------------------------------
* Getters & Setters
* --------------------------------------------------------------------------
*/

/**
* Try to get the requested key from the unscoped scope
*
* @param key The key to search for
*
* @return The value of the key if found
*
* @throws KeyNotFoundException If the key was not found in any scope
*/
public ScopeSearchResult scopeFindNearby( Key key, IScope defaultScope, boolean shallow ) {

// There are no near-by scopes in the runtime context. Everything is global here.

if ( shallow ) {
return null;
}

return scopeFind( key, defaultScope );
}

/**
* Try to get the requested key from the unscoped scope
*
* @param key The key to search for
*
* @return The value of the key if found
*
* @throws KeyNotFoundException If the key was not found in any scope
*/
public ScopeSearchResult scopeFind( Key key, IScope defaultScope ) {

if ( parent != null ) {
return parent.scopeFind( key, defaultScope );
}

// Default scope requested for missing keys
if ( defaultScope != null ) {
return new ScopeSearchResult( defaultScope, null );
}
// Not found anywhere
throw new KeyNotFoundException(
String.format( "The requested key [%s] was not located in any scope or it's undefined", key.getName() )
);
}

/**
* Get a scope from the context. If not found, the parent context is asked.
* Don't search for scopes which are local to an execution context
*
* @return The requested scope
*/
public IScope getScope( Key name ) throws ScopeNotFoundException {

// Check the scopes I know about
if ( name.equals( serverScope.getName() ) ) {
return serverScope;
}

if ( parent != null ) {
return parent.getScope( name );
}

// Not found anywhere
throw new ScopeNotFoundException(
String.format( "The requested scope name [%s] was not located in any context", name.getName() )
);

}

/**
* Get a scope from the context. If not found, the parent context is asked.
* Search all konwn scopes
*
* @return The requested scope
*/
public IScope getScopeNearby( Key name, boolean shallow ) throws ScopeNotFoundException {

if ( shallow ) {
return null;
}

// The RuntimeBoxContext has no "nearby" scopes
return getScope( name );
}

public void registerUDF( UDF udf ) {
// This will prolly be unreachable since all executing code will be wrapped by another scope
serverScope.put( udf.getName(), udf );
}

/**
* Get the default variable assignment scope for this context
*
* @return The scope reference to use
*/
public IScope getDefaultAssignmentScope() {
// This will prolly be unreachable since all executing code will be wrapped by another scope
return serverScope;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import ortus.boxlang.runtime.scopes.IScope;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.scopes.RequestScope;
import ortus.boxlang.runtime.scopes.VariablesScope;
import ortus.boxlang.runtime.types.Struct;
import ortus.boxlang.runtime.types.UDF;
Expand All @@ -27,7 +28,12 @@

/**
* This context represents the context of a scripting execution in BoxLang
* There is only a variables scope present.
* There a variables and request scope present.
*
* The request scope may or may not belong here, but we're sort of using the scripting
* context as the top level context for an execution request right now, so it make the
* most sense here currently.
*
* There may or may NOT be a template defined.
*/
public class ScriptingBoxContext extends BaseBoxContext {
Expand All @@ -41,7 +47,12 @@ public class ScriptingBoxContext extends BaseBoxContext {
/**
* The variables scope
*/
protected IScope variablesScope = new VariablesScope();
protected IScope variablesScope = new VariablesScope();

/**
* The request scope
*/
protected IScope requestScope = new RequestScope();

/**
* --------------------------------------------------------------------------
Expand Down Expand Up @@ -129,8 +140,6 @@ public ScopeSearchResult scopeFindNearby( Key key, IScope defaultScope, boolean
*/
public ScopeSearchResult scopeFind( Key key, IScope defaultScope ) {

// The ScriptingBoxContext has no "global" scopes, so just defer to parent

if ( parent != null ) {
return parent.scopeFind( key, defaultScope );
}
Expand All @@ -153,7 +162,10 @@ public ScopeSearchResult scopeFind( Key key, IScope defaultScope ) {
*/
public IScope getScope( Key name ) throws ScopeNotFoundException {

// The ScriptingBoxContext has no "global" scopes, so just defer to parent
if ( name.equals( requestScope.getName() ) ) {
return requestScope;
}

if ( parent != null ) {
return parent.getScope( name );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ public static String cast( Object object, Boolean fail ) {
if ( object instanceof byte[] ) {
return new String( ( byte[] ) object );
}
// TODO: Figure out which types need specific casting
// For any casting failures, return null if the fail param is set to false!!
return object.toString();

if ( fail ) {
throw new RuntimeException( "Can't cast " + object.getClass().getName() + " to a string." );
} else {
return null;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* [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.scopes;

/**
* represents boxlang request scope container
* Note, this doesn't have to be a "web" request, but could just be a request to execute code
* or a template inside a runtime.
*/
public class RequestScope extends BaseScope {

/**
* --------------------------------------------------------------------------
* Public Properties
* --------------------------------------------------------------------------
*/
public static final Key name = Key.of( "request" );

/**
* --------------------------------------------------------------------------
* Constructors
* --------------------------------------------------------------------------
*/

public RequestScope() {
super( RequestScope.name );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* [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.scopes;

/**
* represents boxlang server scope container
* Note, this doesn't have to be a "web", it can reprsesent any long-running runtime which
* processes one or more "requests" for execution.
*/
public class ServerScope extends BaseScope {

/**
* --------------------------------------------------------------------------
* Public Properties
* --------------------------------------------------------------------------
*/
public static final Key name = Key.of( "server" );

/**
* --------------------------------------------------------------------------
* Constructors
* --------------------------------------------------------------------------
*/

public ServerScope() {
super( ServerScope.name );
}

}
Loading

0 comments on commit d6df40f

Please sign in to comment.