Fabulous Adventures In Coding
Eric Lippert is a principal developer on the C# compiler team. Learn more about Eric.
A reader sent me an email over the weekend asking about some odd behaviour in the guts of the JScript engine. Unfortunately, they didn't include an email address that worked; I just got a bounce message back, Ben. Therefore I'll just answer the question here.
To briefly describe the problem: the user fetches the JScript "Math" object from the script engine by calling ParseScriptText to evaluate the expression "Math". This returns an IDispatch object. The dispatch object can be queried for dispatch ids for "sin" and "cos" and all that. It can be successfully invoked. Everything appears to be good. But when the user calls GetTypeInfo, the type description structure says that the object has zero functions. What's up with that?
Clearly the typeinfo is out of step with reality. The user has in fact found two bugs. The first is that the typeinfo building code only adds to the typeinfo those functions which have already been dereferenced. So, had you called "sin" once already, "sin" would be added but nothing else.
However, the second bug trumps the first one; built-in functions are automatically skipped when adding function descriptions to the typeinfo. I'm not sure what I was thinking when I wrote that logic; it was a long time ago. So even if you have already dereferenced a built-in function object, it won't get added to the typeinfo.
User-defined functions should be always correctly added to the typeinfo; they are automatically dereferenced when they are created.
I wrote almost all the typelibrary code, and I regret the errors. However, it's unlikely that either of these flaws will ever be fixed, and clearly both would have to be fixed in order to make the given scenario work correctly. Both fixes would touch a lot of complex code and are therefore quite risky. We try to not apply risky fixes to fix a problem that, to my knowledge, only one person has ever noticed in the last seven or eight years! If there's a massive outcry to make typeinfos for built-in objects work correctly, I'll pass that along to the sustaining engineering team; I wouldn't hold my breath waiting for a fix if I were you.
Thanks for bringing this to my attention.