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 15, 2023
2 parents fc88244 + e9f5a29 commit 809448a
Show file tree
Hide file tree
Showing 24 changed files with 710 additions and 56 deletions.
17 changes: 9 additions & 8 deletions compiler/src/main/java/ourtus/boxlang/ast/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Node {

/**
* AST node constructor
*
*
* @param position the position within the source code that originated the node
* @param sourceText the original source code that represented by the node
*/
Expand All @@ -42,9 +42,9 @@ public Node( Position position, String sourceText ) {

/**
* Returns the position in code that the node represents
*
*
* @return a Position instance
*
*
* @see Position
*/
public Position getPosition() {
Expand All @@ -53,7 +53,7 @@ public Position getPosition() {

/**
* Returns the source code that originated the Node
*
*
* @return the snipped of the source code
*/
public String getSourceText() {
Expand All @@ -62,7 +62,7 @@ public String getSourceText() {

/**
* Set the parent and the children of the Node
*
*
* @param parent an instance of the parent code
*/
public void setParent( Node parent ) {
Expand All @@ -75,7 +75,7 @@ public void setParent( Node parent ) {

/**
* Returns the parent Node of node or null if has no parent
*
*
* @return the parent Node of the current Node
*/
public Node getParent() {
Expand All @@ -84,7 +84,7 @@ public Node getParent() {

/**
* Returns the list ov children of the current node
*
*
* @return a list of children Node
*/
public List<Node> getChildren() {
Expand All @@ -97,7 +97,7 @@ public Node getOriginator() {

/**
* Walk the tree
*
*
* @return a list of nodes traversed
*/
public List<Node> walk() {
Expand All @@ -108,4 +108,5 @@ public List<Node> walk() {
}
return result;
}

}
38 changes: 30 additions & 8 deletions compiler/src/main/java/ourtus/boxlang/ast/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Position {

/**
* Creates a position
*
*
* @param start the start position in the source code
* @param end the end position in the source code
*/
Expand All @@ -40,7 +40,7 @@ public Position( Point start, Point end ) {

/**
* Creates a position including the file information
*
*
* @param start the start position in the source code
* @param end the end position in the source code
* @param source the source file reference
Expand All @@ -53,7 +53,7 @@ public Position( Point start, Point end, Source source ) {

/**
* Returns the start point
*
*
* @return the start point of the region
*/
public Point getStart() {
Expand All @@ -62,7 +62,7 @@ public Point getStart() {

/**
* Returns the end point
*
*
* @return the end point of the region
*/
public Point getEnd() {
Expand All @@ -71,9 +71,9 @@ public Point getEnd() {

/**
* Returns the source of the position
*
*
* @return the start point of the region
*
*
* @see Source
*/
public Source getSource() {
Expand All @@ -82,12 +82,34 @@ public Source getSource() {

/**
* Set the source of the position
*
*
* @param source The source of the position (i.e. file)
*
*
* @see Source
*/
public void setSource( Source source ) {
this.source = source;
}

/**
* String representation of the Position
* @return a String representation of the position including the source file if available
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
if ( this.getSource() != null ) {
sb.append( this.getSource() );
sb.append( ": " );
}
sb.append( this.getStart().getLine() )
.append( "," )
.append( this.getStart().getColumn() );
sb.append( " - " );
sb.append( this.getEnd().getLine() )
.append( "," )
.append( this.getEnd().getColumn() );

return sb.toString();
}
}
11 changes: 10 additions & 1 deletion compiler/src/main/java/ourtus/boxlang/ast/SourceFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ public SourceFile( File file ) {

/**
* Returns the File associate to the source
*
*
* @return a File instance
*/
public File getFile() {
return file;
}

/**
* String representation of a file source
* @return the absolute path of the File
*/
@Override
public String toString() {
return file.getAbsolutePath();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* [BoxLang]
* <p>
* Copyright [2023] [Ortus Solutions, Corp]
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 ourtus.boxlang.executor;

import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.net.URI;

/**
* JavaFileObject implementation
*/
public class JavaClassByteCode extends SimpleJavaFileObject {

protected ByteArrayOutputStream bos = new ByteArrayOutputStream();

public JavaClassByteCode( String name, Kind kind ) {
super( URI.create( "string:///" + name.replace( '.', '/' )
+ kind.extension ), kind );
}

public byte[] getBytes() {
return bos.toByteArray();
}

@Override
public OutputStream openOutputStream() {
return bos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* [BoxLang]
* <p>
* Copyright [2023] [Ortus Solutions, Corp]
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 ourtus.boxlang.executor;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.Map;

/**
* Dynamic in Memory classloader
*/
public class JavaDynamicClassLoader extends URLClassLoader {

private JavaMemoryManager manager;

public JavaDynamicClassLoader( URL[] urls, ClassLoader parent, JavaMemoryManager manager ) {
super( urls, parent );
this.manager = manager;

}

@Override
protected Class<?> findClass( String name ) throws ClassNotFoundException {

Map<String, JavaClassByteCode> compiledClasses = manager
.getBytesMap();

if ( compiledClasses.containsKey( name ) ) {
byte[] bytes = compiledClasses.get( name )
.getBytes();
return defineClass( name, bytes, 0, bytes.length );
} else {
return super.findClass( name );
}
}

/*
* Required for Java Agents when this classloader is used as the system classloader
*/
@SuppressWarnings( "unused" )
private void appendToClassPathForInstrumentation( String jarfile ) throws IOException {
addURL( Paths.get( jarfile ).toRealPath().toUri().toURL() );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* [BoxLang]
* <p>
* Copyright [2023] [Ortus Solutions, Corp]
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 ourtus.boxlang.executor;

import javax.tools.*;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Hashtable;
import java.util.Map;


/**
* In Memory File Manager
*/
public class JavaMemoryManager extends ForwardingJavaFileManager<JavaFileManager> {

private final Map<String, JavaClassByteCode> compiledClasses;

public JavaMemoryManager( StandardJavaFileManager standardFileManager ) {
super( standardFileManager );
this.compiledClasses = new Hashtable<>();
}

@Override
public JavaFileObject getJavaFileForOutput( Location location, String className, JavaFileObject.Kind kind,
FileObject sibling ) {

JavaClassByteCode classAsBytes = new JavaClassByteCode(
className, kind );
compiledClasses.put( className, classAsBytes );

return classAsBytes;
}

public Map<String, JavaClassByteCode> getBytesMap() {
return compiledClasses;
}
}
Loading

0 comments on commit 809448a

Please sign in to comment.