Skip to content

Commit 20141ec

Browse files
committedOct 6, 2015
first attempt at getting C-API updated to use Ruby obj over C++ obj
1 parent 77bd774 commit 20141ec

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed
 

‎vm/capi/io.cpp

+43-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "builtin/array.hpp"
55
#include "builtin/fixnum.hpp"
66
#include "builtin/io.hpp"
7+
#include "builtin/string.hpp"
78
#include "builtin/thread.hpp"
89
#include "object_memory.hpp"
910
#include "primitives.hpp"
@@ -56,13 +57,17 @@ namespace rubinius {
5657
}
5758

5859
RIO* Handle::as_rio(NativeMethodEnvironment* env) {
59-
IO* io_obj = c_as<IO>(object());
60+
//IO* io_obj = c_as<IO>(object());
6061

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

6465
if(type_ != cRIO) {
65-
int fd = (int)io_obj->descriptor()->to_native();
66+
native_int fd = -1;
67+
//int fd = (int)io_obj->descriptor()->to_native();
68+
if(Fixnum* f = try_as<Fixnum>(env->get_object(rb_funcall(as_value(), rb_intern("fileno"), 0)))) {
69+
fd = f->to_native();
70+
}
6671

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

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

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

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

168-
IO* io = c_as<IO>(env->get_object(io_handle));
169-
return io->descriptor()->to_native();
174+
//IO* io = c_as<IO>(env->get_object(io_handle));
175+
//return io->descriptor()->to_native();
176+
return c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("fileno"), 0)))->to_native();
170177
}
171178

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

356-
IO* io = c_as<IO>(env->get_object(io_handle));
357-
io->set_nonblock(env->state());
363+
//IO* io = c_as<IO>(env->get_object(io_handle));
364+
//io->set_nonblock(env->state());
365+
rb_funcall(io_handle, rb_intern("nonblock="), env->get_handle(cTrue));
358366
}
359367

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

374-
if(io->descriptor()->to_native() == -1) {
382+
//if(io->descriptor()->to_native() == -1) {
383+
if(c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("fileno"), 0)))->to_native() == -1) {
375384
rb_raise(rb_eIOError, "closed stream");
376385
}
377386
}
378387

379388
void rb_io_check_readable(rb_io_t* iot) {
380389
VALUE io_handle = iot->handle;
381390
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
382-
IO* io = c_as<IO>(env->get_object(io_handle));
383-
int io_mode = io->mode()->to_native() & O_ACCMODE;
391+
//IO* io = c_as<IO>(env->get_object(io_handle));
392+
//int io_mode = io->mode()->to_native() & O_ACCMODE;
393+
int io_mode = c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("mode"), 0)))->to_native() & O_ACCMODE;
384394
if(!(O_RDONLY == io_mode || O_RDWR == io_mode)) {
385395
rb_raise(rb_eIOError, "not opened for reading");
386396
}
@@ -389,25 +399,42 @@ extern "C" {
389399
void rb_io_check_writable(rb_io_t* iot) {
390400
VALUE io_handle = iot->handle;
391401
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
392-
IO* io = c_as<IO>(env->get_object(io_handle));
393-
int io_mode = io->mode()->to_native() & O_ACCMODE;
402+
//IO* io = c_as<IO>(env->get_object(io_handle));
403+
//int io_mode = io->mode()->to_native() & O_ACCMODE;
404+
int io_mode = c_as<Fixnum>(env->get_object(rb_funcall(io_handle, rb_intern("mode"), 0)))->to_native() & O_ACCMODE;
394405
if(!(O_WRONLY == io_mode || O_RDWR == io_mode)) {
395406
rb_raise(rb_eIOError, "not opened for writing");
396407
}
397408
}
398409

399410
void rb_update_max_fd(int fd) {
400411
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
401-
IO::update_max_fd(env->state(), fd);
412+
413+
//IO::update_max_fd(env->state(), fd);
414+
VALUE fd_class = rb_path2class("IO::FileDescriptor");
415+
VALUE descriptor = env->get_handle(Fixnum::from(fd));
416+
417+
rb_funcall(fd_class, rb_intern("update_max_fd"), descriptor);
402418
}
403419

404420
void rb_fd_fix_cloexec(int fd) {
405421
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
406-
IO::new_open_fd(env->state(), fd);
422+
//IO::new_open_fd(env->state(), fd);
423+
VALUE fd_class = rb_path2class("IO::FileDescriptor");
424+
VALUE descriptor = env->get_handle(Fixnum::from(fd));
425+
426+
rb_funcall(fd_class, rb_intern("new_open_fd"), descriptor);
407427
}
408428

409429
int rb_cloexec_open(const char *pathname, int flags, int mode) {
410430
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
411-
return IO::open_with_cloexec(env->state(), pathname, mode, flags);
431+
VALUE fd_class = rb_path2class("IO::FileDescriptor");
432+
VALUE pathname_v = env->get_handle(String::create(env->state(), pathname));
433+
VALUE flags_v = env->get_handle(Fixnum::from(flags));
434+
VALUE mode_v = env->get_handle(Fixnum::from(mode));
435+
//return -1; //IO::open_with_cloexec(env->state(), pathname, mode, flags);
436+
VALUE result = rb_funcall(fd_class, rb_intern("open_with_cloexec"), pathname_v, flags_v, mode_v);
437+
438+
return c_as<Fixnum>(env->get_object(result))->to_native();
412439
}
413440
}

0 commit comments

Comments
 (0)
Please sign in to comment.