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"
|
2017-03-09 16:03:13 -05:00
|
|
|
require "json"
|
2016-05-04 02:54:51 -04:00
|
|
|
|
|
|
|
module MiniRacer
|
2016-05-11 20:14:33 -04:00
|
|
|
|
2017-07-13 17:43:43 -04:00
|
|
|
class Error < ::StandardError; end
|
|
|
|
|
|
|
|
class ContextDisposedError < Error; end
|
|
|
|
class SnapshotError < Error; end
|
|
|
|
class PlatformAlreadyInitialized < Error; end
|
|
|
|
|
|
|
|
class EvalError < Error; end
|
2016-05-11 20:14:33 -04:00
|
|
|
class ParseError < EvalError; end
|
2017-07-13 17:43:43 -04:00
|
|
|
class ScriptTerminatedError < EvalError; end
|
2017-08-01 10:33:29 -04:00
|
|
|
class V8OutOfMemoryError < EvalError; end
|
2016-05-11 20:14:33 -04:00
|
|
|
|
2016-06-30 22:51:58 -04:00
|
|
|
class FailedV8Conversion
|
|
|
|
attr_reader :info
|
|
|
|
def initialize(info)
|
|
|
|
@info = info
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-05-11 20:14:33 -04:00
|
|
|
class RuntimeError < EvalError
|
2016-05-11 03:02:20 -04:00
|
|
|
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}"}
|
2016-05-11 21:38:43 -04:00
|
|
|
else
|
|
|
|
@js_backtrace = nil
|
2016-05-11 03:02:20 -04:00
|
|
|
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
|
|
|
|
2016-05-11 21:38:43 -04:00
|
|
|
# helper class returned when we have a JavaScript function
|
2016-05-11 20:59:18 -04:00
|
|
|
class JavaScriptFunction
|
|
|
|
def to_s
|
|
|
|
"JavaScript Function"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:07:36 -04:00
|
|
|
class Isolate
|
|
|
|
def initialize(snapshot = nil)
|
|
|
|
unless snapshot.nil? || snapshot.is_a?(Snapshot)
|
|
|
|
raise ArgumentError, "snapshot must be a Snapshot object, passed a #{snapshot.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# defined in the C class
|
|
|
|
init_with_snapshot(snapshot)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-27 20:51:00 -04:00
|
|
|
class Platform
|
|
|
|
class << self
|
|
|
|
def set_flags!(*args, **kwargs)
|
|
|
|
flags_to_strings([args, kwargs]).each do |flag|
|
|
|
|
# defined in the C class
|
|
|
|
set_flag_as_str!(flag)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def flags_to_strings(flags)
|
|
|
|
flags.flatten.map { |flag| flag_to_string(flag) }.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
# normalize flags to strings, and adds leading dashes if needed
|
|
|
|
def flag_to_string(flag)
|
|
|
|
if flag.is_a?(Hash)
|
|
|
|
flag.map do |key, value|
|
|
|
|
"#{flag_to_string(key)} #{value}"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
str = flag.to_s
|
|
|
|
str = "--#{str}" unless str.start_with?('--')
|
|
|
|
str
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-06-22 14:32:58 -04:00
|
|
|
|
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
|
|
|
|
2016-05-09 18:33:31 -04:00
|
|
|
class ExternalFunction
|
|
|
|
def initialize(name, callback, parent)
|
2016-05-16 21:34:24 -04:00
|
|
|
unless String === name
|
|
|
|
raise ArgumentError, "parent_object must be a String"
|
|
|
|
end
|
|
|
|
parent_object, _ , @name = name.rpartition(".")
|
2016-05-09 18:33:31 -04:00
|
|
|
@callback = callback
|
|
|
|
@parent = parent
|
2016-05-18 23:11:39 -04:00
|
|
|
@parent_object_eval = nil
|
|
|
|
@parent_object = nil
|
|
|
|
|
|
|
|
unless parent_object.empty?
|
|
|
|
@parent_object = parent_object
|
|
|
|
|
|
|
|
@parent_object_eval = ""
|
|
|
|
prev = ""
|
|
|
|
first = true
|
|
|
|
parent_object.split(".").each do |obj|
|
|
|
|
prev << obj
|
|
|
|
if first
|
|
|
|
@parent_object_eval << "if (typeof #{prev} === 'undefined') { #{prev} = {} };\n"
|
|
|
|
else
|
|
|
|
@parent_object_eval << "#{prev} = #{prev} || {};\n"
|
|
|
|
end
|
|
|
|
prev << "."
|
|
|
|
first = false
|
|
|
|
end
|
|
|
|
@parent_object_eval << "#{parent_object};"
|
|
|
|
end
|
2016-05-09 18:33:31 -04:00
|
|
|
notify_v8
|
|
|
|
end
|
|
|
|
end
|
2016-05-08 06:59:10 -04:00
|
|
|
|
2016-05-09 18:33:31 -04:00
|
|
|
def initialize(options = nil)
|
2016-06-21 15:07:36 -04:00
|
|
|
options ||= {}
|
|
|
|
|
|
|
|
check_init_options!(options)
|
2016-06-29 11:56:46 -04:00
|
|
|
|
2016-05-09 18:33:31 -04:00
|
|
|
@functions = {}
|
2016-05-11 04:44:24 -04:00
|
|
|
@timeout = nil
|
2017-08-01 10:33:29 -04:00
|
|
|
@max_memory = nil
|
2016-05-11 04:44:24 -04:00
|
|
|
@current_exception = nil
|
2016-06-21 15:07:36 -04:00
|
|
|
@timeout = options[:timeout]
|
2017-09-26 12:46:45 -04:00
|
|
|
if options[:max_memory].is_a?(Numeric) && options[:max_memory] > 0
|
|
|
|
@max_memory = options[:max_memory]
|
|
|
|
end
|
2018-05-09 13:23:02 -04:00
|
|
|
# false signals it should be fetched if requested
|
|
|
|
@isolate = options[:isolate] || false
|
2017-07-25 12:43:46 -04:00
|
|
|
@disposed = false
|
2016-06-21 21:09:41 -04:00
|
|
|
|
2016-09-22 01:49:06 -04:00
|
|
|
@callback_mutex = Mutex.new
|
|
|
|
@callback_running = false
|
|
|
|
@thread_raise_called = false
|
|
|
|
@eval_thread = nil
|
|
|
|
|
2018-05-09 13:23:02 -04:00
|
|
|
# defined in the C class
|
|
|
|
init_unsafe(options[:isolate], options[:snapshot])
|
|
|
|
end
|
|
|
|
|
|
|
|
def isolate
|
|
|
|
return @isolate if @isolate != false
|
|
|
|
# defined in the C class
|
|
|
|
@isolate = create_isolate_value
|
2016-05-16 20:47:21 -04:00
|
|
|
end
|
2016-05-11 04:44:24 -04:00
|
|
|
|
2016-05-16 20:47:21 -04:00
|
|
|
def load(filename)
|
|
|
|
# TODO do this native cause no need to allocate VALUE here
|
|
|
|
eval(File.read(filename))
|
2016-05-06 03:08:06 -04:00
|
|
|
end
|
2016-05-08 06:59:10 -04:00
|
|
|
|
2019-05-14 03:56:04 -04:00
|
|
|
def write_heap_snapshot(file_or_io)
|
|
|
|
f = nil
|
|
|
|
implicit = false
|
|
|
|
|
|
|
|
|
|
|
|
if String === file_or_io
|
|
|
|
f = File.open(file_or_io, "w")
|
|
|
|
implicit = true
|
|
|
|
else
|
|
|
|
f = file_or_io
|
|
|
|
end
|
|
|
|
|
|
|
|
if !(File === f)
|
|
|
|
raise ArgumentError("file_or_io")
|
|
|
|
end
|
|
|
|
|
|
|
|
write_heap_snapshot_unsafe(f)
|
|
|
|
|
|
|
|
ensure
|
|
|
|
f.close if implicit
|
|
|
|
end
|
|
|
|
|
2017-07-17 11:05:33 -04:00
|
|
|
def eval(str, options=nil)
|
2017-07-13 17:43:43 -04:00
|
|
|
raise(ContextDisposedError, 'attempted to call eval on a disposed context!') if @disposed
|
|
|
|
|
2017-07-17 11:05:33 -04:00
|
|
|
filename = options && options[:filename].to_s
|
|
|
|
|
2016-09-22 01:49:06 -04:00
|
|
|
@eval_thread = Thread.current
|
2018-05-09 13:23:02 -04:00
|
|
|
isolate_mutex.synchronize do
|
2016-05-11 03:58:33 -04:00
|
|
|
@current_exception = nil
|
2016-09-22 01:49:06 -04:00
|
|
|
timeout do
|
2017-07-17 11:05:33 -04:00
|
|
|
eval_unsafe(str, filename)
|
2016-09-22 01:49:06 -04:00
|
|
|
end
|
2016-05-10 22:26:52 -04:00
|
|
|
end
|
2016-09-22 01:49:06 -04:00
|
|
|
ensure
|
|
|
|
@eval_thread = nil
|
2016-05-10 22:26:52 -04:00
|
|
|
end
|
|
|
|
|
2018-03-13 21:31:00 -04:00
|
|
|
def call(function_name, *arguments)
|
|
|
|
raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed
|
2018-03-13 20:57:25 -04:00
|
|
|
|
|
|
|
@eval_thread = Thread.current
|
2018-05-09 13:23:02 -04:00
|
|
|
isolate_mutex.synchronize do
|
2018-03-13 20:57:25 -04:00
|
|
|
timeout do
|
2018-03-13 21:31:00 -04:00
|
|
|
call_unsafe(function_name, *arguments)
|
2018-03-13 20:57:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@eval_thread = nil
|
|
|
|
end
|
|
|
|
|
2017-07-13 17:43:43 -04:00
|
|
|
def dispose
|
2018-05-09 13:23:02 -04:00
|
|
|
return if @disposed
|
|
|
|
isolate_mutex.synchronize do
|
|
|
|
dispose_unsafe
|
2017-07-13 17:43:43 -04:00
|
|
|
end
|
2018-05-09 13:23:02 -04:00
|
|
|
@disposed = true
|
|
|
|
@isolate = nil # allow it to be garbage collected, if set
|
2017-07-13 17:43:43 -04:00
|
|
|
end
|
|
|
|
|
2016-09-22 01:49:06 -04:00
|
|
|
|
2016-05-08 06:59:10 -04:00
|
|
|
def attach(name, callback)
|
2018-05-09 13:23:02 -04:00
|
|
|
raise(ContextDisposedError, 'attempted to call function on a disposed context!') if @disposed
|
2016-09-22 01:49:06 -04:00
|
|
|
|
|
|
|
wrapped = lambda do |*args|
|
|
|
|
begin
|
|
|
|
|
2018-06-26 22:15:03 -04:00
|
|
|
r = nil
|
|
|
|
|
2018-06-26 19:25:54 -04:00
|
|
|
begin
|
|
|
|
@callback_mutex.synchronize{
|
|
|
|
@callback_running = true
|
|
|
|
}
|
2018-06-26 22:15:03 -04:00
|
|
|
r = callback.call(*args)
|
2018-06-26 19:25:54 -04:00
|
|
|
ensure
|
|
|
|
@callback_mutex.synchronize{
|
|
|
|
@callback_running = false
|
|
|
|
}
|
|
|
|
end
|
2016-09-22 01:49:06 -04:00
|
|
|
|
2018-06-26 19:25:54 -04:00
|
|
|
# wait up to 2 seconds for this to be interrupted
|
|
|
|
# will very rarely be called cause #raise is called
|
|
|
|
# in another mutex
|
|
|
|
@callback_mutex.synchronize {
|
2016-09-22 01:49:06 -04:00
|
|
|
if @thread_raise_called
|
2018-06-26 19:25:54 -04:00
|
|
|
sleep 2
|
2016-09-22 01:49:06 -04:00
|
|
|
end
|
2018-06-26 19:25:54 -04:00
|
|
|
}
|
|
|
|
|
2018-06-26 22:15:03 -04:00
|
|
|
r
|
|
|
|
|
2018-06-26 19:25:54 -04:00
|
|
|
ensure
|
|
|
|
@callback_mutex.synchronize {
|
2016-09-22 01:49:06 -04:00
|
|
|
@thread_raise_called = false
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 13:23:02 -04:00
|
|
|
isolate_mutex.synchronize do
|
2016-09-22 01:49:06 -04:00
|
|
|
external = ExternalFunction.new(name, wrapped, self)
|
2016-05-16 21:34:24 -04:00
|
|
|
@functions["#{name}"] = external
|
2016-05-10 22:26:52 -04:00
|
|
|
end
|
2016-05-08 06:59:10 -04:00
|
|
|
end
|
|
|
|
|
2016-06-21 15:07:36 -04:00
|
|
|
private
|
|
|
|
|
2016-09-22 01:49:06 -04:00
|
|
|
def stop_attached
|
|
|
|
@callback_mutex.synchronize{
|
|
|
|
if @callback_running
|
|
|
|
@eval_thread.raise ScriptTerminatedError, "Terminated during callback"
|
|
|
|
@thread_raise_called = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def timeout(&blk)
|
|
|
|
return blk.call unless @timeout
|
|
|
|
|
2016-10-24 22:15:03 -04:00
|
|
|
mutex = Mutex.new
|
|
|
|
done = false
|
|
|
|
|
|
|
|
rp,wp = IO.pipe
|
2016-09-22 01:49:06 -04:00
|
|
|
|
2018-06-20 21:19:01 -04:00
|
|
|
t = Thread.new do
|
2016-09-22 01:49:06 -04:00
|
|
|
begin
|
2016-10-24 22:15:03 -04:00
|
|
|
result = IO.select([rp],[],[],(@timeout/1000.0))
|
2016-09-22 01:49:06 -04:00
|
|
|
if !result
|
2016-10-24 22:15:03 -04:00
|
|
|
mutex.synchronize do
|
|
|
|
stop unless done
|
|
|
|
end
|
2016-09-22 01:49:06 -04:00
|
|
|
end
|
2016-10-24 22:15:03 -04:00
|
|
|
rescue => e
|
|
|
|
STDERR.puts e
|
2016-09-22 01:49:06 -04:00
|
|
|
STDERR.puts "FAILED TO TERMINATE DUE TO TIMEOUT"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rval = blk.call
|
2016-10-24 22:15:03 -04:00
|
|
|
mutex.synchronize do
|
|
|
|
done = true
|
|
|
|
end
|
2016-09-22 01:49:06 -04:00
|
|
|
|
2016-10-24 22:15:03 -04:00
|
|
|
wp.write("done")
|
2018-06-20 21:19:01 -04:00
|
|
|
|
|
|
|
# ensure we do not leak a thread in state
|
|
|
|
t.join
|
|
|
|
|
2016-09-22 01:49:06 -04:00
|
|
|
rval
|
2019-11-10 20:20:26 -05:00
|
|
|
ensure
|
|
|
|
wp.close if wp
|
|
|
|
rp.close if rp
|
2016-09-22 01:49:06 -04:00
|
|
|
end
|
|
|
|
|
2016-06-21 15:07:36 -04:00
|
|
|
def check_init_options!(options)
|
|
|
|
assert_option_is_nil_or_a('isolate', options[:isolate], Isolate)
|
|
|
|
assert_option_is_nil_or_a('snapshot', options[:snapshot], Snapshot)
|
|
|
|
|
|
|
|
if options[:isolate] && options[:snapshot]
|
|
|
|
raise ArgumentError, 'can only pass one of isolate and snapshot options'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_option_is_nil_or_a(option_name, object, klass)
|
|
|
|
unless object.nil? || object.is_a?(klass)
|
|
|
|
raise ArgumentError, "#{option_name} must be a #{klass} object, passed a #{object.inspect}"
|
|
|
|
end
|
|
|
|
end
|
2016-05-06 03:08:06 -04:00
|
|
|
end
|
|
|
|
|
2016-06-28 12:16:26 -04:00
|
|
|
# `size` and `warmup!` public methods are defined in the C class
|
2016-06-16 18:23:28 -04:00
|
|
|
class Snapshot
|
2016-06-17 12:29:08 -04:00
|
|
|
def initialize(str = '')
|
2018-06-26 22:15:03 -04:00
|
|
|
# ensure it first can load
|
|
|
|
begin
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
ctx.eval(str)
|
|
|
|
rescue MiniRacer::RuntimeError => e
|
2018-10-15 04:58:14 -04:00
|
|
|
raise MiniRacer::SnapshotError.new, e.message, e.backtrace
|
2018-06-26 22:15:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@source = str
|
|
|
|
|
2016-06-16 18:23:28 -04:00
|
|
|
# defined in the C class
|
|
|
|
load(str)
|
|
|
|
end
|
2018-06-26 22:15:03 -04:00
|
|
|
|
|
|
|
def warmup!(src)
|
|
|
|
# we have to do something here
|
|
|
|
# we are bloating memory a bit but it is more correct
|
|
|
|
# than hitting an exception when attempty to compile invalid source
|
|
|
|
begin
|
|
|
|
ctx = MiniRacer::Context.new
|
|
|
|
ctx.eval(@source)
|
|
|
|
ctx.eval(src)
|
|
|
|
rescue MiniRacer::RuntimeError => e
|
2018-10-15 04:58:14 -04:00
|
|
|
raise MiniRacer::SnapshotError.new, e.message, e.backtrace
|
2018-06-26 22:15:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
warmup_unsafe!(src)
|
|
|
|
end
|
2016-06-16 18:23:28 -04:00
|
|
|
end
|
2016-05-04 02:54:51 -04:00
|
|
|
end
|