Class: Alterant::Alterant
- Inherits:
-
Object
- Object
- Alterant::Alterant
- Defined in:
- lib/alterant/alterant.rb
Instance Attribute Summary collapse
-
#basedir ⇒ Object
readonly
Returns the value of attribute basedir.
Instance Method Summary collapse
-
#execute(timeout: 500, max_memory: 5000000) ⇒ Object
timeout is in ms returns a hash.
-
#initialize(input:, modifier:, filename:, options: {}) ⇒ Alterant
constructor
input is a hash filename is the modifier filename use for the backtrace modifier is the script in string.
Constructor Details
#initialize(input:, modifier:, filename:, options: {}) ⇒ Alterant
input is a hash filename is the modifier filename use for the backtrace modifier is the script in string
8 9 10 11 12 13 14 |
# File 'lib/alterant/alterant.rb', line 8 def initialize(input:, modifier:, filename:, options: {}) @modifier = modifier @filename = filename @input = input @basedir = [:basedir] @js_preload = [:js_preload] || [] end |
Instance Attribute Details
#basedir ⇒ Object (readonly)
Returns the value of attribute basedir.
3 4 5 |
# File 'lib/alterant/alterant.rb', line 3 def basedir @basedir end |
Instance Method Details
#execute(timeout: 500, max_memory: 5000000) ⇒ Object
timeout is in ms returns a hash
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/alterant/alterant.rb', line 18 def execute(timeout: 500, max_memory: 5000000) jpath = ::Alterant::Helpers::Jpath.new result = [] snapshot = MiniRacer::Snapshot.new("$$ = #{@input.to_json};\n" + @js_preload.join("\n")) # this is more efficient but we lose debug info (filename) of helper classes isolate = MiniRacer::Isolate.new(snapshot) @input.each_with_index do |input, idx| ctx = ::MiniRacer::Context.new(isolate: isolate, timeout: timeout, max_memory: max_memory) ctx.eval("$ = #{input.to_json}") ctx.eval("$['fetch'] = function(key) { return jpath.fetch(JSON.stringify($), key); }") ctx.attach('jpath.fetch', proc{|x, y| jpath.fetch(x, y)}) ctx.attach('console.log', proc{|x| STDERR.puts("DEBUG: #{x.inspect}") if $debug }) ctx.attach('console.exception', proc{|x| raise ::Alterant::RuntimeError, x }) ctx.attach('$$.push', proc{|x| result << x }) ctx.attach('$.index', proc{ idx }) ctx.attach('YamlReader', ::Alterant::Classes::YamlReader.new(self, ctx)) ctx.attach('JsonReader', ::Alterant::Classes::JsonReader.new(self, ctx)) ctx.eval(@modifier, filename: @filename) pre_convert = ctx.eval("JSON.stringify($)") converted = JSON.parse(pre_convert) result << converted ctx.dispose isolate.idle_notification(100) rescue ::MiniRacer::RuntimeError => exc if $debug raise else raise ::Alterant::ParseError, "part: #{idx} - #{exc.}, #{exc.backtrace.first}" end rescue ::Alterant::AlterantError => exc STDERR.puts exc..red return nil end return result end |