@@ -119,26 +119,6 @@ def Native(obj)
119
119
obj
120
120
end
121
121
end
122
-
123
- def Array ( object , *args , &block )
124
- %x{
125
- if (object == null || object === nil) {
126
- return [];
127
- }
128
- else if (#{ native? ( object ) } ) {
129
- return #{ Native ::Array . new ( object , *args , &block ) . to_a } ;
130
- }
131
- else if (#{ object . respond_to? :to_ary } ) {
132
- return #{ object . to_ary } ;
133
- }
134
- else if (#{ object . respond_to? :to_a } ) {
135
- return #{ object . to_a } ;
136
- }
137
- else {
138
- return [object];
139
- }
140
- }
141
- end
142
122
end
143
123
144
124
class Native ::Object < BasicObject
@@ -230,108 +210,11 @@ def class
230
210
`self._klass`
231
211
end
232
212
233
- def to_a ( options = { } , &block )
234
- Native ::Array . new ( @native , options , &block ) . to_a
235
- end
236
-
237
- def to_ary ( options = { } , &block )
238
- Native ::Array . new ( @native , options , &block )
239
- end
240
-
241
213
def inspect
242
214
"#<Native:#{ `String(#@native )` } >"
243
215
end
244
216
end
245
217
246
- class Native ::Array
247
- include Native
248
- include Enumerable
249
-
250
- def initialize ( native , options = { } , &block )
251
- super ( native )
252
-
253
- @get = options [ :get ] || options [ :access ]
254
- @named = options [ :named ]
255
- @set = options [ :set ] || options [ :access ]
256
- @length = options [ :length ] || :length
257
- @block = block
258
-
259
- if `#{ length } == null`
260
- raise ArgumentError , "no length found on the array-like object"
261
- end
262
- end
263
-
264
- def each ( &block )
265
- return enum_for :each unless block
266
-
267
- %x{
268
- for (var i = 0, length = #{ length } ; i < length; i++) {
269
- var value = $opal.$yield1(block, #{ self [ `i` ] } );
270
-
271
- if (value === $breaker) {
272
- return $breaker.$v;
273
- }
274
- }
275
- }
276
-
277
- self
278
- end
279
-
280
- def []( index )
281
- result = case index
282
- when String , Symbol
283
- @named ? `#@native [#@named ](#{ index } )` : `#@native [#{ index } ]`
284
-
285
- when Integer
286
- @get ? `#@native [#@get ](#{ index } )` : `#@native [#{ index } ]`
287
- end
288
-
289
- if result
290
- if @block
291
- @block . call ( result )
292
- else
293
- Native ( result )
294
- end
295
- end
296
- end
297
-
298
- def []=( index , value )
299
- if @set
300
- `#@native [#@set ](#{ index } , #{ Native . convert ( value ) } )`
301
- else
302
- `#@native [#{ index } ] = #{ Native . convert ( value ) } `
303
- end
304
- end
305
-
306
- def last ( count = nil )
307
- if count
308
- index = length - 1
309
- result = [ ]
310
-
311
- while index >= 0
312
- result << self [ index ]
313
- index -= 1
314
- end
315
-
316
- result
317
- else
318
- self [ length - 1 ]
319
- end
320
- end
321
-
322
- def length
323
- `#@native [#@length ]`
324
- end
325
-
326
- def to_ary
327
- self
328
- end
329
-
330
- def inspect
331
- to_a . inspect
332
- end
333
- end
334
-
335
218
class Numeric
336
219
def to_n
337
220
`self.valueOf()`
@@ -363,20 +246,6 @@ def to_n
363
246
end
364
247
365
248
class Struct
366
- def initialize ( *args )
367
- if args . length == 1 && native? ( args [ 0 ] )
368
- object = args [ 0 ]
369
-
370
- members . each { |name |
371
- instance_variable_set "@#{ name } " , Native ( `#{ object } [#{ name } ]` )
372
- }
373
- else
374
- members . each_with_index { |name , index |
375
- instance_variable_set "@#{ name } " , args [ index ]
376
- }
377
- end
378
- end
379
-
380
249
def to_n
381
250
result = `{}`
382
251
@@ -428,6 +297,7 @@ def to_n
428
297
end
429
298
430
299
class Hash
300
+ # TODO: move to Hash.from() or JSON.from() or something similar
431
301
def initialize ( defaults = undefined , &block )
432
302
%x{
433
303
if (defaults != null) {
0 commit comments