/** * Return new {@link Scriptable} instance using the default constructor for the class of the * underlying Java method. Return null to indicate that the call method should be used to create * new objects. */ public Scriptable createObject(Context cx, Scriptable scope) { if (member.isCtor() || (parmsLength == VARARGS_CTOR)) { return null; } Scriptable result; try { result = (Scriptable) member.getDeclaringClass().newInstance(); } catch (Exception ex) { throw Context.throwAsScriptRuntimeEx(ex); } result.setPrototype(getClassPrototype()); result.setParentScope(getParentScope()); return result; }
/** * Define this function as a JavaScript constructor. * * <p>Sets up the "prototype" and "constructor" properties. Also calls setParent and setPrototype * with appropriate values. Then adds the function object as a property of the given scope, using * <code>prototype.getClassName()</code> as the name of the property. * * @param scope the scope in which to define the constructor (typically the global object) * @param prototype the prototype object * @see gov.nbcs.rp.common.js.javascript.Scriptable#setParentScope * @see gov.nbcs.rp.common.js.javascript.Scriptable#setPrototype * @see gov.nbcs.rp.common.js.javascript.Scriptable#getClassName */ public void addAsConstructor(Scriptable scope, Scriptable prototype) { ScriptRuntime.setFunctionProtoAndParent(this, scope); setImmunePrototypeProperty(prototype); prototype.setParentScope(this); final int attr = ScriptableObject.DONTENUM | ScriptableObject.PERMANENT | ScriptableObject.READONLY; defineProperty(prototype, "constructor", this, attr); String name = prototype.getClassName(); defineProperty(scope, name, this, ScriptableObject.DONTENUM); setParentScope(scope); }
public static Object eval(String source, Map<String, Scriptable> bindings) { Context cx = ContextFactory.getGlobal().enterContext(); try { Scriptable scope = cx.initStandardObjects(); if (bindings != null) { for (String id : bindings.keySet()) { Scriptable object = bindings.get(id); object.setParentScope(scope); scope.put(id, scope, object); } } return cx.evaluateString(scope, source, "source", 1, null); } finally { Context.exit(); } }