8187965: dynalink samples under $jdk10/src/sample/nashorn/dynalink are broken

Reviewed-by: jlaskey, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2017-09-26 20:29:13 +05:30
parent c825188cd8
commit 06e15f6982
8 changed files with 118 additions and 20 deletions

View File

@ -0,0 +1,49 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// Usage: jjs --language=es6 base64.js
const Base64 = Java.type("java.util.Base64");
const ByteArray = Java.type("byte[]");
const JString = Java.type("java.lang.String");
function toBase64(s) {
const ba = s instanceof ByteArray? s : String(s).bytes;
return Base64.encoder.encodeToString(ba);
}
function fromBase64(s) {
const ba = s instanceof ByteArray? s : String(s).bytes;
return new JString(Base64.decoder.decode(ba));
}
print(toBase64`hello world`);
print(fromBase64(toBase64`hello world`));

View File

@ -4,12 +4,6 @@ classpath of the jjs tool. Linker samples are named with the naming pattern
"xyz_linker.js". These scripts build dynalink linker jar from java code and exec
another jjs process with appropriate classpath set.
Note: you need to build jdk9 forest and put "images/jdk/bin" in your PATH to use
these scripts. This is because these scripts use javac to build dynalink jar and
exec another jjs with classpath set! Alternatively, you can also manually build
dynalink linker jars and invoke sample scripts by putting linker jar in jjs tool's
classpath as well.
Dynalink samples:
* array_stream_linker.js

View File

@ -1,7 +1,7 @@
#! array stream linker example
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -37,10 +37,12 @@
$EXEC.throwOnError=true
// compile ArrayStreamLinkerExporter
`javac -cp ../../dist/nashorn.jar ArrayStreamLinkerExporter.java`
`javac ArrayStreamLinkerExporter.java`
load("jarutil.js");
// make a jar file out of pluggable linker
`jar cvf array_stream_linker.jar ArrayStreamLinkerExporter*.class META-INF/`
makeJar("array_stream_linker.jar");
// run a sample script that uses pluggable linker
// but make sure classpath points to the pluggable linker jar!

View File

@ -1,7 +1,7 @@
# buffer indexing linker example
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -37,10 +37,12 @@
$EXEC.throwOnError=true
// compile BufferIndexingLinkerExporter
`javac -cp ../../dist/nashorn.jar BufferIndexingLinkerExporter.java`
`javac BufferIndexingLinkerExporter.java`
load("jarutil.js");
// make a jar file out of pluggable linker
`jar cvf buffer_indexing_linker.jar BufferIndexingLinkerExporter*.class META-INF/`
makeJar("buffer_indexing_linker.jar");
// run a sample script that uses pluggable linker
// but make sure classpath points to the pluggable linker jar!

View File

@ -37,10 +37,12 @@
$EXEC.throwOnError=true
// compile DOMLinkerExporter
`javac -cp ../../dist/nashorn.jar DOMLinkerExporter.java`
`javac DOMLinkerExporter.java`
load("jarutil.js");
// make a jar file out of pluggable linker
`jar cvf dom_linker.jar DOMLinkerExporter*.class META-INF/`
makeJar("dom_linker.jar");
// run a sample script that uses pluggable linker
// but make sure classpath points to the pluggable linker jar!

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
function classFiles() {
var arr = new java.io.File(".").listFiles(
new java.io.FilenameFilter() {
accept: function(dir, str) str.endsWith(".class")
});
var str = "";
for (var i in arr) str += " " + arr[i];
return str;
}
function makeJar(name) {
$EXEC("jar cvf " + name + " META-INF/ " + classFiles());
print($ERR);
}

View File

@ -1,7 +1,7 @@
#! missing method linker example
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -37,10 +37,12 @@
$EXEC.throwOnError=true
// compile MissingMethodLinkerExporter
`javac -cp ../../dist/nashorn.jar MissingMethodLinkerExporter.java MissingMethodHandler.java MissingMethodExample.java`
`javac MissingMethodLinkerExporter.java MissingMethodHandler.java MissingMethodExample.java`
load("jarutil.js");
// make a jar file out of pluggable linker
`jar cvf missing_method_linker.jar MissingMethod*.class META-INF/`
makeJar("missing_method_linker.jar");
// run a sample script that uses pluggable linker
// but make sure classpath points to the pluggable linker jar!

View File

@ -1,7 +1,7 @@
# underscore name translator dynalink linker example
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@ -37,10 +37,12 @@
$EXEC.throwOnError=true
// compile UnderscoreNameLinkerExporter
`javac -cp ../../dist/nashorn.jar UnderscoreNameLinkerExporter.java`
`javac UnderscoreNameLinkerExporter.java`
load('jarutil.js');
// make a jar file out of pluggable linker
`jar cvf underscore_linker.jar UnderscoreNameLinkerExporter*.class META-INF/`
makeJar("underscore_linker.jar");
// run a sample script that uses pluggable linker
// but make sure classpath points to the pluggable linker jar!