Skip to content
This repository has been archived by the owner on Feb 2, 2023. It is now read-only.

Commit

Permalink
Fix extern static warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Godhuda authored and wagenet committed Feb 25, 2017
1 parent 4c45ec5 commit 00f6049
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
target
Cargo.lock
.vscode
*.o
*.a
*.so
*.dylib
*.bundle
16 changes: 13 additions & 3 deletions crates/libcruby-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ extern "C" {
#[link_name = "HELIX_RSTRING_PTR"]
pub fn RSTRING_PTR(string: VALUE) -> c_string;

#[link_name = "HELIX_rb_utf8_str_new"]
pub fn rb_utf8_str_new(string: c_string, len: libc::c_long) -> VALUE;

#[link_name = "HELIX_RARRAY_LEN"]
pub fn RARRAY_LEN(array: VALUE) -> isize;

#[link_name = "HELIX_RARRAY_PTR"]
pub fn RARRAY_PTR(array: VALUE) -> void_ptr;

#[link_name = "helix_rb_utf8_str_new"]
pub fn rb_utf8_str_new(string: c_string, len: libc::c_long) -> VALUE;

#[link_name = "HELIX_RB_TYPE_P"]
pub fn RB_TYPE_P(val: VALUE, rb_type: isize) -> bool;

Expand All @@ -88,11 +88,21 @@ extern "C" {
pub fn rb_define_module_under(namespace: VALUE, name: c_string) -> VALUE;
pub fn rb_define_class(name: c_string, superclass: VALUE) -> VALUE;
pub fn rb_define_class_under(namespace: VALUE, name: c_string, superclass: VALUE) -> VALUE;
pub fn rb_define_alloc_func(klass: VALUE, func: extern "C" fn(klass: VALUE) -> VALUE);
pub fn rb_define_method(class: VALUE, name: c_string, func: void_ptr, arity: isize);
pub fn rb_intern(string: c_string) -> ID;
pub fn rb_jump_tag(state: RubyException) -> !;
pub fn rb_protect(try: extern "C" fn(v: void_ptr) -> VALUE,
arg: void_ptr,
state: *mut RubyException)
-> VALUE;

#[link_name = "HELIX_Data_Wrap_Struct"]
pub fn Data_Wrap_Struct(klass: VALUE, mark: extern "C" fn(void_ptr), free: extern "C" fn(void_ptr), data: void_ptr) -> VALUE;

#[link_name = "HELIX_Data_Get_Struct_Value"]
pub fn Data_Get_Struct_Value(obj: VALUE) -> void_ptr;

#[link_name = "HELIX_Data_Set_Struct_Value"]
pub fn Data_Set_Struct_Value(obj: VALUE, data: void_ptr);
}
17 changes: 10 additions & 7 deletions examples/console/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion ruby/ext/helix_runtime/native/helix_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,28 @@ VALUE HELIX_FIX2INT(VALUE v) {
return FIX2INT(v);
}

VALUE helix_rb_utf8_str_new(const char* str, long len) {
VALUE HELIX_rb_utf8_str_new(const char* str, long len) {
return rb_utf8_str_new(str, len);
}

VALUE HELIX_Data_Wrap_Struct(VALUE klass, HELIX_RUBY_DATA_FUNC mark, HELIX_RUBY_DATA_FUNC free, void* data) {
return Data_Wrap_Struct(klass, mark, free, data);
}

void* HELIX_Data_Get_Struct_Value(VALUE obj) {
void* data;
Data_Get_Struct(obj, void*, data);
return data;
}

void HELIX_Data_Set_Struct_Value(VALUE obj, void* data) {
DATA_PTR(obj) = data;
}

// void HELIX_rb_define_alloc_func(VALUE klass, HELIX_rb_alloc_func_t func) {
// rb_define_alloc_func(klass, func);
// }

int HELIX_TYPE(VALUE v) {
return TYPE(v);
}
Expand Down
11 changes: 10 additions & 1 deletion ruby/ext/helix_runtime/native/helix_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ int HELIX_TYPE(VALUE v);
VALUE HELIX_INT2FIX(int c_int);
VALUE HELIX_FIX2INT(VALUE fix);

VALUE helix_rb_utf8_str_new(const char* str, long len);
VALUE HELIX_rb_utf8_str_new(const char* str, long len);

// typedef VALUE (*HELIX_rb_alloc_func_t)(VALUE);
// void HELIX_rb_define_alloc_func(VALUE klass, HELIX_rb_alloc_func_t func);

typedef void (*HELIX_RUBY_DATA_FUNC)(void*);

VALUE HELIX_Data_Wrap_Struct(VALUE klass, HELIX_RUBY_DATA_FUNC mark, HELIX_RUBY_DATA_FUNC free, void* data);
void* HELIX_Data_Get_Struct_Value(VALUE obj);
void HELIX_Data_Set_Struct_Value(VALUE obj, void* data);

extern int HELIX_T_NONE;
extern int HELIX_T_NIL;
Expand Down
47 changes: 47 additions & 0 deletions ruby/spec/helix_runtime_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,52 @@ module TYPES
# expect(Dummy.TYPE({})).to eq(Dummy::T_HASH)
# expect(Dummy.TYPE([])).to_not eq(Dummy::T_OBJECT)
# end

describe "helix_rb_utf8_str_new" do
it "allocates a new string" do
str1 = "hello world"
str2 = Dummy.STR2STR(str1, 5)

expect(str2).to eq("hello")

str1[0...5] = "goodbye"

expect(str1).to eq("goodbye world")
expect(str2).to eq("hello")

str2 << " world!"

expect(str1).to eq("goodbye world")
expect(str2).to eq("hello world!")
end
end

describe "Data_{Wrap,Get,Set}_Struct" do
it "can allocate then change the data" do
wrapper = Dummy::Wrapper.new

expect(Dummy.get_data(wrapper)).to eq(0)

ptr = Dummy.get_data_ptr(wrapper)

expect(Dummy.set_data(wrapper, 1)).to eq(1)

expect(Dummy.get_data(wrapper)).to eq(1)
expect(Dummy.get_data_ptr(wrapper)).to eq(ptr)
end

it "can allocate then replace the data" do
wrapper = Dummy::Wrapper.new

expect(Dummy.get_data(wrapper)).to eq(0)

ptr = Dummy.get_data_ptr(wrapper)

expect(Dummy.replace_data(wrapper, 1)).to eq(1)

expect(Dummy.get_data(wrapper)).to eq(1)
expect(Dummy.get_data_ptr(wrapper)).not_to eq(ptr)
end
end
end

54 changes: 54 additions & 0 deletions ruby/spec/support/dummy/ext/dummy/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,56 @@ static VALUE TEST_FIX2INT(VALUE _self, VALUE val) {
return INT2FIX(HELIX_FIX2INT(val));
}

static VALUE TEST_STR2STR(VALUE _self, VALUE str, VALUE len) {
return HELIX_rb_utf8_str_new(RSTRING_PTR(str), FIX2LONG(len));
}

void deallocate_wrapper(void* num) {
free(num);
}

VALUE allocate_wrapper(VALUE klass) {
int* num = malloc(sizeof(int));

*num = 0;

return HELIX_Data_Wrap_Struct(klass, NULL, deallocate_wrapper, num);
}

static VALUE TEST_get_data(VALUE _self, VALUE wrapped) {
int* num = HELIX_Data_Get_Struct_Value(wrapped);
return INT2FIX(*num);
}

static VALUE TEST_get_data_ptr(VALUE _self, VALUE wrapped) {
int* num = HELIX_Data_Get_Struct_Value(wrapped);
return INT2FIX(num);
}

static VALUE TEST_set_data(VALUE _self, VALUE wrapped, VALUE value) {
int* num = HELIX_Data_Get_Struct_Value(wrapped);
*num = FIX2INT(value);
return value;
}

static VALUE TEST_replace_data(VALUE _self, VALUE wrapped, VALUE value) {
int* old = HELIX_Data_Get_Struct_Value(wrapped);
int* new = malloc(sizeof(int));

*new = FIX2INT(value);

HELIX_Data_Set_Struct_Value(wrapped, new);

free(old);

return value;
}

void Init_dummy() {
VALUE mDummy = rb_define_module("Dummy");
VALUE mRuby = rb_define_module_under(mDummy, "Ruby");
VALUE cWrapper = rb_define_class_under(mDummy, "Wrapper", rb_cObject);
rb_define_alloc_func(cWrapper, allocate_wrapper);

EXPORT_VALUE(Qtrue);
EXPORT_VALUE(Qfalse);
Expand Down Expand Up @@ -93,4 +140,11 @@ void Init_dummy() {
EXPORT_FUNC(TYPE, 1);
EXPORT_FUNC(INT2FIX, 1);
EXPORT_FUNC(FIX2INT, 1);

EXPORT_FUNC(STR2STR, 2);

EXPORT_FUNC(get_data, 1);
EXPORT_FUNC(get_data_ptr, 1);
EXPORT_FUNC(set_data, 2);
EXPORT_FUNC(replace_data, 2);
}
6 changes: 6 additions & 0 deletions src/class_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ impl ClassDefinition {
ClassDefinition { class: Class(raw_class) }
}

pub fn wrapped(name: &str, alloc_func: extern "C" fn(klass: sys::VALUE) -> sys::VALUE) -> ClassDefinition {
let raw_class = unsafe { sys::rb_define_class(CString::new(name).unwrap().as_ptr(), sys::rb_cObject) };
unsafe { sys::rb_define_alloc_func(raw_class, alloc_func) };
ClassDefinition { class: Class(raw_class) }
}

pub fn reopen(name: &str) -> ClassDefinition {
let raw_class = unsafe {
let class_id = sys::rb_intern(CString::new(name).unwrap().as_ptr());
Expand Down
Loading

0 comments on commit 00f6049

Please sign in to comment.