Module: Native
Overview
Provides a complete set of tools to wrap native JavaScript
into nice Ruby objects.
$$
and $global
wrap Opal.global
, which the Opal JS runtime
sets to the global this
object.
Defined Under Namespace
Modules: Helpers
Classes: Array, Object
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.call(obj, key, *args, &block) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'opal/stdlib/native.rb', line 58
def self.call(obj, key, *args, &block)
%x{
var prop = #{obj}[#{key}];
if (prop instanceof Function) {
var converted = new Array(args.length);
for (var i = 0, l = args.length; i < l; i++) {
var item = args[i],
conv = #{try_convert(`item`)};
converted[i] = conv === nil ? item : conv;
}
if (block !== nil) {
converted.push(block);
}
return #{Native(`prop.apply(#{obj}, converted)`)};
}
else {
return #{Native(`prop`)};
}
}
end
|
.convert(value) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'opal/stdlib/native.rb', line 44
def self.convert(value)
%x{
if (#{native?(value)}) {
return #{value};
}
else if (#{value.respond_to? :to_n}) {
return #{value.to_n};
}
else {
#{raise ArgumentError, "#{value.inspect} isn't native"};
}
}
end
|
.included(klass) ⇒ Object
190
191
192
|
# File 'opal/stdlib/native.rb', line 190
def self.included(klass)
klass.extend Helpers
end
|
.is_a?(object, klass) ⇒ Boolean
19
20
21
22
23
24
25
26
27
28
|
# File 'opal/stdlib/native.rb', line 19
def self.is_a?(object, klass)
%x{
try {
return #{object} instanceof #{try_convert(klass)};
}
catch (e) {
return false;
}
}
end
|
.proc(&block) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'opal/stdlib/native.rb', line 84
def self.proc(&block)
raise LocalJumpError, "no block given" unless block
::Kernel.proc {|*args|
args.map! { |arg| Native(arg) }
instance = Native(`this`)
%x{
// if global is current scope, run the block in the scope it was defined
if (this === Opal.global) {
return block.apply(self, #{args});
}
var self_ = block.$$s;
block.$$s = null;
try {
return block.apply(#{instance}, #{args});
}
finally {
block.$$s = self_;
}
}
}
end
|
.try_convert(value, default = nil) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'opal/stdlib/native.rb', line 30
def self.try_convert(value, default=nil)
%x{
if (#{native?(value)}) {
return #{value};
}
else if (#{value.respond_to? :to_n}) {
return #{value.to_n};
}
else {
return #{default};
}
}
end
|
Instance Method Details
#initialize(native) ⇒ Object
194
195
196
197
198
199
200
|
# File 'opal/stdlib/native.rb', line 194
def initialize(native)
unless ::Kernel.native?(native)
::Kernel.raise ArgumentError, "#{native.inspect} isn't native"
end
@native = native
end
|
Returns the internal native JavaScript value
203
204
205
|
# File 'opal/stdlib/native.rb', line 203
def to_n
@native
end
|