Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2416cd367d34
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a2b34efdc033
Choose a head ref
  • 4 commits
  • 3 files changed
  • 1 contributor

Commits on May 12, 2015

  1. Copy the full SHA
    8a3b1ae View commit details
  2. Copy the full SHA
    445a63f View commit details
  3. Copy the full SHA
    a4e83bf View commit details
  4. Copy the full SHA
    a2b34ef View commit details
Showing with 55 additions and 2 deletions.
  1. +52 −0 core/src/main/java/org/jruby/RubyBinding.java
  2. +1 −2 test/mri.index
  3. +2 −0 test/mri/excludes/TestProc.rb
52 changes: 52 additions & 0 deletions core/src/main/java/org/jruby/RubyBinding.java
Original file line number Diff line number Diff line change
@@ -33,15 +33,18 @@
***** END LICENSE BLOCK *****/
package org.jruby;

import java.util.HashSet;
import org.jruby.anno.JRubyClass;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Binding;
import org.jruby.runtime.Block;
import org.jruby.runtime.ClassIndex;
import org.jruby.runtime.DynamicScope;
import org.jruby.runtime.ObjectAllocator;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.Visibility;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.IdUtil;

/**
* @author jpetersen
@@ -136,6 +139,55 @@ public IRubyObject eval(ThreadContext context, IRubyObject[] args) {
return RubyKernel.eval(context, this, newArgs, Block.NULL_BLOCK);
}

@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.getValue(slot & 0xffff, 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(name = "receiver")
public IRubyObject receiver(ThreadContext context) {
3 changes: 1 addition & 2 deletions test/mri.index
Original file line number Diff line number Diff line change
@@ -66,8 +66,7 @@ ruby/test_parse.rb
ruby/test_path.rb
ruby/test_pipe.rb
ruby/test_primitive.rb
# TODO 2.1 parser
#ruby/test_proc.rb
ruby/test_proc.rb
# hangs when run with other tests
ruby/test_process.rb
ruby/test_rand.rb
2 changes: 2 additions & 0 deletions test/mri/excludes/TestProc.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
exclude :test_arity, "needs investigation"
exclude :test_arity2, "needs investigation"
exclude :test_attr_source_location, "needs investigation"
exclude :test_bound_parameters, "needs investigation"
exclude :test_curry, "needs investigation"
exclude :test_curry_from_knownbug, "needs investigation"
exclude :test_curry_optional_params, "needs investigation"