Thread overview
setjmp / longjmp
Apr 25, 2015
Cassio Butrico
Apr 26, 2015
ketmar
Apr 26, 2015
Cassio Butrico
Apr 26, 2015
Stewart Gordon
Apr 27, 2015
Cassio Butrico
Apr 27, 2015
ketmar
Jun 09, 2015
Stewart Gordon
Jun 09, 2015
ketmar
April 25, 2015
Hello everyone , first congratulations for the wonderful forum , I wish someone could help me , I am writing a small basic interpreter in D and I am with some difficulties.

estoutentando manupular the setjmp / longjmp buffers , but the error , I use windows 7 and the dmd 2067 , will be whose manipulate the buffers save and return ? Excuse my english sucks.
April 26, 2015
On Sat, 25 Apr 2015 23:25:13 +0000, Cassio Butrico wrote:

> Hello everyone , first congratulations for the wonderful forum , I wish someone could help me , I am writing a small basic interpreter in D and I am with some difficulties.
> 
> estoutentando manupular the setjmp / longjmp buffers , but the error , I use windows 7 and the dmd 2067 , will be whose manipulate the buffers save and return ? Excuse my english sucks.

you shouldn't use setjmp/longjmp in D. use exceptions instead. something like this:

instead of setjmp:
try {
  doit
} catch (MyException e) {
  process e
}

and instead of longjmp:
throw new MyException();


April 26, 2015
On Sunday, 26 April 2015 at 05:56:46 UTC, ketmar wrote:
> On Sat, 25 Apr 2015 23:25:13 +0000, Cassio Butrico wrote:
>
>> Hello everyone , first congratulations for the wonderful forum , I wish
>> someone could help me , I am writing a small basic interpreter in D and
>> I am with some difficulties.
>> 
>> estoutentando manupular the setjmp / longjmp buffers , but the error , I
>> use windows 7 and the dmd 2067 , will be whose manipulate the buffers
>> save and return ? Excuse my english sucks.
>
> you shouldn't use setjmp/longjmp in D. use exceptions instead. something
> like this:
>
> instead of setjmp:
> try {
>   doit
> } catch (MyException e) {
>   process e
> }
>
> and instead of longjmp:
> throw new MyException();


Thank you for answering me ketmar
I will try this
April 26, 2015
On 26/04/2015 06:56, ketmar wrote:
<snip>
> you shouldn't use setjmp/longjmp in D. use exceptions instead. something
> like this:
<snip>

True in the general case.  Still, there must be some reason that trying it in D causes an AV (even if I disable the GC).  I remain curious about what that reason is.

Some time ago, just for fun, I wrote an Unlambda to D compiler.  Except that I couldn't get the c builtin to work properly.  Exceptions cover typical uses cases, but a subtlety of it is that (roughly speaking) the continuation it emits can escape from the function it is passed into, and then when the continuation is later invoked it should return the program to the point at which c was invoked.  Essentially, it can wind the stack as well as unwinding it.  I envisaged that, maybe with the aid of setjmp and some trick to get GC to work with it, it could be made to work.

Stewart.

-- 
My email address is valid but not my primary mailbox and not checked regularly.  Please keep replies on the 'group where everybody may benefit.
April 27, 2015
I'm just building a small interpreter with a script and wanted to handle errors diverting the flow and returning .
I am grateful for having responded my question , thank you.
April 27, 2015
On Sun, 26 Apr 2015 21:45:41 +0100, Stewart Gordon wrote:

> On 26/04/2015 06:56, ketmar wrote:
> <snip>
>> you shouldn't use setjmp/longjmp in D. use exceptions instead. something like this:
> <snip>
> 
> True in the general case.  Still, there must be some reason that trying it in D causes an AV (even if I disable the GC).  I remain curious about what that reason is.

i believe this has something to do with exception frames. but it needs further investigation.

June 09, 2015
On 27/04/2015 10:41, ketmar wrote:
<snip>
> i believe this has something to do with exception frames. but it needs
> further investigation.

What is an exception frame, exactly?

Moreover, are these frames applicable even in sections of code where no throwing or catching of exceptions takes place?

Stewart.

-- 
My email address is valid but not my primary mailbox and not checked regularly.  Please keep replies on the 'group where everybody may benefit.
June 09, 2015
On Tue, 09 Jun 2015 11:57:03 +0100, Stewart Gordon wrote:

> On 27/04/2015 10:41, ketmar wrote:
> <snip>
>> i believe this has something to do with exception frames. but it needs further investigation.
> 
> What is an exception frame, exactly?

to correctly do unwinding and other interesting things exception handler should do, compiler must establish special hidden structures in the form of single-linked list, so runtime can traverse it backwards if it needs to unwind the stack (i.e. calling destructors for structs, for example).

> Moreover, are these frames applicable even in sections of code where no throwing or catching of exceptions takes place?

exception can be thrown by function that is called from your code. compiler needs to setup structures to correctly "unwind" the stack in this case. if nothing will throw, that setup cost as almost zero and can be ignored. but playing games with stack can lead to corruption of such structures (runtime doesn't know that stack was changed, and it can't update it's internal data structures accordingly). this may be harmless, or may lead to crash, or may lead to memory corruption without immediate crashing, or... effects are unpredictable.

tldr; don't use setjmp/longjmp in the language with exceptions, unless you can describe it's runtime internals and exception handling down to assembler code when you're awaken at night completely drunk. ;-)