Sha256: b310924b6a437eadef0a9c40d97b4a6b55ac7e24a91f83f90bb2b5401f442b2f

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

require "execjs/runtime"
require "json"

module ExecJS
  class DuktapeRuntime < Runtime
    class Context < Runtime::Context
      def initialize(runtime, source = "")
        @ctx = Duktape::Context.new(complex_object: nil)
        @ctx.exec_string(encode(source), '(execjs)')
      rescue Exception => e
        raise wrap_error(e)
      end

      def exec(source, options = {})
        return unless /\S/ =~ source
        @ctx.eval_string("(function(){#{encode(source)}})()", '(execjs)')
      rescue Exception => e
        raise wrap_error(e)
      end

      def eval(source, options = {})
        return unless /\S/ =~ source
        @ctx.eval_string("(#{encode(source)})", '(execjs)')
      rescue Exception => e
        raise wrap_error(e)
      end

      def call(identifier, *args)
        @ctx.call_prop(identifier.split("."), *args)
      rescue Exception => e
        raise wrap_error(e)
      end

      private
        def wrap_error(e)
          klass = case e
          when Duktape::SyntaxError
            RuntimeError
          when Duktape::Error
            ProgramError
          when Duktape::InternalError
            RuntimeError
          end

          if klass
            re = / \(line (\d+)\)$/
            lineno = e.message[re, 1] || 1
            error = klass.new(e.message.sub(re, ""))
            error.set_backtrace(["(execjs):#{lineno}"] + e.backtrace)
            error
          else
            e
          end
        end
    end

    def name
      "Duktape"
    end

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

Version data entries

13 entries across 13 versions & 6 rubygems

Version Path
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/execjs-2.6.0/lib/execjs/duktape_runtime.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/execjs-2.6.0/lib/execjs/duktape_runtime.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/execjs-2.6.0/lib/execjs/duktape_runtime.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/execjs-2.6.0/lib/execjs/duktape_runtime.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/execjs-2.6.0/lib/execjs/duktape_runtime.rb
execjs-2.6.0 lib/execjs/duktape_runtime.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/execjs-2.5.2/lib/execjs/duktape_runtime.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/execjs-2.5.2/lib/execjs/duktape_runtime.rb
scoot-0.0.4 .bundle/gems/ruby/2.2.0/gems/execjs-2.5.2/lib/execjs/duktape_runtime.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/execjs-2.5.2/lib/execjs/duktape_runtime.rb
execjs-2.5.2 lib/execjs/duktape_runtime.rb
execjs-2.5.1 lib/execjs/duktape_runtime.rb
execjs-2.5.0 lib/execjs/duktape_runtime.rb