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
|
2016-05-11 20:14:33 -04:00
|
|
|
|
|
|
|
class EvalError < StandardError; end
|
|
|
|
class ScriptTerminatedError < EvalError; end
|
|
|
|
class ParseError < EvalError; end
|
2016-06-16 18:23:28 -04:00
|
|
|
class SnapshotError < StandardError; 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-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)
|
|
|
|
@functions = {}
|
2016-05-10 22:26:52 -04:00
|
|
|
@lock = Mutex.new
|
2016-05-11 04:44:24 -04:00
|
|
|
@timeout = nil
|
|
|
|
@current_exception = nil
|
|
|
|
|
2016-06-16 18:23:28 -04:00
|
|
|
snapshot = nil
|
2016-05-06 03:08:06 -04:00
|
|
|
if options
|
|
|
|
@timeout = options[:timeout]
|
2016-06-16 18:23:28 -04:00
|
|
|
snapshot = options[:snapshot]
|
2016-05-06 03:08:06 -04:00
|
|
|
end
|
2016-06-16 18:23:28 -04:00
|
|
|
|
|
|
|
unless snapshot.nil? || snapshot.is_a?(Snapshot)
|
|
|
|
raise ArgumentError, "snapshot must be a MiniRacer::Snapshot object, passed a #{snapshot.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# defined in the C class
|
|
|
|
init_with_snapshot(snapshot)
|
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
|
|
|
|
2016-05-10 22:26:52 -04:00
|
|
|
def eval(str)
|
|
|
|
@lock.synchronize do
|
2016-05-11 03:58:33 -04:00
|
|
|
@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)
|
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-05-06 03:08:06 -04:00
|
|
|
end
|
|
|
|
|
2016-06-17 12:29:08 -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 = '')
|
2016-06-16 18:23:28 -04:00
|
|
|
# defined in the C class
|
|
|
|
load(str)
|
|
|
|
end
|
|
|
|
end
|
2016-05-04 02:54:51 -04:00
|
|
|
end
|