summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDaniel Bovensiepen <[email protected]>2012-12-01 14:07:23 +0800
committerDaniel Bovensiepen <[email protected]>2012-12-01 14:07:23 +0800
commit40bfc86a5e0a1a70f904a080569da9662747181f (patch)
tree8f5f1d28650e8194b918293ebc7d826f0580af4f
parent5f00a837edd0f55ed56d4d532d06ee45d9850f3c (diff)
parent26600ca4ec2c9547927056b35b61328f12cda79a (diff)
downloadmruby-40bfc86a5e0a1a70f904a080569da9662747181f.tar.gz
mruby-40bfc86a5e0a1a70f904a080569da9662747181f.zip
Merge remote-tracking branch 'upstream/master' into mrbgems
-rw-r--r--include/mruby/proc.h2
-rw-r--r--src/class.c34
-rw-r--r--src/codegen.c1
-rw-r--r--src/hash.c4
-rw-r--r--src/string.c4
-rw-r--r--src/variable.c2
-rw-r--r--test/t/module.rb24
-rw-r--r--test/t/syntax.rb11
-rw-r--r--tools/mirb/mirb.c8
9 files changed, 76 insertions, 14 deletions
diff --git a/include/mruby/proc.h b/include/mruby/proc.h
index 50e55f231..96f7ddbd7 100644
--- a/include/mruby/proc.h
+++ b/include/mruby/proc.h
@@ -52,7 +52,7 @@ struct RProc *mrb_closure_new(mrb_state*, mrb_irep*);
void mrb_proc_copy(struct RProc *a, struct RProc *b);
#include "mruby/khash.h"
-KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1);
+KHASH_DECLARE(mt, mrb_sym, struct RProc*, 1)
#if defined(__cplusplus)
} /* extern "C" { */
diff --git a/src/class.c b/src/class.c
index 69aa31249..618a6d1f6 100644
--- a/src/class.c
+++ b/src/class.c
@@ -15,7 +15,7 @@
#include "mruby/array.h"
#include "error.h"
-KHASH_DEFINE(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal);
+KHASH_DEFINE(mt, mrb_sym, struct RProc*, 1, kh_int_hash_func, kh_int_hash_equal)
typedef struct fc_result {
mrb_sym name;
@@ -630,7 +630,7 @@ mrb_get_args(mrb_state *mrb, const char *format, ...)
}
break;
default:
- mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalide argument specifier %c", c);
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid argument specifier %c", c);
break;
}
}
@@ -848,6 +848,32 @@ mrb_mod_instance_methods(mrb_state *mrb, mrb_value mod)
return class_instance_method_list(mrb, argc, argv, c, 0);
}
+mrb_value mrb_yield_internal(mrb_state *mrb, mrb_value b, int argc, mrb_value *argv, mrb_value self, struct RClass *c);
+
+/* 15.2.2.4.35 */
+/*
+ * call-seq:
+ * mod.class_eval {| | block } -> obj
+ * mod.module_eval {| | block } -> obj
+ *
+ * Evaluates block in the context of _mod_. This can
+ * be used to add methods to a class. <code>module_eval</code> returns
+ * the result of evaluating its argument.
+ */
+
+mrb_value
+mrb_mod_module_eval(mrb_state *mrb, mrb_value mod)
+{
+ mrb_value a, b;
+ struct RClass *c;
+
+ if (mrb_get_args(mrb, "|S&", &a, &b) == 1) {
+ mrb_raise(mrb, E_NOTIMP_ERROR, "module_eval/class_eval with string not implemented");
+ }
+ c = mrb_class_ptr(mod);
+ return mrb_yield_internal(mrb, b, 0, 0, mod, c);
+}
+
mrb_value
mrb_singleton_class(mrb_state *mrb, mrb_value v)
{
@@ -1504,9 +1530,11 @@ mrb_init_class(mrb_state *mrb)
mrb_define_method(mrb, mod, "include", mrb_mod_include, ARGS_ANY()); /* 15.2.2.4.27 */
mrb_define_method(mrb, mod, "include?", mrb_mod_include_p, ARGS_REQ(1)); /* 15.2.2.4.28 */
mrb_define_method(mrb, mod, "append_features", mrb_mod_append_features, ARGS_REQ(1)); /* 15.2.2.4.10 */
- mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */
+ mrb_define_method(mrb, mod, "class_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.15 */
+ mrb_define_method(mrb, mod, "included", mrb_bob_init, ARGS_REQ(1)); /* 15.2.2.4.29 */
mrb_define_method(mrb, mod, "included_modules", mrb_mod_included_modules, ARGS_NONE()); /* 15.2.2.4.30 */
mrb_define_method(mrb, mod, "instance_methods", mrb_mod_instance_methods, ARGS_ANY()); /* 15.2.2.4.33 */
+ mrb_define_method(mrb, mod, "module_eval", mrb_mod_module_eval, ARGS_ANY()); /* 15.2.2.4.35 */
mrb_define_method(mrb, mod, "to_s", mrb_mod_to_s, ARGS_NONE());
mrb_define_method(mrb, mod, "inspect", mrb_mod_to_s, ARGS_NONE());
diff --git a/src/codegen.c b/src/codegen.c
index fd41e3cb8..1e468867c 100644
--- a/src/codegen.c
+++ b/src/codegen.c
@@ -1412,7 +1412,6 @@ codegen(codegen_scope *s, node *tree, int val)
codegen(s, tree->cdr->cdr->car, VAL);
pop();
gen_assignment(s, tree->car, cursp(), val);
- if (val) pop();
dispatch(s, pos);
break;
}
diff --git a/src/hash.c b/src/hash.c
index c74ac837b..c7a419250 100644
--- a/src/hash.c
+++ b/src/hash.c
@@ -29,8 +29,8 @@ mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b)
return mrb_eql(mrb, a, b);
}
-KHASH_DECLARE(ht, mrb_value, mrb_value, 1);
-KHASH_DEFINE (ht, mrb_value, mrb_value, 1, mrb_hash_ht_hash_func, mrb_hash_ht_hash_equal);
+KHASH_DECLARE(ht, mrb_value, mrb_value, 1)
+KHASH_DEFINE (ht, mrb_value, mrb_value, 1, mrb_hash_ht_hash_func, mrb_hash_ht_hash_equal)
static void mrb_hash_modify(mrb_state *mrb, mrb_value hash);
diff --git a/src/string.c b/src/string.c
index 425b79ca7..7ab6e5806 100644
--- a/src/string.c
+++ b/src/string.c
@@ -2511,7 +2511,7 @@ mrb_cstr_to_inum(mrb_state *mrb, const char *str, int base, int badcheck)
return mrb_fixnum_value(result);
}
bad:
- mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalide string for number(%s)", str);
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for number(%s)", str);
/* not reached */
return mrb_fixnum_value(0);
}
@@ -2621,7 +2621,7 @@ mrb_cstr_to_dbl(mrb_state *mrb, const char * p, int badcheck)
if (p == end) {
if (badcheck) {
bad:
- mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalide string for float(%s)", p);
+ mrb_raisef(mrb, E_ARGUMENT_ERROR, "invalid string for float(%s)", p);
/* not reached */
}
return d;
diff --git a/src/variable.c b/src/variable.c
index 7d583da40..6907a8e2a 100644
--- a/src/variable.c
+++ b/src/variable.c
@@ -256,7 +256,7 @@ iv_free(mrb_state *mrb, iv_tbl *t)
#endif
KHASH_DECLARE(iv, mrb_sym, mrb_value, 1)
-KHASH_DEFINE(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal);
+KHASH_DEFINE(iv, mrb_sym, mrb_value, 1, kh_int_hash_func, kh_int_hash_equal)
typedef struct iv_tbl {
khash_t(iv) h;
diff --git a/test/t/module.rb b/test/t/module.rb
index e666a1763..5e825c6b5 100644
--- a/test/t/module.rb
+++ b/test/t/module.rb
@@ -31,6 +31,22 @@ assert('Module#append_features', '15.2.2.4.10') do
Test4AppendFeatures2.const_get(:Const4AppendFeatures2) == Test4AppendFeatures2
end
+assert('Module#class_eval', '15.2.2.4.15') do
+ class Test4ClassEval
+ @a = 11
+ @b = 12
+ end
+ Test4ClassEval.class_eval do
+ def method1
+ end
+ end
+ r = Test4ClassEval.instance_methods
+ Test4ClassEval.class_eval{ @a } == 11 and
+ Test4ClassEval.class_eval{ @b } == 12 and
+ r.class == Array and r.include?(:method1)
+end
+
+
assert('Module#class_variables', '15.2.2.4.19') do
class Test4ClassVariables1
@@var1 = 1
@@ -151,6 +167,14 @@ assert('Module#instance_methods', '15.2.2.4.33') do
r.class == Array and r.include?(:method3) and r.include?(:method2)
end
+assert('Module#module_eval', '15.2.2.4.35') do
+ module Test4ModuleEval
+ @a = 11
+ @b = 12
+ end
+ Test4ModuleEval.module_eval{ @a } == 11 and
+ Test4ModuleEval.module_eval{ @b } == 12
+end
# Not ISO specified
diff --git a/test/t/syntax.rb b/test/t/syntax.rb
index 47221d425..0501608e5 100644
--- a/test/t/syntax.rb
+++ b/test/t/syntax.rb
@@ -58,3 +58,14 @@ assert('Nested const reference') do
Syntax4Const::CONST1 == "hello world" and
Syntax4Const::Const2.new.const1 == "hello world"
end
+
+assert('Abbreviated variable assignment as returns') do
+ module Syntax4AbbrVarAsgnAsReturns
+ class A
+ def b
+ @c ||= 1
+ end
+ end
+ end
+ Syntax4AbbrVarAsgnAsReturns::A.new.b == 1
+end
diff --git a/tools/mirb/mirb.c b/tools/mirb/mirb.c
index 502400c72..d3903ef57 100644
--- a/tools/mirb/mirb.c
+++ b/tools/mirb/mirb.c
@@ -5,7 +5,7 @@
** an interactive way and executes it
** immediately. It's a REPL...
*/
-
+
#include <string.h>
#include <mruby.h>
@@ -143,7 +143,7 @@ print_cmdline(int code_block_open)
int
main(void)
{
- char last_char;
+ int last_char;
char ruby_code[1024] = { 0 };
char last_code_line[1024] = { 0 };
int char_index;
@@ -156,7 +156,7 @@ main(void)
print_hint();
- /* new interpreter instance */
+ /* new interpreter instance */
mrb = mrb_open();
if (mrb == NULL) {
fprintf(stderr, "Invalid mrb interpreter, exiting mirb");
@@ -207,7 +207,7 @@ main(void)
parser->send = ruby_code + strlen(ruby_code);
parser->lineno = 1;
mrb_parser_parse(parser, cxt);
- code_block_open = is_code_block_open(parser);
+ code_block_open = is_code_block_open(parser);
if (code_block_open) {
/* no evaluation of code */