dukluv icon indicating copy to clipboard operation
dukluv copied to clipboard

Passing on C argv to the script environment

Open svaarala opened this issue 9 years ago • 12 comments

Are there plans to pass on the C argv into the script environment to allow scripts to do command line parsing? This would be quite useful for e.g. Duktape's DukLuv based JSON proxy.

I tried to figure out if there's any binding currently and it seems src/main.c gives argc/argv to uv_setup_args() but I guess it's not passed on otherwise?

I'll be happy to contribute a pull if necessary.

svaarala avatar Feb 16 '16 16:02 svaarala

You're welcome to submit a pull request. I've been super busy with other projects at the moment.

creationix avatar Feb 16 '16 19:02 creationix

Ok, I'll do that. The only non-trivial thing is to figure out where that array should go :)

svaarala avatar Feb 16 '16 19:02 svaarala

It seems uv has a bunch of misc bindings as well, so maybe uv.argv or uv.get_argv()?

svaarala avatar Feb 16 '16 19:02 svaarala

Initially I don't think uv is the right place, but then again it needs access to it anyway for proper setup so maybe it's not crazy after all.

creationix avatar Feb 16 '16 19:02 creationix

To mimic Node.js it could beprocess.argv but since DukLuv is not trying to emulate Node.js that's not really a big point.

Placing it in uv would at least have the merit that it wouldn't pollute the global object for example, in case a better place is later found.

svaarala avatar Feb 16 '16 19:02 svaarala

The way I do this in luvit is to pass the arg list to the script as part of the main bootstrap.
https://github.com/luvit/luvi/blob/master/src/main.c#L130-L135 https://github.com/luvit/luvi/blob/master/src/lua/init.lua#L91

creationix avatar Feb 16 '16 19:02 creationix

I thought of misc.c as the place to implement because of this: https://github.com/creationix/dukluv/blob/master/src/duv.c#L104-L123; it already contains things like cpu_info() and other odds and ends.

svaarala avatar Feb 16 '16 19:02 svaarala

Right, that would work. uv.argv() that returns an array of strings would work well. I don't have any idea how we would pull the argv our of libuv though. I guess we could dig through the private data.

It would be nice if libuv accepted this as an official API. I wonder what they think of adding it.

creationix avatar Feb 16 '16 19:02 creationix

Yeah, one simple approach would be to construct the argv array in main() and just stick it in the global stash and let uv.argv() then read it from there. Or construct it in main() and pass on to duv_main() (which currently gets argv[1] i.e. script filename).

svaarala avatar Feb 16 '16 19:02 svaarala

Looking through libuv, I don't think they store the full argv, but we could store a copy in our bindings to uv_setup_args somewhere.

creationix avatar Feb 16 '16 19:02 creationix

I like the global stash idea. Do whatever you think is best. My only preference is to diverge from libuv as little as possible.

creationix avatar Feb 16 '16 19:02 creationix

It'd be nice to read it back from libuv but that doesn't seem possible. I'll open a pull with a proposal, thanks!

svaarala avatar Feb 16 '16 19:02 svaarala