Skip to content

Commit

Permalink
Implement rb_obj_freeze and rb_str_freeze in C-API
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Phoenix committed Feb 5, 2010
1 parent 616c0a8 commit 3411175
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions spec/capi/ext/object_spec.c
Expand Up @@ -132,6 +132,10 @@ VALUE object_spec_rb_obj_id(VALUE self, VALUE obj) {
return rb_obj_id(obj);
}

VALUE object_spec_rb_obj_freeze(VALUE self, VALUE obj) {
return rb_obj_freeze(obj);
}

void Init_object_spec() {
VALUE cls;
cls = rb_define_class("CApiObjectSpecs", rb_cObject);
Expand Down Expand Up @@ -163,4 +167,5 @@ void Init_object_spec() {
rb_define_method(cls, "OBJ_TAINTED", object_spec_OBJ_TAINTED, 1);

rb_define_method(cls, "rb_obj_id", object_spec_rb_obj_id, 1);
rb_define_method(cls, "rb_obj_freeze", object_spec_rb_obj_freeze, 1);
}
7 changes: 7 additions & 0 deletions spec/capi/ext/string_spec.c
Expand Up @@ -194,6 +194,11 @@ VALUE string_spec_STR2CSTR_replace(VALUE self, VALUE str) {
return Qnil;
}

VALUE string_spec_rb_str_freeze(VALUE self, VALUE str) {
rb_str_freeze(str);
return Qnil;
}

#ifdef RUBINIUS
VALUE string_spec_rb_str_ptr_iterate(VALUE self, VALUE str) {
int i;
Expand Down Expand Up @@ -315,6 +320,8 @@ void Init_string_spec() {
rb_define_method(cls, "STR2CSTR", string_spec_STR2CSTR, 1);
rb_define_method(cls, "STR2CSTR_replace", string_spec_STR2CSTR_replace, 1);

rb_define_method(cls, "rb_str_freeze", string_spec_rb_str_freeze, 1);

#ifdef RUBINIUS
rb_define_method(cls, "rb_str_ptr_iterate", string_spec_rb_str_ptr_iterate, 1);
rb_define_method(cls, "rb_str_ptr_assign", string_spec_rb_str_ptr_assign, 2);
Expand Down
8 changes: 8 additions & 0 deletions spec/capi/object_spec.rb
Expand Up @@ -240,4 +240,12 @@ def reach
@o.OBJ_TAINTED(obj).should be_false
end
end

describe "rb_obj_freeze" do
it "freezes the object passed to it" do
obj = ""
@o.rb_obj_freeze(obj).should == obj
obj.frozen?.should be_true
end
end
end
8 changes: 8 additions & 0 deletions spec/capi/string_spec.rb
Expand Up @@ -336,6 +336,14 @@ def to_str
end
end

describe "rb_str_freeze" do
it "freezes the string" do
s = ""
@s.rb_str_freeze(s)
s.frozen?.should be_true
end
end

extended_on :rubinius do
describe "rb_str_ptr" do
it "returns struct with a pointer to the string's contents" do
Expand Down
6 changes: 6 additions & 0 deletions vm/capi/object.cpp
Expand Up @@ -15,6 +15,12 @@ extern "C" {
/* @todo implement when rbx supports frozen objects. */
}

VALUE rb_obj_freeze(VALUE hndl) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();
env->get_object(hndl)->freeze(env->state());
return hndl;
}

// Copied from MRI
static struct types {
int type;
Expand Down
13 changes: 5 additions & 8 deletions vm/capi/ruby.h
Expand Up @@ -944,14 +944,8 @@ double rb_num2dbl(VALUE);
/** Remove a previously declared global variable. */
void rb_free_global(VALUE global_handle);

/**
* Freeze object and return it.
*
* NOT supported in Rubinius.
*/

// Things use this as an expression, so be sure it expands to be the argument.
#define rb_obj_freeze(obj) obj
/** Freeze object and return it. */
VALUE rb_obj_freeze(VALUE obj);

/**
* Call method on receiver, args as varargs. Calls private methods.
Expand Down Expand Up @@ -1295,6 +1289,9 @@ double rb_num2dbl(VALUE);

void rb_str_modify(VALUE str);

/** Deprecated alias for rb_obj_freeze */
void rb_str_freeze(VALUE str);

/** Returns a new String created from concatenating self with other. */
VALUE rb_str_plus(VALUE self_handle, VALUE other_handle);

Expand Down
4 changes: 4 additions & 0 deletions vm/capi/string.cpp
Expand Up @@ -102,6 +102,10 @@ extern "C" {
self->unshare(env->state());
}

void rb_str_freeze(VALUE str) {
rb_obj_freeze(str);
}

VALUE rb_str_append(VALUE self_handle, VALUE other_handle) {
NativeMethodEnvironment* env = NativeMethodEnvironment::get();

Expand Down

0 comments on commit 3411175

Please sign in to comment.