Skip to content

Commit

Permalink
Generate named function inside class body with '$' prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
adambeynon committed Nov 13, 2013
1 parent 8c1ce51 commit 1765c8c
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 21 deletions.
8 changes: 4 additions & 4 deletions lib/opal/nodes/class.rb
Expand Up @@ -12,13 +12,13 @@ def compile
helper :klass

push "(function($base, $super) {"
line " function #{name}(){};"
line " var self = #{name} = $klass($base, $super, '#{name}', #{name});"
line " function $#{name}(){};"
line " var self = $#{name} = $klass($base, $super, '#{name}', $#{name});"

in_scope(:class) do
scope.name = name
add_temp "#{scope.proto} = #{name}._proto"
add_temp "$scope = #{name}._scope"
add_temp "#{scope.proto} = $#{name}._proto"
add_temp "$scope = $#{name}._scope"

body_code = self.body_code
empty_line
Expand Down
2 changes: 1 addition & 1 deletion lib/opal/nodes/super.rb
Expand Up @@ -18,7 +18,7 @@ def compile_dispatcher
if scope.def?
scope.uses_block!
scope_name = scope.identify!
class_name = scope.parent.name || 'self._klass._proto'
class_name = scope.parent.name ? "$#{scope.parent.name}" : 'self._klass._proto'

if scope.defs
push "$opal.find_super_dispatcher(self, '#{scope.mid.to_s}', #{scope_name}, "
Expand Down
2 changes: 1 addition & 1 deletion opal/core/hash.rb
Expand Up @@ -664,7 +664,7 @@ def to_a

def to_h
%x{
var hash = new Hash._alloc,
var hash = new Opal.Hash._alloc,
cloned = #{clone};
hash.map = cloned.map;
Expand Down
6 changes: 3 additions & 3 deletions opal/core/module.rb
Expand Up @@ -2,15 +2,15 @@ class Module
def self.new(&block)
%x{
function AnonModule(){}
var klass = Opal.boot(Module, AnonModule);
var klass = Opal.boot(Opal.Module, AnonModule);
klass._name = nil;
klass._klass = Module;
klass._klass = Opal.Module;
klass.__dep__ = []
klass.__mod__ = true;
klass._proto = {};
// inherit scope from parent
$opal.create_scope(Module._scope, klass);
$opal.create_scope(Opal.Module._scope, klass);
if (block !== nil) {
var block_self = block._s;
Expand Down
2 changes: 1 addition & 1 deletion opal/core/range.rb
@@ -1,7 +1,7 @@
class Range
include Enumerable

`Range._proto._isRange = true;`
`def._isRange = true;`

attr_reader :begin, :end

Expand Down
21 changes: 10 additions & 11 deletions opal/core/string.rb
Expand Up @@ -2,7 +2,6 @@ class String
include Comparable

`def._isString = true`
`var native_string = "".constructor;`

def self.try_convert(what)
what.to_str
Expand All @@ -11,7 +10,7 @@ def self.try_convert(what)
end

def self.new(str = '')
`new native_string(str)`
`new String(str)`
end

def %(data)
Expand Down Expand Up @@ -439,7 +438,7 @@ def next
}
var initial = #{self}.substr(0, #{self}.length - 1);
var last = native_string.fromCharCode(#{self}.charCodeAt(#{self}.length - 1) + 1);
var last = String.fromCharCode(#{self}.charCodeAt(#{self}.length - 1) + 1);
return initial + last;
}
Expand All @@ -466,7 +465,7 @@ def reverse
def rindex(search, offset = undefined)
%x{
var search_type = (search == null ? Opal.NilClass : search.constructor);
if (search_type != native_string && search_type != RegExp) {
if (search_type != String && search_type != RegExp) {
var msg = "type mismatch: " + search_type + " given";
#{raise TypeError.new(`msg`)};
}
Expand All @@ -481,7 +480,7 @@ def rindex(search, offset = undefined)
offset = #{self}.length + offset;
}
if (search_type == native_string) {
if (search_type == String) {
result = #{self}.lastIndexOf(search, offset);
}
else {
Expand All @@ -492,7 +491,7 @@ def rindex(search, offset = undefined)
}
}
else {
if (search_type == native_string) {
if (search_type == String) {
result = #{self}.lastIndexOf(search);
}
else {
Expand Down Expand Up @@ -658,7 +657,7 @@ def swapcase
return $1 ? $0.toUpperCase() : $0.toLowerCase();
});
if (#{self}.constructor === native_string) {
if (#{self}.constructor === String) {
return str;
}
Expand Down Expand Up @@ -761,7 +760,7 @@ def tr(from, to)
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
from_chars_expanded.push(native_string.fromCharCode(c));
from_chars_expanded.push(String.fromCharCode(c));
}
from_chars_expanded.push(char);
in_range = null;
Expand Down Expand Up @@ -807,7 +806,7 @@ def tr(from, to)
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
to_chars_expanded.push(native_string.fromCharCode(c));
to_chars_expanded.push(String.fromCharCode(c));
}
to_chars_expanded.push(char);
in_range = null;
Expand Down Expand Up @@ -896,7 +895,7 @@ def tr_s(from, to)
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
from_chars_expanded.push(native_string.fromCharCode(c));
from_chars_expanded.push(String.fromCharCode(c));
}
from_chars_expanded.push(char);
in_range = null;
Expand Down Expand Up @@ -942,7 +941,7 @@ def tr_s(from, to)
var start = last_from.charCodeAt(0) + 1;
var end = char.charCodeAt(0);
for (var c = start; c < end; c++) {
to_chars_expanded.push(native_string.fromCharCode(c));
to_chars_expanded.push(String.fromCharCode(c));
}
to_chars_expanded.push(char);
in_range = null;
Expand Down

0 comments on commit 1765c8c

Please sign in to comment.