Skip to content

Commit b22350c

Browse files
committedDec 25, 2013
Support String inheritance
1 parent b15ea9f commit b22350c

File tree

3 files changed

+87
-29
lines changed

3 files changed

+87
-29
lines changed
 

‎opal/corelib/string.rb

+86-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ class String
33

44
`def._isString = true`
55

6+
def self.inherited(klass)
7+
replace = Class.new(String::Wrapper)
8+
9+
%x{
10+
klass._proto = replace._proto;
11+
klass._proto._klass = klass;
12+
klass._alloc = replace._alloc;
13+
klass.__parent = #{String::Wrapper};
14+
15+
klass.$allocate = replace.$allocate;
16+
klass.$new = replace.$new;
17+
}
18+
end
19+
620
def self.try_convert(what)
721
what.to_str
822
rescue
@@ -69,9 +83,12 @@ def <=>(other)
6983
end
7084

7185
def ==(other)
72-
`!!(other._isString && self.valueOf() === other.valueOf())`
86+
return false unless String === other
87+
88+
`#{to_s} == #{other.to_s}`
7389
end
7490

91+
alias eql? ==
7592
alias === ==
7693

7794
def =~(other)
@@ -767,8 +784,8 @@ def sum(n = 16)
767784
%x{
768785
var result = 0;
769786
770-
for (var i = 0, length = #{self}.length; i < length; i++) {
771-
result += (#{self}.charCodeAt(i) % ((1 << n) - 1));
787+
for (var i = 0, length = self.length; i < length; i++) {
788+
result += (self.charCodeAt(i) % ((1 << n) - 1));
772789
}
773790
774791
return result;
@@ -840,7 +857,7 @@ def to_proc
840857
end
841858

842859
def to_s
843-
`#{self}.toString()`
860+
`self.toString()`
844861
end
845862

846863
alias to_str to_s
@@ -1149,3 +1166,68 @@ def frozen?
11491166
end
11501167

11511168
Symbol = String
1169+
1170+
class String::Wrapper
1171+
def self.allocate(string = "")
1172+
obj = super()
1173+
`obj.literal = string`
1174+
obj
1175+
end
1176+
1177+
def self.new(*args, &block)
1178+
obj = allocate
1179+
obj.initialize(*args, &block)
1180+
obj
1181+
end
1182+
1183+
def self.[](*objects)
1184+
allocate(objects)
1185+
end
1186+
1187+
def initialize(string = '')
1188+
@literal = string
1189+
end
1190+
1191+
def method_missing(*args, &block)
1192+
result = @literal.__send__(*args, &block)
1193+
1194+
if `result._isString != null`
1195+
if `result == #@literal`
1196+
self
1197+
else
1198+
self.class.allocate(result)
1199+
end
1200+
else
1201+
result
1202+
end
1203+
end
1204+
1205+
def initialize_copy(other)
1206+
@literal = `other.literal`.clone
1207+
end
1208+
1209+
def respond_to?(name, *)
1210+
super || @literal.respond_to?(name)
1211+
end
1212+
1213+
def ==(other)
1214+
@literal == other
1215+
end
1216+
1217+
alias eql? ==
1218+
alias === ==
1219+
1220+
def to_s
1221+
@literal
1222+
end
1223+
1224+
def to_str
1225+
self
1226+
end
1227+
1228+
def inspect
1229+
@literal.inspect
1230+
end
1231+
1232+
# unwrapped results
1233+
end

‎spec/opal/filters/bugs/string.rb

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
fails "String#split with Regexp returns a type error if limit can't be converted to an integer"
178178
fails "String#split with Regexp returns subclass instances based on self"
179179
fails "String#split with Regexp does not call constructor on created subclass instances"
180+
fails "String#split with String does not call constructor on created subclass instances"
180181

181182
fails "String#squeeze negates sets starting with ^"
182183
fails "String#squeeze squeezes all chars in a sequence"

‎spec/opal/filters/unsupported/string_subclasses.rb

-25
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.