core.sys.posix.setjmp unavailable for OS X?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Jan 15 19:37:07 UTC 2018


On Mon, Jan 15, 2018 at 07:06:42PM +0000, bpr via Digitalmars-d-learn wrote:
> Is there a reason that it's unavailable on OS X when it works fine on
> Linux?  The functions exist on OS X, and it's easy enough to compile C
> programs using setjmp there; but not D programs. I don't think I'm
> getting a betterC experience on the Mac.
[...]

It's probably just a matter of adding the appropriate prototypes /
declarations to druntime. Provided that they actually work as
advertised, of course. There may be incompatibilities that I'm not aware
of that would preclude it from being added to druntime.

In any case, if druntime doesn't provide the right declarations, you
*could* just declare them yourself with an extern(C). E.g.:

	extern(C) int setjmp(jmp_buf env);
	extern(C) void longjmp(jmp_buf env, int val);
	...
	jmp_buf buf;
	int rc = setjmp(buf);
	if (rc == 0) {
		...
		longjmp(buf, rc);
	} else {
		// handle error
	}

You'll need to obtain your system's definition of jmp_buf, of course,
and translate it into D appropriately. You can find it in the usual
system include directories, like /usr/include or something along those
lines.  Most C structs can be copied verbatim into D and it would work.

(Note that you'll need to find the *exact* declaration used; especially
for structs, you may not be able to just use generic declarations given
by the Posix spec, because if your OS has additional fields for internal
use, you want to make sure those fields are declared properly, otherwise
the struct will have the wrong size and you may get problems at
runtime.)

Better yet, once you figure out how to do it, submit a PR against
druntime so that future users of your OS will benefit from it. ;-)


T

-- 
A program should be written to model the concepts of the task it performs rather than the physical world or a process because this maximizes the potential for it to be applied to tasks that are conceptually similar and, more important, to tasks that have not yet been conceived. -- Michael B. Allen


More information about the Digitalmars-d-learn mailing list