|
1 |
| -module Kernel |
2 |
| - def native?(value) |
3 |
| - `value == null || !value._klass` |
4 |
| - end |
5 |
| - |
6 |
| - def Native(obj) |
7 |
| - if `#{obj} == null` |
8 |
| - nil |
9 |
| - elsif native?(obj) |
10 |
| - Native::Object.new(obj) |
11 |
| - else |
12 |
| - obj |
13 |
| - end |
14 |
| - end |
15 |
| - |
16 |
| - def Array(object, *args, &block) |
17 |
| - %x{ |
18 |
| - if (object == null || object === nil) { |
19 |
| - return []; |
20 |
| - } |
21 |
| - else if (#{native?(object)}) { |
22 |
| - return #{Native::Array.new(object, *args, &block).to_a}; |
23 |
| - } |
24 |
| - else if (#{object.respond_to? :to_ary}) { |
25 |
| - return #{object.to_ary}; |
26 |
| - } |
27 |
| - else if (#{object.respond_to? :to_a}) { |
28 |
| - return #{object.to_a}; |
29 |
| - } |
30 |
| - else { |
31 |
| - return [object]; |
32 |
| - } |
33 |
| - } |
34 |
| - end |
35 |
| -end |
36 |
| - |
37 | 1 | module Native
|
38 | 2 | def self.is_a?(object, klass)
|
39 | 3 | %x{
|
@@ -146,188 +110,224 @@ def initialize(native)
|
146 | 110 | def to_n
|
147 | 111 | @native
|
148 | 112 | end
|
| 113 | +end |
149 | 114 |
|
150 |
| - class Object < BasicObject |
151 |
| - include Native |
| 115 | +module Kernel |
| 116 | + def native?(value) |
| 117 | + `value == null || !value._klass` |
| 118 | + end |
152 | 119 |
|
153 |
| - def ==(other) |
154 |
| - `#@native === #{Native.try_convert(other)}` |
| 120 | + def Native(obj) |
| 121 | + if `#{obj} == null` |
| 122 | + nil |
| 123 | + elsif native?(obj) |
| 124 | + Native::Object.new(obj) |
| 125 | + else |
| 126 | + obj |
155 | 127 | end
|
| 128 | + end |
156 | 129 |
|
157 |
| - def has_key?(name) |
158 |
| - `#@native.hasOwnProperty(#{name})` |
159 |
| - end |
| 130 | + def Array(object, *args, &block) |
| 131 | + %x{ |
| 132 | + if (object == null || object === nil) { |
| 133 | + return []; |
| 134 | + } |
| 135 | + else if (#{native?(object)}) { |
| 136 | + return #{Native::Array.new(object, *args, &block).to_a}; |
| 137 | + } |
| 138 | + else if (#{object.respond_to? :to_ary}) { |
| 139 | + return #{object.to_ary}; |
| 140 | + } |
| 141 | + else if (#{object.respond_to? :to_a}) { |
| 142 | + return #{object.to_a}; |
| 143 | + } |
| 144 | + else { |
| 145 | + return [object]; |
| 146 | + } |
| 147 | + } |
| 148 | + end |
| 149 | +end |
160 | 150 |
|
161 |
| - alias key? has_key? |
162 |
| - alias include? has_key? |
163 |
| - alias member? has_key? |
| 151 | +class Native::Object < BasicObject |
| 152 | + include Native |
164 | 153 |
|
165 |
| - def each(*args) |
166 |
| - if block_given? |
167 |
| - %x{ |
168 |
| - for (var key in #@native) { |
169 |
| - #{yield `key`, `#@native[key]`} |
170 |
| - } |
171 |
| - } |
| 154 | + def ==(other) |
| 155 | + `#@native === #{Native.try_convert(other)}` |
| 156 | + end |
172 | 157 |
|
173 |
| - self |
174 |
| - else |
175 |
| - method_missing(:each, *args) |
176 |
| - end |
177 |
| - end |
| 158 | + def has_key?(name) |
| 159 | + `#@native.hasOwnProperty(#{name})` |
| 160 | + end |
178 | 161 |
|
179 |
| - def [](key) |
180 |
| - %x{ |
181 |
| - var prop = #@native[key]; |
| 162 | + alias key? has_key? |
| 163 | + alias include? has_key? |
| 164 | + alias member? has_key? |
182 | 165 |
|
183 |
| - if (prop instanceof Function) { |
184 |
| - return prop; |
185 |
| - } |
186 |
| - else { |
187 |
| - return #{::Native.call(@native, key)} |
| 166 | + def each(*args) |
| 167 | + if block_given? |
| 168 | + %x{ |
| 169 | + for (var key in #@native) { |
| 170 | + #{yield `key`, `#@native[key]`} |
188 | 171 | }
|
189 | 172 | }
|
| 173 | + |
| 174 | + self |
| 175 | + else |
| 176 | + method_missing(:each, *args) |
190 | 177 | end
|
| 178 | + end |
191 | 179 |
|
192 |
| - def []=(key, value) |
193 |
| - native = Native.try_convert(value) |
| 180 | + def [](key) |
| 181 | + %x{ |
| 182 | + var prop = #@native[key]; |
194 | 183 |
|
195 |
| - if `#{native} === nil` |
196 |
| - `#@native[key] = #{value}` |
197 |
| - else |
198 |
| - `#@native[key] = #{native}` |
199 |
| - end |
| 184 | + if (prop instanceof Function) { |
| 185 | + return prop; |
| 186 | + } |
| 187 | + else { |
| 188 | + return #{::Native.call(@native, key)} |
| 189 | + } |
| 190 | + } |
| 191 | + end |
| 192 | + |
| 193 | + def []=(key, value) |
| 194 | + native = Native.try_convert(value) |
| 195 | + |
| 196 | + if `#{native} === nil` |
| 197 | + `#@native[key] = #{value}` |
| 198 | + else |
| 199 | + `#@native[key] = #{native}` |
200 | 200 | end
|
| 201 | + end |
201 | 202 |
|
202 |
| - def method_missing(mid, *args, &block) |
203 |
| - %x{ |
204 |
| - if (mid.charAt(mid.length - 1) === '=') { |
205 |
| - return #{self[mid.slice(0, mid.length - 1)] = args[0]}; |
206 |
| - } |
207 |
| - else { |
208 |
| - return #{::Native.call(@native, mid, *args, &block)}; |
209 |
| - } |
| 203 | + def method_missing(mid, *args, &block) |
| 204 | + %x{ |
| 205 | + if (mid.charAt(mid.length - 1) === '=') { |
| 206 | + return #{self[mid.slice(0, mid.length - 1)] = args[0]}; |
210 | 207 | }
|
211 |
| - end |
| 208 | + else { |
| 209 | + return #{::Native.call(@native, mid, *args, &block)}; |
| 210 | + } |
| 211 | + } |
| 212 | + end |
212 | 213 |
|
213 |
| - def nil? |
214 |
| - false |
215 |
| - end |
| 214 | + def nil? |
| 215 | + false |
| 216 | + end |
216 | 217 |
|
217 |
| - def is_a?(klass) |
218 |
| - klass == Native |
219 |
| - end |
| 218 | + def is_a?(klass) |
| 219 | + klass == Native |
| 220 | + end |
220 | 221 |
|
221 |
| - alias kind_of? is_a? |
| 222 | + alias kind_of? is_a? |
222 | 223 |
|
223 |
| - def instance_of?(klass) |
224 |
| - klass == Native |
225 |
| - end |
| 224 | + def instance_of?(klass) |
| 225 | + klass == Native |
| 226 | + end |
226 | 227 |
|
227 |
| - def class |
228 |
| - `self._klass` |
229 |
| - end |
| 228 | + def class |
| 229 | + `self._klass` |
| 230 | + end |
230 | 231 |
|
231 |
| - def to_a(options = {}, &block) |
232 |
| - Native::Array.new(@native, options, &block).to_a |
233 |
| - end |
| 232 | + def to_a(options = {}, &block) |
| 233 | + Native::Array.new(@native, options, &block).to_a |
| 234 | + end |
234 | 235 |
|
235 |
| - def to_ary(options = {}, &block) |
236 |
| - Native::Array.new(@native, options, &block) |
237 |
| - end |
| 236 | + def to_ary(options = {}, &block) |
| 237 | + Native::Array.new(@native, options, &block) |
| 238 | + end |
238 | 239 |
|
239 |
| - def inspect |
240 |
| - "#<Native:#{`String(#@native)`}>" |
241 |
| - end |
| 240 | + def inspect |
| 241 | + "#<Native:#{`String(#@native)`}>" |
242 | 242 | end
|
| 243 | +end |
243 | 244 |
|
244 |
| - class Array |
245 |
| - include Native |
246 |
| - include Enumerable |
| 245 | +class Native::Array |
| 246 | + include Native |
| 247 | + include Enumerable |
247 | 248 |
|
248 |
| - def initialize(native, options = {}, &block) |
249 |
| - super(native) |
| 249 | + def initialize(native, options = {}, &block) |
| 250 | + super(native) |
250 | 251 |
|
251 |
| - @get = options[:get] || options[:access] |
252 |
| - @named = options[:named] |
253 |
| - @set = options[:set] || options[:access] |
254 |
| - @length = options[:length] || :length |
255 |
| - @block = block |
| 252 | + @get = options[:get] || options[:access] |
| 253 | + @named = options[:named] |
| 254 | + @set = options[:set] || options[:access] |
| 255 | + @length = options[:length] || :length |
| 256 | + @block = block |
256 | 257 |
|
257 |
| - if `#{length} == null` |
258 |
| - raise ArgumentError, "no length found on the array-like object" |
259 |
| - end |
| 258 | + if `#{length} == null` |
| 259 | + raise ArgumentError, "no length found on the array-like object" |
260 | 260 | end
|
| 261 | + end |
261 | 262 |
|
262 |
| - def each(&block) |
263 |
| - return enum_for :each unless block |
| 263 | + def each(&block) |
| 264 | + return enum_for :each unless block |
264 | 265 |
|
265 |
| - %x{ |
266 |
| - for (var i = 0, length = #{length}; i < length; i++) { |
267 |
| - var value = $opal.$yield1(block, #{self[`i`]}); |
| 266 | + %x{ |
| 267 | + for (var i = 0, length = #{length}; i < length; i++) { |
| 268 | + var value = $opal.$yield1(block, #{self[`i`]}); |
268 | 269 |
|
269 |
| - if (value === $breaker) { |
270 |
| - return $breaker.$v; |
271 |
| - } |
| 270 | + if (value === $breaker) { |
| 271 | + return $breaker.$v; |
272 | 272 | }
|
273 | 273 | }
|
| 274 | + } |
274 | 275 |
|
275 |
| - self |
276 |
| - end |
277 |
| - |
278 |
| - def [](index) |
279 |
| - result = case index |
280 |
| - when String, Symbol |
281 |
| - @named ? `#@native[#@named](#{index})` : `#@native[#{index}]` |
| 276 | + self |
| 277 | + end |
282 | 278 |
|
283 |
| - when Integer |
284 |
| - @get ? `#@native[#@get](#{index})` : `#@native[#{index}]` |
285 |
| - end |
| 279 | + def [](index) |
| 280 | + result = case index |
| 281 | + when String, Symbol |
| 282 | + @named ? `#@native[#@named](#{index})` : `#@native[#{index}]` |
286 | 283 |
|
287 |
| - if result |
288 |
| - if @block |
289 |
| - @block.call(result) |
290 |
| - else |
291 |
| - Native(result) |
292 |
| - end |
293 |
| - end |
| 284 | + when Integer |
| 285 | + @get ? `#@native[#@get](#{index})` : `#@native[#{index}]` |
294 | 286 | end
|
295 | 287 |
|
296 |
| - def []=(index, value) |
297 |
| - if @set |
298 |
| - `#@native[#@set](#{index}, #{Native.convert(value)})` |
| 288 | + if result |
| 289 | + if @block |
| 290 | + @block.call(result) |
299 | 291 | else
|
300 |
| - `#@native[#{index}] = #{Native.convert(value)}` |
| 292 | + Native(result) |
301 | 293 | end
|
302 | 294 | end
|
| 295 | + end |
303 | 296 |
|
304 |
| - def last(count = nil) |
305 |
| - if count |
306 |
| - index = length - 1 |
307 |
| - result = [] |
| 297 | + def []=(index, value) |
| 298 | + if @set |
| 299 | + `#@native[#@set](#{index}, #{Native.convert(value)})` |
| 300 | + else |
| 301 | + `#@native[#{index}] = #{Native.convert(value)}` |
| 302 | + end |
| 303 | + end |
308 | 304 |
|
309 |
| - while index >= 0 |
310 |
| - result << self[index] |
311 |
| - index -= 1 |
312 |
| - end |
| 305 | + def last(count = nil) |
| 306 | + if count |
| 307 | + index = length - 1 |
| 308 | + result = [] |
313 | 309 |
|
314 |
| - result |
315 |
| - else |
316 |
| - self[length - 1] |
| 310 | + while index >= 0 |
| 311 | + result << self[index] |
| 312 | + index -= 1 |
317 | 313 | end
|
318 |
| - end |
319 | 314 |
|
320 |
| - def length |
321 |
| - `#@native[#@length]` |
| 315 | + result |
| 316 | + else |
| 317 | + self[length - 1] |
322 | 318 | end
|
| 319 | + end |
323 | 320 |
|
324 |
| - def to_ary |
325 |
| - self |
326 |
| - end |
| 321 | + def length |
| 322 | + `#@native[#@length]` |
| 323 | + end |
327 | 324 |
|
328 |
| - def inspect |
329 |
| - to_a.inspect |
330 |
| - end |
| 325 | + def to_ary |
| 326 | + self |
| 327 | + end |
| 328 | + |
| 329 | + def inspect |
| 330 | + to_a.inspect |
331 | 331 | end
|
332 | 332 | end
|
333 | 333 |
|
|
0 commit comments