Sha256: d02f894c546118f5c6003d40f5223fc6acd496bf95d32e99543a9ef4ef1a6f10

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require "execjs/runtime"
require "json"

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

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

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

      def call(identifier, *args)
        @ctx.exec_string("__execjs_duktape_call = #{identifier}", '(execjs)')
        @ctx.call_prop("__execjs_duktape_call", *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

7 entries across 6 versions & 4 rubygems

Version Path
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/execjs-2.10.0/lib/execjs/duktape_runtime.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/execjs-2.10.0/lib/execjs/duktape_runtime.rb
execjs-2.10.0 lib/execjs/duktape_runtime.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/execjs-2.9.1/lib/execjs/duktape_runtime.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/execjs-2.9.1/lib/execjs/duktape_runtime.rb
execjs-2.9.1 lib/execjs/duktape_runtime.rb
execjs-2.9.0 lib/execjs/duktape_runtime.rb