Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update WDM for newer Ruby versions #29

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in wdm.gemspec
gemspec

group :development do
gem 'rake-compiler'
gem 'rspec'
gem 'guard-rspec'
gem 'guard-shell'
end
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ end

desc "Open an irb session preloaded with WDM"
task :console do
sh "irb -rubygems -I lib -r wdm"
sh "irb -I lib -r wdm"
end
37 changes: 20 additions & 17 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
---
image: Visual Studio 2022
version: '{build}'

skip_tags: true

environment:
matrix:
- ruby_version: "193"
devkit: C:\Ruby193\DevKit
- ruby_version: "200"
devkit: C:\Ruby21\DevKit
- ruby_version: "200-x64"
devkit: C:\Ruby21-x64\DevKit
- ruby_version: "21"
devkit: C:\Ruby21\DevKit
- ruby_version: "21-x64"
devkit: C:\Ruby21-x64\DevKit
- ruby_version: "22"
devkit: C:\Ruby21\DevKit
- ruby_version: "22-x64"
devkit: C:\Ruby21-x64\DevKit
- ruby_version: "head-x64"
- ruby_version: "25"
- ruby_version: "30"
- ruby_version: "33-x64"

install:
init:
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
- "%devkit%\\devkitvars.bat"
- gem install bundler --no-rdoc --no-ri

install:
- ps: |
if ($env:ruby_version -like "*head*") {
$(new-object net.webclient).DownloadFile("https://github.com/oneclick/rubyinstaller2/releases/download/rubyinstaller-head/rubyinstaller-$env:ruby_version.exe", "$pwd/ruby-setup.exe")
cmd /c ruby-setup.exe /verysilent /currentuser /dir=C:/Ruby$env:ruby_version
}
- ruby --version
- gem --version
- ridk version
- ridk enable
- c:/msys64/usr/bin/bash -lc "pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-gcc"
- gcc -v
- gem install bundler:2.3.27 --conservative --no-doc
- bundler env
- bundle install --retry=3

Expand Down
11 changes: 4 additions & 7 deletions ext/wdm/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@ def windows?
end

if windows? and
have_library("kernel32") and
have_header("windows.h") and
have_header("ruby.h") and
have_const('HAVE_RUBY_ENCODING_H')
have_library("kernel32") and
have_header("windows.h")
then
have_func('rb_thread_call_without_gvl')
generate_makefile()
generate_makefile()
else
generate_dummy_makefile()
generate_dummy_makefile()
end
4 changes: 1 addition & 3 deletions ext/wdm/rb_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ extract_absolute_path_from_notification(const LPWSTR base_dir, const PFILE_NOTIF
multibyte_filepath_buffer_size - 1, // -1 because this func takes the chars count, not bytes count
wdm_rb_enc_utf8);

OBJ_TAINT(path);

return path;
}

Expand Down Expand Up @@ -196,4 +194,4 @@ wdm_rb_change_init()

rb_define_attr(cWDM_Change, "path", 1, 0);
rb_define_attr(cWDM_Change, "type", 1, 0);
}
}
42 changes: 23 additions & 19 deletions ext/wdm/rb_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void CALLBACK handle_entry_change(DWORD, DWORD, LPOVERLAPPED);
static BOOL register_monitoring_entry(WDM_PEntry);
static DWORD WINAPI start_monitoring(LPVOID);

static VALUE wait_for_changes(LPVOID);
static void *wait_for_changes(void *);
static void process_changes(WDM_PQueue);
static void stop_monitoring(LPVOID);
static VALUE rb_monitor_run_bang(VALUE);
Expand Down Expand Up @@ -99,14 +99,24 @@ monitor_free(LPVOID param)
wdm_monitor_free(monitor);
}

static const rb_data_type_t monitor_data_type = {
.wrap_struct_name = "WDM::Monitor",
.function = {
.dmark = monitor_mark,
.dfree = monitor_free,
.dsize = NULL,
},
.flags = 0
};

static VALUE
rb_monitor_alloc(VALUE self)
{
WDM_DEBUG("--------------------------------");
WDM_DEBUG("Allocating a new monitor object!");
WDM_DEBUG("--------------------------------");

return Data_Wrap_Struct(self, monitor_mark, monitor_free, wdm_monitor_new());
return TypedData_Wrap_Struct(self, &monitor_data_type, wdm_monitor_new());
}

static DWORD
Expand Down Expand Up @@ -156,7 +166,7 @@ combined_watch(BOOL recursively, int argc, VALUE *argv, VALUE self)
// TODO: Maybe raise a more user-friendly error?
rb_need_block();

Data_Get_Struct(self, WDM_Monitor, monitor);
TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor);

EnterCriticalSection(&monitor->lock);
running = monitor->running;
Expand Down Expand Up @@ -367,14 +377,14 @@ start_monitoring(LPVOID param)
return 0;
}

static VALUE
wait_for_changes(LPVOID param)
static void *
wait_for_changes(void *param)
{
HANDLE process_event;
HANDLE process_event = (HANDLE)param;
VALUE rb_res;

process_event = (HANDLE)param;

return WaitForSingleObject(process_event, INFINITE) == WAIT_OBJECT_0 ? Qtrue : Qfalse;
rb_res = WaitForSingleObject(process_event, INFINITE) == WAIT_OBJECT_0 ? Qtrue : Qfalse;
return (void *)rb_res;
}

static void
Expand Down Expand Up @@ -467,7 +477,7 @@ rb_monitor_run_bang(VALUE self)

WDM_DEBUG("Running the monitor!");

Data_Get_Struct(self, WDM_Monitor, monitor);
TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor);
already_running = FALSE;

EnterCriticalSection(&monitor->lock);
Expand Down Expand Up @@ -503,13 +513,7 @@ rb_monitor_run_bang(VALUE self)

while ( monitor->running ) {

// Ruby 2.2 removed the 'rb_thread_blocking_region' function. Hence, we now need
// to check if the replacement function is defined and use it if it's available.
#ifdef HAVE_RB_THREAD_CALL_WITHOUT_GVL
waiting_succeeded = rb_thread_call_without_gvl(wait_for_changes, monitor->process_event, stop_monitoring, monitor);
#else
waiting_succeeded = rb_thread_blocking_region(wait_for_changes, monitor->process_event, stop_monitoring, monitor);
#endif
waiting_succeeded = (VALUE)rb_thread_call_without_gvl(wait_for_changes, monitor->process_event, stop_monitoring, monitor);

if ( waiting_succeeded == Qfalse ) {
rb_raise(eWDM_Error, "Failed while waiting for a change in the watched directories!");
Expand All @@ -535,7 +539,7 @@ rb_monitor_stop(VALUE self)
{
WDM_PMonitor monitor;

Data_Get_Struct(self, WDM_Monitor, monitor);
TypedData_Get_Struct(self, WDM_Monitor, &monitor_data_type, monitor);

stop_monitoring(monitor);

Expand Down Expand Up @@ -572,4 +576,4 @@ wdm_rb_monitor_init()
rb_define_method(cWDM_Monitor, "watch_recursively", RUBY_METHOD_FUNC(rb_monitor_watch_recursively), -1);
rb_define_method(cWDM_Monitor, "run!", RUBY_METHOD_FUNC(rb_monitor_run_bang), 0);
rb_define_method(cWDM_Monitor, "stop", RUBY_METHOD_FUNC(rb_monitor_stop), 0);
}
}
7 changes: 4 additions & 3 deletions ext/wdm/wdm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Support Windows 2000 and later,
// this is needed for 'GetLongPathNameW' (both of the following defines)
#ifndef WINVER
#define WINVER 0x0500
#define WINVER 0x0500
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
Expand All @@ -14,13 +14,14 @@
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN
#define VC_EXTRALEAN
#endif

#include <Windows.h>

#include <ruby.h>
#include <ruby/encoding.h>
#include <ruby/thread.h>

#ifndef WDM_H
#define WDM_H
Expand Down Expand Up @@ -79,4 +80,4 @@ void Init_wdm_ext();
}
#endif // __cplusplus

#endif // WDM_H
#endif // WDM_H
4 changes: 2 additions & 2 deletions spec/support/fixture_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def fixture

ensure
FileUtils.cd pwd
FileUtils.rm_rf(path) if File.exists?(path)
FileUtils.rm_rf(path) if File.exist?(path)
end
end
end
end
10 changes: 1 addition & 9 deletions spec/wdm/monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@
end
end

it 'marks changed paths as tainted' do
result = run_with_fixture(subject) do
touch 'file.txt'
end

expect(result.change.path).to be_tainted
end

it 'reports changes with absolute paths even when passed relative directory to watch' do
fixture do |dir|
relative_dir = Pathname.new(dir).relative_path_from(Pathname.new(Dir.pwd)).to_s
Expand Down Expand Up @@ -227,4 +219,4 @@
expect(result.change.path.encoding.name).to be == "UTF-8"
end
end
end
end
9 changes: 1 addition & 8 deletions wdm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,5 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.version = '0.1.1'

gem.required_ruby_version = '>= 1.9.2'

gem.add_development_dependency 'rake-compiler'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'guard-shell'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'devkit'
gem.required_ruby_version = '>= 2.5'
end