Class: MiniRacer::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_racer.rb

Overview

eval is defined in the C class

Defined Under Namespace

Classes: ExternalFunction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Context

Returns a new instance of Context.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/mini_racer.rb', line 136

def initialize(options = nil)
  options ||= {}

  check_init_options!(options)

  @functions = {}
  @timeout = nil
  @current_exception = nil
  @timeout = options[:timeout]
  @isolate = options[:isolate] || Isolate.new(options[:snapshot])

  @callback_mutex = Mutex.new
  @callback_running = false
  @thread_raise_called = false
  @eval_thread = nil

  isolate.with_lock do
    # defined in the C class
    init_with_isolate(@isolate)
  end
end

Instance Attribute Details

#isolateObject (readonly)

Returns the value of attribute isolate.



134
135
136
# File 'lib/mini_racer.rb', line 134

def isolate
  @isolate
end

Instance Method Details

#attach(name, callback) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/mini_racer.rb', line 176

def attach(name, callback)

  wrapped = lambda do |*args|
    begin
      @callback_mutex.synchronize{
        @callback_running = true
      }

      callback.call(*args)
    ensure
      @callback_mutex.synchronize {
        @callback_running = false

        # this is some odd code, but it is required
        # if we raised on this thread we better wait for it
        # otherwise we may end up raising in an unsafe spot
        if @thread_raise_called
          sleep 0.1
        end
        @thread_raise_called = false
      }
    end
  end

  isolate.with_lock do
    external = ExternalFunction.new(name, wrapped, self)
    @functions["#{name}"] = external
  end
end

#eval(str) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/mini_racer.rb', line 163

def eval(str)
  @eval_thread = Thread.current
  isolate.with_lock do
    @current_exception = nil
    timeout do
      eval_unsafe(str)
    end
  end
ensure
  @eval_thread = nil
end

#load(filename) ⇒ Object



158
159
160
161
# File 'lib/mini_racer.rb', line 158

def load(filename)
  # TODO do this native cause no need to allocate VALUE here
  eval(File.read(filename))
end