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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mini_racer.rb', line 141

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

  check_init_options!(options)

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

  @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.



139
140
141
# File 'lib/mini_racer.rb', line 139

def isolate
  @isolate
end

Instance Method Details

#attach(name, callback) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mini_racer.rb', line 197

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

#disposeObject



187
188
189
190
191
192
193
194
# File 'lib/mini_racer.rb', line 187

def dispose
  if !@disposed
    isolate.with_lock do
      dispose_unsafe
    end
    @disposed = true
  end
end

#eval(str, options = nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/mini_racer.rb', line 171

def eval(str, options=nil)
  raise(ContextDisposedError, 'attempted to call eval on a disposed context!') if @disposed

  filename = options && options[:filename].to_s

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

#load(filename) ⇒ Object



166
167
168
169
# File 'lib/mini_racer.rb', line 166

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