Refine search
String script = "function abc(x,y) {return x+y;}" + "function def(u,v) {return u-v;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, script, "script", 1, null); Function fct = (Function)scope.get("abc", scope); Object result = fct.call( context, scope, scope, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
private String decryptSignature(String encryptedSig, String decryptionCode) throws DecryptException { Context context = Context.enter(); context.setOptimizationLevel(-1); Object result; try { ScriptableObject scope = context.initStandardObjects(); context.evaluateString(scope, decryptionCode, "decryptionCode", 1, null); Function decryptionFunc = (Function) scope.get("decrypt", scope); result = decryptionFunc.call(context, scope, scope, new Object[]{encryptedSig}); } catch (Exception e) { throw new DecryptException("could not get decrypt signature", e); } finally { Context.exit(); } return result == null ? "" : result.toString(); }
@Override public Object invokeMethod(Object thiz, final String name, final Object... args) throws ScriptException, NoSuchMethodException { if (thiz == null) { throw new java.lang.IllegalArgumentException("thiz is null!"); } if (name == null) { throw new java.lang.IllegalArgumentException("method name is null"); } if (!(thiz instanceof Scriptable)) { thiz = Context.toObject(thiz, topLevel); } final Scriptable localScope = (Scriptable) thiz; final Object obj = ScriptableObject.getProperty(localScope, name); if (!(obj instanceof org.mozilla.javascript.Function)) { throw new NoSuchMethodException("no such method: " + name); } return callInContext((cx) -> { final org.mozilla.javascript.Function func = (org.mozilla.javascript.Function) obj; Scriptable parentScope = func.getParentScope(); if (parentScope == null) { parentScope = topLevelProto(); } Object result = func.call(cx, parentScope, localScope, wrapArguments(args)); return unwrapReturnValue(result); }); }
@SuppressWarnings("deprecation") public void handleEvent(Event event){ List<Plugin> pul = getPluginEvents(event); Context context = Context.enter(); try { for(int i = 0; i < pul.toArray().length; i++){ try { ScriptableObject scope = pul.get(i).scope; Function fct = (Function)scope.get(event.type, scope); fct.call(context, scope, scope, event.param); } catch (SecurityException e) { c.gui.addText(ChatColor.DARK_AQUA + "Warning: Security error encountered while trying to pass " + event.type + " to " + pul.get(i).getName() + "\n" + e.getMessage()); } catch (IllegalArgumentException e) { c.gui.addText(ChatColor.DARK_AQUA + "Warning: Wrong amount of argument error encountered while trying to pass " + event.type + " to " + pul.get(i).getName() + "\n" + e.getMessage()); } } }finally{ Context.exit(); } }
public Object invoke(Object thisArg) throws Exception { Context cx = Context.enter(); try { Scriptable thisObj = !(thisArg instanceof Scriptable) ? Context.toObject(thisArg, scope) : (Scriptable)thisArg; Object result = ScriptableObject.getProperty(thisObj, name); if (result == Scriptable.NOT_FOUND) { result = ScriptableObject.getProperty(thisObj, "get" + StringUtils.capitalize(name)); if (result != Scriptable.NOT_FOUND && result instanceof Function) { try { result = ((Function)result).call( cx, ScriptableObject.getTopLevelScope(thisObj), thisObj, new Object[] {}); } catch (JavaScriptException exc) { exc.printStackTrace(); result = null; } } } return unwrap(result); } finally { Context.exit(); } }
String script = "function abc(x,y) {return x+y;}"; Context context = Context.enter(); try { ScriptableObject scope = context.initStandardObjects(); Scriptable that = context.newObject(scope); Function fct = context.compileFunction(scope, script, "script", 1, null); Object result = fct.call( context, scope, that, new Object[] {2, 3}); System.out.println(Context.jsToJava(result, int.class)); } finally { Context.exit(); }
/** * Call a method of an object. * @param cx the Context object associated with the current thread. * @param obj the JavaScript object * @param methodName the name of the function property * @param args the arguments for the call */ public static Object callMethod(Context cx, Scriptable obj, String methodName, Object[] args) { Object funObj = getProperty(obj, methodName); if (!(funObj instanceof Function)) { throw ScriptRuntime.notFunctionError(obj, methodName); } Function fun = (Function)funObj; // XXX: What should be the scope when calling funObj? // The following favor scope stored in the object on the assumption // that is more useful especially under dynamic scope setup. // An alternative is to check for dynamic scope flag // and use ScriptableObject.getTopLevelScope(fun) if the flag is not // set. But that require access to Context and messy code // so for now it is not checked. Scriptable scope = ScriptableObject.getTopLevelScope(obj); if (cx != null) { return fun.call(cx, scope, obj, args); } else { return Context.call(null, fun, scope, obj, args); } }
@Initialize public void init() { _contextFactory = new ContextFactory(); final Context context = _contextFactory.enterContext(); try { _script = context.compileString(sourceCode, this.getClass().getSimpleName(), 1, null); _sharedScope = context.initStandardObjects(); JavaScriptUtils.addToScope(_sharedScope, new JavaScriptLogger(), "logger", "log"); JavaScriptUtils.addToScope(_sharedScope, System.out, "out"); _script.exec(context, _sharedScope); _transformerObj = (NativeObject) _sharedScope.get("transformerObj"); if (_transformerObj == null) { throw new IllegalStateException("Required JS object 'transformerObj' not found!"); } _initializeFunction = (Function) _transformerObj.get("initialize"); _transformFunction = (Function) _transformerObj.get("transform"); _closeFunction = (Function) _transformerObj.get("close"); _initializeFunction.call(context, _sharedScope, _sharedScope, new Object[0]); } finally { Context.exit(); } }
private Object call(Function func, Object[] args) { Context cx = Context.getCurrentContext(); Scriptable thisObj = getAdaptee(); Scriptable scope = func.getParentScope(); try { return func.call(cx, scope, thisObj, args); } catch (RhinoException re) { throw Context.reportRuntimeError(re.getMessage()); } }
@Override public String apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toString(res) : null; } };
public Object exec(List arguments) throws TemplateModelException { Context cx = Context.getCurrentContext(); Object[] args = arguments.toArray(); BeansWrapper wrapper = getWrapper(); for (int i = 0; i < args.length; i++) { args[i] = wrapper.unwrap((TemplateModel) args[i]); } return wrapper.wrap(((Function) getScriptable()).call(cx, ScriptableObject.getTopLevelScope(fnThis), fnThis, args)); } }
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { Scriptable curr = cx.newObject(scope); Scriptable prev = cx.newObject(scope); long mtime = new File(item).lastModified(); Object date = cx.evaluateString(scope, "new Date(" + mtime + ");", "date", 0, null); ScriptableObject.putProperty(prev, "mtime", date); ScriptableObject.putProperty(curr, "mtime", date); callback.call(cx,scope,thisObj, new Object[]{prev,curr}); return Undefined.instance; } });
@Override public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) { Scriptable repl = (Scriptable) ScriptableObject.getTypedProperty(scope, "require", Function.class) .call(cx, scope, thisObj, new Object[]{"repl"}); Function start = ScriptableObject.getTypedProperty(repl, "start", Function.class); Scriptable options = cx.newObject(scope); ScriptableObject.putProperty(options, "prompt", Context.javaToJS(PROMPT, scope)); ScriptableObject.putProperty(options, "terminal", Context.javaToJS(true, scope)); start.call(cx, scope, thisObj, new Object[]{options}); return Undefined.instance; } });
@Override public Object apply(Object input) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } final Object res = fn.call(cx, scope, scope, new Object[]{input}); return res != null ? Context.toObject(res, scope) : null; } };
/** * @deprecated The method is only present for compatibility. */ public static Object call(Context cx, Object fun, Object thisArg, Object[] args, Scriptable scope) { if (!(fun instanceof Function)) { throw notFunctionError(toString(fun)); } Function function = (Function)fun; Scriptable thisObj = toObjectOrNull(cx, thisArg); if (thisObj == null) { throw undefCallError(thisObj, "function"); } return function.call(cx, scope, thisObj, args); }
@Override public double apply(Object[] args) { // ideally we need a close() function to discard the context once it is not used anymore Context cx = Context.getCurrentContext(); if (cx == null) { cx = contextFactory.enterContext(); } return Context.toNumber(fn.call(cx, scope, scope, args)); } };
/** * Get a string representing the script stack in a way that is compatible with V8. * If the function "Error.prepareStackTrace" is defined, then call that function, * passing it an array of CallSite objects. Otherwise, behave as if "getScriptStackTrace" * was called instead. * @since 1.7R5 */ public Object getPreparedScriptStackTrace(Context cx, Scriptable scope, Scriptable err) { Scriptable top = ScriptableObject.getTopLevelScope(scope); Scriptable error = TopLevel.getBuiltinCtor(cx, top, TopLevel.Builtins.Error); Object prepare = error.get("prepareStackTrace", error); if (prepare instanceof Function) { Function prepareFunc = (Function)prepare; ScriptStackElement[] elts = getScriptStack(); Object[] rawStack = new Object[elts.length]; for (int i = 0; i < elts.length; i++) { rawStack[i] = cx.newObject(top, "CallSite"); ((NativeCallSite)rawStack[i]).setElement(elts[i]); } return prepareFunc.call(cx, scope, null, new Object[] { err, cx.newArray(top, rawStack) }); } return getScriptStackTrace(); }
return callInContext((cx) -> { final Scriptable localScope = cx.newObject(topLevel); localScope.setPrototype(topLevel); localScope.setParentScope(null); final Object obj = ScriptableObject.getProperty(localScope, name); if (!(obj instanceof org.mozilla.javascript.Function)) { throw new RuntimeException(new ScriptException(format("no such method: %s", name))); Scriptable parentScope = func.getParentScope(); if (parentScope == null) { parentScope = topLevelProto(); Object result = func.call(cx, parentScope, localScope, wrapArguments(args)); return unwrapReturnValue(result);
public Scriptable construct(Context cx, Scriptable scope, Object[] args) throws RhinoException { if (isPrototype) { Scriptable topLevel = ScriptableObject.getTopLevelScope(scope); JSAdapter newObj; if (args.length > 0) { newObj = new JSAdapter(Context.toObject(args[0], topLevel)); } else { throw Context.reportRuntimeError("JSAdapter requires adaptee"); } return newObj; } else { Scriptable tmp = getAdaptee(); if (tmp instanceof Function) { return ((Function)tmp).construct(cx, scope, args); } else { throw Context.reportRuntimeError("TypeError: not a constructor"); } } }
public Object run(Context cx) { Object[] args = ab.buildArguments(); handler.call(cx, handler.getParentScope(), globalObject, args); return null; } });