Nashorn has 100% support for ECMAScript 5.1, but not full support for ECMAScript 2015 and after version. It will be a painful experience if you want to use some third-party library that requires CMAScript 2015 in nashorn.This extension pack may be able to ease your pain.
Feature | ECMAScript | Explication |
---|---|---|
Math | ||
log10 | 2015 | The Math.log10() static method returns the base 10 logarithm of a number. |
log2 | 2015 | The Math.log2() static method returns the base 2 logarithm of a number. |
log1p | 2015 | The Math.log1p() static method returns the natural logarithm (base e) of 1 + x, where x is the argument. |
expm1 | 2015 | Math.expm1The Math.expm1() static method returns e raised to the power of a number, subtracted by 1. |
sinh | 2015 | The Math.sinh() static method returns the hyperbolic sine of a number. |
cosh | 2015 | The Math.cosh() static method returns the hyperbolic cosine of a number. |
tanh | 2015 | The Math.tanh() static method returns the hyperbolic tangent of a number. |
asinh | 2015 | The Math.asinh() static method returns the inverse hyperbolic sine of a number. |
acosh | 2015 | The Math.acosh() static method returns the inverse hyperbolic cosine of a number. |
atanh | 2015 | The Math.atanh() static method returns the inverse hyperbolic tangent of a number. |
hypot | 2015 | The Math.hypot() static method returns the square root of the sum of squares of its arguments. |
trunc | 2015 | The Math.trunc() static method returns the integer part of a number by removing any fractional digits. |
sign | 2015 | The Math.sign() static method returns 1 or -1, indicating the sign of the number passed as argument. |
cbrt | 2015 | The Math.cbrt() static method returns the cube root of a number. |
imul | 2015 | The Math.imul() static method returns the result of the C-like 32-bit multiplication of the two parameters. |
fround | 2015 | The Math.fround() static method returns the nearest 32-bit single precision float representation of a number. |
clz32 | 2015 | The Math.clz32() static method returns the number of leading zero bits in the 32-bit binary representation of a number. |
Number | ||
EPSILON | 2015 | The Number.EPSILON static data property represents the difference between 1 and the smallest floating point number greater than 1. |
isFinite | 2015 | The Number.isFinite() static method determines whether the passed value is a finite number. |
isInteger | 2015 | The Number.isInteger() static method determines whether the passed value is an integer. |
isNaN | 2015 | The Number.isNaN() static method determines whether the passed value is the number value NaN, and returns false if the input is not of the Number type. |
parseFloat | 2015 | The Number.parseFloat() static method parses an argument and returns a floating point number. |
parseInt | 2015 | The Number.parseInt() static method parses a string argument and returns an integer of the specified radix or base. |
String | ||
codePointAt | 2015 | The codePointAt() method of String values returns a non-negative integer that is the Unicode code point value of the character starting at the given index. |
fromCodePoint | 2015 | The String.fromCodePoint() static method returns a string created from the specified sequence of code points. |
includes | 2015 | The includes() method of String values performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate. |
repeat | 2015 | The repeat() method of String values constructs and returns a new string which contains the specified number of copies of this string, concatenated together. |
trimStart | 2019 | The trimStart() method of String values removes whitespace from the beginning of this string and returns a new string, without modifying the original string. |
trimEnd | 2019 | The trimEnd() method of String values removes whitespace from the end of this string and returns a new string, without modifying the original string. trimRight() is an alias of this method. |
padStart | 2017 | The padStart() method of String values pads this string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of this string. |
padEnd | 2017 | The padEnd() method of String values pads this string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of this string. |
Array | ||
of | 2015 | The Array.of() static method creates a new Array instance from a variable number of arguments, regardless of number or type of the arguments. |
copyWithin | 2015 | The copyWithin() method of Array instances shallow copies part of this array to another location in the same array and returns this array without modifying its length. |
includes | 2016 | The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate. |
flatMap | 2019 | The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. |
from | 2015 | The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object. nashorn15.4 is required. |
flat | 2015 | The flat() method of Array instances creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. nashorn15.4 is required. |
Object | ||
assign | 2015 | The Object.assign() static method copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object. |
is | 2015 | The Object.is() static method determines whether two values are the same value. |
values | 2017 | The Object.values() static method returns an array of a given object's own enumerable string-keyed property values. |
getOwnPropertyDescriptors | 2017 | The Object.getOwnPropertyDescriptors() static method returns all own property descriptors of a given object. |
entries | 2017 | The Object.entries() static method returns an array of a given object's own enumerable string-keyed property key-value pairs. nashorn15.4 is required. |
This module mimics the browser's timeout functionality.I added it to make Promise work. Idears from https://github.com/nikku/nashorn-async | ||
[global] | ||
setTimeout(fn, millis) | The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires. | |
clearTimeout(task) | The global clearTimeout() method cancels a timeout previously established by calling setTimeout(). | |
setInterval(fn, millis) | The setInterval() method, offered on the Window and Worker interfaces, repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. | |
clearInterval(task) | The global clearInterval() method cancels a timed, repeating action which was previously established by a call to setInterval(). | |
This module provides a simple Promise functionality implementation. Referenced from https://www.promisejs.org | ||
Promise | ||
new Promise(fn) | The Promise() constructor creates Promise objects. It is primarily used to wrap callback-based APIs that do not already support promises. | |
Promise.resolve(value) | The Promise.resolve() static method "resolves" a given value to a Promise. If the value is a promise, that promise is returned; if the value is a thenable, Promise.resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value. | |
Promise.reject(value) | The Promise.reject() static method returns a Promise object that is rejected with a given reason. | |
then(onFulfilled, onRejected) | The then() method of Promise instances takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise. It immediately returns an equivalent Promise object, allowing you to chain calls to other promise methods. | |
Promise.all(arr) | The Promise.all() static method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values. It rejects when any of the input's promises rejects, with this first rejection reason. | |
catch(onRejected) | The catch() method of Promise instances schedules a function to be called when the promise is rejected. It immediately returns an equivalent Promise object, allowing you to chain calls to other promise methods. It is a shortcut for Promise.prototype.then(undefined, onRejected). | |
Promise.race(values) | The Promise.race() static method takes an iterable of promises as input and returns a single Promise. This returned promise settles with the eventual state of the first promise that settles. | |
catch(onRejected) | catch metod is equival to calling Promise.prototype.then(undefined, onRejected) | |
async/await These functions are similar, but not identical, to the original in ECMAScript 2015. | ||
async(fnc) | The async() method changes fnc to a Promise instanse and returns it.If fnc has been a Promise instanse,it will be returned without changing. | |
await(prms) | The await() method calls prms.then() method and waits till fulfill is executed, then returns the value of fulfill. | |
And I added a simple window mock implementation to cheat pdfmake environment checking.
context.console={log:function(v){
if (typeof v =="object"){
java.lang.System.out.println(JSON.stringify(v));
}else{
java.lang.System.out.println(v);
}
}
}
context.navigator={
//to cheat the checker of pdfmake
userAgent:"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
};
context.self=context;
context.window=context; |
The sample about async/await. It is amizing for nashorn, isn't it?
load("nashorn-ext-for-es6.min.js");
var f = async(function(){
java.lang.System.out.println(new Date()+" The async started.");
return new Promise(function(resolve, reject){
setTimeout(function(param){resolve("hello world,"+param);},1000,"hello tokyo");
});
});
var ret=await(f);
java.lang.System.out.println(new Date()+" The await return value is : "+ret);
The sample about Promise.all. It is added for running pdfmake. And you can use console.log() for output.
load("nashorn-ext-for-es6.min.js");
const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 100, 'foo');
});
var a=await(Promise.all([promise1, promise2, promise3]));
console.log(a);
The sample to creat pdf using pdf-lib. Besure your nashorn is 15.4 or later. To run the sample in JDK8 will be error. You can download pdf-lib.min.js from https://pdf-lib.js.org/ or https://unpkg.com/[email protected]/dist/pdf-lib.min.js.
load("nashorn-ext-for-es6.min.js");
load("pdf-lib.min.js");
var pdfDoc=await(PDFLib.PDFDocument.create());
var page = pdfDoc.addPage([350, 400]);
page.moveTo(110, 200);
page.drawText("Hello World!");
var pdfDataUri=await(pdfDoc.saveAsBase64({ dataUri: true }));
java.lang.System.out.println(pdfDataUri);
Try to send the pdfDataUri to the src attribute of iframe, you will see your pdf.
The sample to creat pdf using pdfmake. Besure your nashorn is 15.4 or later. To run the sample in JDK8 will be error. You can download pdfmake.min.js from http://pdfmake.org/ or https://cdnjs.com/libraries/pdfmake .
load("nashorn-ext-for-es6.min.js");
load("pdfmake.min.js");
load("vfs_fonts.min.js");
var docDefinition = {
content: [
"This is a standard paragraph, using default style",
{ text: "This paragraph will have a bigger font", fontSize: 15 },
{
text: [
"This paragraph is defined as an array of elements to make it possible to ",
{ text: "restyle part of it and make it bigger ", fontSize: 15 },
"than the rest.",
],
},
],
};
var pdfDataUri;
var doc=pdfMake.createPdf(docDefinition);
pdfDataUri=await(
new Promise(function(resolve,reject){
doc.getDataUrl(function(data){
resolve(data);
});
})
);
console.log(pdfDataUri);
Try to send the pdfDataUri to the src attribute of iframe, you will see your pdf.