// build a Map Map<String, String> map = new HashMap<String, String>(); map.put("bye", "now"); // Convert it to a NativeObject (yes, this could have been done directly) NativeObject nobj = new NativeObject(); for (Map.Entry<String, String> entry : map.entrySet()) { nobj.defineProperty(entry.getKey(), entry.getValue(), NativeObject.READONLY); } // Get Engine and place native object into the context ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("javascript"); engine.put("map", nobj); // Standard Javascript dot notation prints 'now' (as it should!) engine.eval("println(map.bye);");
final Map<String,String> javaMap = new HashMap<>(); javaMap.put("alpha", "bravo"); javaMap.put("charlie", "delta"); final NativeObject jsMap = new NativeObject(); for (Map.Entry<String,String> entry : javaMap.entrySet()) { jsMap.defineProperty( entry.getKey(), entry.getValue(), NativeObject.READONLY ); } final ScriptEngine jsEngine = (new ScriptEngineManager()).getEngineByName("JavaScript"); jsEngine.put("map", jsMap); jsEngine.eval( "for (var idx in map) print(idx + '; ' + map[idx] + '\\n');" );
private void setupDefaultPrototype() { NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } }
private void setupDefaultPrototype() { NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } }
private synchronized Object setupDefaultPrototype() { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } return obj; }
private synchronized Object setupDefaultPrototype() { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } return obj; }
private synchronized Object setupDefaultPrototype() { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } return obj; }
private synchronized Object setupDefaultPrototype() { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); final int attr = ScriptableObject.DONTENUM; obj.defineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = getObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.setPrototype(proto); } return obj; }
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, catchScopeObject), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);
catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM);