Module: Native
  
  
  
Overview
  
    Provides a complete set of tools to wrap native JavaScript
into nice Ruby objects.
   
 
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 
  
  
  
  
    | 
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 | # File 'opal/stdlib/native.rb', line 55
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 
  
  
  
  
    | 
41
42
43
44
45
46
47
48
49
50
51
52
53 | # File 'opal/stdlib/native.rb', line 41
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 
  
  
  
  
    | 
187
188
189 | # File 'opal/stdlib/native.rb', line 187
def self.included(klass)
  klass.extend Helpers
end | 
 
    
      
  
  
    .is_a?(object, klass)  ⇒ Boolean 
  
  
  
  
    | 
16
17
18
19
20
21
22
23
24
25 | # File 'opal/stdlib/native.rb', line 16
def self.is_a?(object, klass)
  %x{
    try {
      return #{object} instanceof #{try_convert(klass)};
    }
    catch (e) {
      return false;
    }
  }
end | 
 
    
      
  
  
    .proc(&block)  ⇒ Object 
  
  
  
  
    | 
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105 | # File 'opal/stdlib/native.rb', line 81
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 
  
  
  
  
    | 
27
28
29
30
31
32
33
34
35
36
37
38
39 | # File 'opal/stdlib/native.rb', line 27
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 
  
  
  
  
    | 
191
192
193
194
195
196
197 | # File 'opal/stdlib/native.rb', line 191
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
   
 
  
  
    | 
200
201
202 | # File 'opal/stdlib/native.rb', line 200
def to_n
  @native
end |