Module: Kernel

Defined in:
opal/opal/rspec/fixes/opal/kernel.rb

Instance Method Summary collapse

Instance Method Details

#callerObject

RSpec tries to add context with this. something like this: https://github.com/stacktracejs/stacktrace.js would be better than this but avoiding adding an NPM dependency for now



36
37
38
39
40
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
73
74
75
76
77
78
79
# File 'opal/opal/rspec/fixes/opal/kernel.rb', line 36

def caller
  %x{
    function getErrorObject(){
        try { throw Error('') } catch(err) { return err; }
    }


    var err = getErrorObject();
  }
  stack = `err.stack`
  caller_lines = stack.split("\n")[4..-1]
  caller_lines.reject! { |l| l.strip.empty? }

  result_formatter = lambda do |filename, line, method=nil|
    "#{filename}:#{line} in `(#{method ? method : 'unknown method'})'"
  end

  caller_lines.map do |raw_line|
    if match = /\s*at (.*) \((\S+):(\d+):\d+/.match(raw_line)
      method, filename, line = match.captures
      result_formatter[filename, line, method]
    elsif match = /\s*at (\S+):(\d+):\d+/.match(raw_line)
      filename, line = match.captures
      result_formatter[filename, line]
      # catch phantom/no 2nd line/col #
    elsif match = /\s*at (.*) \((\S+):(\d+)/.match(raw_line)
      method, filename, line = match.captures
      result_formatter[filename, line, method]
    elsif match = /\s*at (.*):(\d+)/.match(raw_line)
      filename, line = match.captures
      result_formatter[filename, line]
      # Firefox - Opal.modules["rspec/core/metadata"]/</</</</def.$populate@http://192.168.59.103:9292/assets/rspec/core/metadata.self.js?body=1:102:13
    elsif match = /(.*?)@(\S+):(\d+):\d+/.match(raw_line)
      method, filename, line = match.captures
      result_formatter[filename, line, method]
      # webkit - http://192.168.59.103:9292/assets/opal/rspec/sprockets_runner.js:45117:314
    elsif match = /(\S+):(\d+):\d+/.match(raw_line)
      filename, line = match.captures
      result_formatter[filename, line]
    else
      "#{filename}:-1 in `(can't parse this stack trace)`"
    end
  end
end

#cloneObject



23
24
25
26
27
28
29
30
31
# File 'opal/opal/rspec/fixes/opal/kernel.rb', line 23

def clone
  copy = self.class.allocate

  copy.copy_instance_variables(self)
  copy.copy_singleton_methods(self)
  copy.initialize_clone(self)

  copy
end

#copy_singleton_methods(other) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'opal/opal/rspec/fixes/opal/kernel.rb', line 3

def copy_singleton_methods(other)
  %x{
        var name;
        if (other.hasOwnProperty('$$meta')) {
          var other_singleton_class_proto = Opal.get_singleton_class(other).$$proto;
          var self_singleton_class_proto = Opal.get_singleton_class(self).$$proto;
          for (name in other_singleton_class_proto) {
            if (name.charAt(0) === '$' && other_singleton_class_proto.hasOwnProperty(name)) {
              self_singleton_class_proto[name] = other_singleton_class_proto[name];
            }
          }
        }
        for (name in other) {
          if (name.charAt(0) === '$' && name.charAt(1) !== '$' && other.hasOwnProperty(name)) {
            self[name] = other[name];
          }
        }
      }
end