Skip to content

Commit

Permalink
Support absolute paths in CRYSTAL_INTERPRETER_LOADER_INFO (#14147)
Browse files Browse the repository at this point in the history
  • Loading branch information
HertzDevil authored Jan 1, 2024
1 parent 4b5ef59 commit 6bfc90d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
12 changes: 4 additions & 8 deletions src/compiler/crystal/interpreter/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -410,13 +410,6 @@ class Crystal::Repl::Context
args.delete("-lgc")

Crystal::Loader.parse(args).tap do |loader|
if ENV["CRYSTAL_INTERPRETER_LOADER_INFO"]?.presence
STDERR.puts "Crystal::Loader loaded libraries:"
loader.loaded_libraries.each do |path|
STDERR.puts " #{path}"
end
end

# FIXME: Part 2: This is a workaround for initial integration of the interpreter:
# We append a handle to the current executable (i.e. the compiler program)
# to the loader's handle list. This gives the loader access to all the symbols in the compiler program,
Expand All @@ -426,7 +419,10 @@ class Crystal::Repl::Context
loader.load_current_program_handle

if ENV["CRYSTAL_INTERPRETER_LOADER_INFO"]?.presence
STDERR.puts " current program handle"
STDERR.puts "Crystal::Loader loaded libraries:"
loader.loaded_libraries.each do |path|
STDERR.puts " #{path}"
end
end
end
}
Expand Down
16 changes: 15 additions & 1 deletion src/compiler/crystal/loader/msvc.cr
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class Crystal::Loader
return false unless handle

@handles << handle
@loaded_libraries << dll
@loaded_libraries << (module_filename(handle) || dll)
end

true
Expand All @@ -162,6 +162,7 @@ class Crystal::Loader
def load_current_program_handle
if LibC.GetModuleHandleExW(0, nil, out hmodule) != 0
@handles << hmodule
@loaded_libraries << (Process.executable_path || "current program handle")
end
end

Expand All @@ -172,6 +173,19 @@ class Crystal::Loader
@handles.clear
end

private def module_filename(handle)
Crystal::System.retry_wstr_buffer do |buffer, small_buf|
len = LibC.GetModuleFileNameW(handle, buffer, buffer.size)
if 0 < len < buffer.size
break String.from_utf16(buffer[0, len])
elsif small_buf && len == buffer.size
next 32767 # big enough. 32767 is the maximum total path length of UNC path.
else
break nil
end
end
end

# Returns a list of directories used as the default search paths.
#
# For MSVC this is simply the contents of the `LIB` environment variable,
Expand Down
1 change: 1 addition & 0 deletions src/compiler/crystal/loader/unix.cr
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Crystal::Loader
def load_current_program_handle
if program_handle = LibC.dlopen(nil, LibC::RTLD_LAZY | LibC::RTLD_GLOBAL)
@handles << program_handle
@loaded_libraries << (Process.executable_path || "current program handle")
end
end

Expand Down

0 comments on commit 6bfc90d

Please sign in to comment.