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.
   
 
Defined Under Namespace
  
    
      Modules: Helpers, Wrapper
    
  
    
      Classes: Array, Object
    
  
  
    
      Class Method Summary
      collapse
    
    
  
  
    Class Method Details
    
      
  
  
    .call(obj, key, *args, &block)  ⇒ Object 
  
  
  
  
    | 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 | # File 'opal/stdlib/native.rb', line 61
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 
  
  
  
  
    | 
47
48
49
50
51
52
53
54
55
56
57
58
59 | # File 'opal/stdlib/native.rb', line 47
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 
  
  
  
  
    | 
211
212
213
214 | # File 'opal/stdlib/native.rb', line 211
def self.included(base)
  warn 'Including ::Native is deprecated. Please include Native::Wrapper instead.'
  base.include Wrapper
end | 
 
    
      
  
  
    .is_a?(object, klass)  ⇒ Boolean 
  
  
  
  
    | 
22
23
24
25
26
27
28
29
30
31 | # File 'opal/stdlib/native.rb', line 22
def self.is_a?(object, klass)
  %x{
    try {
      return #{object} instanceof #{try_convert(klass)};
    }
    catch (e) {
      return false;
    }
  }
end | 
 
    
      
  
  
    .proc(&block)  ⇒ Object 
  
  
  
  
    | 
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 | # File 'opal/stdlib/native.rb', line 87
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 
  
  
  
  
    | 
33
34
35
36
37
38
39
40
41
42
43
44
45 | # File 'opal/stdlib/native.rb', line 33
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 |