Skip to content

Commit

Permalink
Reduce number of method combinations used in process_def
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Oct 21, 2013
1 parent 56f54b5 commit c91d3f0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 26 deletions.
21 changes: 15 additions & 6 deletions corelib/runtime.js
Expand Up @@ -668,6 +668,9 @@
if (obj === RubyBasicObject) {
define_basic_object_method(jsid, body);
}
else if (obj === RubyObject) {
Opal.donate(obj, [jsid]);
}
}
else {
obj[jsid] = body;
Expand All @@ -676,6 +679,18 @@
return nil;
};

/*
* Define a singleton method on the given object.
*/
Opal.defs = function(obj, jsid, body) {
if (obj._isClass || obj.__mod__) {
obj.constructor.prototype[jsid] = body;
}
else {
obj[jsid] = body;
}
};

function define_basic_object_method(jsid, body) {
for (var i = 0, len = bridged_classes.length; i < len; i++) {
bridged_classes[i]._proto[jsid] = body;
Expand Down Expand Up @@ -709,12 +724,6 @@
RubyModule._super = RubyObject;
RubyClass._super = RubyModule;

// Defines methods onto Object (which are then donated to bridged classes)
RubyObject._defn = function (mid, body) {
this._proto[mid] = body;
Opal.donate(this, [mid]);
};

// Internally, Object acts like a module as it is "included" into bridged
// classes. In other words, we donate methods from Object into our bridged
// classes as their prototypes don't inherit from our root Object, so they
Expand Down
24 changes: 4 additions & 20 deletions lib/opal/parser.rb
Expand Up @@ -1091,12 +1091,7 @@ def process_defs(sexp, level)
def js_def(recvr, mid, args, stmts, line, end_line, sexp, level)
jsid = mid_to_jsid mid.to_s

if recvr
smethod = true if @scope.class_scope? && recvr.first == :self
recv = process(recvr)
else
recv = 'self'
end
recvr = process(recvr) if recvr

code = []
params = nil
Expand Down Expand Up @@ -1190,23 +1185,12 @@ def js_def(recvr, mid, args, stmts, line, end_line, sexp, level)
result << f("\n#@indent}", sexp)

def_code = if recvr
if smethod
[f("self.constructor.prototype['$#{mid}'] = ", sexp), result]
else
[recv, f("#{jsid} = ", sexp), result]
end
elsif @scope.class? and @scope.name == 'Object'
[f("self._defn('$#{mid}', ", sexp), result, f(")", sexp)]
elsif @scope.class? and @scope.name == 'BasicObject'
[f("$opal.defs("), recvr, f(", '$#{mid}', "), result, f(")")]
elsif @scope.class? and %w(Object BasicObject).include?(@scope.name)
[f("$opal.defn(self, '$#{mid}', "), result, f(")")]
elsif @scope.class_scope?
@scope.methods << "$#{mid}"
if uses_super
@scope.add_temp uses_super
uses_super = "#{uses_super} = #{@scope.proto}#{jsid},\n#@indent"
end

[f("#{uses_super}#{@scope.proto}#{jsid} = ", sexp), result]
[f("#{@scope.proto}#{jsid} = ", sexp), result]
elsif @scope.iter?
[f("$opal.defn(self, '$#{mid}', "), result, f(")")]
elsif @scope.type == :sclass
Expand Down

0 comments on commit c91d3f0

Please sign in to comment.