Example of adding custom C functions#187
Example of adding custom C functions#187guberathome wants to merge 23 commits intophilburk:masterfrom
Conversation
there have been several recent changes. You will need to rebase to clear the merge conflicts. |
Done, changes are merged. |
philburk
left a comment
There was a problem hiding this comment.
The "custom" folder should be in an "examples" folder.
|
done:
open:
|
Please consider if these changes are relevant to be merged (discussion on #188 contains my pitch in favour). I can removed the last offending passages from custom/01-be-gone/demo.fth if that settles it. |
| asprintf( &result, fmtFile, info.st_size, path ); | ||
| } | ||
| PUSH_DATA_STACK( (cell_t) result ); | ||
| return (cell_t)strlen(result); |
There was a problem hiding this comment.
path is not getting freed.
Can you avoid doing the dangerous memory allocation for the C string? Maybe allocate on the stack.
There was a problem hiding this comment.
added FREE-C word.
Thus deallocation is done in demo.fth.
| char* result; | ||
| /* MSYS/Cygwin may warn than asprintf() is not defined but compile and run just fine :-/ */ | ||
| if( stat(path, &info) == -1 ) | ||
| asprintf( &result, fmtErr, errno, strerror(errno), path ); |
There was a problem hiding this comment.
This does not go through the Forth output path so it will not work over a serial port.
Demonstrating custom C calling does not require calling complex host functions.
It could just be a function that modifies a string.
The user will just replace these custom functions so the simpler the better.
There was a problem hiding this comment.
This does not go through the Forth output path so it will not work over a serial port.
Just to clarify: asprintf() allocates a buffer of correct size into which it sprintf's the formatted output.
Which then gets returned.
I'm not aware of bypassing any PForth functionality here.
Demonstrating custom C calling does not require calling complex host functions. It could just be a function that modifies a string. The user will just replace these custom functions so the simpler the better.
That's what is done here.
Creating a new string (of previously unknown length) requires either hairy assumptions about maximal length or allocating a new buffer. All the fun of programming C.
One short demonstration how to compile custom code with PForth for the "unix" build.
Custom code resides in separate "custom" folder.
Setup, compilation and cleanup are handled by one shell script.
Tested with MSYS2-Cygwin, Linux, FreeBSD and NetBSD.
Edit: Changes from #184 seem to have crept in here, these still need to be fixed. Apologies.