shithub: mc

ref: 3ff9e5e0a03fa26113c7968878ac248f0e3464ef
dir: /lib/std/fndup.myr/

View raw version
use "alloc"
use "die"
use "sldup"
use "types"

pkg std =
	generic fndup	: (fn : @fn::function -> @fn::function)
	generic fnfree	: (fn : @fn::function -> void)
;;

generic fndup = {fn
	var repr : intptr[2]

	repr = (&fn : intptr[2]#)#
	repr[0] = (sldup(envslice(repr[0])) : intptr)
	-> (&repr : @fn::function#)#
}

generic fnfree = {fn
	var repr : intptr[2]

	repr = (&fn : intptr[2]#)#
	std.slfree(envslice(repr[0]))
}

const envslice = {ep : intptr
	var env : byte#
	var szp : intptr#

	env = (ep : byte#)
	szp = (env : intptr#)
	-> env[:szp#]
}