<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head><body style='font-size: 10pt; font-family: Verdana,Geneva,sans-serif'>
<p>There is a presentation on "<em>How to write a simple VM plugin in Cuis</em>" on the <a href="https://cuis.st/community">https://cuis.st/community</a> page, with link to the meeting video at <a href="https://youtu.be/XcgPTewqutQ">https://youtu.be/XcgPTewqutQ</a></p>
<p>Dave</p>
<div id="signature"></div>
<p><br /></p>
<p id="reply-intro">On 2024-09-30 18:54, Vanessa Freudenberg via Cuis-dev wrote:</p>
<blockquote type="cite" style="padding: 0 0.4em; border-left: #1010ff 2px solid; margin: 0">
<div id="replybody1">
<div dir="ltr">For one, there's no reason to subclass <span style="color: #500050; font-family: monospace;">ExternalLibrary</span>.
<div> </div>
<div>But otherwise, it works for me:</div>
<div> </div>
<img src="cid:172772628366fb02cba2785540203914@mail.msen.com" alt="image.png" width="562" height="130" /><br />
<div> </div>
<div><img style="margin-right: 0px;" src="cid:172772628366fb02cba2178821732472@mail.msen.com" alt="image.png" width="562" height="121" /></div>
<div> </div>
<div>That said, if you actually want to use C functions in your application, the better way is to build a VM plugin. It's a little bit more overhead to learn but fits the Smalltalk philosophy much better, where the VM provides the environment for the image to live in, rather than the image talking to the outside world directly. The job of the VM is to provide a safe world so Smalltalk developers don't have to worry about the harsh reality. FFI breaks that illusion.</div>
<div> </div>
<div>Vanessa</div>
</div>
<br />
<div class="v1gmail_quote">
<div class="v1gmail_attr" dir="ltr">On Mon, Sep 30, 2024 at 8:10 AM Mark Volkmann via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" rel="noreferrer">cuis-dev@lists.cuis.st</a>> wrote:</div>
<blockquote class="v1gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid #cccccc; padding-left: 1ex;">
<div dir="ltr">Vanessa's getenv example works for me. Now I want to learn how to invoke my own C functions.
<div>I created the file "mylib.c" containing this:</div>
<div> </div>
<div><span style="font-family: monospace;">char* greet() {<br /> return "Hello, World!";<br />}</span></div>
<div> </div>
<div>Then I created a shared library for macOS with this:</div>
<div> </div>
<div><span style="font-family: monospace;">clang -dynamiclib -o libmylib.dylib mylib.c<br /></span></div>
<div> </div>
<div>Then I created this Smalltalk class:</div>
<div> </div>
<div><span style="font-family: monospace;">ExternalLibrary subclass: #FFIDemo<br /> instanceVariableNames: ''<br /> classVariableNames: ''<br /> poolDictionaries: ''<br /> category: 'Volkmann'</span></div>
<div> </div>
<div>Then I added this class method:</div>
<div> </div>
<div><span style="font-family: monospace;">greet<br /> <cdecl: char* 'greet' () module: '/Users/volkmannm/Documents/dev/lang/smalltalk/Cuis-Smalltalk-Volkmann/libmylib.dylib'><br /> ^self externalCallFailed</span></div>
<div> </div>
<div>Side note: I need to learn where this looks by default for shared libraries so I don't have to specify an absolute path.</div>
<div> </div>
<div>Then I entered this in a Workspace and did a "Print it".</div>
<div> </div>
<div><span style="font-family: monospace;">FFIDemo greet</span></div>
<div> </div>
<div>That opens a Debug window with the message "Error: Unable to find function address".</div>
<div> </div>
<div>Can anyone spot where I'm going wrong?</div>
</div>
<br />
<div class="v1gmail_quote">
<div class="v1gmail_attr" dir="ltr">On Sun, Sep 29, 2024 at 10:10 PM Andres Valloud via Cuis-dev <<a href="mailto:cuis-dev@lists.cuis.st" rel="noreferrer">cuis-dev@lists.cuis.st</a>> wrote:</div>
<blockquote class="v1gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left: 1px solid #cccccc; padding-left: 1ex;">Except of course getenv() is ruled by POSIX so doing that is at the very <br />least not portable, if not outright wrong.<br /><br />On 9/29/24 6:29 PM, Vanessa Freudenberg via Cuis-dev wrote:<br />> And, defining a method myself works fine:<br />> <br />> getenv: var<br />> <cdecl: char* 'getenv' (char*) module: 'libSystem.dylib'><br />> ^self externalCallFailed<br />> <br />> And then<br />> <br />> MyClass getenv: 'HOME'<br />> <br />> <br />> returns<br />> <br />> '/Users/vanessa' .<br />> <br />> <br />> There really isn't much more to it.<br />> <br />> Vanessa<br />> <br />> On Sun, Sep 29, 2024 at 6:26 PM Vanessa Freudenberg <br />> <<a href="mailto:vanessa@codefrau.net" rel="noreferrer">vanessa@codefrau.net</a> <mailto:<a href="mailto:vanessa@codefrau.net" rel="noreferrer">vanessa@codefrau.net</a>>> wrote:<br />> <br />> I just loaded the "Tests-FFI" package and it works.<br />> <br />> And you can e.g. run<br />> <br />> FFITestLibrary new ffiTestFloats: 3 with: 4<br />> <br />> which returns 7.0 because the plugin happens to implement<br />> "ffiTestFloats()" as returning the sum of its two arguments.<br />> <br />> Vanessa<br />> <br />> <br /><br />-- <br />Cuis-dev mailing list<br /><a href="mailto:Cuis-dev@lists.cuis.st" rel="noreferrer">Cuis-dev@lists.cuis.st</a><br /><a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" target="_blank" rel="noopener noreferrer">https://lists.cuis.st/mailman/listinfo/cuis-dev</a></blockquote>
</div>
<br clear="all" />
<div> </div>
<span class="v1gmail_signature_prefix">-- </span><br />
<div class="v1gmail_signature" dir="ltr">
<div dir="ltr">
<div>
<div dir="ltr">
<div>
<div dir="ltr">
<div dir="ltr">
<div><span style="font-family: arial, helvetica, sans-serif;">R. Mark Volkmann</span></div>
<div><span style="font-size: 12.8px;"><span style="font-family: arial, helvetica, sans-serif;">Object Computing, Inc.</span></span></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
-- <br />Cuis-dev mailing list<br /><a href="mailto:Cuis-dev@lists.cuis.st" rel="noreferrer">Cuis-dev@lists.cuis.st</a><br /><a href="https://lists.cuis.st/mailman/listinfo/cuis-dev" target="_blank" rel="noopener noreferrer">https://lists.cuis.st/mailman/listinfo/cuis-dev</a></blockquote>
</div>
</div>
<br />
<div class="pre" style="margin: 0; padding: 0; font-family: monospace"> </div>
</blockquote>
</body></html>