Skip to content

Commit

Permalink
#126 Support/Document Transpilation via API
Browse files Browse the repository at this point in the history
Adding CLI documentation
  • Loading branch information
mirkosertic committed Jun 13, 2019
1 parent f87c1f6 commit 5109d3c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
13 changes: 10 additions & 3 deletions cli/src/main/java/de/mirkosertic/bytecoder/cli/BytecoderCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;

public class BytecoderCLI {

Expand All @@ -42,6 +44,9 @@ public class BytecoderCLI {
@Command(name = "Bytecoder")
public static class CLIOptions {

@Option(names = "-classpath", required = true, description = "Die Directory containing the JVM class files to be compiled.")
protected String classpath;

@Option(names = "-mainclass", required = true, description = "Name of the class that contains the main() method")
protected String mainClass;

Expand Down Expand Up @@ -73,7 +78,7 @@ public static class CLIOptions {
protected boolean minifyCompileResult = true;
}

public static void main(String[] args) throws IOException, ClassNotFoundException {
public static void main(final String[] args) throws IOException, ClassNotFoundException {

final CLIOptions theCLIOptions = new CLIOptions();
try {
Expand All @@ -93,9 +98,11 @@ public static void main(String[] args) throws IOException, ClassNotFoundExceptio
theBytecoderDirectory.mkdirs();

final ClassLoader theLoader = BytecoderCLI.class.getClassLoader();
final Class theTargetClass = theLoader.loadClass(theCLIOptions.mainClass);
final URLClassLoader classLoader = new URLClassLoader(new URL[] {new File(theCLIOptions.classpath).toURI().toURL()}, theLoader);

final Class theTargetClass = classLoader.loadClass(theCLIOptions.mainClass);

final CompileTarget theCompileTarget = new CompileTarget(theLoader, CompileTarget.BackendType.valueOf(theCLIOptions.backend));
final CompileTarget theCompileTarget = new CompileTarget(classLoader, CompileTarget.BackendType.valueOf(theCLIOptions.backend));

final BytecodeMethodSignature theSignature = new BytecodeMethodSignature(BytecodePrimitiveTypeRef.VOID,
new BytecodeTypeRef[] { new BytecodeArrayTypeRef(BytecodeObjectTypeRef.fromRuntimeClass(String.class), 1) });
Expand Down
67 changes: 64 additions & 3 deletions manual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,68 @@ The *WebAssembly* backend transforms the intermediate representation into WebAss
The *OpenCL* backend is used to compile single algorithms into OpenCL and execute them on the GPU. This backend is designed to enhance
existing programs running on the JVM to utilize the vast power of modern GPUs.

## Hello World!
## Jump-Start using the Command-Line Interface

Lets assume we have the following Java class, and we want to compile it to JavaScript and run it in the Browser:

```
package bytecodertest;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
```

First of all, we need to compile the Java sources to a JVM class file. This is done by using the `javac` tool from the JDK:

```
javac HelloWorld.java
```

Now, we have the compiled .class files. Now we can use the Bytecoder CLI to compile it to JavaScript!

Step 1: Download the CLI from Maven central:

```
wget http://central.maven.org/maven2/de/mirkosertic/bytecoder/bytecoder-cli/2019-06-13/bytecoder-cli-2019-06-13-executable.jar
```

Step 2: Invoke the CLI:

```
java -jar bytecoder-cli-2019-06-13-executable.jar -classpath=. -mainclass=bytecodertest.HelloWorld -builddirectory=. -backend=js -minify=false
```

Step 3: Create an embedding HTML document

Now we have a `bytecoder.js` file, which needs to be embedded into a HTML document. Here is a sample
HTML file embedding the Bytecoder JavaScript and invoking it:

```
<html>
<head>
<title>Bytecoder Test</title>
</head>
<body>
<script type="text/javascript" src="bytecoder.js"></script>
<script type="text/javascript">
console.log("Init");
bytecoder.bootstrap();
bytecoder.exports.main();
console.log("Done");
</script>
</body>
</html>
```

After opening this HTML file in a Browser you will see the "Hello World!" output in the JavaScript console log.

## Hello World, extended edition!

Let's see how a simple Java program can be written that changes the document title in the Browser. Here it is:

Expand Down Expand Up @@ -271,7 +332,7 @@ __kernel void BytecoderKernel(__global const float* val$theA,
<plugin>
<groupId>de.mirkosertic.bytecoder</groupId>
<artifactId>bytecoder-mavenplugin</artifactId>
<version>2019-03-22</version>
<version>2019-06-13</version>
<configuration>
<mainClass>de.mirkosertic.bytecoder.integrationtest.SimpleMainClass</mainClass>
<backend>js</backend>
Expand Down Expand Up @@ -303,7 +364,7 @@ JavaScript will be placed in the Maven `target/bytecoder` directory.
<plugin>
<groupId>de.mirkosertic.bytecoder</groupId>
<artifactId>bytecoder-mavenplugin</artifactId>
<version>2019-03-22</version>
<version>2019-06-13</version>
<configuration>
<mainClass>de.mirkosertic.bytecoder.integrationtest.SimpleMainClass</mainClass>
<backend>wasm</backend>
Expand Down

0 comments on commit 5109d3c

Please sign in to comment.