Skip to content

Commit 4d4082e

Browse files
committed
WIP, making generated code "use strict" compliant
1 parent 0815306 commit 4d4082e

File tree

5 files changed

+17
-14
lines changed

5 files changed

+17
-14
lines changed

lib/opal/nodes/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def variable(name)
4444
end
4545

4646
def valid_ivar_name?(name)
47-
not (PROTO_SPECIAL_PROPS =~ name or PROTO_SPECIAL_METHODS =~ name)
47+
not (PROTO_SPECIAL_PROPS =~ name or PROTO_SPECIAL_METHODS =~ name or /^length$/ =~ name)
4848
end
4949

5050
def ivar(name)

lib/opal/nodes/top.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def compile
1515

1616
opening
1717
in_scope do
18+
line '"use strict"'
19+
1820
body_code = stmt(stmts)
1921
body_code = [body_code] unless body_code.is_a?(Array)
2022

opal/corelib/array.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -924,17 +924,17 @@ def fill(*args, &block)
924924
raise TypeError, "length invalid with range" if two
925925

926926
left = Opal.coerce_to one.begin, Integer, :to_int
927-
`left += #@length` if `left < 0`
927+
`left += self.length` if `left < 0`
928928
raise RangeError, "#{one.inspect} out of range" if `left < 0`
929929

930930
right = Opal.coerce_to one.end, Integer, :to_int
931-
`right += #@length` if `right < 0`
931+
`right += self.length` if `right < 0`
932932
`right += 1` unless one.exclude_end?
933933

934934
return self if `right <= left`
935935
elsif one
936936
left = Opal.coerce_to one, Integer, :to_int
937-
`left += #@length` if `left < 0`
937+
`left += self.length` if `left < 0`
938938
left = 0 if `left < 0`
939939

940940
if two
@@ -944,35 +944,35 @@ def fill(*args, &block)
944944

945945
`right += left`
946946
else
947-
right = @length
947+
right = `self.length`
948948
end
949949
else
950950
left = 0
951-
right = @length
951+
right = `self.length`
952952
end
953953

954-
if `left > #@length`
954+
if `left > self.length`
955955
%x{
956-
for (i = #@length; i < right; i++) {
956+
for (i = self.length; i < right; i++) {
957957
self[i] = nil;
958958
}
959959
}
960960
end
961961

962-
if `right > #@length`
963-
@length = right
962+
if `right > self.length`
963+
`self.length = right`
964964
end
965965

966966
if block
967967
%x{
968-
for (length = #@length; left < right; left++) {
968+
for (length = self.length; left < right; left++) {
969969
value = block(left);
970970
self[left] = value;
971971
}
972972
}
973973
else
974974
%x{
975-
for (length = #@length; left < right; left++) {
975+
for (length = self.length; left < right; left++) {
976976
self[left] = #{obj};
977977
}
978978
}

opal/corelib/runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313
// The way the code is digested before going through Yardoc is a secret kept
1414
// in the docs repo (https://github.com/opal/docs/tree/master).
15+
"use strict"
1516

1617
if (typeof(this.Opal) !== 'undefined') {
1718
console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.');

opal/corelib/string.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ def center(width, padstr = ' ')
253253
return self if `width <= self.length`
254254

255255
%x{
256-
var ljustified = #{ljust ((width + @length) / 2).ceil, padstr},
257-
rjustified = #{rjust ((width + @length) / 2).floor, padstr};
256+
var ljustified = #{ljust ((width + `self.length`) / 2).ceil, padstr},
257+
rjustified = #{rjust ((width + `self.length`) / 2).floor, padstr};
258258
259259
return rjustified + ljustified.slice(self.length);
260260
}

0 commit comments

Comments
 (0)