Sha256: 29b0a05a13853831160072317022b482c6447c4a16c4b1d9d0b16e23296b87de

Contents?: true

Size: 1.16 KB

Versions: 16

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require 'mini_racer'
require 'opal/paths'

module Opal
  module CliRunners
    class MiniRacer
      def self.call(data)
        ::MiniRacer::Platform.set_flags! :harmony

        builder = data.fetch(:builder).call
        output = data.fetch(:output)
        # TODO: pass it
        argv = data.fetch(:argv)

        # MiniRacer doesn't like to fork. Let's build Opal first
        # in a forked environment.
        code = builder.to_s + "\n" + builder.source_map.to_data_uri_comment

        v8 = ::MiniRacer::Context.new
        v8.attach('prompt', ->(_msg = '') { $stdin.gets&.chomp })
        v8.attach('console.log', ->(i) { output.print(i); output.flush })
        v8.attach('console.warn', ->(i) { $stderr.print(i); $stderr.flush })
        v8.attach('crypto.randomBytes', method(:random_bytes).to_proc)
        v8.attach('opalminiracer.exit', ->(status) { Kernel.exit(status) })
        v8.attach('opalminiracer.argv', argv)

        v8.eval(code)
      end

      # A polyfill so that SecureRandom works in repl correctly.
      def self.random_bytes(bytes)
        ::SecureRandom.bytes(bytes).split('').map(&:ord)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 lib/opal/cli_runners/mini_racer.rb
opal-1.8.2 lib/opal/cli_runners/mini_racer.rb
opal-1.8.1 lib/opal/cli_runners/mini_racer.rb
opal-1.8.0 lib/opal/cli_runners/mini_racer.rb
opal-1.8.0.beta1 lib/opal/cli_runners/mini_racer.rb
opal-1.7.4 lib/opal/cli_runners/mini_racer.rb
opal-1.8.0.alpha1 lib/opal/cli_runners/mini_racer.rb
opal-1.7.3 lib/opal/cli_runners/mini_racer.rb
opal-1.7.2 lib/opal/cli_runners/mini_racer.rb
opal-1.7.1 lib/opal/cli_runners/mini_racer.rb
opal-1.7.0 lib/opal/cli_runners/mini_racer.rb
opal-1.7.0.rc1 lib/opal/cli_runners/mini_racer.rb
opal-1.6.1 lib/opal/cli_runners/mini_racer.rb
opal-1.6.0 lib/opal/cli_runners/mini_racer.rb
opal-1.6.0.rc1 lib/opal/cli_runners/mini_racer.rb
opal-1.6.0.alpha1 lib/opal/cli_runners/mini_racer.rb