Sha256: 6bf1cf0019eccac9ee4080ddfd0e3de85b2abcc6f53af0a50dac029abed28594

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

class Code
  EMPTY_STRING = ""
  GLOBALS = %i[output error context object].freeze
  DEFAULT_TIMEOUT = 0

  def initialize(
    input,
    output: StringIO.new,
    error: StringIO.new,
    timeout: DEFAULT_TIMEOUT,
    ruby: {}
  )
    @input = input
    @parsed = Timeout.timeout(timeout) { ::Code::Parser.parse(input).to_raw }
    @output = output
    @error = error
    @timeout = timeout || DEFAULT_TIMEOUT
    @ruby = ::Code::Ruby.to_code(ruby || {}).code_to_context
  end

  def self.evaluate(
    input,
    context = EMPTY_STRING,
    output: StringIO.new,
    error: StringIO.new,
    timeout: DEFAULT_TIMEOUT,
    ruby: {}
  )
    new(input, output:, error:, timeout:, ruby:).evaluate(context)
  end

  def evaluate(context = EMPTY_STRING)
    Timeout.timeout(timeout) do
      context =
        if context == EMPTY_STRING
          Object::Context.new
        else
          Code.evaluate(context, timeout:, output:, error:, ruby:).code_to_context
        end

      context = ruby.merge(context)

      Node::Code.new(parsed).evaluate(context:, output:, error:)
    end
  end

  private

  attr_reader :input, :parsed, :timeout, :output, :error, :ruby
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
code-ruby-0.7.4 lib/code.rb
code-ruby-0.7.3 lib/code.rb
code-ruby-0.7.2 lib/code.rb
code-ruby-0.7.1 lib/code.rb
code-ruby-0.7.0 lib/code.rb