-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of adding custom C functions #187
base: master
Are you sure you want to change the base?
Conversation
there have been several recent changes. You will need to rebase to clear the merge conflicts. |
Done, changes are merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.