Bug 8544 – Expose "cArgs" in druntime

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-08-12T14:22:00Z
Last change time
2012-10-27T08:22:06Z
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2012-08-12T14:22:34Z
When interfacing with C/C++ libraries one common problem is having to extract argc/argv arguments from D main() and passing them back to library functions. A common solution is: // Array of pointers to command line parameters. char*[] argv = args.map!((a)=>(a.dup~'\0').ptr).array; But it would be much simpler (and more efficient) if argc & argv were stored somewhere in druntime. There's a Runtime.args() function in core.runtime which exports the runtime args as a D-friendly array, so we could provide a cArgs equivalent which returns a struct with argc/argv. druntime/src/rt/dmain2.d is where main is called from: extern (C) int main(int argc, char** argv) { ... } From there we could easily store argc+argv into a global struct variable, similar to how _d_args is exposed as a __gshared string[].
Comment #1 by andrej.mitrovich — 2012-10-02T16:53:34Z
I'd like to fix this myself. But I'm not sure how this should be exposed in the Runtime struct. Should it be two properties, 'argc' and 'argv', or a single property such as the following, in core.runtime: extern (C) CArgs rt_cArgs(); static @property string[] cArgs() { return rt_cArgs(); } and in src\rt\dmain2.d: struct CArgs { int argc char** argv; } __gshared CArgs _cArgs = null; extern (C) string[] rt_cArgs() { return _cArgs; } extern (C) int main(int argc, char** argv) { _cArgs.argc = argc; _cArgs.argv = argv; ... } Thoughts?
Comment #2 by andrej.mitrovich — 2012-10-02T16:54:34Z
(In reply to comment #1) > I'd like to fix this myself. But I'm not sure how this should be exposed in the > Runtime struct. Should it be two properties, 'argc' and 'argv', or a single > property such as the following, in core.runtime: > > extern (C) CArgs rt_cArgs(); > > static @property string[] cArgs() > { > return rt_cArgs(); > } That should be: > static @property CArgs cArgs() and > extern (C) CArgs rt_cArgs() Obviously I'm just pseudocoding. :)
Comment #3 by alex — 2012-10-02T17:00:47Z
I suppose it wouldn't be a problem to have a cArgs property on the Runtime struct. I'd prefer returning a struct over two separate properties.
Comment #4 by github-bugzilla — 2012-10-23T13:56:59Z
Commits pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/2e73c0fd3139af781f3afe926c4485b20f88a16d Fixes Issue 8544 - Expose cArgs in Druntime struct for easier interfacing with C libraries. https://github.com/D-Programming-Language/druntime/commit/84beb97864444e11b419cfa5eb4792d7fbdc8e1a Merge pull request #336 from AndrejMitrovic/Fix8544 Fix Issue 8544 - Expose cArgs in Druntime struct for C interfacing
Comment #5 by github-bugzilla — 2012-10-27T08:22:06Z