@JRubyMethod public IRubyObject local_variable_set(ThreadContext context, IRubyObject symbol, IRubyObject value) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) { // Yay! New variable associated with this binding slot = evalScope.getStaticScope().addVariable(name.intern()); evalScope.growIfNeeded(); } return evalScope.setValue(slot & 0xffff, value, slot >> 16); }
@JRubyMethod public IRubyObject local_variable_set(ThreadContext context, IRubyObject symbol, IRubyObject value) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) { // Yay! New variable associated with this binding slot = evalScope.getStaticScope().addVariable(name.intern()); evalScope.growIfNeeded(); } return evalScope.setValue(slot & 0xffff, value, slot >> 16); }
@JRubyMethod public IRubyObject local_variables(ThreadContext context) { final Ruby runtime = context.runtime; HashSet<String> encounteredLocalVariables = new HashSet<>(); RubyArray allLocalVariables = runtime.newArray(); DynamicScope currentScope = binding.getEvalScope(context.runtime); while (currentScope != null) { for (String name : currentScope.getStaticScope().getVariables()) { if (IdUtil.isLocal(name) && !encounteredLocalVariables.contains(name)) { allLocalVariables.push(runtime.newSymbol(name)); encounteredLocalVariables.add(name); } } currentScope = currentScope.getParentScope(); } return allLocalVariables; }
@JRubyMethod public IRubyObject local_variables(ThreadContext context) { final Ruby runtime = context.runtime; HashSet<String> encounteredLocalVariables = new HashSet<>(); RubyArray allLocalVariables = runtime.newArray(); DynamicScope currentScope = binding.getEvalScope(context.runtime); while (currentScope != null) { for (String name : currentScope.getStaticScope().getVariables()) { if (IdUtil.isLocal(name) && !encounteredLocalVariables.contains(name)) { allLocalVariables.push(runtime.newSymbol(name)); encounteredLocalVariables.add(name); } } currentScope = currentScope.getParentScope(); } return allLocalVariables; }
@JRubyMethod public IRubyObject local_variable_get(ThreadContext context, IRubyObject symbol) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) throw context.runtime.newNameError("local variable `" + name + "' not defined for " + inspect(), name); return evalScope.getValueOrNil(slot & 0xffff, slot >> 16, context.nil); }
@JRubyMethod(name = "local_variable_defined?") public IRubyObject local_variable_defined_p(ThreadContext context, IRubyObject symbol) { return context.runtime.newBoolean(binding.getEvalScope(context.runtime).getStaticScope().isDefined(symbol.asJavaString()) != -1); }
@JRubyMethod public IRubyObject local_variable_get(ThreadContext context, IRubyObject symbol) { String name = symbol.asJavaString().intern(); DynamicScope evalScope = binding.getEvalScope(context.runtime); int slot = evalScope.getStaticScope().isDefined(name); if (slot == -1) throw context.runtime.newNameError("local variable `" + name + "' not defined for " + inspect(), name); return evalScope.getValueOrNil(slot & 0xffff, slot >> 16, context.nil); }
@JRubyMethod(name = "local_variable_defined?") public IRubyObject local_variable_defined_p(ThreadContext context, IRubyObject symbol) { return context.runtime.newBoolean(binding.getEvalScope(context.runtime).getStaticScope().isDefined(symbol.asJavaString()) != -1); }
/** * Evaluate the given string under the specified binding object. If the binding is not a Proc or Binding object * (RubyProc or RubyBinding) throw an appropriate type error. * @param context the thread context for the current thread * @param self the self against which eval was called; used as self in the eval in 1.9 mode * @param src The string containing the text to be evaluated * @param binding The binding object under which to perform the evaluation * @return An IRubyObject result from the evaluation */ public static IRubyObject evalWithBinding(ThreadContext context, IRubyObject self, IRubyObject src, Binding binding) { Ruby runtime = context.runtime; DynamicScope evalScope = binding.getEvalScope(runtime); evalScope.getStaticScope().determineModule(); // FIXME: It would be nice to just set this or remove it from staticScope altogether Frame lastFrame = context.preEvalWithBinding(binding); try { return evalCommon(context, evalScope, self, src, binding.getFile(), binding.getLine(), binding.getMethod(), binding.getFrame().getBlock(), EvalType.BINDING_EVAL); } finally { context.postEvalWithBinding(binding, lastFrame); } }
/** * Evaluate the given string under the specified binding object. If the binding is not a Proc or Binding object * (RubyProc or RubyBinding) throw an appropriate type error. * @param context the thread context for the current thread * @param self the self against which eval was called; used as self in the eval in 1.9 mode * @param src The string containing the text to be evaluated * @param binding The binding object under which to perform the evaluation * @return An IRubyObject result from the evaluation */ public static IRubyObject evalWithBinding(ThreadContext context, IRubyObject self, IRubyObject src, Binding binding) { Ruby runtime = context.runtime; DynamicScope evalScope = binding.getEvalScope(runtime); evalScope.getStaticScope().determineModule(); // FIXME: It would be nice to just set this or remove it from staticScope altogether Frame lastFrame = context.preEvalWithBinding(binding); try { return evalCommon(context, evalScope, self, src, binding.getFile(), binding.getLine(), binding.getMethod(), binding.getFrame().getBlock(), EvalType.BINDING_EVAL); } finally { context.postEvalWithBinding(binding, lastFrame); } }
evalScope = binding.getEvalScope(runtime); } else {
evalScope = binding.getEvalScope(runtime); } else {