There is shockingly very little documentation on how to use uFFI. In particular, I have the following situation...
A shared C lib has the following function: void get_machine(char *machine) { uname(&uname_s); strcpy(machine, uname_s.machine); } It copies the content of a string value into the buffer passed to it (char *machine). I want to call this function from Pharo, thus: buffer := String new: 64. self ffiCall: #( void get_machine(String buffer) ). The buffer never receives the string value (it just contains the 64 initiialized spaces). Clearly, I'm misunderstanding the proper mechanism for doing this. [There are other functions in the shared C lib that work fine, such as: uint get_uptime() { sysinfo(&info); return info.uptime; } Returning integers or floats is no problem. But passing arguments by reference seems to be verboten.] |
On Mon, May 22, 2017 at 01:42:50PM -0700, horrido wrote:
> buffer := String new: 64. > self ffiCall: #( void get_machine(String buffer) ). > > The buffer never receives the string value (it just contains the 64 > initiialized spaces). See FFICalloutAPITests>>testPrintString. HTH. Pierce |
In reply to this post by horrido
Je comprends enfin...
I accidentally came across a StackOverflow answer that suggested the String object is immutable. That makes perfect sense, as I know the C function is actually trying to write to the buffer. That gave me the idea to use a ByteArray instead. Now, everything works!
|
Let's say you have a shared C library (called testlib.so) with the following function signature:
void func1(int *buf); How would you use uFFI to call this function? How would you complete the following code fragment: self ffiCall: #( void func1(??? buf) ) module: 'testlib.so'. where buf should hold whatever integer value func1 puts into it. Maybe buf needs to be prepared somehow before the ffiCall?
|
In reply to this post by horrido
Did you read the UFFI chapter? I'm turning it into a booklet but I will not write it because I'm bad in FFI Now if you have corrections, suggestions, please do some pull requests Stef On Mon, May 22, 2017 at 10:42 PM, horrido <[hidden email]> wrote: There is shockingly very little documentation on how to use uFFI. In |
Yes, I did. I found it difficult to understand. It would be nice to have some clear examples in the documentation, for example, really simple and common situations such as a C function returning an integer in a passed-by-reference argument.
Speaking of documentation, in "Pharo by Example 50," I found this statement: > (Recall that you should set halosEnabled in the Preferences browser.) However, nowhere else in the book is there any reference to halosEnabled or Preferences browser. Did you guys forget and leave out a chunk of the book?
|
On Sat, May 27, 2017 at 9:15 PM, horrido <[hidden email]> wrote:
> Yes, I did. I found it difficult to understand. It would be nice to have some > clear examples in the documentation, for example, really simple and common > situations such as a C function returning an integer in a > passed-by-reference argument. > > Speaking of documentation, in "Pharo by Example 50," I found this statement: > >> (Recall that you should set halosEnabled in the Preferences > browser.) > > However, nowhere else in the book is there any reference to halosEnabled or > Preferences browser. Nice pick up. Now in Settings (in a fresh 60473 image), filtering on "halo" shows only: * Cycle both directions * Encloses fullbounds * Exhibits bounds so the comment seems irrelevant and #halosEnabled & #halosEnabled: each have one implementor class-side of Morph with one sender from MorphTest each, and one sender from Morph>>addHalo: and "Morph halosEnabled" ==> true, so the statement seems unnecessary. Would you like to contribute a pull request removing it? https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/Morphic/Morphic.pier > Did you guys forget and leave out a chunk of the book? Its an evolutionary book originally from Squeak. It could well be that some parts of the book have been cleaned better than others to match Image changes. Thanks for reporting, and even better if you can correct it. cheers -ben |
I don't use GitHub. I've never used GitHub. I don't know how to use GitHub.
I looked at making a pull request and I was totally stymied. "Clueless" doesn't even begin to describe how I felt.
|
Time to learn. Phil On Mon, May 29, 2017 at 1:24 AM, horrido <[hidden email]> wrote: I don't use GitHub. I've never used GitHub. I don't know how to use GitHub. |
In reply to this post by horrido
On Mon, May 29, 2017 at 7:24 AM, horrido <[hidden email]> wrote:
> I don't use GitHub. I've never used GitHub. I don't know how to use GitHub. > > I looked at making a pull request and I was totally stymied. "Clueless" > doesn't even begin to describe how I felt. Everyone starts there. Its a useful skill to have, so if you are interested to learn, here's a quickstart guide... (Note since its a text file, we are shortcutting so you don't even need git on your local machine) 1. Click on this link. https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/Morphic/Morphic.pier 2. Click the <Fork> button in the top tight, then browse to "Morphic.pier" in your fork. 3. To the right of the <Raw/Blame/History> buttons, click the <Pencil> button to edit the file. 4. After editing, at the bottom select "Create a new branch for this commit and start a pull request" and enter commit title, comment and new-branch-name to reflect your contribution. Click <Propose file change> 5. Now looking at "Open a pull request", this is issuing a PR to your own fork. We need to change to the upstream fork. At the top click on "forked from SquareBracketAssociates/UpdatedPharoByExample" . 6. Under "Your recently pushed branches" click <Compare & pull request> 7. Check... base fork: SquareBracketAssociates/UpdatedPharoByExample base: master head fork: YourName/UpdatedPharoByExample compare: your new-branch-name Review the diff, enter PR title and comment, then click <Create pull request>. If you have problems, try to catch me on Discord... https://discord.gg/dKv4qR/KCKQSSt cheers -ben P.S. @Stef, btw, it looks like "the guidelines for contributing" is out of date... https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/CONTRIBUTING.md "This is a test to see if it makes sense to convert PharoByExample into pier format." > > > Ben Coman wrote >> On Sat, May 27, 2017 at 9:15 PM, horrido < > >> horrido.hobbies@ > >> > wrote: >>> Yes, I did. I found it difficult to understand. It would be nice to have >>> some >>> clear examples in the documentation, for example, really simple and >>> common >>> situations such as a C function returning an integer in a >>> passed-by-reference argument. >>> >>> Speaking of documentation, in "Pharo by Example 50," I found this >>> statement: >>> >>>> (Recall that you should set halosEnabled in the Preferences >>> browser.) >>> >>> However, nowhere else in the book is there any reference to halosEnabled >>> or >>> Preferences browser. >> >> Nice pick up. Now in Settings (in a fresh 60473 image), filtering on >> "halo" shows only: >> * Cycle both directions >> * Encloses fullbounds >> * Exhibits bounds >> so the comment seems irrelevant >> >> and #halosEnabled & #halosEnabled: each have one implementor class-side of >> Morph >> with one sender from MorphTest each, and one sender from Morph>>addHalo: >> >> and "Morph halosEnabled" ==> true, so the statement seems unnecessary. >> Would you like to contribute a pull request removing it? >> https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/Morphic/Morphic.pier >> >> >>> Did you guys forget and leave out a chunk of the book? >> >> Its an evolutionary book originally from Squeak. It could well be >> that some parts of the book have been cleaned >> better than others to match Image changes. Thanks for reporting, and >> even better if you can correct it. >> >> cheers -ben > > > > > > -- > View this message in context: http://forum.world.st/How-to-use-uFFI-with-String-tp4947890p4948512.html > Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. > |
In reply to this post by horrido
On Sat, May 27, 2017 at 06:15:47AM -0700, horrido wrote:
> Yes, I did. I found it difficult to understand. It would be nice to have some > clear examples in the documentation, for example, really simple and common > situations such as a C function returning an integer in a > passed-by-reference argument. I've written such a thing: https://github.com/PierceNg/libffidemo See these functions in the C library: int get_by_filling_pointer(demo_thing *pthing, int *pvalue) int get_by_returned_value(demo_thing *pthing) Two follow-up blog posts: http://www.samadhiweb.com/blog/2016.03.12.demoffi.html http://www.samadhiweb.com/blog/2016.03.17.demoffi.html I intend to modify my blog posts to contribute to the booklet, sometime in this month of June. Pierce |
In reply to this post by Ben Coman
Hi ben
Yes I changed it :) Stef On Mon, May 29, 2017 at 10:16 AM, Ben Coman <[hidden email]> wrote: > On Mon, May 29, 2017 at 7:24 AM, horrido <[hidden email]> wrote: >> I don't use GitHub. I've never used GitHub. I don't know how to use GitHub. >> >> I looked at making a pull request and I was totally stymied. "Clueless" >> doesn't even begin to describe how I felt. > > Everyone starts there. Its a useful skill to have, so if you are > interested to learn, here's a quickstart guide... > (Note since its a text file, we are shortcutting so you don't even > need git on your local machine) > 1. Click on this link. > https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/Morphic/Morphic.pier > 2. Click the <Fork> button in the top tight, then browse to > "Morphic.pier" in your fork. > 3. To the right of the <Raw/Blame/History> buttons, click the <Pencil> > button to edit the file. > 4. After editing, at the bottom select "Create a new branch for this > commit and start a pull request" > and enter commit title, comment and new-branch-name to reflect your > contribution. Click <Propose file change> > 5. Now looking at "Open a pull request", this is issuing a PR to your > own fork. We need to change to the upstream fork. At the top click > on "forked from SquareBracketAssociates/UpdatedPharoByExample" . > 6. Under "Your recently pushed branches" click <Compare & pull request> > 7. Check... > base fork: SquareBracketAssociates/UpdatedPharoByExample > base: master > head fork: YourName/UpdatedPharoByExample > compare: your new-branch-name > Review the diff, enter PR title and comment, then click <Create pull request>. > > If you have problems, try to catch me on Discord... > https://discord.gg/dKv4qR/KCKQSSt > > cheers -ben > > P.S. @Stef, btw, it looks like "the guidelines for contributing" is > out of date... > https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/CONTRIBUTING.md > "This is a test to see if it makes sense to convert PharoByExample > into pier format." > > >> >> >> Ben Coman wrote >>> On Sat, May 27, 2017 at 9:15 PM, horrido < >> >>> horrido.hobbies@ >> >>> > wrote: >>>> Yes, I did. I found it difficult to understand. It would be nice to have >>>> some >>>> clear examples in the documentation, for example, really simple and >>>> common >>>> situations such as a C function returning an integer in a >>>> passed-by-reference argument. >>>> >>>> Speaking of documentation, in "Pharo by Example 50," I found this >>>> statement: >>>> >>>>> (Recall that you should set halosEnabled in the Preferences >>>> browser.) >>>> >>>> However, nowhere else in the book is there any reference to halosEnabled >>>> or >>>> Preferences browser. >>> >>> Nice pick up. Now in Settings (in a fresh 60473 image), filtering on >>> "halo" shows only: >>> * Cycle both directions >>> * Encloses fullbounds >>> * Exhibits bounds >>> so the comment seems irrelevant >>> >>> and #halosEnabled & #halosEnabled: each have one implementor class-side of >>> Morph >>> with one sender from MorphTest each, and one sender from Morph>>addHalo: >>> >>> and "Morph halosEnabled" ==> true, so the statement seems unnecessary. >>> Would you like to contribute a pull request removing it? >>> https://github.com/SquareBracketAssociates/UpdatedPharoByExample/blob/master/Morphic/Morphic.pier >>> >>> >>>> Did you guys forget and leave out a chunk of the book? >>> >>> Its an evolutionary book originally from Squeak. It could well be >>> that some parts of the book have been cleaned >>> better than others to match Image changes. Thanks for reporting, and >>> even better if you can correct it. >>> >>> cheers -ben >> >> >> >> >> >> -- >> View this message in context: http://forum.world.st/How-to-use-uFFI-with-String-tp4947890p4948512.html >> Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com. >> > |
Cool. Looks good.
cheers -ben On Wed, Jun 7, 2017 at 2:20 AM, Stephane Ducasse <[hidden email]> wrote: Hi ben |
Free forum by Nabble | Edit this page |