List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror isFunction
@Override
public boolean isFunction()
From source file:JavaScriptTest.java
public static String serialize(Object obj) { StringBuilder ret = new StringBuilder(); if (obj instanceof ScriptObjectMirror) { ScriptObjectMirror om = (ScriptObjectMirror) obj; //System.out.println(om+" isArray "+om.isArray()); //System.out.println(om+" isEmpty "+om.isEmpty()); //System.out.println(om+" isExtensible "+om.isExtensible()); //System.out.println(om+" isFrozen "+om.isFrozen()); //System.out.println(om+" isFunction "+om.isFunction()); //System.out.println(om+" isSealed "+om.isSealed()); //System.out.println(om+" isStrictFunction "+om.isStrictFunction()); //System.out.println(om+" getOwnKeys "+Arrays.asList(om.getOwnKeys(true))); if (om.isFunction()) { ret.append(om.toString());//from w ww . j a v a2 s. c o m } else if (om.isArray()) { ret.append("["); //ret.append("isArray:"+om.toString()); for (int x = 0; x < om.size(); x++) { Object o = om.getSlot(x); ret.append(serialize(o)); if (x + 1 < om.size()) { ret.append(","); } } ret.append("]"); } else if (om.toString().indexOf("global") > -1) { Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); ret.append("var " + entry.getKey() + "=" + serialize(entry.getValue()) + ";\n"); } } else { ret.append("{"); Iterator<Map.Entry<String, Object>> it = om.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, Object> entry = it.next(); ret.append(entry.getKey() + ":" + serialize(entry.getValue())); if (it.hasNext()) { ret.append(","); } } ret.append("}"); } } else if (obj instanceof String) { ret.append("\"" + obj + "\""); } else { ret.append(obj); } return ret.toString(); }
From source file:com.baasbox.service.scripting.js.NashornMapper.java
License:Apache License
private JsonNode convertMirror(ScriptObjectMirror mirror) throws ScriptEvalException { if (mirror == null) { return NullNode.getInstance(); } else if (ScriptObjectMirror.isUndefined(mirror)) { return MissingNode.getInstance(); } else if (mirror.isFunction()) { return MissingNode.getInstance(); } else if (mirror.isArray()) { Collection<Object> values = mirror.values(); ArrayNode node = Json.mapper().createArrayNode(); for (Object o : values) { JsonNode e = convertDeepJson(o); if (e.isMissingNode()) { continue; }/*from w w w . ja va2s . c o m*/ node.add(e); } return node; } else if (mirror.hasMember("toJSON")) { Object toJSON = mirror.callMember("toJSON"); return convertDeepJson(toJSON); } else { ObjectNode obj = Json.mapper().createObjectNode(); Set<Map.Entry<String, Object>> entries = mirror.entrySet(); for (Map.Entry<String, Object> e : entries) { Object obv = e.getValue(); JsonNode jsonNode = convertDeepJson(obv); if (jsonNode.isMissingNode()) { continue; } obj.put(e.getKey(), jsonNode); } return obj; } }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public void forEach(Map<String, Object> map, ScriptObjectMirror som) { if (map == null) { return;/* w ww . j a v a2 s . co m*/ } if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); } AtomicInteger i = new AtomicInteger(); map.forEach((k, v) -> som.call(som, k, v, i.getAndIncrement())); }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public void forEach(List list, ScriptObjectMirror som) { if (list == null) { return;//from www . ja va2 s . co m } 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); } }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public Object map(List list, ScriptObjectMirror som) { if (list == null) { return new ArrayList(); }// w ww . java 2s. c om 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; }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public Object filter(List list, ScriptObjectMirror som) { if (list == null) { return new ArrayList(); }/*w ww. jav a 2 s . c om*/ 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; }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public Object repeat(int n, ScriptObjectMirror som) { if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); }/*from w w w. ja v a2 s . c o m*/ List res = new ArrayList(); for (int i = 0; i < n; i++) { Object o = som.call(som, i); res.add(o); } return res; }
From source file:com.intuit.karate.core.ScriptBridge.java
License:Open Source License
public Object listen(long timeout, ScriptObjectMirror som) { if (!som.isFunction()) { throw new RuntimeException("not a JS function: " + som); }// w w w. j a v a 2s. co m return context.listen(timeout, () -> Script.evalJsFunctionCall(som, null, context)); }
From source file:com.intuit.karate.ScriptValue.java
License:Open Source License
public ScriptValue(Object value, String source) { // pre-process and convert any nashorn js objects into vanilla Map / List if (value instanceof ScriptObjectMirror) { ScriptObjectMirror som = (ScriptObjectMirror) value; if (!som.isFunction()) { value = JsonUtils.toJsonDoc(value).read("$"); // results in Map or List if (value instanceof Map) { Map map = (Map) value; som.forEach((k, v) -> { // check if any special objects need to be preserved if (v instanceof ScriptObjectMirror) { ScriptObjectMirror child = (ScriptObjectMirror) v; if (child.isFunction()) { // only 1st level JS functions will be retained map.put(k, child); }// w ww . j a v a 2s. c om } else { // only 1st level non-JS (e.g. Java) objects will be retained map.put(k, v); } }); } } } this.value = value; this.source = source; if (value == null) { type = Type.NULL; } else if (value instanceof DocumentContext) { DocumentContext doc = (DocumentContext) value; listLike = doc.json() instanceof List; mapLike = !listLike; type = Type.JSON; } else if (value instanceof Node) { mapLike = true; type = Type.XML; } else if (value instanceof List) { listLike = true; type = Type.LIST; } else if (value instanceof ScriptObjectMirror) { // has to be before Map type = Type.JS_FUNCTION; } else if (value instanceof Map) { mapLike = true; type = Type.MAP; } else if (value instanceof String) { type = Type.STRING; } else if (value instanceof byte[]) { type = Type.BYTE_ARRAY; } else if (value instanceof InputStream) { type = Type.INPUT_STREAM; } else if (Script.isPrimitive(value.getClass())) { type = Type.PRIMITIVE; } else if (value instanceof Feature) { type = Type.FEATURE; } else if (value instanceof Function) { type = Type.JAVA_FUNCTION; } else { type = Type.UNKNOWN; } }
From source file:io.github.djxy.spongejavascript.script.util.Scheduler.java
License:Open Source License
public String setInterval(ScriptObjectMirror function, long milliseconds) { if (function.isFunction()) { UUID taskId = UUID.randomUUID(); Task.Builder taskBuilder = scheduler.createTaskBuilder(); Task task = taskBuilder.execute(new Runnable() { @Override/* w w w .j a va 2s .c o m*/ public void run() { function.call(function); } }).interval(milliseconds, TimeUnit.MILLISECONDS).name(taskId.toString()).submit(plugin); tasks.put(taskId, task); return taskId.toString(); } return null; }