Class: CodeEvaluation

Inherits:
Object show all
Defined in:
lib/maglev-database-explorer/code_evaluation.rb

Class Method Summary collapse

Class Method Details

.wait_for_eval_thread(&block) ⇒ Object



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
40
41
42
43
44
# File 'lib/maglev-database-explorer/code_evaluation.rb', line 3

def wait_for_eval_thread(&block)
  eval_thread = Thread.start do
    value_proc = Proc.new do
      if Maglev::System.__DBEPersistenceMode
        Maglev.persistent do
          block.call
        end
      else
        Maglev.transient do
          block.call
        end
      end
    end

    value_proc.__call_and_rescue do |eval_result|
      is_exception = eval_result[0]

      if is_exception
        Thread.current.__set_exception(eval_result[1])
        eval_result[1] = Thread.current
      else
        Thread.current.__set_exception(nil)
      end

      Thread.current[:result] = eval_result
      Thread.current[:manual_stop] = true

      if is_exception
        Thread.stop
        Thread.current[:manual_stop] = false
        eval_result[1].__exception.__resume
      end
    end

    Thread.current[:manual_stop] = true
  end
  
  sleep 0.1 until eval_thread.stop? and eval_thread[:manual_stop]

  result = eval_thread[:result]
  return result
end