Skip to content

Commit

Permalink
issue #8 - rhino support refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Aug 18, 2018
1 parent d5c5a31 commit 63bfebc
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -29,7 +30,7 @@ public enum Compatibility {
public String javaType(String fqn) {
switch (this.ordinal()) {
case 1:
return format("eval(\"%s\")", fqn );
return format("Packages.%s", fqn );
default:
return format("Java.type(\"%s\")", fqn );
}
Expand All @@ -43,6 +44,14 @@ public TypescriptConverter(Compatibility compatibility) {
this.compatibility = compatibility;
}

/**
*
* @return
*/
public final boolean isRhino() {
return compatibility == Compatibility.RHINO;
}

/**
*
* @param declaredClass
Expand Down Expand Up @@ -400,25 +409,40 @@ public String processClass(int level, TSType tstype, java.util.Map<String, TSTyp

final Context ctx = contextOf(tstype, declaredTypeMap, compatibility);

final java.util.Set<Method> methods = tstype.getMethods();

if (tstype.supportNamespace())
ctx.append("declare namespace ").append(tstype.getNamespace()).append(" {\n\n");

ctx.getClassDecl().append("\n\n");

if (tstype.isFunctional()) {

tstype.getMethods().stream().filter(m -> Modifier.isAbstract(m.getModifiers())).findFirst().ifPresent(
m -> ctx.append('\t').append(getMethodParametersAndReturnDecl(ctx, m, false)).append(ENDL));

tstype.getMethods().stream().filter(m -> !Modifier.isAbstract(m.getModifiers()))
.map(m -> getMethodDecl(ctx, m, true /* optional */)).sorted()
.forEach(decl -> ctx.append('\t').append(decl).append(ENDL));
final Function<Method,String> genAbstractMethod =
m -> isRhino() ?
getMethodDecl(ctx, m, false /* non optional */) :
getMethodParametersAndReturnDecl(ctx, m, false);

methods.stream()
.filter(m -> Modifier.isAbstract(m.getModifiers()))
.findFirst()
.ifPresent(
m -> ctx.append('\t')
.append( genAbstractMethod.apply(m) )
.append(ENDL));

methods.stream()
.filter(m -> !Modifier.isAbstract(m.getModifiers()))
.map(m -> getMethodDecl(ctx, m, true /* optional */))
.sorted()
.forEach(decl -> ctx.append('\t')
.append(decl)
.append(ENDL));

} else {

ctx.processEnumDecl();

final java.util.Set<Method> methodSet = tstype.getMethods().stream()
final java.util.Set<Method> methodSet = methods.stream()
.filter(md -> (tstype.isExport() && isStatic(md)) == false).filter((md) -> {
final String name = md.getName();
return !(name.contains("$") || // remove unnamed
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/resources/headerD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ declare namespace java.io {
interface Serializable {}
}

//
// Rhino
//

declare const Packages:any;

//
// Nashorn
Expand Down
2 changes: 0 additions & 2 deletions core/src/main/resources/headerT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@
* TYPESCRIPT EXPORTED DECLARATIONS
*
*/


2 changes: 1 addition & 1 deletion sample/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require.paths = [
"target/ts/target"
];

java.lang.System.setProperty( "jvm-npm.debug", "true");
java.lang.System.setProperty( "jvm-npm.debug", "false");


// PLOYFILL
Expand Down
24 changes: 24 additions & 0 deletions sample/app.rhino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// STARTUP
//

load('classpath:jvm-npm.js');

require.paths = [
"src/main/ts",
"target/ts/src/main/ts",
"target/ts/target"
];

java.lang.System.setProperty( "jvm-npm.debug", "false");


// PLOYFILL
//The $ENV variable provides the shell environment variables.
//The $ARG variable is an array of the program command-line arguments.
print( "args", $ARG.length );

var process = { argv:$ARG, env:{TERM:'color'} } ;

var exports = {};
load('target/ts/src/main/ts/main.rhino.js');
4 changes: 2 additions & 2 deletions sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<executions>
<execution>
<id>tsc</id>
<phase>process-sources</phase>
<!--phase>process-sources</phase-->
<goals>
<goal>exec</goal>
</goals>
Expand Down Expand Up @@ -220,7 +220,7 @@
<classpath />
<!--argument>org.mozilla.javascript.tools.shell.Main</argument-->
<argument>org.bsc.java2ts.JSRun$Rhino</argument>
<argument>app.js</argument>
<argument>app.rhino.js</argument>

</arguments>
</configuration>
Expand Down
59 changes: 59 additions & 0 deletions sample/src/main/ts/future.test.rhino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
CompletableFuture,
Executor,
Optional,
RuntimeException
} from "ts/jdk8-types";

function doSometing() {
return CompletableFuture.completedFuture("DONE!");
}

function error(msg:string) {
throw msg;
}

export function test() {
print(`
========================================
CompletableFuture TEST
========================================
`);

const currentThreadExecutor = new Executor( { execute: runnable => runnable.run() } );

let future = CompletableFuture.supplyAsync( { get: () => {

//throw "ERROR0"
return "complete";

}}, currentThreadExecutor );


future
//.thenApply( result => {throw "ERROR1"} )
.thenAccept( { accept: result => print(result) } )
.exceptionally( { apply: (err) => print( "ERROR:", err ) } )
;

let future1 = CompletableFuture.supplyAsync( { get: () => {

Optional.ofNullable( null ).orElseThrow( { get: () => new RuntimeException("RTE") } )

return "complete1";

}}, currentThreadExecutor);


future1
.thenApply( { apply: result => error("ERROR1") } )
.exceptionally( { apply: err => print( "ERROR 1:", err ) } )
.thenAccept( { accept: result => error("ERROR2") } )
.exceptionally( { apply: (err) => print( "ERROR 2:", err ) } )
;

let future2 = doSometing().thenAccept( { accept: result => print(result) } );

}
37 changes: 37 additions & 0 deletions sample/src/main/ts/main.rhino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

import {test as future_test} from "./future.test.rhino";

import {
URI,
Optional,
MemoryType,
} from "ts/jdk8-types";


const u1 = "http://localhost:8000/site/";
const u2 = "/spaces/flyingpdf/pdfpageexport.action?pageId=100532618";
const u3 = "http://localhost:8000/spaces/flyingpdf/pdfpageexport.action?pageId=100532618";

let uri = URI.create( u1 );

print( uri.resolve( u2 ).toString() );
print( URI.create( u1 + u2 ).normalize().toString() );
print( uri.resolve( u3 ).toString() );

print( Optional.empty().map( { apply:e => "element: " + e } ).orElse("nil") );
print( Optional.of("HELLO").map( { apply:e => "element: " + e } ).orElse("nil") );


// TEST ENUM

print(MemoryType.HEAP);
print(MemoryType.HEAP.name());
print(MemoryType.HEAP.ordinal());

/*
color_test();
mustache_test();
validator_test();
stream_test();
*/
future_test();

0 comments on commit 63bfebc

Please sign in to comment.