Skip to content

Commit

Permalink
Subclasses should inherit contants from superclasses, and modules should
Browse files Browse the repository at this point in the history
donate constans to their target module/class.
  • Loading branch information
adambeynon committed Oct 12, 2013
1 parent d4d68fc commit c7a1b52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions corelib/module.rb
Expand Up @@ -123,6 +123,8 @@ def append_features(klass)
if (klass.__dep__) {
$opal.donate(klass, methods.slice(), true);
}
$opal.donate_constants(module, klass);
}

self
Expand Down
21 changes: 17 additions & 4 deletions corelib/runtime.js
Expand Up @@ -149,10 +149,8 @@
base[id] = base._scope[id] = klass;

// Copy all parent constants to child, unless parent is Object
if (superklass !== RubyObject) {
for (var i = 0, len = superklass._scope.constants.length; i < len; i++) {
klass._scope.constants.push(superklass._scope.constants[i]);
}
if (superklass !== RubyObject && superklass !== RubyBasicObject) {
Opal.donate_constants(superklass, klass);
}

// call .inherited() hook with new class on the superclass
Expand Down Expand Up @@ -346,6 +344,21 @@
return base_scope[name] = value;
};

/*
* When a source module is included into the target module, we must also copy
* its constants to the target.
*/
Opal.donate_constants = function(source_mod, target_mod) {
var source_constants = source_mod._scope.constants,
target_scope = target_mod._scope,
target_constants = target_scope.constants;

for (var i = 0, length = source_constants.length; i < length; i++) {
target_constants.push(source_constants[i]);
target_scope[source_constants[i]] = source_mod._scope[source_constants[i]];
}
};

/*
* Methods stubs are used to facilitate method_missing in opal. A stub is a
* placeholder function which just calls `method_missing` on the receiver.
Expand Down

0 comments on commit c7a1b52

Please sign in to comment.