Module: Native

Included in:
Buffer, Buffer::View, Console, Array, Object
Defined in:
opal/stdlib/native.rb

Overview

Provides a complete set of tools to wrap native JavaScript into nice Ruby objects.

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 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

Returns:



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

Raises:

  • (LocalJumpError)


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

#to_nObject

Returns the internal native JavaScript value



200
201
202
# File 'opal/stdlib/native.rb', line 200

def to_n
  @native
end