Skip to content

Instantly share code, notes, and snippets.

@lordofthejars
Created June 20, 2014 09:53
Show Gist options
  • Select an option

  • Save lordofthejars/c2f2cb1b0c5b5515bb47 to your computer and use it in GitHub Desktop.

Select an option

Save lordofthejars/c2f2cb1b0c5b5515bb47 to your computer and use it in GitHub Desktop.
Hi Viktor,
thank you so much and sorry for disturbing you. I am developing the integration between asciidoctor.js and asciidoctorJ, so users can choose between using JRuby or Javascript.
Look basically I need to be able to create an Opal hash object and fill with options. My first approach works but I don't like so much:
```java
ScriptObjectMirror hash = (ScriptObjectMirror) engine.eval("Opal.hash2(['header_footer', 'attributes'], { 'header_footer': true, 'attributes': ['icons=font'] }); ", asciidoctorScriptCompilerCtx);
asciidoctorScriptCompilerCtx.getBindings(ScriptContext.ENGINE_SCOPE).put("options", hash);
```
But the problem is that I don't want to be concatenating strings depending on options, I would like to do something that JRuby which I can create a Proxy class and all calls from this class are translated to Ruby world.
So I suppose I should do something like:
```java
hash.to(MyHash.class);
```
But the problem is that I am not an expert on Javascript and I can't see methods on Javascript hash2 class. Do you know how I would be able to add keys and values from Java world to Javascript directly, so instead of having to compose an string and calling Opal.hash2 with that string I could so something like:
```java
ScriptObjectMirror hash = (ScriptObjectMirror) engine.eval("Opal.hash2();", asciidoctorScriptCompilerCtx);
Hash2 hash = hash.to(Hash2.class);
hash.put("header_footer", true);
```
Maybe because of nature of Opal this is impossible but I am sure you know a lot of more Javascript and internals of Nashorn than me you can teach me an alternative.
The opal class is https://github.com/asciidoctor/asciidoctor.js/blob/master/dist/opal.js and the hash2 definition is:
```javascript
Opal.hash2 = function(keys, map) {
var hash = new Opal.Hash._alloc;
hash.keys = keys;
hash.map = map;
return hash;
};
```
Thank you so much for your help.
Alex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment