How to call a DLL function with a variable amount of arguments

Viewed 42

I'm currently working on writing my own statically typed scripting language, and I'm trying to make it so you can import native functions exported to shared libraries (.dll, .so).

I want to be able to make something like this work:

// My language, int is 64 bits
extern "math_lib.dll" {
    fun add(int a, int b) -> int;
    fun sub(int a, int b) -> int;
}

math_lib.c, compiled to math_lib.dll:

__declspec(dllexport) int64_t add(int64_t a, int64_t b) {
    return a + b;
}

__declspec(dllexport) int64_t sub(int64_t a, int64_t b) {
    return a - b;
}

I'm implementing my language in Rust. I can load functions just fine using a crate like libloading, but the problem is I don't know how to import a function with a non-compile time constant signature. I would assume it's not really possible, but C# lets you do this with the DllImport attribute. I could just require all functions to only take one argument, a pointer my language's vm's stack, and pop off the arguments manually. I don't like this though as it's less ergonomic for those implementing native functions, and it means you'd have to create wrapper libraries to import functions from existing native libraries (which could be avoided with my original idea, especially if I added more widths for integers etc.).

0 Answers
Related