From 29104540d4303b901ff4f168fe611e056aecde26 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 31 Jul 2009 22:26:27 -0700 Subject: [PATCH] setting ints --- ext/phuby/phuby.c | 12 ++++++++++++ test/test_phuby.rb | 16 +++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ext/phuby/phuby.c b/ext/phuby/phuby.c index 3743c32..c8951c6 100644 --- a/ext/phuby/phuby.c +++ b/ext/phuby/phuby.c @@ -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"); @@ -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); } diff --git a/test/test_phuby.rb b/test/test_phuby.rb index 17116c1..57f7f92 100644 --- a/test/test_phuby.rb +++ b/test/test_phuby.rb @@ -15,22 +15,22 @@ 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'] @@ -38,8 +38,14 @@ def test_hash_return_bool 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