@@ -4,6 +4,20 @@ class Array
4
4
# Mark all javascript arrays as being valid ruby arrays
5
5
`def._isArray = true`
6
6
7
+ def self . inherited ( klass )
8
+ replace = Class . new ( Array ::Wrapper )
9
+
10
+ %x{
11
+ klass._proto = replace._proto;
12
+ klass._proto._klass = klass;
13
+ klass._alloc = replace._alloc;
14
+ klass.__parent = #{ Array ::Wrapper } ;
15
+
16
+ klass.$new = replace.$new;
17
+ klass["$[]"] = replace["$[]"];
18
+ }
19
+ end
20
+
7
21
def self . []( *objects )
8
22
objects
9
23
end
@@ -1357,3 +1371,100 @@ def zip(*others, &block)
1357
1371
}
1358
1372
end
1359
1373
end
1374
+
1375
+ class Array ::Wrapper < BasicObject
1376
+ def self . new ( *args , &block )
1377
+ obj = allocate
1378
+ obj . initialize ( *args , &block )
1379
+ obj
1380
+ end
1381
+
1382
+ def self . []( *objects )
1383
+ obj = allocate
1384
+ `obj.literal = objects`
1385
+ obj
1386
+ end
1387
+
1388
+ def initialize ( *args , &block )
1389
+ @literal = Array . new ( *args , &block )
1390
+ end
1391
+
1392
+ def method_missing ( *args , &block )
1393
+ result = @literal . __send__ ( *args , &block )
1394
+
1395
+ if `result === #@literal `
1396
+ self
1397
+ elsif `result._isArray != null`
1398
+ result
1399
+ else
1400
+ result
1401
+ end
1402
+ end
1403
+
1404
+ def respond_to? ( name , *)
1405
+ super || @literal . respond_to? ( name )
1406
+ end
1407
+
1408
+ def is_a? ( klass )
1409
+ `$opal.is_a(self, klass)`
1410
+ end
1411
+
1412
+ alias kind_of? is_a?
1413
+
1414
+ def instance_of? ( klass )
1415
+ `self._klass === klass`
1416
+ end
1417
+
1418
+ alias send __send__
1419
+
1420
+ def class
1421
+ `self._klass`
1422
+ end
1423
+
1424
+ def clone
1425
+ self . class [ *@literal ]
1426
+ end
1427
+
1428
+ alias dup clone
1429
+
1430
+ def to_a
1431
+ @literal
1432
+ end
1433
+
1434
+ def to_ary
1435
+ self
1436
+ end
1437
+
1438
+ # wrapped results
1439
+ def *( other )
1440
+ %x{
1441
+ var result = #{ @literal * other } ;
1442
+
1443
+ if (result._isArray) {
1444
+ return #{ self . class [ *`result` ] }
1445
+ }
1446
+ else {
1447
+ return result;
1448
+ }
1449
+ }
1450
+ end
1451
+
1452
+ def []( index , length = undefined )
1453
+ %x{
1454
+ var result = #{ @literal . slice ( index , length ) } ;
1455
+
1456
+ if (result._isArray && (index._isRange || length !== undefined)) {
1457
+ return #{ self . class [ *`result` ] }
1458
+ }
1459
+ else {
1460
+ return result;
1461
+ }
1462
+ }
1463
+ end
1464
+
1465
+ alias slice []
1466
+
1467
+ def uniq
1468
+ self . class [ *@literal . uniq ]
1469
+ end
1470
+ end
0 commit comments