4 ms·
That's where it's tricky, the compiler doesn't know, it's checked at runtime by the C++ function, which receives a list of Values, count them (actually quite co
by jackrabbit_ 7y ago
That's where it's tricky, the compiler doesn't know, it's checked at runtime by the C++ function, which receives a list of Values, count them (actually quite convenient if we need to make variadic argument functions) and decide if it's fine or not, then does type checking on the needed argument. I know it sounds crazy because it can cause performance loss, but in a dynamic language like this one that's a cost I must pay.
- blattimwind 7y agoThat's not what you'd typically understand as an FFI; that's just an interpreter / VM API. A foreign function interface would generally allow something running in the VM to call arbitrary external (hence foreign) functions that were not written against the VM's API, i.e. don't have a binding. For example, this is what FFI could look like in some language: lib = ffi.load("kernel32) lib.TerminateProcess.args = (ffi.voidptr, ffi.unsigned) lib.TerminateProcess.returns = ffi.int lib.TerminateProcess.callconv = "stdcall" lib.TerminateProcess(1234, 56) Arguably you neither want nor need FFI for a game scripting language.
- eatonphil 7y agoRight, I'm confused /curious about FFI calls not regular function calls.
- jackrabbit_ 7y agoI'm sorry, I used the wrong word (see the details in the above comment: https://news.ycombinator.com/item?id=22698450 https://news.ycombinator.com/item?id=22698450) for this.. It's not a FFI but an interface using the VM API.
- eatonphil 7y agoWait but when you `(import "librandom.so")` and call `(random)` in your examples, that's an FFI. But I guess I'm getting the picture anyway. When there's a foreign function call, you count the number of arguments and convert them to their C++ types and call a helper function for making a call against some function pointer with so many arguments? I'm still confused though how you'd pass strings or function pointers for example.
- jackrabbit_ 7y agoThat's the idea yes. Since an example is worth a thousand words, here is a function for the http module, counting argument, checking the types and then using the values to create an http client (to make http requests to web servers): https://github.com/ArkScript-lang/modules/blob/refactoring/http/src/client.cpp#L72 https://github.com/ArkScript-lang/modules/blob/refactoring/h...
- eatonphil 7y agoThat's not exactly an FFI, that's an extension since you're coding against ArkScript's C++ API. An FFI is when you use only ArkScript to make calls against other languages that don't know about ArkScript's API.
- saagarjha 7y agoThe wiki (https://github.com/ArkScript-lang/Ark/wiki/Embedding#creating-plugins https://github.com/ArkScript-lang/Ark/wiki/Embedding#creatin...) calls them “plugins” and they’re also called “modules”, which I think is a lot clearer than “FFI”. Essentially it’s a bunch of native functions that are exported with C linkage and compiled into a set of shared libraries, which are then loaded and the functions in them looked up at runtime. On the other (C++) side it’s similar to e.g. a JNI binding or any other language where you can pull values out of the runtime.
- jackrabbit_ 7y ago> An FFI is when you use only ArkScript to make calls against other languages that don't know about ArkScript's API I should definitly rename this part as stated in another comment, since the "FFI" isn't a real one, I messed up with the vocabulary
- ngold 7y agoSuper fun. This is a great project.
- jackrabbit_ 7y ago
- jackrabbit_ 7y agoYou're right, I'm using the wrong name for this. At the beginning the goal was to have what you described as a FFI and what I thought a primitive "FFI" would look like (eg. what it is today, a system using the VM API), but I never managed to do both and the name stayed, and now I'm here a bit confused, messing up and mixing words..