Nashorn and AsciidoctorJs

Jim Laskey (Oracle) james.laskey at
Tue Jul 29 13:08:23 UTC 2014

[Just getting back]
Granted I tweaked the main to simplify the testing outside an IDE, but when I ran it it seems to work up to a point (asciidoctor.js:12234 timings.$start is undefined.):

>> java -cp ../java nashorn.RenderDocumentWithNashorn
Working Directory = /Projects/asciidoctorjs/src/test/resources
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 1.5.0-preview.8">
<link rel="stylesheet" href="./asciidoctor.css">
<body class="article">
<div id="header">
<div id="content">
<div class="paragraph">
<p><strong>Hello World</strong></p>
<div id="footer">
<div id="footer-text">
Last updated 2014-07-29 09:56:41 ADT
*Hello World*
[object Object]
nashorn.AsciidoctorJs$$NashornJavaAdapter at 4a70d302
Exception in thread "main" <eval>:12235 TypeError: Cannot call undefined
	at jdk.nashorn.internal.runtime.ECMAErrors.error(
	at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
	at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
	at jdk.nashorn.internal.runtime.ECMAErrors.typeError(
	at jdk.nashorn.internal.runtime.Undefined.lookupTypeError(
	at jdk.nashorn.internal.runtime.Undefined.lookup(
	at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(
	at jdk.internal.dynalink.DynamicLinker.relink(
	at jdk.nashorn.internal.scripts.Script$\^eval\_$32._L11888$_L11906$_L12223$_L12226(<eval>:12235)
	at jdk.nashorn.internal.scripts.Script$\^eval\_$34._L11888$_L11906$_L12223$_L12310(<eval>:12336)
	at jdk.nashorn.internal.scripts.Script$\^eval\_._L1(<eval>:2)
	at nashorn.AsciidoctorJs$$NashornJavaAdapter.render(Unknown Source)
	at nashorn.RenderDocumentWithNashorn.main(

package nashorn;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import javax.script.*;

public class RenderDocumentWithNashorn {
    static void evalFile(String filename, ScriptEngine engine, SimpleScriptContext context) throws ScriptException, FileNotFoundException {
        engine.eval(new, context);

    public static void main(String[] args) throws ScriptException, NoSuchMethodException, FileNotFoundException {
        System.out.println("Working Directory = " + System.getProperty("user.dir"));

        ScriptEngineManager engineManager =
                new ScriptEngineManager();
        ScriptEngine engine =

        SimpleScriptContext simpleScriptContext = new SimpleScriptContext();
        Bindings bindings = new SimpleBindings();

        simpleScriptContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

        try {
            evalFile("opal.js", engine, simpleScriptContext);
            evalFile("asciidoctor.js", engine, simpleScriptContext);
            evalFile("asciidoctor_extensions.js", engine, simpleScriptContext);
            evalFile("asciidoctorjava.js", engine, simpleScriptContext);
        } catch (ScriptException e) {
            throw new IllegalArgumentException(e);

        String content = "*Hello World*";
        Map<String, Object> options = new HashMap<>();
        options.put("header_footer", true);

        simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("listOptions", options.keySet().toArray());
        simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("options", options);
        simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("content", content);

        Invocable invocable = (Invocable) engine;

        Object eval = engine.eval("Opal.hash2(listOptions, options)", simpleScriptContext);

        simpleScriptContext.getBindings(ScriptContext.ENGINE_SCOPE).put("hash2", eval);


        System.out.println(engine.eval("Opal.Asciidoctor.$render(content, hash2);", simpleScriptContext));

        AsciidoctorJs asciidoctorJs =
                asciidoctorJs.render(content, eval));



On Jul 26, 2014, at 9:02 AM, Alex Soto <asotobu at> wrote:

> Hi guys,
> currently I am working on integrating Asciidoctor to Java using Nashorn and
> Opal. Nowadays we have got an asciidoctor java running above JRuby.
> Apart from Asciidoctor java we also have an asciidoctorjs which is created
> using Opal (a gem to convert Ruby programs to JavaScript).
> So next step is to integrate this js to java using nashorn. But we are
> having one problem.
> I have created a project with a test so you can take a look as well:
> The test is located at
> (although it is called Rhino currently we are using Nashorn).
> If I create an script engine an I use the eval method everywhere it works
> perfectly but because I would like to use some more typesafe approach I
> decided to create an interface mirroring some asciidoctor operations so I
> call invocable.getInterface(AsciidoctorJs.class) to create the proxied
> instance and then I call defined methods.
> This interface mimics the js asciidoctorjava.js which internally calls
> asciidoctor.js operations. This is done because I only want some operations
> available from Java part, not the whole operations that asciidoctor.js
> offers.
> asciidoctorjava.js looks like:
> var render = function(content, optionsHash2) {
>    return Opal.Asciidoctor.$render(content, optionsHash2);
> };
> The problem is that when I run the test an exception is thrown notifying
> that Opal.Asciidoctor.$render is not found. If I change that line to:
> var render = function(content, optionsHash2) {
>    return "Hello World";
> };
> then it works so it seems that Nashorn don't load the asciidoctor.js but
> this is not exactly true because if I execute the same operation but using
> eval method it works.
> Probably I am missing something in the creation of proxied interface, but
> currently I cannot see what.
> Any idea on why this happens?
> Thank you so much for your help in advance.
> Alex.
> -- 
> +----------------------------------------------------------+
>  Alex Soto Bueno - Computer Engineer
> +----------------------------------------------------------+

More information about the nashorn-dev mailing list