Script language native extensions - avoiding name collisions and cluttering others' namespace
Posted
by
H2CO3
on Programmers
See other posts from Programmers
or by H2CO3
Published on 2013-10-07T17:47:06Z
Indexed on
2013/11/09
22:09 UTC
Read the original article
Hit count: 313
I have developed a small scripting language and I've just started writing the very first native library bindings. This is practically the first time I'm writing a native extension to a script language, so I've run into a conceptual issue.
I'd like to write glue code for popular libraries so that they can be used from this language, and because of the design of the engine I've written, this is achieved using an array of C struct
s describing the function name visible by the virtual machine, along with a function pointer.
Thus, a native binding is really just a global array variable, and now I must obviously give it a (preferably good) name. In C, it's idiomatic to put one's own functions in a "namespace" by prepending a custom prefix to function names, as in myscript_parse_source()
or myscript_run_bytecode()
. The custom name shall ideally describe the name of the library which it is part of. Here arises the confusion.
Let's say I'm writing a binding for libcURL
. In this case, it seems reasonable to call my extension library curl_myscript_binding
, like this:
MYSCRIPT_API const MyScriptExtFunc curl_myscript_lib[10];
But now this collides with the curl
namespace. (I have even thought about calling it curlmyscript_lib
but unfortunately, libcURL does not exclusively use the curl_
prefix -- the public APIs contain macros like CURLCODE_*
and CURLOPT_*
, so I assume this would clutter the namespace as well.)
Another option would be to declare it as myscript_curl_lib
, but that's good only as long as I'm the only one who writes bindings (since I know what I am doing with my namespace). As soon as other contributors start to add their own native bindings, they now clutter the myscript
namespace. (I've done some research, and it seems that for example the Perl cURL binding follows this pattern. Not sure what I should think about that...)
So how do you suggest I name my variables? Are there any general guidelines that should be followed?
© Programmers or respective owner