Skip to content

Commit

Permalink
setting ints
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Aug 1, 2009
1 parent 4f56f8f commit 2910454
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
12 changes: 12 additions & 0 deletions ext/phuby/phuby.c
Expand Up @@ -75,6 +75,17 @@ static VALUE get(VALUE self, VALUE key)
return Qnil;
}

static VALUE set(VALUE self, VALUE key, VALUE value)
{
zval *php_value;

MAKE_STD_ZVAL(php_value);
ZVAL_LONG(php_value, NUM2INT(value));
ZEND_SET_SYMBOL(EG(active_symbol_table), StringValuePtr(key), php_value);

return value;
}

void Init_phuby()
{
mPhuby = rb_define_module("Phuby");
Expand All @@ -85,6 +96,7 @@ void Init_phuby()
rb_define_method(cPhubyRuntime, "start", start, 0);
rb_define_method(cPhubyRuntime, "stop", stop, 0);
rb_define_method(cPhubyRuntime, "[]", get, 1);
rb_define_method(cPhubyRuntime, "[]=", set, 2);

rb_define_private_method(cPhubyRuntime, "native_eval", native_eval, 2);
}
16 changes: 11 additions & 5 deletions test/test_phuby.rb
Expand Up @@ -15,31 +15,37 @@ def test_eval
@rt.eval("$hi = 'Hello World';")
end

def test_hash_return_int
def test_get_return_int
@rt.eval("$hi = 2;")
assert_equal 2, @rt['hi']
end

def test_hash_return_nil
def test_get_return_nil
@rt.eval("$hi = null;")
assert_nil @rt['hi']
end

def test_hash_return_float
def test_get_return_float
@rt.eval("$hi = 3.14159;")
assert_equal 3.14159, @rt['hi']
end

def test_hash_return_bool
def test_get_return_bool
@rt.eval("$hi = true;")
assert_equal true, @rt['hi']

@rt.eval("$hi = false;")
assert_equal false, @rt['hi']
end

def test_hash_return_string
def test_get_return_string
@rt.eval("$hi = 'world';")
assert_equal 'world', @rt['hi']
end

def test_set_int
assert_equal 10, @rt['hi'] = 10
@rt.eval('$hi += 2;')
assert_equal 12, @rt['hi']
end
end

0 comments on commit 2910454

Please sign in to comment.