Sha256: 528482d9add2f0af0a358dffdf760c610f5c1a26add0ffae74c29195647bcc4b

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

module PAC
  module Runtimes
    class RubyRacerRuntime
      class Context
        def initialize(source = "")
          source = source.encode("UTF-8") if source.respond_to?(:encode)

          lock do
            @v8_context = V8::Context.new
            @v8_context.eval(source)
          end
        end

        def include(mod)
          (mod.methods - Module.methods).each do |name|
            @v8_context[name] = mod.method(name)
          end
        end

        def call(properties, *args)
          lock do
            begin
              unbox @v8_context.eval(properties).call(*args)
            rescue ::V8::JSError => e
              if e.value["name"] == "SyntaxError"
                raise RuntimeError, e.message
              else
                raise ProgramError, e.message
              end
            end
          end
        end

        def unbox(value)
          case value
          when ::V8::Function
            nil
          when ::V8::Array
            value.map { |v| unbox(v) }
          when ::V8::Object
            value.inject({}) do |vs, (k, v)|
              vs[k] = unbox(v) unless v.is_a?(::V8::Function)
              vs
            end
          when String
            value.respond_to?(:force_encoding) ? value.force_encoding("UTF-8") : value
          else
            value
          end
        end

        private
          def lock
            result, exception = nil, nil
            V8::C::Locker() do
              begin
                result = yield
              rescue Exception => e
                exception = e
              end
            end

            if exception
              raise exception
            else
              result
            end
          end
      end

      def name
        "therubyracer (V8)"
      end

      def compile(source)
        Context.new(source)
      end

      def available?
        require "v8"
        true
      rescue LoadError
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pac-1.0.0 lib/pac/runtimes/rubyracer.rb