Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing on C argv to the script environment #16

Open
svaarala opened this issue Feb 16, 2016 · 12 comments
Open

Passing on C argv to the script environment #16

svaarala opened this issue Feb 16, 2016 · 12 comments

Comments

@svaarala
Copy link
Contributor

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.

@creationix
Copy link
Owner

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

@svaarala
Copy link
Contributor Author

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

@svaarala
Copy link
Contributor Author

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

@creationix
Copy link
Owner

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.

@svaarala
Copy link
Contributor Author

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.

@creationix
Copy link
Owner

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

@svaarala
Copy link
Contributor Author

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.

@creationix
Copy link
Owner

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.

@svaarala
Copy link
Contributor Author

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).

@creationix
Copy link
Owner

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
Copy link
Owner

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

@svaarala
Copy link
Contributor Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants