Class: Puppeteer::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/puppeteer/execution_context.rb

Instance Method Summary collapse

Instance Method Details

#await_ruby(ruby_source = '', file = nil, line = nil, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/isomorfeus/puppeteer/execution_context.rb', line 83

def await_ruby(ruby_source = '', file = nil, line = nil, &block)
  ruby_source, file = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  ruby_source = "#{ruby_source}.then { |result| $promise_result = result; $promise_resolved = true }.fail { |e| $await_ruby_exception = e; $promise_resolved = true }"
  compiled_ruby = Isomorfeus::Puppetmaster.compile_ruby_source(ruby_source, file)
  evaluate <<~JAVASCRIPT
    function(){
      fun = function() {
        if (Opal) {
          Opal.gvars.promise_resolved = false;
          Opal.await_ruby_exception = null;
          try {
            #{compiled_ruby}
          } catch (e) {
            Opal.await_ruby_exception = e;
            Opal.gvars.promise_resolved = true;
          }
        } else {
          if (typeof Opal.gvars.do_not_have_opal !== 'undefined' && Opal.gvars.do_not_have_opal >= 0) { Opal.gvars.do_not_have_opal++; }
          else { Opal.gvars.do_not_have_opal = 1; }
          if (Opal.gvars.do_not_have_opal < 50) {
            Opal.gvars.do_not_have_opal = 0;
            setTimeout(fun, 100);
          } else {
            Opal.await_ruby_exception = new Error('Opal is not available!');
            Opal.gvars.promise_resolved = true;
            Opal.gvars.do_not_have_opal = 0;
          }
        }
      }
      fun();
    }
  JAVASCRIPT
  have_result = false
  start = Time.now
  until have_result do
    raise "await_ruby: execution timed out! Is Opal available?" if (Time.now - start) > 30
    have_result = evaluate 'function() { return (Opal ? Opal.gvars.promise_resolved : null); }'
    sleep 0.1 unless have_result
  end
  res, exception = evaluate <<~JAVASCRIPT
    function () {
      var res;
      var exception = false;
      if (Opal.await_ruby_exception) {
        var e = Opal.await_ruby_exception;
        exception = { message: e.message, name: e.name, stack: e.stack }
      } else if (Opal.gvars.promise_result['$respond_to?']('is_a?') && Opal.gvars.promise_result['$is_a?'](Opal.Exception)) {
        let r = Opal.gvars.promise_result;
        exception = { message: r.$message(), name: r.$class().$name(), stack: r.$backtrace() }
      } else { res = Opal.gvars.promise_result };
      Opal.gvars.promise_result = null;
      Opal.gvars.promise_resolved = null;
      if (res && res.$to_n) res = res.$to_n();
      let conv = function(o) {
        if (o === Opal.nil) o = null;
        if (o && typeof(o) === 'object') {
          if (o.constructor && o.constructor.name === 'Map') o = Object.fromEntries(o);
          if (Array.isArray(o)) {
            for (let i = 0; i < o.length; i++) o[i] = conv(o[i]);
          } else {
            let d;
            for (let i in o) {
              d = Object.getOwnPropertyDescriptor(o, i);
              if (d && d.writable)
                o[i] = conv(o[i]);
            }
          }
        }
        return o;
      }
      return [conv(res), exception];
    }
  JAVASCRIPT
  if exception
    e = RuntimeError.new("#{exception['name']}: #{exception['message']}")
    e.set_backtrace(exception['stack'])
    raise e
  end
  res
end

#eval_ruby(ruby_source = '', file = nil, _line = nil, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/isomorfeus/puppeteer/execution_context.rb', line 2

def eval_ruby(ruby_source = '', file = nil, _line = nil, &block)
  ruby_source, file = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  compiled_ruby = Isomorfeus::Puppetmaster.compile_ruby_source(ruby_source, file)
  res = evaluate <<~JAVASCRIPT
    function(){
      let fun = function() {
        try { return #{compiled_ruby} }
        catch (e) { return { error: e.name, message: e.message, stack: e.stack }; }
      }
      let res = fun();
      if (res && res.$to_n) res = res.$to_n();
      let conv = function(o) {
        if (o === Opal.nil) o = null;
        if (o && typeof(o) === 'object') {
          if (o.constructor && o.constructor.name === 'Map') o = Object.fromEntries(o);
          if (Array.isArray(o)) {
            for (let i = 0; i < o.length; i++) o[i] = conv(o[i]);
          } else {
            let d;
            for (let i in o) {
              d = Object.getOwnPropertyDescriptor(o, i);
              if (d && d.writable)
                o[i] = conv(o[i]);
            }
          }
        }
        return o;
      }
      return conv(res);
    }
  JAVASCRIPT
  if res.is_a?(Hash) && res.key?('error') && res.key?('message') && res.key?('stack')
    e = RuntimeError.new("#{res['error']}: #{res['message']}")
    e.set_backtrace(res['stack'].lines)
    raise e
  end
  res
end

#eval_with_opal(ruby_source = '', file = nil, _line = nil, &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
73
74
75
76
77
78
79
80
81
# File 'lib/isomorfeus/puppeteer/execution_context.rb', line 41

def eval_with_opal(ruby_source = '', file = nil, _line = nil, &block)
  ruby_source, file = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given?
  compiled_ruby = Isomorfeus::Puppetmaster.compile_ruby_source(ruby_source, file)
  res = evaluate <<~JAVASCRIPT
    function(){
      let fun = function(){
        if (typeof Opal === "undefined") {
          #{Isomorfeus::Puppetmaster.opal_prelude}
        }
        try { return #{compiled_ruby} }
        catch (e) { return { error: e.name, message: e.message, stack: e.stack }; }
      }
      let res = fun();
      if (res && res.$to_n) res = res.$to_n();
      let conv = function(o) {
        if (o === Opal.nil) o = null;
        if (o && typeof(o) === 'object') {
          if (o.constructor && o.constructor.name === 'Map') o = Object.fromEntries(o);
          if (Array.isArray(o)) {
            for (let i = 0; i < o.length; i++) o[i] = conv(o[i]);
          } else {
            let d;
            for (let i in o) {
              d = Object.getOwnPropertyDescriptor(o, i);
              if (d && d.writable)
                o[i] = conv(o[i]);
            }
          }
        }
        return o;
      }
      return conv(res);
    }
  JAVASCRIPT
  if res.is_a?(Hash) && res.key?('error') && res.key?('message') && res.key?('stack')
    e = RuntimeError.new("#{res['error']}: #{res['message']}")
    e.set_backtrace(res['stack'].lines)
    raise e
  end
  res
end