Refine search
public Object next() { return get((key = ids[index++])); }
NativeObject toNativeObject() { NativeObject obj = new NativeObject(); for (Map.Entry<String,String> ent : optMap.entrySet()) { obj.put(ent.getKey(), obj, ent.getValue()); } return obj; } }
@Override public Iterator<Object> iterator() { return new Iterator<Object>() { Object[] ids = getIds(); Object key; int index = 0; public boolean hasNext() { return index < ids.length; } public Object next() { return get((key = ids[index++])); } public void remove() { if (key == null) { throw new IllegalStateException(); } NativeObject.this.remove(key); key = null; } }; }
// 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);");
static void init(Scriptable scope, boolean sealed) { NativeObject obj = new NativeObject(); obj.exportAsJSClass(MAX_PROTOTYPE_ID, scope, sealed); }
protected static ScriptableObject buildDataDescriptor(Scriptable scope, Object value, int attributes) { ScriptableObject desc = new NativeObject(); ScriptRuntime.setBuiltinProtoAndParent(desc, scope, TopLevel.Builtins.Object); desc.defineProperty("value", value, EMPTY); desc.defineProperty("writable", (attributes & READONLY) == 0, EMPTY); desc.defineProperty("enumerable", (attributes & DONTENUM) == 0, EMPTY); desc.defineProperty("configurable", (attributes & PERMANENT) == 0, EMPTY); return desc; }
obj = last.getAssociatedValue(t); if (obj == null) Kit.codeBug(); break getObj; ScriptableObject.putProperty(errorObject, "name", errorName); if (isVisible(cx, re)) { Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null); ScriptableObject.defineProperty( errorObject, "rhinoException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY); NativeObject catchScopeObject = new NativeObject(); catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); if (isVisible(cx, t)) { catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM); catchScopeObject.associateValue(t, obj);
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { NativeObject newGlobal = new NativeObject(); if (args.length == 1) { ScriptableObject initialContents = (ScriptableObject) args[0]; for (Object o : initialContents.getAllIds()) { String s = (String) o; ScriptableObject.putProperty(newGlobal, s, ScriptableObject.getProperty(initialContents, s)); } } return newGlobal; } }
public BoundFunction(Context cx, Scriptable scope, Callable targetFunction, Scriptable boundThis, Object[] boundArgs) { this.targetFunction = targetFunction; this.boundThis = boundThis; this.boundArgs = boundArgs; if (targetFunction instanceof BaseFunction) { length = Math.max(0, ((BaseFunction) targetFunction).getLength() - boundArgs.length); } else { length = 0; } ScriptRuntime.setFunctionProtoAndParent(this, scope); Function thrower = ScriptRuntime.typeErrorThrower(cx); NativeObject throwing = new NativeObject(); throwing.put("get", throwing, thrower); throwing.put("set", throwing, thrower); throwing.put("enumerable", throwing, false); throwing.put("configurable", throwing, false); throwing.preventExtensions(); this.defineOwnProperty(cx, "caller", throwing, false); this.defineOwnProperty(cx, "arguments", throwing, false); }
/** * javascript helper for binding parameters. * See: {@link Pig#bind(Map)} * @param o a javascript object to be converted into a Map * @return the bound script * @throws IOException if {@link Pig#bind(Map)} throws an IOException */ public BoundScript bind(Object o) throws IOException { NativeObject vars = (NativeObject)o; Map<String, Object> params = new HashMap<String, Object>(); Object[] ids = vars.getIds(); for (Object id : ids) { if (id instanceof String) { String name = (String) id; Object value = vars.get(name, vars); if (!(value instanceof NativeFunction) && value != null) { params.put(name, value.toString()); } } } return pig.bind(params); }
obj = last.getAssociatedValue(t); if (obj == null) Kit.codeBug(); } else { obj = wrapException(t, scope, cx); NativeObject catchScopeObject = new NativeObject(); catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); if (isVisible(cx, t)) { catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM); catchScopeObject.associateValue(t, obj);
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); } }
public Object[] buildArguments() { ScriptableObject so = new NativeObject(); so.put("success", so, (success) ? Boolean.TRUE : Boolean.FALSE); if (mime != null) { so.put("contentType", so, Context.toObject(mime, windowWrapper)); } if (content != null) { so.put("content", so, Context.toObject(content, windowWrapper)); } return new Object [] { so }; } }
obj = last.getAssociatedValue(t); if (obj == null) Kit.codeBug(); break getObj; Scriptable errorObject = newNativeError(cx, scope, type, args); if (isVisible(cx, re)) { Object wrap = cx.getWrapFactory().wrap(cx, scope, re, null); ScriptableObject.defineProperty( errorObject, "rhinoException", wrap, ScriptableObject.PERMANENT | ScriptableObject.READONLY | ScriptableObject.DONTENUM); NativeObject catchScopeObject = new NativeObject(); catchScopeObject.defineProperty( exceptionName, obj, ScriptableObject.PERMANENT); catchScopeObject.defineProperty( "__exception__", Context.javaToJS(t, scope), ScriptableObject.PERMANENT|ScriptableObject.DONTENUM); catchScopeObject.associateValue(t, obj);
String strFunction = "function add(x,y){ " + "return { " + "id:x+y " + "}; " + "}"; Context context = Context.enter(); ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, strFunction, "test", 1, null); Function functionAdd = (Function)scope.get("add"); NativeObject untypedResult = (NativeObject)functionAdd.call(context, scope, scope, new Object[] { 1, 2 }); double id = (Double)untypedResult.get("id", untypedResult);
private void setPrefValue(String[] keys, Serializable value, NativeObject object) { NativeObject currentObject = object; int index = 0; for (String key : keys) { if (index == keys.length-1) { currentObject.put(key, currentObject, value); } else { NativeObject newObject = null; Object temp = currentObject.get(key, currentObject); if (temp == null || temp instanceof NativeObject == false) { newObject = new NativeObjectDV(); currentObject.put(key, currentObject, newObject); } else { newObject = (NativeObject)temp; } currentObject = newObject; } index ++; } }
/** * Create a new JavaScript object. * * Equivalent to evaluating "new Object()". * @param scope the scope to search for the constructor and to evaluate * against * @return the new object */ public Scriptable newObject(Scriptable scope) { NativeObject result = new NativeObject(); ScriptRuntime.setBuiltinProtoAndParent(result, scope, TopLevel.Builtins.Object); return result; }
import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.SimpleBindings; import sun.org.mozilla.javascript.internal.NativeObject; String buildInfoJS = "var build_info = new Object;build_info.BUILD_DISPLAY_VERSION = 'Build 201302232300'"; ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); jsEngine.eval(buildInfoJS); Object nativeObject = jsEngine.get("build_info"); if (nativeObject instanceof NativeObject) { NativeObject nObj = (NativeObject)nativeObject; for (String key: nObj.getAllIds()) { System.out.println(key); System.out.println(nObj.get(key, nObj)); } }