1
0
Fork 0
mirror of https://github.com/rubyjs/mini_racer synced 2023-03-27 23:21:28 -04:00
mini_racer/lib/mini_racer.rb

66 lines
1.2 KiB
Ruby
Raw Normal View History

2016-05-04 02:54:51 -04:00
require "mini_racer/version"
require "mini_racer_extension"
2016-05-10 22:26:52 -04:00
require "thread"
2016-05-04 02:54:51 -04:00
module MiniRacer
class JavaScriptError < StandardError
def initialize(message)
message, js_backtrace = message.split("\n", 2)
if js_backtrace && !js_backtrace.empty?
@js_backtrace = js_backtrace.split("\n")
@js_backtrace.map!{|f| "JavaScript #{f.strip}"}
end
super(message)
end
def backtrace
val = super
return unless val
if @js_backtrace
@js_backtrace + val
else
val
end
end
end
2016-05-06 03:08:06 -04:00
# eval is defined in the C class
class Context
2016-05-08 06:59:10 -04:00
class ExternalFunction
def initialize(name, callback, parent)
@name = name
@callback = callback
@parent = parent
notify_v8
end
end
2016-05-08 06:59:10 -04:00
def initialize(options = nil)
@functions = {}
2016-05-10 22:26:52 -04:00
@lock = Mutex.new
2016-05-06 03:08:06 -04:00
if options
@timeout = options[:timeout]
end
end
2016-05-08 06:59:10 -04:00
2016-05-10 22:26:52 -04:00
def eval(str)
@lock.synchronize do
@current_exception = nil
2016-05-10 22:26:52 -04:00
eval_unsafe(str)
end
end
2016-05-08 06:59:10 -04:00
def attach(name, callback)
2016-05-10 22:26:52 -04:00
@lock.synchronize do
external = ExternalFunction.new(name, callback, self)
@functions[name.to_s] = external
end
2016-05-08 06:59:10 -04:00
end
2016-05-06 03:08:06 -04:00
end
2016-05-04 02:54:51 -04:00
end