How to make Io call C function #472
-
How do I complete the following code?
|
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 3 replies
-
Hey, Io's binding API is designed around objects so it's best to create an object in which to place you're C functions. The object can also be used to store any C state/context pointers associated with the C library you are binding. Check out the Date binding for an example: https://github.com/IoLanguage/io/blob/master/libs/iovm/source/IoDate.h If the C library you are binding to involves allocating C structs or other pointers that need to be passed around, it's best to create an Io wrapper class for each. The ODE addon is an example of that: https://github.com/IoLanguage/ODE/tree/master/source It has an Io object wrapper for each of the object types in the ODE library. |
Beta Was this translation helpful? Give feedback.
-
It's been a while since I've used the C binding so I asked ChatGPT4 for a simple example with just one C function added to the lobby. Here's what it wrote (which looks reasonable, but I'm not sure it's correct). This just adds a method for the function and calls it using doCString so it's not and general purpose binding: #include <stdio.h> IoObject* hello(IoObject *self, IoObject *locals, IoMessage *m) { int main(int argc, const char *argv[]) {
} |
Beta Was this translation helpful? Give feedback.
-
Here's the full anwser
|
Beta Was this translation helpful? Give feedback.
-
Thanks. Glad it worked. It's got me thinking I should have GPT4 write the unfinished binding section of the Io Programming Guide. Btw, here's the session info: https://chat.openai.com/share/f4dbb24b-7c93-4105-ac55-0b3599622c29 |
Beta Was this translation helpful? Give feedback.
-
There is also a commented example of writing native addon here, which might be helpful |
Beta Was this translation helpful? Give feedback.
It's been a while since I've used the C binding so I asked ChatGPT4 for a simple example with just one C function added to the lobby. Here's what it wrote (which looks reasonable, but I'm not sure it's correct). This just adds a method for the function and calls it using doCString so it's not and general purpose binding:
#include <stdio.h>
#include "IoState.h"
#include "IoMessage.h"
#include "IoNumber.h"
#include "IoObject.h"
IoObject* hello(IoObject *self, IoObject *locals, IoMessage *m) {
puts("Hello world!");
return self;
}
int main(int argc, const char *argv[]) {
IoState *self = IoState_new();