Skip to content

Commit

Permalink
Fixes for Process::Status changes in Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
MSP-Greg committed Jun 29, 2021
1 parent ec27b62 commit daeb121
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def find_openssl_library
add_define "HAVE_KQUEUE" if have_header("sys/event.h") && have_header("sys/queue.h")
end

# Add for changes to Process::Status in Ruby 3
add_define("IS_RUBY_3_OR_LATER") if RUBY_VERSION > "3.0"

# Adjust number of file descriptors (FD) on Windows

if RbConfig::CONFIG["host_os"] =~ /mingw/
Expand Down
36 changes: 30 additions & 6 deletions ext/rubymain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,24 @@ static VALUE Intern_proxy_target_unbound;
static VALUE Intern_proxy_completed;
static VALUE Intern_connection_completed;

static VALUE rb_cProcStatus;
static VALUE rb_cProcessStatus;

#ifdef IS_RUBY_3_OR_LATER
struct rb_process_status {
rb_pid_t pid;
int status;
int error;
};

static const rb_data_type_t rb_process_status_type = {
.wrap_struct_name = "Process::Status",
.function = {
.dfree = RUBY_DEFAULT_FREE,
},
.data = NULL,
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
};
#endif

struct em_event {
uintptr_t signature;
Expand Down Expand Up @@ -553,11 +570,18 @@ static VALUE t_get_subprocess_status (VALUE self UNUSED, VALUE signature)

if (evma_get_subprocess_status (NUM2BSIG (signature), &status)) {
if (evma_get_subprocess_pid (NUM2BSIG (signature), &pid)) {
proc_status = rb_obj_alloc(rb_cProcStatus);

#ifdef IS_RUBY_3_OR_LATER
struct rb_process_status *data = NULL;
proc_status = TypedData_Make_Struct(rb_cProcessStatus, struct rb_process_status, &rb_process_status_type, data);
data->pid = pid;
data->status = status;
#else
proc_status = rb_obj_alloc(rb_cProcessStatus);
/* MRI Ruby uses hidden instance vars */
rb_iv_set(proc_status, "status", INT2FIX(status));
rb_iv_set(proc_status, "pid", INT2FIX(pid));
rb_ivar_set(proc_status, rb_intern_const("status"), INT2FIX(status));
rb_ivar_set(proc_status, rb_intern_const("pid"), INT2FIX(pid));
#endif

#ifdef RUBINIUS
/* Rubinius uses standard instance vars */
Expand All @@ -572,7 +596,7 @@ static VALUE t_get_subprocess_status (VALUE self UNUSED, VALUE signature)
#endif
}
}

rb_obj_freeze(proc_status);
return proc_status;
}

Expand Down Expand Up @@ -1431,7 +1455,7 @@ extern "C" void Init_rubyeventmachine()
{
// Lookup Process::Status for get_subprocess_status
VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
rb_cProcessStatus = rb_const_get(rb_mProcess, rb_intern("Status"));

// Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
Intern_at_signature = rb_intern ("@signature");
Expand Down

0 comments on commit daeb121

Please sign in to comment.