[フレーム]

Module: Native

Defined in:
opal/stdlib/native.rb

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.

Examples:


$$.document.querySelector('p').classList.add('blue')
# => adds "blue" class to <p>

$$.location.href = 'https://google.com'
# => changes page location

do_later = $$[:setTimeout] # Accessing the "setTimeout" property
do_later.call(->{ puts :hello}, 500)

Defined Under Namespace

Modules: Helpers , Wrapper Classes: Array , Object

Class 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(base) ⇒ Object

208
209
210
211
# File 'opal/stdlib/native.rb', line 208
def self.included(base)
 warn 'Including ::Native is deprecated. Please include Native::Wrapper instead.'
 base.include Wrapper 
end

.is_a?(object, klass) ⇒ Boolean

Returns:

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

Raises:

  • (LocalJumpError)
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

AltStyle によって変換されたページ (->オリジナル) /