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
66
67
68
69
70
71
72
# File 'opal/stdlib/native.rb', line 41

def self.call(obj, key, *args, &block)
  %x{
    var prop = #{obj}[#{key}];

    if (prop == null) {
      return nil;
    }
    else if (prop instanceof Function) {
      if (block !== nil) {
        args.push(block);
      }

      args = #{args.map {|value|
        native = try_convert(value)

        if nil === native
          value
        else
          native
        end
      }};

      return #{Native(`prop.apply(#{obj}, #{args})`)};
    }
    else if (#{native?(`prop`)}) {
      return #{Native(`prop`)};
    }
    else {
      return 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, "the passed value isn't a native"};
    }
  }
end

.included(klass) ⇒ Object



98
99
100
# File 'opal/stdlib/native.rb', line 98

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 #{Native.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



102
103
104
105
106
107
108
# File 'opal/stdlib/native.rb', line 102

def initialize(native)
  unless Kernel.native?(native)
    Kernel.raise ArgumentError, "the passed value isn't native"
  end

  @native = native
end

#to_nObject



110
111
112
# File 'opal/stdlib/native.rb', line 110

def to_n
  @native
end