@Override @SuppressWarnings("unchecked") public Object eval(ScriptContext context) throws ScriptException { try { Map<String, Object> map = new HashMap<String, Object>(); for (Iterator it = context.getScopes().iterator(); it.hasNext();) { int scope = ((Integer) it.next()).intValue(); Bindings bindings = context.getBindings(scope); Set keys = bindings.keySet(); for (Object key : keys) { map.put((String) key, bindings.get(key)); } } return this.expression.execute(map); } catch (Exception e) { throw new ScriptException(e); } }
@Override public Set<String> keySet() { return userBindings.keySet();//TODO: join with systemBindings }
ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine se = mgr.getEngineByName("JavaScript"); try { se.eval("var x;var a,b,c,d;var y = \"wow\";var z = y+'x';"); Bindings bindings = se.getBindings(ScriptContext.ENGINE_SCOPE); System.out.println(bindings.keySet()); } catch (ScriptException e) { //e.printStackTrace(); }
private List<String> engineVariablesCompleter(final String code, @SuppressWarnings("unused") final int index, final ScriptEngine engine) { final List<String> matches = new ArrayList<>(); final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for (final String key : bindings.keySet()) { if (key.toLowerCase().startsWith(code.toLowerCase())) { matches.add(key); } } return matches; }
@Override public int size() { return Stream .concat(automatic.keySet().stream(), Stream.concat(bindings.keySet().stream(), ctx.keySet().stream())) .distinct().collect(Collectors.counting()).intValue(); }
public static HashMap<String, Object> getBindings(Bindings bindings) { HashMap<String, Object> engineScopes = new HashMap<String, Object>(); for (String key : bindings.keySet()) { //Do not add mcApi and __builtins__ if (!key.equals(MC_API) && !key.startsWith("__")) { engineScopes.put(key, bindings.get(key)); } } return engineScopes; }
public static HashMap<String, Object> getBindings(Bindings bindings) { HashMap<String, Object> engineScopes = new HashMap<String, Object>(); for (String key : bindings.keySet()) { //Do not add mcApi and __builtins__ if (!key.equals(MC_API) && !key.startsWith("__")) { engineScopes.put(key, bindings.get(key)); } } return engineScopes; }
ScriptEngine engine; // create the engine and have it load your javascript Bindings bind = engine.getBindings(ScriptContext.ENGINE_SCOPE); Set<String> allAttributes = bind.keySet(); Set<String> allFunctions = new HashSet<String>(); for ( String attr : allAttributes ) { if ( "function".equals( engine.eval("typeof " + attr) ) ) { allFunctions.add(attr); } } System.out.println(allFunctions);
private List<String> engineVariablesCompleter(final String code, @SuppressWarnings("unused") final int index, final ScriptEngine engine) { final List<String> matches = new ArrayList<>(); final Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); for (final String key : bindings.keySet()) { if (key.toLowerCase().startsWith(code.toLowerCase())) { matches.add(key); } } return matches; }
@Override public Set<String> bindingNames() { return context.getBindings(ScriptContext.ENGINE_SCOPE).keySet(); }
private Set<String> totalKeySet() { Set<String> keys = new HashSet<String>(); List<Integer> scopes = context.getScopes(); for (int i : scopes) { keys.addAll(context.getBindings(i).keySet()); } return Collections.unmodifiableSet(keys); }
@Override public Set getSharedVariableNames() { LinkedHashSet<String> keys = new LinkedHashSet<String>(); if (map != null) { keys.addAll(map.getBindings(map.ENGINE_SCOPE).keySet()); } if (fo != null) { Enumeration<String> en = fo.getAttributes(); while (en.hasMoreElements()) { keys.add(en.nextElement()); } } return keys; }
@Override public Object[] getIds() { ArrayList<String> list = new ArrayList<String>(); synchronized (context) { for (int scope : context.getScopes()) { Bindings bindings = context.getBindings(scope); if (bindings != null) { list.ensureCapacity(bindings.size()); for (String key : bindings.keySet()) { list.add(key); } } } } return list.toArray(new String[list.size()]); }
private void initBindings(Bindings bindings, ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) { Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); bindings.keySet().forEach((String key) -> { currentBindings.put(key, scriptLanguage.decode(bindings.get(key))); }); }
/** Lists variables in the script context. */ public void vars() { if (interpreter == null) return; // no active script language final List<String> keys = new ArrayList<>(); final List<Object> types = new ArrayList<>(); final Bindings bindings = interpreter.getBindings(); for (final String key : bindings.keySet()) { final Object value = bindings.get(key); keys.add(key); types.add(type(value)); } printColumns(keys, types); }
/** Lists variables in the script context. */ public void vars() { if (interpreter == null) return; // no active script language final List<String> keys = new ArrayList<>(); final List<Object> types = new ArrayList<>(); final Bindings bindings = interpreter.getBindings(); for (final String key : bindings.keySet()) { final Object value = bindings.get(key); keys.add(key); types.add(type(value)); } printColumns(keys, types); }
private void syncBindings(ScriptEngine scriptEngine, ScriptLanguage scriptLanguage) { Bindings currentBindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE); this.scriptEngines.forEach((String name, ScriptEngine engine) -> { Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); currentBindings.keySet().forEach((String key) -> { bindings.put(key, scriptLanguage.decode(currentBindings.get(key))); }); }); }
/** Transfers variables from one interpreter's bindings to another. */ private void copyBindings(final ScriptInterpreter src, final ScriptInterpreter dest) { if (src == null) return; // nothing to copy final Bindings srcBindings = src.getBindings(); final Bindings destBindings = dest.getBindings(); for (final String key : src.getBindings().keySet()) { final Object value = src.getLanguage().decode(srcBindings.get(key)); destBindings.put(key, value); } }
/** Transfers variables from one interpreter's bindings to another. */ private void copyBindings(final ScriptInterpreter src, final ScriptInterpreter dest) { if (src == null) return; // nothing to copy final Bindings srcBindings = src.getBindings(); final Bindings destBindings = dest.getBindings(); for (final String key : src.getBindings().keySet()) { final Object value = src.getLanguage().decode(srcBindings.get(key)); destBindings.put(key, value); } }
@ExposedMethod public PyObject scope_keys() { PyList members = new PyList(); List<Integer> scopes = context.getScopes(); for (int scope : scopes) { Bindings bindings = context.getBindings(scope); if (bindings == null) continue; for (String key : bindings.keySet()) members.append(new PyString(key)); } members.sort(); return members; }