Skip to content

Commit

Permalink
[Truffle] Implement Module#extend_object and Module#extended.
Browse files Browse the repository at this point in the history
* Fixes #2804.
  • Loading branch information
eregon committed Apr 9, 2015
1 parent ab47c74 commit 2bd3f4c
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 30 deletions.
Expand Up @@ -669,30 +669,6 @@ public RubyNilClass exit(int exitCode) {

}

@CoreMethod(names = "extend", argumentsAsArray = true, required = 1)
public abstract static class ExtendNode extends CoreMethodNode {

public ExtendNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ExtendNode(ExtendNode prev) {
super(prev);
}

@Specialization
public RubyBasicObject extend(RubyBasicObject self, Object[] args) {
notDesignedForCompilation();

for (int n = 0; n < args.length; n++) {
self.extend((RubyModule) args[n], this);
}

return self;
}

}

@CoreMethod(names = "fork", isModuleFunction = true, argumentsAsArray = true)
public abstract static class ForkNode extends CoreMethodNode {

Expand Down
Expand Up @@ -999,6 +999,27 @@ private RubySymbol defineMethod(RubyModule module, String name, RubyProc proc) {

}

@CoreMethod(names = "extend_object", required = 1)
public abstract static class ExtendObjectNode extends CoreMethodNode {

public ExtendObjectNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public ExtendObjectNode(ExtendObjectNode prev) {
super(prev);
}

@Specialization
public RubyBasicObject extendObject(RubyModule module, RubyBasicObject object) {
notDesignedForCompilation();

object.getSingletonClass(this).include(this, module);
return module;
}

}

@CoreMethod(names = "initialize", needsBlock = true)
public abstract static class InitializeNode extends CoreMethodNode {

Expand Down
Expand Up @@ -157,11 +157,6 @@ public Object[] getFieldNames() {
return getOperations().getFieldNames(this);
}

public void extend(RubyModule module, Node currentNode) {
RubyNode.notDesignedForCompilation();
getSingletonClass(currentNode).include(currentNode, module);
}

public Object getInstanceVariable(String name) {
RubyNode.notDesignedForCompilation();

Expand Down
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core.rb
Expand Up @@ -86,7 +86,7 @@
require_relative 'core/rubinius/common/string_mirror'
#require_relative 'core/rubinius/common/class'
#require_relative 'core/rubinius/common/autoload'
#require_relative 'core/rubinius/common/module'
require_relative 'core/rubinius/common/module'
#require_relative 'core/rubinius/common/binding'
require_relative 'core/rubinius/common/proc'
require_relative 'core/rubinius/common/enumerable'
Expand Down
16 changes: 16 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/kernel.rb
Expand Up @@ -170,6 +170,22 @@ def define_singleton_method(*args, &block)
singleton_class.send(:define_method, *args, &block)
end

def extend(*modules)
raise ArgumentError, "wrong number of arguments (0 for 1+)" if modules.empty?
Rubinius.check_frozen

modules.reverse_each do |mod|
Rubinius.privately do
mod.extend_object self
end

Rubinius.privately do
mod.extended self
end
end
self
end

def itself
self
end
Expand Down
47 changes: 47 additions & 0 deletions truffle/src/main/ruby/core/rubinius/common/module.rb
@@ -0,0 +1,47 @@
# Copyright (c) 2007-2014, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Only part of Rubinius' module.rb

##
# Some terminology notes:
#
# [Encloser] The Class or Module inside which this one is defined or, in the
# event we are at top-level, Object.
#
# [Direct superclass] Whatever is next in the chain of superclass invocations.
# This may be either an included Module, a Class or nil.
#
# [Superclass] The real semantic superclass and thus only applies to Class
# objects.

class Module

def extended(name)
end
private :extended

end

0 comments on commit 2bd3f4c

Please sign in to comment.