Skip to content

Commit 16af36e

Browse files
committedNov 5, 2013
Make Native method calls unwrap arguments and wrap results
1 parent 49ab70f commit 16af36e

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed
 

‎corelib/native.rb

+36-4
Original file line numberDiff line numberDiff line change
@@ -181,18 +181,28 @@ def self.convert(value)
181181
end
182182

183183
def self.call(obj, key, *args, &block)
184-
args << block if block
185-
186184
%x{
187185
var prop = #{obj}[#{key}];
188186
189187
if (prop == null) {
190188
return nil;
191189
}
192190
else if (prop instanceof Function) {
193-
var result = prop.apply(#{obj}, #{args});
191+
if (block !== nil) {
192+
args.push(block);
193+
}
194+
195+
args = #{args.map {|value|
196+
native = try_convert(value)
194197
195-
return result == null ? nil : result;
198+
if nil === native
199+
value
200+
else
201+
native
202+
end
203+
}};
204+
205+
return #{Native(`prop.apply(#{obj}, #{args})`)};
196206
}
197207
else if (#{native?(`prop`)}) {
198208
return #{Native(`prop`)};
@@ -205,6 +215,10 @@ def self.call(obj, key, *args, &block)
205215

206216
include Base
207217

218+
def ==(other)
219+
`#@native === #{Native.try_convert(other)}`
220+
end
221+
208222
def has_key?(name)
209223
`#@native.hasOwnProperty(#{name})`
210224
end
@@ -264,6 +278,24 @@ def method_missing(mid, *args, &block)
264278
def nil?
265279
false
266280
end
281+
282+
def is_a?(klass)
283+
klass == Native
284+
end
285+
286+
alias kind_of? is_a?
287+
288+
def instance_of?(klass)
289+
klass == Native
290+
end
291+
292+
def class
293+
`self._klass`
294+
end
295+
296+
def inspect
297+
"#<Native:#{`String(#@native)`}>"
298+
end
267299
end
268300

269301
# native global

‎spec/corelib/native/method_missing_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@
3636
it "should pass the block as function" do
3737
Native(`{ a: function(func) { return func(); } }`).a { 42 }.should == 42
3838
end
39+
40+
it "should unwrap arguments" do
41+
x = `{}`
42+
43+
Native(`{ a: function(a, b) { return a === b } }`).a(Native(x), x).should == true
44+
end
45+
46+
it "should wrap result" do
47+
Native(`{ a: function() { return {}; } }`).a.class.should == Native
48+
end
3949
end

0 commit comments

Comments
 (0)
Please sign in to comment.