ext/noderb_extension/noderb_fs.c in noderb-0.0.10 vs ext/noderb_extension/noderb_fs.c in noderb-0.0.11

- old
+ new

@@ -5,17 +5,17 @@ #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> typedef struct { - long target; + VALUE target; char* buffer; } nodeRb_file_handle; void nodeRb_fs_operation_callback(uv_fs_t* handle){ nodeRb_file_handle* data = (nodeRb_file_handle*) handle->data; - VALUE target = nodeRb_get_class_from_id(data->target); + VALUE target = data->target; if(handle->result == -1){ rb_funcall(target, rb_intern("call"), 1, Qnil); }else{ switch(handle->fs_type) { case UV_FS_UNLINK: @@ -61,19 +61,20 @@ } break; } } nodeRb_unregister_instance(target); - free(handle->data); + free(data); uv_fs_req_cleanup(handle); } VALUE nodeRb_fs_operation(VALUE self, VALUE roperation, VALUE path, VALUE params, VALUE callback){ uv_fs_t* handle = malloc(sizeof(uv_fs_t)); nodeRb_file_handle* data = malloc(sizeof(nodeRb_file_handle)); nodeRb_register_instance(callback); - data->target = rb_num2long(rb_obj_id(callback)); + data->target = callback; + handle->data = data; long operation = rb_num2long(roperation); switch(operation){ case 0: uv_fs_unlink(uv_default_loop(), handle, rb_string_value_cstr(&path), nodeRb_fs_operation_callback); break; @@ -115,16 +116,16 @@ break; case 12: uv_fs_chown(uv_default_loop(), handle, rb_string_value_cstr(&path), (int) rb_num2long(rb_ary_entry(params, 0)), (int) rb_num2long(rb_ary_entry(params, 1)), nodeRb_fs_operation_callback); break; } - handle->data = data; } void nodeRb_fs_file_operation_callback(uv_fs_t* handle){ + printf("Test\n"); nodeRb_file_handle* data = (nodeRb_file_handle*) handle->data; - VALUE target = nodeRb_get_class_from_id(data->target); + VALUE target = data->target; if(handle->result == -1){ rb_funcall(target, rb_intern("on_error"), 0, 0); }else{ switch(handle->fs_type) { case UV_FS_SENDFILE: @@ -176,11 +177,11 @@ } break; case UV_FS_OPEN: { uv_file fd = (uv_file) handle->result; - rb_iv_set(target, "@_handle", INT2NUM(fd)); + rb_iv_set(target, "@_handle", INT2NUM(fd)); //INT2NUM(fd) rb_funcall(target, rb_intern("on_open"), 0, 0); } break; case UV_FS_READ: { @@ -195,11 +196,11 @@ nodeRb_unregister_instance(target); rb_iv_set(target, "@_handle", Qnil); break; } } - free(handle->data); + free(data); uv_fs_req_cleanup(handle); } VALUE nodeRb_fs_file_operation(VALUE self, VALUE roperation, VALUE params){ long operation = rb_num2long(roperation); @@ -209,11 +210,12 @@ } // Allocate memory uv_fs_t* handle = malloc(sizeof(uv_fs_t)); nodeRb_file_handle* data = malloc(sizeof(nodeRb_file_handle)); // Save Ruby data - data->target = rb_num2long(rb_obj_id(self)); + data->target = self; + handle->data = data; // Open file switch(operation){ case 0: { int mode = (int) rb_num2long(rb_ary_entry(params,1)); @@ -270,7 +272,6 @@ break; case 11: uv_fs_close(uv_default_loop(), handle, fd, nodeRb_fs_file_operation_callback); break; } - handle->data = data; } \ No newline at end of file