Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror#call()
The following examples show how to use
jdk.nashorn.api.scripting.ScriptObjectMirror#call() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WithObject.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 2
Source File: ScriptExecutorImpl.java From purplejs with Apache License 2.0 | 6 votes |
private Object executeRequire( final ResourcePath script, final ScriptObjectMirror func ) { try { final ScriptObjectMirror exports = this.nashornRuntime.newJsObject(); final ScriptObjectMirror module = this.nashornRuntime.newJsObject(); module.put( "id", script.toString() ); module.put( "exports", exports ); final ExecutionContextImpl context = new ExecutionContextImpl( this, script ); final Function<String, Object> requireFunc = context::require; final Function<String, ResourcePath> resolveFunc = context::resolve; func.call( exports, context, requireFunc, resolveFunc, context.getLogger(), exports, module ); return module.get( "exports" ); } catch ( final Exception e ) { throw ErrorHelper.INSTANCE.handleError( e ); } }
Example 3
Source File: WithObject.java From hottub with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 4
Source File: WithObject.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 5
Source File: WithObject.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 6
Source File: ScriptBridge.java From karate with MIT License | 6 votes |
public Object filter(List list, ScriptObjectMirror som) { if (list == null) { return new ArrayList(); } if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); } List res = new ArrayList(); for (int i = 0; i < list.size(); i++) { Object x = list.get(i); Object y = som.call(som, x, i); if (y instanceof Boolean) { if ((Boolean) y) { res.add(x); } } else if (y instanceof Number) { // support truthy numbers as a convenience String exp = y + " == 0"; ScriptValue sv = Script.evalJsExpression(exp, null); if (!sv.isBooleanTrue()) { res.add(x); } } } return res; }
Example 7
Source File: Script.java From karate with MIT License | 6 votes |
public static ScriptValue evalJsFunctionCall(ScriptObjectMirror som, Object callArg, ScenarioContext context) { Object result; try { if (callArg != null) { result = som.call(som, callArg); } else { result = som.call(som); } return new ScriptValue(result); } catch (Exception e) { String message = "javascript function call failed: " + e.getMessage(); context.logger.error(message); context.logger.error("failed function body: " + som); throw new KarateException(message); } }
Example 8
Source File: WithObject.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 9
Source File: DFJsActor.java From dfactor with MIT License | 6 votes |
@Override public int mongoGetDb(int poolId, String dbName, Object cb) { MongoDatabase db = null; try{ db = mongo.getDatabase(poolId, dbName); ScriptObjectMirror mirCb = (ScriptObjectMirror) cb; if(db == null){ //error mirCb.call(_js, "no db available"); }else{ //succ mirCb.call(_js, null, db); } }catch(Throwable e){ e.printStackTrace(); }finally{ } return -1; }
Example 10
Source File: DFJsActor.java From dfactor with MIT License | 6 votes |
@Override public int redisGetConn(int poolId, Object cb) { Jedis conn = null; try{ conn = redis.getConn(poolId); ScriptObjectMirror mirCb = (ScriptObjectMirror) cb; if(conn == null){ //error mirCb.call(_js, "no conn available"); }else{ //succ mirCb.call(_js, null, conn); } }catch(Throwable e){ e.printStackTrace(); }finally{ if(conn != null){ conn.close(); } } return -1; }
Example 11
Source File: DFJsActor.java From dfactor with MIT License | 6 votes |
@Override public void onTcpConnOpen(int requestId, DFTcpChannel channel) { if(_jsEvent == null) _jsEvent = new DFJsEvent(null, false, null); ScriptObjectMirror cb = null; if(requestId > 0){ //as server cb = _mapTcpSvrJsFunc.get(requestId); }else{ //as client cb = _mapTcpCliJsFunc.get(requestId); } if(_mapChJsFunc==null) _mapChJsFunc = new HashMap<>(); if(cb != null){ JsTcpChannel chWrap = new JsTcpChannel(cb, channel); int chId = channel.getChannelId(); _mapChJsFunc.put(chId, chWrap); _jsEvent.type = "open"; cb.call(_js, _jsEvent, chId); //notify js } }
Example 12
Source File: WithObject.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
@SuppressWarnings("unused") private static Object bindToExpression(final Object fn, final Object receiver) { if (fn instanceof ScriptFunction) { return bindToExpression((ScriptFunction) fn, receiver); } else if (fn instanceof ScriptObjectMirror) { final ScriptObjectMirror mirror = (ScriptObjectMirror)fn; if (mirror.isFunction()) { // We need to make sure correct 'this' is used for calls with Ident call // expressions. We do so here using an AbstractJSObject instance. return new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { return mirror.call(withFilterExpression(receiver), args); } }; } } return fn; }
Example 13
Source File: Tags.java From karate with MIT License | 5 votes |
public boolean isEach(ScriptObjectMirror som) { if (!som.isFunction()) { return false; } for (String s : values) { Object o = som.call(som, s); ScriptValue sv = new ScriptValue(o); if (!sv.isBooleanTrue()) { return false; } } return true; }
Example 14
Source File: ScriptBridge.java From karate with MIT License | 5 votes |
public void forEach(List list, ScriptObjectMirror som) { if (list == null) { return; } if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); } for (int i = 0; i < list.size(); i++) { som.call(som, list.get(i), i); } }
Example 15
Source File: ScriptBridge.java From karate with MIT License | 5 votes |
public Object map(List list, ScriptObjectMirror som) { if (list == null) { return new ArrayList(); } if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); } List res = new ArrayList(list.size()); for (int i = 0; i < list.size(); i++) { Object y = som.call(som, list.get(i), i); res.add(y); } return res; }
Example 16
Source File: ScriptBridge.java From karate with MIT License | 5 votes |
public Object repeat(int n, ScriptObjectMirror som) { if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); } List res = new ArrayList(); for (int i = 0; i < n; i++) { Object o = som.call(som, i); res.add(o); } return res; }
Example 17
Source File: DFJsActor.java From dfactor with MIT License | 5 votes |
@Override public void onTcpClientConnResult(int requestId, boolean isSucc, String errMsg) { if(_jsEvent == null) _jsEvent = new DFJsEvent(null, false, null); if(_mapTcpCliJsFunc != null){ ScriptObjectMirror cb = _mapTcpCliJsFunc.get(requestId); if(cb != null){ //has cb if(!isSucc){ _mapTcpCliJsFunc.remove(requestId); } _jsEvent.type = "ret"; _jsEvent.succ = isSucc; _jsEvent.err = errMsg; cb.call(_js, _jsEvent); } } }
Example 18
Source File: DFJsActor.java From dfactor with MIT License | 5 votes |
@Override public void onTcpServerListenResult(int requestId, boolean isSucc, String errMsg) { if(_jsEvent == null) _jsEvent = new DFJsEvent(null, false, null); ScriptObjectMirror cb = _mapTcpSvrJsFunc.get(requestId); if(cb != null){ //has cb if(!isSucc){ _mapTcpSvrJsFunc.remove(requestId); } _jsEvent.type = "ret"; _jsEvent.succ = isSucc; _jsEvent.err = errMsg; cb.call(_js, _jsEvent); } }
Example 19
Source File: Module.java From nashorn-commonjs-modules with MIT License | 4 votes |
private Module compileJavaScriptModule(Folder parent, String fullPath, String code) throws ScriptException { Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE); Bindings module = createSafeBindings(); module.putAll(engineScope); // If we have cached bindings, use them to rebind exports instead of creating new ones Bindings exports = refCache.get().get(fullPath); if (exports == null) { exports = createSafeBindings(); } Module created = new Module(engine, parent, cache, fullPath, module, exports, this, this.main); String[] split = Paths.splitPath(fullPath); String filename = split[split.length - 1]; String dirname = fullPath.substring(0, Math.max(fullPath.length() - filename.length() - 1, 0)); String previousFilename = (String) engine.get(ScriptEngine.FILENAME); // set filename before eval so file names/lines in // exceptions are accurate engine.put(ScriptEngine.FILENAME, fullPath); try { // This mimics how Node wraps module in a function. I used to pass a 2nd parameter // to eval to override global context, but it caused problems Object.create. // // The \n at the end is to take care of files ending with a comment ScriptObjectMirror function = (ScriptObjectMirror) engine.eval( "(function (exports, require, module, __filename, __dirname) {" + code + "\n})"); function.call(created, created.exports, created, created.module, filename, dirname); } finally { engine.put(ScriptEngine.FILENAME, previousFilename); } // Scripts are free to replace the global exports symbol with their own, so we // reload it from the module object after compiling the code. created.exports = created.module.get("exports"); created.setLoaded(); return created; }
Example 20
Source File: EngineCompiler.java From vertx-lang-typescript with Apache License 2.0 | 4 votes |
@Override public String compile(String filename, SourceFactory sourceFactory) throws IOException { ScriptEngine e = getEngine(); ScriptObjectMirror o = (ScriptObjectMirror)e.get("compileTypescript"); return (String)o.call(null, filename, sourceFactory); }