Use built-in Win32API on JRuby

It's fixed for JRuby dedicatedly.
This commit is contained in:
aycabta 2019-10-05 23:46:16 +09:00
parent 309f6a7726
commit 136116819e

View File

@ -11,41 +11,45 @@ class Reline::Windows
[224, 79] => :ed_move_to_end, # End [224, 79] => :ed_move_to_end, # End
}.each_key(&:freeze).freeze }.each_key(&:freeze).freeze
class Win32API if defined? JRUBY_VERSION
DLL = {} require 'win32api'
TYPEMAP = {"0" => Fiddle::TYPE_VOID, "S" => Fiddle::TYPE_VOIDP, "I" => Fiddle::TYPE_LONG} else
POINTER_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q*' : 'l!*' class Win32API
DLL = {}
TYPEMAP = {"0" => Fiddle::TYPE_VOID, "S" => Fiddle::TYPE_VOIDP, "I" => Fiddle::TYPE_LONG}
POINTER_TYPE = Fiddle::SIZEOF_VOIDP == Fiddle::SIZEOF_LONG_LONG ? 'q*' : 'l!*'
WIN32_TYPES = "VPpNnLlIi" WIN32_TYPES = "VPpNnLlIi"
DL_TYPES = "0SSI" DL_TYPES = "0SSI"
def initialize(dllname, func, import, export = "0", calltype = :stdcall) def initialize(dllname, func, import, export = "0", calltype = :stdcall)
@proto = [import].join.tr(WIN32_TYPES, DL_TYPES).sub(/^(.)0*$/, '\1') @proto = [import].join.tr(WIN32_TYPES, DL_TYPES).sub(/^(.)0*$/, '\1')
import = @proto.chars.map {|win_type| TYPEMAP[win_type.tr(WIN32_TYPES, DL_TYPES)]} import = @proto.chars.map {|win_type| TYPEMAP[win_type.tr(WIN32_TYPES, DL_TYPES)]}
export = TYPEMAP[export.tr(WIN32_TYPES, DL_TYPES)] export = TYPEMAP[export.tr(WIN32_TYPES, DL_TYPES)]
calltype = Fiddle::Importer.const_get(:CALL_TYPE_TO_ABI)[calltype] calltype = Fiddle::Importer.const_get(:CALL_TYPE_TO_ABI)[calltype]
handle = DLL[dllname] ||= handle = DLL[dllname] ||=
begin begin
Fiddle.dlopen(dllname) Fiddle.dlopen(dllname)
rescue Fiddle::DLError rescue Fiddle::DLError
raise unless File.extname(dllname).empty? raise unless File.extname(dllname).empty?
Fiddle.dlopen(dllname + ".dll") Fiddle.dlopen(dllname + ".dll")
end end
@func = Fiddle::Function.new(handle[func], import, export, calltype) @func = Fiddle::Function.new(handle[func], import, export, calltype)
rescue Fiddle::DLError => e rescue Fiddle::DLError => e
raise LoadError, e.message, e.backtrace raise LoadError, e.message, e.backtrace
end end
def call(*args) def call(*args)
import = @proto.split("") import = @proto.split("")
args.each_with_index do |x, i| args.each_with_index do |x, i|
args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S" args[i], = [x == 0 ? nil : x].pack("p").unpack(POINTER_TYPE) if import[i] == "S"
args[i], = [x].pack("I").unpack("i") if import[i] == "I" args[i], = [x].pack("I").unpack("i") if import[i] == "I"
end
ret, = @func.call(*args)
return ret || 0
end end
ret, = @func.call(*args)
return ret || 0
end end
end end