11module Kernel
2+ def native? ( value )
3+ `value == null || !value._klass`
4+ end
5+
26 def Native ( obj )
3- if Native === obj
4- Native :: Object . new ( obj )
7+ if native? ( obj )
8+ Native . new ( obj )
59 else
610 obj
711 end
812 end
913end
1014
1115class Native
12- def self . ===( value )
13- if self == Native
14- `value == null || !value._klass`
15- else
16- super
16+ module Base
17+ module Helpers
18+ def alias_native ( new , old )
19+ define_method new do |*args |
20+ Native . call ( @native , old , *args )
21+ end
22+ end
23+ end
24+
25+ def self . included? ( klass )
26+ klass . instance_eval {
27+ extend Helpers
28+ }
29+ end
30+
31+ def initialize ( native )
32+ unless native? ( native )
33+ raise ArgumentError , "the passed value isn't native"
34+ end
35+
36+ @native = native
37+ end
38+
39+ def to_n
40+ @native
1741 end
1842 end
1943
2044 def self . try_convert ( value )
2145 %x{
22- if (#{ self === value } ) {
23- return value.valueOf();
46+ if (#{ native? ( value ) } ) {
47+ return #{ value } .valueOf();
2448 }
2549 else if (#{ value . respond_to? :to_n } ) {
2650 return #{ value . to_n } ;
@@ -41,12 +65,6 @@ def self.convert(value)
4165 native
4266 end
4367
44- def self . alias_native ( new , old )
45- define_method new do |*args |
46- Native . call ( @native , old , *args )
47- end
48- end
49-
5068 def self . call ( obj , key , *args , &block )
5169 args << block if block
5270
@@ -61,7 +79,7 @@ def self.call(obj, key, *args, &block)
6179
6280 return result == null ? nil : result;
6381 }
64- else if (#{ self === `prop` } ) {
82+ else if (#{ native? ( `prop` ) } ) {
6583 return #{ Native ( `prop` ) } ;
6684 }
6785 else {
@@ -70,35 +88,7 @@ def self.call(obj, key, *args, &block)
7088 }
7189 end
7290
73- def self . new ( *)
74- if self == Native
75- raise ArgumentError , "cannot instantiate non derived Native"
76- else
77- super
78- end
79- end
80-
81- def initialize ( native )
82- @native = Native . convert ( native )
83- end
84-
85- def to_n
86- @native
87- end
88- end
89-
90- class Native ::Object < BasicObject
91- def initialize ( native )
92- unless Native === native
93- raise ArgumentError , "the passed value isn't native"
94- end
95-
96- @native = native
97- end
98-
99- def to_n
100- @native
101- end
91+ include Base
10292
10393 def nil?
10494 `#@native == null`
0 commit comments