Permalink
Browse files

Merge pull request #2 from omghax/fiximport

Fixed Sandbox#import.
  • Loading branch information...
2 parents 8b50475 + 8d3377e commit a1b8c2b52273d503ba62882fa2bf79ef2016cb03 @omghax committed Sep 12, 2011
Showing with 49 additions and 36 deletions.
  1. +1 −1 Gemfile
  2. +19 −8 ext/java/sandbox/SandboxFull.java
  3. +29 −27 spec/sandbox_spec.rb
View
@@ -4,4 +4,4 @@ source "http://rubygems.org"
gemspec
# this fork has some recent work done for 1.9, like File.foreach
-gem 'fakefs', git: 'git://github.com/nanothief/fakefs.git', require: 'fakefs/safe'
+gem 'fakefs', :git => 'git://github.com/nanothief/fakefs.git', :require => 'fakefs/safe'
@@ -96,7 +96,6 @@ private RubyModule importClassPath(String path, final boolean link) {
for (String name : path.split("::")) {
runtimeModule = (RubyModule) runtimeModule.getConstantAt(name);
-
// Create the module when it did not exist yet...
if (wrappedModule.const_defined_p(wrapped.getCurrentContext(), wrapped.newString(name)).isFalse()) {
// The BoxedClass takes the place of Object as top of the inheritance
@@ -106,27 +105,39 @@ private RubyModule importClassPath(String path, final boolean link) {
if (!link && runtimeModule instanceof RubyClass) {
// If we're importing a class, recursively import all of its
// superclasses as well.
- sup = importClassPath(runtimeModule.getSuperClass().getName(), link);
+ sup = importClassPath(runtimeModule.getSuperClass().getName(), true);
}
+
RubyClass klass = (RubyClass) sup;
if (wrappedModule == wrapped.getObject()) {
- wrappedModule = wrapped.defineClass(name, klass, klass.getAllocator());
+
+ if (link || runtimeModule instanceof RubyClass){ // if this is a ref and not an import
+ wrappedModule = wrapped.defineClass(name, klass, klass.getAllocator());
+ } else {
+ wrappedModule = wrapped.defineModule(name);
+ }
+
} else {
- wrappedModule = wrappedModule.defineClassUnder(name, klass, klass.getAllocator());
+ if (runtimeModule instanceof RubyClass){
+ wrappedModule = wrappedModule.defineClassUnder(name, klass, klass.getAllocator());
+ } else {
+ wrappedModule = wrappedModule.defineModuleUnder(name);
+ }
+
}
} else {
// ...or just resolve it, if it was already known
wrappedModule = (RubyModule) wrappedModule.getConstantAt(name);
}
-
+
// Check the consistency of the hierarchy
if (runtimeModule instanceof RubyClass) {
if (!link && !runtimeModule.getSuperClass().getName().equals(wrappedModule.getSuperClass().getName())) {
throw getRuntime().newTypeError("superclass mismatch for class " + runtimeModule.getSuperClass().getName());
}
}
- if (link) {
+ if (link || runtimeModule instanceof RubyClass) {
linkObject(runtimeModule, wrappedModule);
} else {
copyMethods(runtimeModule, wrappedModule);
@@ -137,8 +148,8 @@ private RubyModule importClassPath(String path, final boolean link) {
}
private void copyMethods(RubyModule from, RubyModule to) {
- to.getMethods().putAll(from.getMethods());
- to.getSingletonClass().getMethods().putAll(from.getSingletonClass().getMethods());
+ to.getMethodsForWrite().putAll(from.getMethods());
+ to.getSingletonClass().getMethodsForWrite().putAll(from.getSingletonClass().getMethods());
}
@JRubyMethod(required=2)
View
@@ -136,6 +136,35 @@
end
end
end
+
+ describe "#import" do
+ subject { Sandbox.new }
+
+ it "should be able to call a referenced namespaced module method" do
+ Foo = Class.new
+ Foo::Bar = Module.new do
+ def baz
+ 'baz'
+ end
+ module_function :baz
+ end
+
+ subject.import(Foo::Bar)
+ subject.eval('Foo::Bar.baz').should == 'baz'
+ end
+
+ it "should be able to include a module from the environment" do
+ Foo = Module.new do
+ def baz
+ 'baz'
+ end
+ end
+
+ subject.import(Foo)
+ subject.eval("class Bar; include Foo; end; nil")
+ subject.eval('Bar.new.baz').should == 'baz'
+ end
+ end
describe "#ref" do
subject { Sandbox.new }
@@ -167,33 +196,6 @@ def Foo.foo; 'baz'; end
subject.eval('Foo.foo').should == 'baz'
end
- it "should be able to call a referenced namespaced module method" do
- Foo = Class.new
- Foo::Bar = Module.new do
- def baz
- 'baz'
- end
- module_function :baz
- end
-
- subject.ref(Foo::Bar)
- subject.eval('Foo::Bar.baz').should == 'baz'
- end
-
- it "should be able to include a module from the environment" do
- pending 'insert BoxedObject into the sandbox object hierarchy?' do
- Foo = Module.new do
- def baz
- 'baz'
- end
- end
-
- subject.ref(Foo)
- subject.eval("class Bar\n include Foo\nend")
- subject.eval('Bar.new.baz').should == 'baz'
- end
- end
-
it "should be possible to call a method on the class that receives a block" do
Foo = Class.new do
def self.bar

0 comments on commit a1b8c2b

Please sign in to comment.