shithub: mc

Download patch

ref: 9dbef7492971df74e2ecd58ee02f5fe69e979149
parent: 08442dd9c3426e2dbe598ef1f6fb10f2f67638aa
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Oct 2 19:56:21 EDT 2015

Make assert able to take format args.

    This is useful outside of libstd. Libstd mostly can't use
    it due to dependency loops, so we get iassert for that.

--- a/lib/std/alloc.myr
+++ b/lib/std/alloc.myr
@@ -142,7 +142,7 @@
 	addr = p castto(size)
 	addr -= align(sizeof(slheader), Align)
 	phdr = addr castto(slheader#)
-	assert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
+	iassert(phdr.magic == (0xdeadbeefbadf00d castto(size)), "corrupt memory\n")
 }
 
 /* Frees a slice */
--- /dev/null
+++ b/lib/std/assert.myr
@@ -1,0 +1,18 @@
+use "fmt.use"
+use "syswrap.use"
+use "varargs.use"
+
+pkg std =
+	const assert	: (cond : bool, fmt : byte[:], args : ... -> void)
+;;
+
+const assert = {cond, msg, args
+	var ap
+
+	ap = vastart(&args)
+	if !cond
+		std.fputv(2, msg, &ap)
+		suicide()
+	;;
+}
+
--- a/lib/std/bigint.myr
+++ b/lib/std/bigint.myr
@@ -647,7 +647,7 @@
 	var off, shift
 	var t, carry
 
-	assert(s >= 0, "shift amount must be positive")
+	iassert(s >= 0, "shift amount must be positive")
 	off = (s castto(uint64)) / 32
 	shift = (s castto(uint64)) % 32
 
@@ -678,7 +678,7 @@
 	var off, shift
 	var t, carry
 
-	assert(s >= 0, "shift amount must be positive")
+	iassert(s >= 0, "shift amount must be positive")
 	off = (s castto(uint64)) / 32
 	shift = (s castto(uint64)) % 32
 
--- a/lib/std/bld.sub
+++ b/lib/std/bld.sub
@@ -3,6 +3,7 @@
 
 	# portable files
 	alloc.myr
+	assert.myr
 	bigint.myr
 	bitset.myr
 	blat.myr
--- a/lib/std/die.myr
+++ b/lib/std/die.myr
@@ -3,7 +3,7 @@
 
 pkg std = 
 	$noret const die	: (msg : byte[:] -> void)
-	const assert	: (cond : bool, msg : byte[:] -> void)
+	pkglocal const iassert	: (cond : bool, msg : byte[:] -> void)
 ;;
 
 const die = {msg
@@ -11,9 +11,8 @@
 	suicide()
 }
 
-const assert = {cond, msg
+const iassert = {cond, msg
 	if !cond
 		die(msg)
 	;;
 }
-
--- a/lib/std/fmt.myr
+++ b/lib/std/fmt.myr
@@ -440,7 +440,7 @@
 		| _:	std.die("unreachable")
 		;;
 	;;
-	std.assert(ip.padto >= 0, "pad must be >= 0")
+	iassert(ip.padto >= 0, "pad must be >= 0")
 	std.slfree(opts)
 	-> ip
 }
@@ -461,7 +461,7 @@
 		| _:	std.die("unreachable")
 		;;
 	;;
-	std.assert(p >= 0, "pad must be >= 0")
+	iassert(p >= 0, "pad must be >= 0")
 	std.slfree(opts)
 	for i = 0; i < w - graphemewidth(str); i++
 		sbputc(sb, p)
--- a/lib/std/putint.myr
+++ b/lib/std/putint.myr
@@ -1,5 +1,5 @@
+use "assert.use"
 use "types.use"
-use "die.use"
 
 pkg std =
 	generic putle64	: (buf : byte[:], v :  @a::(numeric,integral) -> size)
--- a/lib/std/rand.myr
+++ b/lib/std/rand.myr
@@ -1,4 +1,5 @@
 use "die.use"
+use "assert.use"
 use "types.use"
 use "alloc.use"
 use "now.use"
--- a/lib/std/slcp.myr
+++ b/lib/std/slcp.myr
@@ -7,6 +7,6 @@
 ;;
 
 generic slcp = {a : @a[:], b : @a[:]
-	assert(a.len == b.len, "arguments to slcp() must be of equal length\n")
+	iassert(a.len == b.len, "arguments to slcp() must be of equal length\n")
 	memblit(a castto(byte#), b castto(byte#), a.len * sizeof(@a))
 }
--- a/lib/std/strbuf.myr
+++ b/lib/std/strbuf.myr
@@ -87,7 +87,7 @@
 }
 
 const sbtrim = {sb, len
-	assert(abs(len) <= sb.len, "trim out of range\n")
+	iassert(abs(len) <= sb.len, "trim out of range\n")
 	if len < 0
 		sb.len -= abs(len)
 	else
--- a/lib/std/test/slcp.myr
+++ b/lib/std/test/slcp.myr
@@ -9,8 +9,6 @@
 
 	std.slcp(a[:a.len-2], a[2:])
 	std.slcp(b[2:], b[:b.len-2])
-	std.put("a: {}, a_cped: {}\n", a[:], a_cped[:])
-	std.put("b: {}, b_cped: {}\n", b[:], b_cped[:])
-	std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed")
-	std.assert(std.sleq(b[:], b_cped[:]), "slcp of b failed")
+	std.assert(std.sleq(a[:], a_cped[:]), "slcp of a failed: got {}, wanted {}", a[:], a_cped[:])
+	std.assert(std.sleq(b[:], b_cped[:]), "slcp of b failed: got {}, wanted {}", a[:], a_cped[:])
 }