shithub: mc

Download patch

ref: 6a4040c99ee0127808d77ad2dc38220f1a8a1c46
parent: 501c03fca3a77243ae2e7415a199b08e806c96b2
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Sep 28 21:33:27 EDT 2015

Implement casting functions to pointers.

    Useful for interfacing with C ABIs.

--- a/6/simp.c
+++ b/6/simp.c
@@ -933,10 +933,14 @@
             switch (t->type) {
                 /* ptr -> slice conversion is disallowed */
                 case Tyslice:
-                    if (t->type == Typtr)
-                        fatal(val, "bad cast from %s to %s",
-                              tystr(exprtype(val)), tystr(to));
+                    if (to->type != Typtr)
+                        fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
                     r = slicebase(s, val, NULL);
+                    break;
+                case Tyfunc:
+                    if (to->type != Typtr)
+                        fatal(val, "bad cast from %s to %s", tystr(exprtype(val)), tystr(to));
+                    r = getcode(s, val);
                     break;
                 /* signed conversions */
                 case Tyint8: case Tyint16: case Tyint32: case Tyint64:
--- /dev/null
+++ b/test/fncast.myr
@@ -1,0 +1,16 @@
+use std
+
+const main = {
+	var fn, envfn
+	var ptr
+	
+	fn = {
+	}
+	envfn = {
+		ptr = ptr
+	}
+	ptr = fn castto(byte#)
+	ptr = envfn castto(byte#)
+	std.put("ok\n", ptr)
+}
+
--- a/test/tests
+++ b/test/tests
@@ -59,6 +59,7 @@
 B nestfn	E	42
 B foldidx	P	123,456
 B closure	P	111555333666
+B fncast	P	ok
 B loop		P	0123401236789
 B subrangefor	P       12
 B patiter	P	23512