/** * Returns a {@link Set} view of the mappings contained in this map. * The set is backed by the map, so changes to the map should be * reflected in the set, and vice-versa. However, the implementation * does not reflect changes currently. * * @return an entry set of a map */ @Override public Set<Entry<String, Object>> entrySet() { return getMap().entrySet(); }
public BiVariableMap getVarMap(LocalContextProvider provider) { if (varMap == null) { synchronized(this) { if (varMap == null) { varMap = new BiVariableMap(provider, lazy); } } } return varMap; }
/** * Returns <tt>true</tt> if this map contains a mapping for the specified * key. * * @param key is a key to be tested its presence * @return <tt>true</tt> if this map contains a mapping for the specified key */ @Override public boolean containsKey(final Object key) { if ( varNames == null || key == null ) return false; return varNames.contains( checkKey(key) ); }
/** * Returns a {@link Set} view of the mappings contained in this map. * The set is backed by the map, so changes to the map should be * reflected in the set, and vice-versa. However, the implementation * does not reflect changes currently. * * @return an entry set of a map */ public Set entrySet() { if (isEmpty()) return null; return getMap().entrySet(); }
/** * Adds a key-value pair of Ruby local variable to double array. * * @param name is a Ruby's local variable name * @param value is BiVariable type object corresponding to the name */ public void update(final String name, final BiVariable value) { getNames().add(name); getVariables().add(value); }
void terminate() { VariableInterceptor.terminateGlobalVariables(getLocalVariableBehavior(), getVariables(), getRuntime()); VariableInterceptor.terminateLocalVariables(getLocalVariableBehavior(), getNames(), getVariables()); }
private static void updateGlobalVar(BiVariableMap vars, RubyObject receiver, String name, IRubyObject value) { BiVariable var; if (vars.containsKey((Object) name)) { var = vars.getVariable(receiver, name); var.setRubyObject(value); } else { var = new GlobalVariable(receiver, name, value); vars.update(name, var); } }
/** * Associates the specified value with the specified key in this map. * The values is a simple Java object. If the map previously contained a mapping for * the key, the old value is replaced by the specified value. * * @param receiver a receiver object to associate a given key-value pair with * @param key the key with which the specified value is to be associated * @param value a simple Java object to be associated with the specified key * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. */ public Object put(Object receiver, String key, Object value) { checkKey(key); final RubyObject robj = getReceiverObject(receiver); final String name = key.intern(); BiVariable var = getVariable(robj, name); Object oldValue = null; if ( var != null ) { // updates oldValue = var.getJavaObject(); var.setJavaObject(robj.getRuntime(), value); } else { // creates new value var = VariableInterceptor.getVariableInstance(provider.getLocalVariableBehavior(), robj, name, value); if ( var != null ) update(name, var); } return oldValue; }
/** * Removes all of the mappings from this map. * The map will be empty after this call returns. Ruby variables are also * removed from Ruby instance. However, Ruby instance keep having global variable * names with null value. */ @Override public void clear() { if ( variables == null ) return; BiVariable argv = null; for ( BiVariable var : getVariables() ) { if ( var != null ) { if ( "ARGV".equals(var.getName()) ) { argv = var; } else { var.remove(); } } } getNames().clear(); getVariables().clear(); if ( argv != null ) update("ARGV", argv); }
/** * Returns the value in simple Java object to which the specified receiver * and key is mapped, or {@code null} if this map contains no mapping * for the key in a given receiver. * * @param receiver is a receiver object to get the value from * @param key is the key whose associated value is to be returned * @return the value in simple Java object to which the specified key is mapped, or * {@code null} if this map contains no mapping for the key */ public Object get(Object receiver, Object key) { checkKey(key); final RubyObject robj = getReceiverObject(receiver); // attemps to retrieve global variables if ( isLazy() ) { VariableInterceptor.tryLazyRetrieval(provider.getLocalVariableBehavior(), this, robj, key); } BiVariable var = getVariable(robj, (String) key); return var == null ? null : var.getJavaObject(); }
@Override public String toString() { final StringBuilder str = new StringBuilder(); str.append( getClass().getName() ); str.append('{'); for ( int i = 0; i < size(); i++ ) { final String name = getNames().get(i); final BiVariable variable = getVariables().get(i); str.append(name).append('=').append(variable.getJavaObject()); if ( i == size() - 1 ) break; str.append(',').append(' '); } return str.append('}').toString(); }
/** * Returns the value in simple Java object to which the specified receiver * and key is mapped, or {@code null} if this map contains no mapping * for the key in a given receiver. * * @param receiver is a receiver object to get the value from * @param key is the key whose associated value is to be returned * @return the value in simple Java object to which the specified key is mapped, or * {@code null} if this map contains no mapping for the key */ public V get(Object receiver, Object key) { checkKey(key); RubyObject robj = getReceiverObject(receiver); // attemps to retrieve global variables if (lazy) VariableInterceptor.tryLazyRetrieval(provider.getLocalVariableBehavior(), this, robj, key); BiVariable var = getVariable(robj, (String)key); if (var == null) return null; else return (V) var.getJavaObject(); }
public IRubyObject getInstanceVariable(IRubyObject obj, String variableName) { BiVariableMap map = container.getVarMap(); synchronized (map) { if (map.containsKey(variableName)) { BiVariable bv = map.getVariable((RubyObject)container.getProvider().getRuntime().getTopSelf(), variableName); return bv.getRubyObject(); } } return null; }
/** * Injects variable values from Java to Ruby just before an evaluation or * method invocation. * * @param map a variable map that has name-value pairs to be injected * @param runtime Ruby runtime * @param scope scope to inject local variable values * @param depth depth of a frame to inject local variable values * @param receiver a receiver when the script has been evaluated once */ public static void inject(BiVariableMap map, Ruby runtime, ManyVarsDynamicScope scope, int depth, IRubyObject receiver) { // lvar might not be given while parsing but be given when evaluating. // to avoid ArrayIndexOutOfBoundsException, checks the length of scope.getValues() if (scope != null && scope.getValues().length > 0) { IRubyObject[] values4Injection = map.getLocalVarValues(); if (values4Injection != null && values4Injection.length > 0) { for (int i = 0; i < values4Injection.length; i++) { scope.setValue(i, values4Injection[i], depth); } } } List<BiVariable> variables = map.getVariables(); if (variables == null) return; for (int i=0; i<variables.size(); i++) { variables.get(i).inject(); } }
/** * Removes the mapping for a key from this map if it is present in a given * receiver. * * <p>Returns the value to which this map previously associated the key, * or <tt>null</tt> if the map contained no mapping for the key. * @param key the key whose mapping is to be removed from the map * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. */ public Object removeFrom(final Object receiver, final Object key) { if ( variables == null ) return null; checkKey(key); final RubyObject robj = getReceiverObject(receiver); for ( int i = 0; i < size(); i++ ) { if ( ((String) key).equals( varNames.get(i) ) ) { final BiVariable var = variables.get(i); if ( var.isReceiverIdentical(robj) ) { varNames.remove(i); variables.remove(i); return var.getJavaObject(); } } } return null; }
public void remove() { if (attributes != null) { synchronized(this) { attributes.clear(); } } if (varMap != null) { synchronized(this) { varMap.clear(); } } }
/** * Returns the value in simple Java object to which the specified key is mapped, * or {@code null} if this map contains no mapping for the key. * * @param key is the key whose associated value is to be returned * @return the value in simple Java object to which the specified key is mapped, or * {@code null} if this map contains no mapping for the key */ public V get(Object key) { return get(null, key); }
static void postEval(ScriptingContainer container, ScriptContext context) { if (context == null) return; Object receiver = Utils.getReceiverObject(context); Bindings engineMap = context.getBindings(ScriptContext.ENGINE_SCOPE); Iterator<Map.Entry<String, Object>> iter = engineMap.entrySet().iterator(); for (;iter.hasNext();) { Map.Entry<String, Object> entry = iter.next(); if (Utils.shouldLVarBeDeleted(container, entry.getKey())) { iter.remove(); } } Set<String> keys = container.getVarMap().keySet(); if (keys != null && keys.size() > 0) { for (String key : keys) { Object value = container.getVarMap().get(key); engineMap.put(Utils.adjustKey(key), value); } } Bindings globalMap = context.getBindings(ScriptContext.GLOBAL_SCOPE); if (globalMap == null) return; keys = globalMap.keySet(); if (keys != null && keys.size() > 0) { for (String key : keys) { if (engineMap.containsKey(key)) continue; Object value = container.getVarMap().get(receiver, key); globalMap.put(key, value); } } }
/** * Removes the mapping for a key from this map if it is present in a given * receiver. * * <p>Returns the value to which this map previously associated the key, * or <tt>null</tt> if the map contained no mapping for the key. * @param key the key whose mapping is to be removed from the map * @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. */ public V removeFrom(Object receiver, Object key) { if (varNames == null) return null; checkKey(key); RubyObject robj = getReceiverObject(receiver); String name = ((String)key).intern(); for (int i=0; i<varNames.size(); i++) { if (name.equals(varNames.get(i))) { BiVariable var = variables.get(i); if (var.getReceiver() == robj) { varNames.remove(i); BiVariable v = variables.remove(i); v.remove(); return (V)v.getJavaObject(); } } } return null; }
@Override public ScriptExecutionResult execute(GameScript<EmbedEvalUnit> s, ScriptBindings bindings, boolean returnResult) throws Exception { PerThreadGameScript<EmbedEvalUnit> script = (PerThreadGameScript<EmbedEvalUnit>) s; ScriptingContainer scriptingContainer = executorPool.getLocalScriptingContainer(); scriptingContainer.getVarMap().putAll(bindings); if (!script.hasScript()) { script.setScript(scriptingContainer.parse(script.getContent())); } try { EmbedEvalUnit embedEvalUnit = script.getScript(); embedEvalUnit.run(); } catch (Exception e) { if(e.getCause() instanceof ScriptSkippedException) { throw new ScriptSkippedException(); } else { throw e; } } if (!returnResult) { scriptingContainer.clear(); return null; } ScriptExecutionResult executionResult = new ScriptExecutionResult(scriptingContainer.getVarMap().getMap()); scriptingContainer.clear(); return executionResult; }