List of usage examples for jdk.nashorn.api.scripting JSObject isFunction
public boolean isFunction();
From source file:com.eas.client.changes.ChangeValue.java
@Override public JSObject getPublished() { if (published == null) { JSObject publisher = Scripts.getSpace().getPublisher(this.getClass().getName()); if (publisher == null || !publisher.isFunction()) { throw new NoPublisherException(); }//from w w w . j a v a2s . c o m published = (JSObject) publisher.call(null, new Object[] { this }); } return published; }
From source file:com.eas.client.queries.ScriptedQuery.java
@Override public JSObject execute(Scripts.Space aSpace, Consumer<JSObject> onSuccess, Consumer<Exception> onFailure) throws Exception { assert Scripts.getSpace() == aSpace : "Scripts.Space TLS assumption failed"; if (onSuccess != null) { ScriptedResource._require(new String[] { entityName }, null, aSpace, new HashSet<>(), (Void v) -> { JSObject source = aSpace.createModule(entityName); if (source.hasMember("fetch")) { Object oFetch = source.getMember("fetch"); if (oFetch instanceof JSObject) { JSObject jsFetch = (JSObject) oFetch; if (jsFetch.isFunction()) { JSObject jsParams = aSpace.makeObj(); for (int i = 0; i < params.getParametersCount(); i++) { Parameter p = params.get(i + 1); jsParams.setMember(p.getName(), aSpace.toJs(p.getValue())); }/*w w w. j ava2 s . co m*/ final ExecutionChecker exChecker = new ExecutionChecker(); Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { if (exChecker.isExecutionNeeded()) { try { JSObject jsRowset = args.length > 0 ? (JSObject) aSpace.toJava(args[0]) : null; try { onSuccess.accept(jsRowset); } catch (Exception ex) { Logger.getLogger(ScriptedQuery.class.getName()) .log(Level.SEVERE, null, ex); } } catch (Exception ex) { if (onFailure != null) { onFailure.accept(ex); } } } return null; } }, new AbstractJSObject() { @Override public Object call(final Object thiz, final Object... args) { if (exChecker.isExecutionNeeded()) { if (onFailure != null) { if (args.length > 0) { if (args[0] instanceof Exception) { onFailure.accept((Exception) args[0]); } else { onFailure.accept(new Exception( String.valueOf(aSpace.toJava(args[0])))); } } else { onFailure.accept(new Exception( "No error information from fetch method")); } } } return null; } } })); if (!JSType.nullOrUndefined(oRowset)) { onSuccess.accept((JSObject) aSpace.toJava(oRowset)); exChecker.setExecutionNeeded(false); } } } } }, onFailure); return null; } else { JSObject source = aSpace.createModule(entityName); if (source.hasMember("fetch")) { Object oFetch = source.getMember("fetch"); if (oFetch instanceof JSObject) { JSObject jsFetch = (JSObject) oFetch; if (jsFetch.isFunction()) { JSObject jsParams = aSpace.makeObj(); Object oRowset = jsFetch.call(source, aSpace.toJs(new Object[] { jsParams })); if (!JSType.nullOrUndefined(oRowset)) { return (JSObject) aSpace.toJava(oRowset); } } } } return null; } }
From source file:com.eas.client.reports.JSDynaBean.java
public static Object wrap(Object aValue, int aTimezoneOffset) { aValue = Scripts.getSpace().toJava(aValue); if (aValue instanceof Date) { return convertDateToExcelDate((Date) aValue, aTimezoneOffset); } else if (aValue instanceof Number || aValue instanceof Boolean || aValue instanceof CharSequence || aValue == null) {//from www . j a va 2s. c o m return aValue; } else if (aValue instanceof JSObject) { JSObject jsValue = (JSObject) aValue; if (jsValue.isArray() || Scripts.getSpace().isArrayDeep(jsValue)) { return new JSDynaList(jsValue, aTimezoneOffset); } else if (!jsValue.isFunction()) { return new JSDynaBean(jsValue, aTimezoneOffset); } else { return null; } } else { return aValue; } }
From source file:de.axelfaust.alfresco.nashorn.repo.utils.SpecialModuleHandler.java
License:Open Source License
public SpecialModuleHandler(final JSObject delegate, final boolean callerProvided, final boolean callerTagged, final String callerUrl, final WithTaggedCaller withTaggedCallerFn) { LOGGER.debug(/* w w w. j ava 2 s . c om*/ "Constructing special module handler for {} - callerProvided: {}, callerTagged: {}, caller: {}", delegate.isFunction() ? delegate.getMember("name") : new NativeLogMessageArgumentWrapper(delegate), callerProvided, callerTagged, callerUrl); this.delegate = delegate; this.callerProvided = callerProvided; this.callerTagged = callerTagged; this.callerUrl = callerUrl; this.isFunction = delegate.isFunction(); this.withTaggedCallerFn = withTaggedCallerFn; }
From source file:de.axelfaust.alfresco.nashorn.repo.utils.SpecialModuleHandler.java
License:Open Source License
/** * {@inheritDoc}//from w ww . j a va 2 s. co m */ @Override public Object getMember(final String name) { LOGGER.debug("Handling getMember {} on {}", name, this.delegate.isFunction() ? this.delegate.getMember("name") : new NativeLogMessageArgumentWrapper(this.delegate)); // can't handle callerProvided for getMember final AtomicReference<Object> resultRef = new AtomicReference<>(); if (this.callerTagged) { LOGGER.debug("Tagging callerUrl {}", this.callerUrl); this.withTaggedCallerFn.withTaggedCaller(() -> { resultRef.set(this.delegate.getMember(name)); }, this.callerUrl); } else { resultRef.set(this.delegate.getMember(name)); } Object result = resultRef.get(); LOGGER.debug("getMember {} yielded {}", name, result instanceof JSObject ? (((JSObject) result).isFunction() ? ((JSObject) result).getMember("name") : new NativeLogMessageArgumentWrapper((JSObject) result)) : result); if (result instanceof JSObject) { final JSObject scriptResult = (JSObject) result; if (scriptResult.isFunction() || (!scriptResult.isArray() && !ScriptObjectMirror.isUndefined(scriptResult))) { LOGGER.debug("Wrapping function result {} of getMember in special module handler", scriptResult.isFunction() ? scriptResult.getMember("name") : new NativeLogMessageArgumentWrapper(scriptResult)); result = new SpecialModuleHandler(scriptResult, this.callerProvided, this.callerTagged, this.callerUrl, this.withTaggedCallerFn); if (scriptResult.isFunction()) { ((SpecialModuleHandler) result).setThis(this.delegate, this); } } } return result; }
From source file:de.axelfaust.alfresco.nashorn.repo.utils.SpecialModuleHandler.java
License:Open Source License
/** * {@inheritDoc}//from w ww. ja v a 2s . c o m */ @Override public Object getSlot(final int slot) { LOGGER.debug("Handling getSlot {} on {}", slot, this.delegate.isFunction() ? this.delegate.getMember("name") : new NativeLogMessageArgumentWrapper(this.delegate)); // can't handle callerProvided for getSlot final AtomicReference<Object> resultRef = new AtomicReference<>(); if (this.callerTagged) { LOGGER.debug("Tagging callerUrl {}", this.callerUrl); this.withTaggedCallerFn.withTaggedCaller(() -> { resultRef.set(this.delegate.getSlot(slot)); }, this.callerUrl); } else { resultRef.set(this.delegate.getSlot(slot)); } Object result = resultRef.get(); LOGGER.debug("getSlot {} yielded {}", slot, result instanceof JSObject ? (((JSObject) result).isFunction() ? ((JSObject) result).getMember("name") : new NativeLogMessageArgumentWrapper((JSObject) result)) : result); if (result instanceof JSObject) { final JSObject scriptResult = (JSObject) result; if (scriptResult.isFunction() || (!scriptResult.isArray() && !ScriptObjectMirror.isUndefined(scriptResult))) { LOGGER.debug("Wrapping function result {} of getSlot in special module handler", scriptResult.isFunction() ? scriptResult.getMember("name") : new NativeLogMessageArgumentWrapper(scriptResult)); result = new SpecialModuleHandler(scriptResult, this.callerProvided, this.callerTagged, this.callerUrl, this.withTaggedCallerFn); if (scriptResult.isFunction()) { ((SpecialModuleHandler) result).setThis(this.delegate, this); } } } return result; }
From source file:de.axelfaust.alfresco.nashorn.repo.utils.SpecialModuleHandler.java
License:Open Source License
/** * {@inheritDoc}// w w w . j ava 2s .co m */ @Override public Collection<Object> values() { LOGGER.debug("Handling values on {}", this.delegate.isFunction() ? this.delegate.getMember("name") : new NativeLogMessageArgumentWrapper(this.delegate)); // can't handle callerProvided for values final AtomicReference<Collection<Object>> resultRef = new AtomicReference<>(); if (this.callerTagged) { LOGGER.debug("Tagging callerUrl {}", this.callerUrl); this.withTaggedCallerFn.withTaggedCaller(() -> { resultRef.set(this.delegate.values()); }, this.callerUrl); } else { resultRef.set(this.delegate.values()); } final Collection<Object> result = resultRef.get(); LOGGER.debug("values yielded {}", result); final List<Object> resultList = new ArrayList<>(); for (final Object resultElement : result) { if (resultElement instanceof JSObject) { final JSObject scriptResult = (JSObject) resultElement; if (scriptResult.isFunction() || (!scriptResult.isArray() && !ScriptObjectMirror.isUndefined(scriptResult))) { LOGGER.debug("Wrapping function result {} of values in special module handler", scriptResult.isFunction() ? scriptResult.getMember("name") : new NativeLogMessageArgumentWrapper(scriptResult)); final Object resultElementMod = new SpecialModuleHandler(scriptResult, this.callerProvided, this.callerTagged, this.callerUrl, this.withTaggedCallerFn); if (scriptResult.isFunction()) { ((SpecialModuleHandler) resultElementMod).setThis(this.delegate, this); } resultList.add(resultElementMod); } else { resultList.add(resultElement); } } else { resultList.add(resultElement); } } return resultList; }
From source file:game.engine.game.JavascriptConsole.java
License:Open Source License
/** * // w w w.j a va 2 s . c o m */ private void dump(Object o, StringBuilder builder) { // handle known types if (o == null) { builder.append("null"); return; } else if (o instanceof String) { builder.append('"').append(o).append('"'); return; } // try to wrap JS values and dump them, or fall back to toString() o = jdk.nashorn.api.scripting.ScriptUtils.wrap(o); if (o instanceof JSObject) { JSObject object = (JSObject) o; if (object.isArray()) { builder.append('['); boolean first = true; for (Object element : object.values()) { if (first) { first = false; } else { builder.append(", "); } dump(element, builder); } builder.append("]"); } else if (object.isFunction()) { builder.append("function"); } else { builder.append('{'); boolean first = true; for (Object key : object.keySet()) { if (first) { first = false; } else { builder.append(", "); } dump(key, builder); builder.append(": "); dump(object.getMember(key.toString()), builder); } builder.append("}"); } } else { builder.append(o); } }
From source file:io.apigee.rowboat.internal.ScriptRunner.java
License:Open Source License
/** * Execute the script./*from w w w . j av a2 s. co m*/ */ @Override public ScriptStatus call() throws NodeException { threadRunner.set(this); ScriptStatus status; try { engine = engineManager.getEngineByName("nashorn"); assert (engine instanceof Compilable); assert (engine instanceof Invocable); invocableEngine = (Invocable) engine; context = engine.getContext(); // Lazy first-time init of the node version. registry.load(); try { initGlobals(); } catch (NodeException ne) { return new ScriptStatus(ne); } finally { initialized.countDown(); } // Run "trireme.js," which is our equivalent of "node.js". It returns a function that takes // "process". When done, we may have ticks to execute. String mainScript = registry.getMainScriptSource(); CompiledScript mainCompiled = ((Compilable) engine).compile(mainScript); mainCompiled.eval(); //engine.eval(mainScript); boolean timing = startTiming(); try { // Pass the bindings (the globals) to the main function because it has to set its // "global" variables in there. invocableEngine.invokeFunction("_triremeMain", process, engine.getBindings(ScriptContext.ENGINE_SCOPE)); } catch (Throwable t) { boolean handled = handleScriptException(t); if (!handled) { throw t; } } finally { if (timing) { endTiming(); } } status = mainLoop(); } catch (NodeExitException ne) { // This exception is thrown by process.exit() status = ne.getStatus(); } catch (IOException ioe) { log.debug("I/O exception processing script: {}", ioe); status = new ScriptStatus(ioe); } catch (Throwable t) { if (exitCode >= 0) { return new ScriptStatus(exitCode); } log.debug("Unexpected script error: {}", t); status = new ScriptStatus(t); } log.debug("Script exiting with exit code {}", status.getExitCode()); if (!status.hasCause() && !Boolean.TRUE.equals(process.getMember("_exiting"))) { // Fire the exit callback, but only if we aren't exiting due to an unhandled exception, and "exit" // wasn't already fired because we called "exit" try { process.setMember("_exiting", true); JSObject emit = (JSObject) process.getMember("emit"); assert (emit != null); assert (emit.isFunction()); emit.call(process, "exit", status.getExitCode()); } catch (NodeExitException ee) { // Exit called exit -- allow it to replace the exit code log.debug("Script replacing exit code with {}", ee.getCode()); status = ee.getStatus(); } catch (Throwable t) { if (exitCode >= 0) { status = new ScriptStatus(exitCode); } // Many of the unit tests fire exceptions inside exit. status = new ScriptStatus(t); } } closeCloseables(); try { OutputStream stdout = getStdout(); if (stdout != System.out) { stdout.close(); } OutputStream stderr = getStderr(); if (stderr != System.err) { stderr.close(); } } catch (IOException ignore) { } return status; }
From source file:me.finalchild.nashornbukkit.script.Script.java
License:MIT License
public void disable() { Object obj = getContext().getAttribute("onDisable", ScriptContext.ENGINE_SCOPE); if (obj instanceof JSObject) { JSObject jsobj = (JSObject) obj; if (jsobj.isFunction()) { jsobj.call(null);// w w w .ja v a 2 s .c o m } } }