Skip to content

Commit 40f7f00

Browse files
committed
Use $$constructor instead of constructor
Standardize the access to the class' constructor, allowing it to be different from the class constructor function itself.
1 parent 4e40d0a commit 40f7f00

File tree

6 files changed

+13
-9
lines changed

6 files changed

+13
-9
lines changed

opal/corelib/array.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,9 @@ def ==(other)
237237
}
238238
}
239239
240-
if (array.constructor !== Array)
240+
if (array.$$constructor !== Array)
241241
array = #{`array`.to_a};
242-
if (other.constructor !== Array)
242+
if (other.$$constructor !== Array)
243243
other = #{`other`.to_a};
244244
245245
if (array.length !== other.length) {

opal/corelib/class.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def self.new(superclass = Object, &block)
1616

1717
def allocate
1818
%x{
19-
var obj = new self();
19+
var obj = new self.$$constructor();
2020
obj.$$id = Opal.uid();
2121
return obj;
2222
}

opal/corelib/error.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Exception < `Error`
55
def self.new(*args)
66
%x{
77
var message = (args.length > 0) ? args[0] : nil;
8-
var error = new self(message);
8+
var error = new self.$$constructor(message);
99
error.name = self.$$name;
1010
error.message = message;
1111
Opal.send(error, error.$initialize, args);

opal/corelib/hash.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def self.[](*argv)
6565

6666
def self.allocate
6767
%x{
68-
var hash = new self();
68+
var hash = new self.$$constructor();
6969
7070
Opal.hash_init(hash);
7171

opal/corelib/runtime.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
// calling original JS constructors
407407
constructor = function() {
408408
var args = $slice.call(arguments),
409-
self = new ($bind.apply(superclass, [null].concat(args)))();
409+
self = new ($bind.apply(superclass.$$constructor, [null].concat(args)))();
410410

411411
// and replacing a __proto__ manually
412412
$setPrototype(self, klass.$$prototype);
@@ -420,6 +420,7 @@
420420
$defineProperty(constructor, 'displayName', '::'+name);
421421

422422
$defineProperty(klass, '$$name', name);
423+
$defineProperty(klass, '$$constructor', constructor);
423424
$defineProperty(klass, '$$prototype', constructor.prototype);
424425
$defineProperty(klass, '$$const', {});
425426
$defineProperty(klass, '$$is_class', true);
@@ -1186,6 +1187,9 @@
11861187
$defineProperty(constructor, '$$ancestors', []);
11871188
$defineProperty(constructor, '$$ancestors_cache_version', null);
11881189
$setPrototype(constructor, Opal.Class.prototype);
1190+
$defineProperty(constructor, '$$bridge', klass);
1191+
$defineProperty(klass, 'constructor', constructor);
1192+
$defineProperty(klass, '$$constructor', constructor);
11891193
};
11901194

11911195
function protoToModule(proto) {

opal/corelib/string.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class String < `String`
99
1010
Opal.defineProperty(#{self}.$$prototype, '$$cast', function(string) {
1111
var klass = this.$$class;
12-
if (klass === String) {
12+
if (klass.$$constructor === String) {
1313
return string;
1414
} else {
15-
return new klass(string);
15+
return new klass.$$constructor(string);
1616
}
1717
});
1818
}
@@ -29,7 +29,7 @@ def self.try_convert(what)
2929

3030
def self.new(str = '')
3131
str = Opal.coerce_to(str, String, :to_str)
32-
`new self(str)`
32+
`new self.$$constructor(str)`
3333
end
3434

3535
def initialize(str = undefined)

0 commit comments

Comments
 (0)