Module: JS::Helpers

Included in:
Proxy
Defined in:
lib/js/proxy.rb

Overview

Shared conversion helpers for values crossing the Ruby/JavaScript boundary.

Instance Method Summary collapse

Instance Method Details

#native_methodsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/js/proxy.rb', line 24

def native_methods
  %x{
    let object = #{to_n};
    const properties = new Set();

    while (object !== null) {
      for (const key of Reflect.ownKeys(object)) {
        if (typeof key === "symbol") continue;

        const nativeName = key.toString();
        const rubyName = #{to_rb_name(`nativeName`)};

        properties.add(nativeName);
        properties.add(rubyName);
      }
      object = Object.getPrototypeOf(object);
    }

    return Array.from(properties);
  }
end

#wrap_result(result) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/js/proxy.rb', line 10

def wrap_result(result)
  return nil if `result == null`

  if `typeof result.then === "function" && !result.then.$$owner`
    Promise.new(result)
  elsif `result instanceof Number || result instanceof String || result instanceof Boolean`
    `result.valueOf()`
  elsif `typeof result === "object"`
    Proxy.new(result)
  else
    result
  end
end