Skip to content

Commit

Permalink
Add rb_define_global_const to C-API
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Feb 5, 2010
1 parent d94820f commit 6db232e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions spec/capi/ext/module_spec.c
Expand Up @@ -5,6 +5,11 @@ VALUE sm_define_const(VALUE self, VALUE klass, VALUE val) {
return Qnil;
}

VALUE sm_define_global_const(VALUE self, VALUE obj) {
rb_define_global_const("GC_FOO_TEST", obj);
return Qnil;
}

VALUE sm_const_set(VALUE self, VALUE klass, VALUE val) {
rb_const_set(klass, rb_intern("FOO"), val);
return Qnil;
Expand Down Expand Up @@ -41,6 +46,7 @@ void Init_module_spec() {
rb_define_method(cls, "rb_const_set", sm_const_set, 2);
rb_define_method(cls, "rb_const_get", sm_const_get, 2);
rb_define_method(cls, "rb_define_const", sm_define_const, 2);
rb_define_method(cls, "rb_define_global_const", sm_define_global_const, 1);
rb_define_method(cls, "rb_const_defined", sm_const_defined, 2);

cls = rb_define_class("CApiDefineAliasSpecs", rb_cObject);
Expand Down
6 changes: 6 additions & 0 deletions spec/capi/module_spec.rb
Expand Up @@ -18,6 +18,12 @@ module SubtendModuleTest
(SubtendModuleTest::FOO != 5).should == true
end

it "rb_define_global_const should define a constant on Object" do
@m.rb_define_global_const(7)
::GC_FOO_TEST.should == 7
Object.send :remove_const, "GC_FOO_TEST"
end

it "rb_define_const should define a constant on a module" do
@m.rb_define_const(SubtendModuleTest, 7)
SubtendModuleTest::FOO.should == 7
Expand Down
4 changes: 4 additions & 0 deletions vm/capi/module.cpp
Expand Up @@ -91,6 +91,10 @@ extern "C" {
module->set_const(env->state(), name, object);
}

void rb_define_global_const(const char* name, VALUE obj) {
rb_define_const(rb_cObject, name, obj);
}

void rb_define_global_function(const char* name, CApiGenericFunction func, int argc) {
rb_define_module_function(rb_mKernel, name, func, argc);
}
Expand Down
3 changes: 3 additions & 0 deletions vm/capi/ruby.h
Expand Up @@ -884,6 +884,9 @@ double rb_num2dbl(VALUE);
/** Reopen or create new class with superclass and name under parent module. Returns the Class object. */
VALUE rb_define_class_under(VALUE parent_handle, const char* name, VALUE superclass_handle);

/** Define a toplevel constant */
void rb_define_global_const(const char* name, VALUE obj);

/** Define a constant in given Module's namespace. */
void rb_define_const(VALUE module_handle, const char* name, VALUE obj_handle);

Expand Down

0 comments on commit 6db232e

Please sign in to comment.