[macruby-changes] [2984] MacRuby/trunk

source_changes at macosforge.org source_changes at macosforge.org
Mon Nov 9 12:10:40 PST 2009


Revision: 2984
          http://trac.macosforge.org/projects/ruby/changeset/2984
Author:   martinlagardette at apple.com
Date:     2009-11-09 12:10:37 -0800 (Mon, 09 Nov 2009)
Log Message:
-----------

 - Added NODE_LAMBDA compatibility (for the "->" syntax)
 - Changed Proc#to_s to show if a Proc is a lambda or not

Modified Paths:
--------------
    MacRuby/trunk/compiler.cpp
    MacRuby/trunk/id.c
    MacRuby/trunk/id.h
    MacRuby/trunk/proc.c

Modified: MacRuby/trunk/compiler.cpp
===================================================================
--- MacRuby/trunk/compiler.cpp	2009-11-09 07:32:30 UTC (rev 2983)
+++ MacRuby/trunk/compiler.cpp	2009-11-09 20:10:37 UTC (rev 2984)
@@ -4966,6 +4966,7 @@
 
 	case NODE_FOR:
 	case NODE_ITER:
+	case NODE_LAMBDA:
 	    {
 		std::vector<ID> old_dvars = dvars;
 
@@ -5013,29 +5014,40 @@
 		current_block_func = cast<Function>(block);
 		current_block_node = node->nd_body;
 
+		const int node_type = nd_type(node);
+		const bool is_lambda = (node_type == NODE_LAMBDA);
 		Value *caller;
-		assert(node->nd_iter != NULL);
-		if (nd_type(node) == NODE_ITER) {
+
+		if (is_lambda == 0) {
+		    assert(node->nd_iter != NULL);
+		}
+
+		if (node_type == NODE_ITER) {
 		    caller = compile_node(node->nd_iter);
 		}
 		else {
 		    // dispatch #each on the receiver
 		    std::vector<Value *> params;
 
-		    params.push_back(compile_mcache(selEach, false));
+		    params.push_back(compile_mcache((is_lambda ? selLambda : selEach), false));
 		    params.push_back(current_self);
 
-		    // the block must not be passed to the code
-		    // that generates the values we loop on
-		    current_block_func = NULL;
-		    current_block_node = NULL;
-		    params.push_back(compile_node(node->nd_iter));
-		    current_block_func = cast<Function>(block);
-		    current_block_node = node->nd_body;
+		    if (is_lambda == 0) {
+			// the block must not be passed to the code
+			// that generates the values we loop on
+			current_block_func = NULL;
+			current_block_node = NULL;
+			params.push_back(compile_node(node->nd_iter));
+			current_block_func = cast<Function>(block);
+			current_block_node = node->nd_body;
+		    }
+		    else {
+			params.push_back(current_self);
+		    }
 
-		    params.push_back(compile_sel(selEach));
+		    params.push_back(compile_sel((is_lambda ? selLambda : selEach)));
 		    params.push_back(compile_block_create(NULL));
-		    params.push_back(ConstantInt::get(Int8Ty, 0));
+		    params.push_back(ConstantInt::get(Int8Ty, (is_lambda ? DISPATCH_FCALL : 0)));
 		    params.push_back(ConstantInt::get(Int32Ty, 0));
 
 		    caller = compile_dispatch_call(params);

Modified: MacRuby/trunk/id.c
===================================================================
--- MacRuby/trunk/id.c	2009-11-09 07:32:30 UTC (rev 2983)
+++ MacRuby/trunk/id.c	2009-11-09 20:10:37 UTC (rev 2984)
@@ -104,6 +104,7 @@
     selIsEqual = sel_registerName("isEqual:");
     selWrite = sel_registerName("write:");
     selInherited = sel_registerName("inherited:");
+    selLambda = sel_registerName("lambda");
 
     cacheEach = rb_vm_get_call_cache(selEach);
 #endif

Modified: MacRuby/trunk/id.h
===================================================================
--- MacRuby/trunk/id.h	2009-11-09 07:32:30 UTC (rev 2983)
+++ MacRuby/trunk/id.h	2009-11-09 20:10:37 UTC (rev 2984)
@@ -113,6 +113,7 @@
 extern SEL selIsEqual;
 extern SEL selWrite;
 extern SEL selInherited;
+extern SEL selLambda;
 extern ID idIncludedModules;
 extern ID idIncludedInClasses;
 extern ID idAncestors;

Modified: MacRuby/trunk/proc.c
===================================================================
--- MacRuby/trunk/proc.c	2009-11-09 07:32:30 UTC (rev 2983)
+++ MacRuby/trunk/proc.c	2009-11-09 20:10:37 UTC (rev 2984)
@@ -577,6 +577,24 @@
  * an indication of where the proc was defined.
  */
 
+static VALUE
+proc_to_s(VALUE self, SEL sel)
+{
+    const char		*cname = rb_obj_classname(self);
+    rb_vm_block_t	*proc;
+
+    GetProcPtr(self, proc);
+
+    const char *is_lambda = (proc->flags & VM_BLOCK_LAMBDA) ? " (lambda)" : "";
+    VALUE str = rb_sprintf("#<%s:%p%s>", cname, (void *)self, is_lambda);
+
+    if (OBJ_TAINTED(self)) {
+	OBJ_TAINT(str);
+    }
+
+    return str;
+}
+
 #if 0 // TODO
 static VALUE
 proc_to_s(VALUE self, SEL sel)
@@ -1498,7 +1516,7 @@
     rb_objc_define_method(rb_cProc, "==", proc_eq, 1);
     rb_objc_define_method(rb_cProc, "eql?", proc_eq, 1);
     rb_objc_define_method(rb_cProc, "hash", proc_hash, 0);
-    //rb_objc_define_method(rb_cProc, "to_s", proc_to_s, 0);
+    rb_objc_define_method(rb_cProc, "to_s", proc_to_s, 0);
     rb_objc_define_method(rb_cProc, "lambda?", proc_lambda_p, 0);
     rb_objc_define_method(rb_cProc, "binding", proc_binding, 0);
     rb_objc_define_method(rb_cProc, "curry", proc_curry, -1);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20091109/2b1f935b/attachment.html>


More information about the macruby-changes mailing list