Sha256: bcd691ba322c3388f031ed9e85561be97bb7bbe70b725149899ad25ad2735a0b

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module Csso
  # Utility for calling into the JavaScript runtime.
  module CallJS

    # @private
    # Wrap JavaScript invocations with uniform error handling
    #
    # @yield code to wrap
    def calljs err_cls=WrappedError
      lock do
        yield
      end
    rescue V8::JSError => e
      raise err_cls.new(e)
    end

    # @private
    # Ensure proper locking before entering the V8 API
    #
    # @yield code to wrap in lock
    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

  class WrappedError < StandardError

    # Copies over `error`'s message and backtrace
    # @param [V8::JSError] error native error
    def initialize(error)
      super(error.message)
      @backtrace = error.backtrace
    end

    # @return [Array] the backtrace frames
    def backtrace
      @backtrace
    end
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
csso-rails-0.0.3 lib/csso/utils.rb
csso-rails-0.0.2 lib/csso/utils.rb
csso-rails-0.0.1 lib/csso/utils.rb