Skip to content
Permalink
Browse files
first attempt at getting C-API updated to use Ruby obj over C++ obj
  • Loading branch information
chuckremes committed Oct 6, 2015
1 parent 77bd774 commit 20141ecebc58a5c3b3452b63bd0dddf941b680e8
Showing 1 changed file with 43 additions and 16 deletions.
@@ -4,6 +4,7 @@
#include "builtin/array.hpp"
#include "builtin/fixnum.hpp"
#include "builtin/io.hpp"
#include "builtin/string.hpp"
#include "builtin/thread.hpp"
#include "object_memory.hpp"
#include "primitives.hpp"
@@ -56,13 +57,17 @@ namespace rubinius {
}

RIO* Handle::as_rio(NativeMethodEnvironment* env) {
IO* io_obj = c_as<IO>(object());
//IO* io_obj = c_as<IO>(object());

if(type_ != cRIO) {
env->shared().capi_ds_lock().lock();

if(type_ != cRIO) {
int fd = (int)io_obj->descriptor()->to_native();
native_int fd = -1;
//int fd = (int)io_obj->descriptor()->to_native();
if(Fixnum* f = try_as<Fixnum>(env->get_object(rb_funcall(as_value(), rb_intern("fileno"), 0)))) {
fd = f->to_native();
}

env->shared().capi_ds_lock().unlock();

@@ -72,7 +77,8 @@ namespace rubinius {
rb_raise(rb_eIOError, "%s (%d)", err, errno);
}

FILE* f = fdopen(fd, flags_modestr(io_obj->mode()->to_native()));
//FILE* f = fdopen(fd, flags_modestr(io_obj->mode()->to_native()));
FILE* f = fdopen(fd, flags_modestr(c_as<Fixnum>(env->get_object(rb_funcall(as_value(), rb_intern("mode"), 0)))->to_native()));

if(!f) {
char buf[RBX_STRERROR_BUFSIZE];
@@ -165,8 +171,9 @@ extern "C" {
int rb_io_fd(VALUE io_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();

IO* io = c_as<IO>(env->get_object(io_handle));
return io->descriptor()->to_native();
//IO* io = c_as<IO>(env->get_object(io_handle));
//return io->descriptor()->to_native();
return c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("fileno"), 0)))->to_native();
}

long rb_io_fread(char* ptr, int len, FILE* f) {
@@ -353,8 +360,9 @@ extern "C" {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();

IO* io = c_as<IO>(env->get_object(io_handle));
io->set_nonblock(env->state());
//IO* io = c_as<IO>(env->get_object(io_handle));
//io->set_nonblock(env->state());
rb_funcall(io_handle, rb_intern("nonblock="), env->get_handle(cTrue));
}

VALUE rb_io_check_io(VALUE io) {
@@ -369,18 +377,20 @@ extern "C" {
void rb_io_check_closed(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
//IO* io = c_as<IO>(env->get_object(io_handle));

if(io->descriptor()->to_native() == -1) {
//if(io->descriptor()->to_native() == -1) {
if(c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("fileno"), 0)))->to_native() == -1) {
rb_raise(rb_eIOError, "closed stream");
}
}

void rb_io_check_readable(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
int io_mode = io->mode()->to_native() & O_ACCMODE;
//IO* io = c_as<IO>(env->get_object(io_handle));
//int io_mode = io->mode()->to_native() & O_ACCMODE;
int io_mode = c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("mode"), 0)))->to_native() & O_ACCMODE;
if(!(O_RDONLY == io_mode || O_RDWR == io_mode)) {
rb_raise(rb_eIOError, "not opened for reading");
}
@@ -389,25 +399,42 @@ extern "C" {
void rb_io_check_writable(rb_io_t* iot) {
VALUE io_handle = iot->handle;
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO* io = c_as<IO>(env->get_object(io_handle));
int io_mode = io->mode()->to_native() & O_ACCMODE;
//IO* io = c_as<IO>(env->get_object(io_handle));
//int io_mode = io->mode()->to_native() & O_ACCMODE;
int io_mode = c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("mode"), 0)))->to_native() & O_ACCMODE;
if(!(O_WRONLY == io_mode || O_RDWR == io_mode)) {
rb_raise(rb_eIOError, "not opened for writing");
}
}

void rb_update_max_fd(int fd) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO::update_max_fd(env->state(), fd);

//IO::update_max_fd(env->state(), fd);
VALUE fd_class = rb_path2class("IO::FileDescriptor");
VALUE descriptor = env->get_handle(Fixnum::from(fd));

rb_funcall(fd_class, rb_intern("update_max_fd"), descriptor);
}

void rb_fd_fix_cloexec(int fd) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
IO::new_open_fd(env->state(), fd);
//IO::new_open_fd(env->state(), fd);
VALUE fd_class = rb_path2class("IO::FileDescriptor");
VALUE descriptor = env->get_handle(Fixnum::from(fd));

rb_funcall(fd_class, rb_intern("new_open_fd"), descriptor);
}

int rb_cloexec_open(const char *pathname, int flags, int mode) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
return IO::open_with_cloexec(env->state(), pathname, mode, flags);
VALUE fd_class = rb_path2class("IO::FileDescriptor");
VALUE pathname_v = env->get_handle(String::create(env->state(), pathname));
VALUE flags_v = env->get_handle(Fixnum::from(flags));
VALUE mode_v = env->get_handle(Fixnum::from(mode));
//return -1; //IO::open_with_cloexec(env->state(), pathname, mode, flags);
VALUE result = rb_funcall(fd_class, rb_intern("open_with_cloexec"), pathname_v, flags_v, mode_v);

return c_as<Fixnum>(env->get_object(result))->to_native();
}
}

0 comments on commit 20141ec

Please sign in to comment.