Skip to content

Commit

Permalink
replace calls to C++ IO obj with Ruby IO obj
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckremes committed Jan 22, 2016
1 parent 910d9f8 commit 03319cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions kernel/common/io.rb
Expand Up @@ -138,6 +138,10 @@ def self.update_max_fd(new_fd)
@@max_descriptors.get_and_set(new_fd)
end

def self.max_fd
@@max_descriptors.get
end

def self.get_flags(fd)
if IO::F_GETFL
if FFI.call_failed?(flags = FFI::Platform::POSIX.fcntl(fd, IO::F_GETFL, 0))
Expand Down
21 changes: 16 additions & 5 deletions vm/builtin/system.cpp
Expand Up @@ -13,6 +13,7 @@
#include "builtin/location.hpp"
#include "builtin/lookup_table.hpp"
#include "builtin/method_table.hpp"
#include "builtin/native_method.hpp"
#include "builtin/thread.hpp"
#include "builtin/tuple.hpp"
#include "builtin/string.hpp"
Expand Down Expand Up @@ -340,7 +341,10 @@ namespace rubinius {
}

if(CBOOL(table->has_key(state, state->symbol("close_others")))) {
int max = IO::max_descriptors();
NativeMethodEnvironment* native_env = state->vm()->native_method_environment;
Class* fd_class = (Class*) G(io)->get_const(state, "FileDescriptor");
Fixnum* max_fd = (Fixnum*)fd_class->send(state, native_env->current_call_frame(), state->symbol("max_fd"));
int max = max_fd->to_native();
int flags;

for(int fd = STDERR_FILENO + 1; fd < max; fd++) {
Expand All @@ -354,13 +358,20 @@ namespace rubinius {
table->fetch(state, state->symbol("assign_fd"))))
{
native_int size = assign->size();
NativeMethodEnvironment* native_env = state->vm()->native_method_environment;
Class* fd_class = (Class*) G(io)->get_const(state, "FileDescriptor");
for(native_int i = 0; i < size; i += 4) {
int from = as<Fixnum>(assign->get(state, i))->to_native();
int mode = as<Fixnum>(assign->get(state, i + 2))->to_native();
int perm = as<Fixnum>(assign->get(state, i + 3))->to_native();
const char* name = as<String>(assign->get(state, i + 1))->c_str_null_safe(state);
Fixnum* mode = (Fixnum*)assign->get(state, i + 2);
Fixnum* perm = (Fixnum*)assign->get(state, i + 3);
String* name = (String*)assign->get(state, i + 1);

int to = IO::open_with_cloexec(state, name, mode, perm);
Array* ary = Array::create(state, 3);
ary->set(state, 0, name);
ary->set(state, 1, mode);
ary->set(state, 2, perm);

int to = as<Fixnum>(fd_class->send(state, native_env->current_call_frame(), state->symbol("open_with_cloexec"), ary))->to_native();
redirect_file_descriptor(from, to);
}
}
Expand Down

0 comments on commit 03319cd

Please sign in to comment.