Module: Native

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

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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'opal/stdlib/native.rb', line 41

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, length = args.length; i < length; 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



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'opal/stdlib/native.rb', line 27

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



112
113
114
# File 'opal/stdlib/native.rb', line 112

def self.included(klass)
  klass.extend Helpers
end

.is_a?(object, klass) ⇒ Boolean

Returns:



2
3
4
5
6
7
8
9
10
11
# File 'opal/stdlib/native.rb', line 2

def self.is_a?(object, klass)
  %x{
    try {
      return #{object} instanceof #{try_convert(klass)};
    }
    catch (e) {
      return false;
    }
  }
end

.try_convert(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'opal/stdlib/native.rb', line 13

def self.try_convert(value)
  %x{
    if (#{native?(value)}) {
      return #{value};
    }
    else if (#{value.respond_to? :to_n}) {
      return #{value.to_n};
    }
    else {
      return nil;
    }
  }
end

Instance Method Details

#initialize(native) ⇒ Object



116
117
118
119
120
121
122
# File 'opal/stdlib/native.rb', line 116

def initialize(native)
  unless Kernel.native?(native)
    Kernel.raise ArgumentError, "#{native.inspect} isn't native"
  end

  @native = native
end

#to_nObject



124
125
126
# File 'opal/stdlib/native.rb', line 124

def to_n
  @native
end