Re: Web scripting key does not work
Christiaan Hofman <[email protected]> Thu, 21 Oct 2010 12:11:35 +0200
| Newsgroups | gmane.comp.macosx.devel |
|---|---|
| Message-ID | <[email protected]> |
On Oct 21, 2010, at 5:36, Andy Lee wrote: > On Oct 20, 2010, at 5:22 PM, Christiaan Hofman wrote: >> Anyway, thanks for your comments, at least it pointed me to the cause. What happened is that the (bar) property I am trying to expose to JS does not correspond to an ivar, but to a generated value (in fact a value in another object; my exposed Foo object is mostly a wrapper to expose stuff to JS). And apparently web object scripting checks /ivars/ of the class, rather than (KVC) /properties/ of the class. Of course there is in general no reason why ivars and properties need to have any relation. Certainly, the docs are wrong on this point. > > I noticed in the CallJS example there is a comment that says the getter and setter are not called. I was going to mention that in my testing they *were* called, at least judging by the NSLogs. But maybe I was misreading the logs, since you say scripting does indeed go through the ivars. > > Anyway, glad you at least got to the bottom of it. > > --Andy That comment is definitely wrong, it does seem to go through valueForKey: and setValue:forKey:, the accessors are indeed used, also in the CallJS sample as you can see in the logs. Perhaps it was not in an earlier OS version, this sample seems to be somewhat older. Also in my situation I tried just adding a dummy ivar with the same name, then things worked, and the accessors where called. The dummy (unused) ivar is used to provide the 'key' name, but it is never changed. So basically it uses KVC to /access/ the properties, but it uses the ivars to get the 'key' names. Bit weird and inconsistent, and certainly wrong. I guess this is also related to the weird inverted search for valid keys: JS knows a JS key, and needs to find the corresponding Cocoa key in order to access it, however the methods isKeyExcludedFromWebScript: and webScriptNameForKey: start from the Cocoa key, which the system cannot know yet at that point, so it has to guess a bunch of keys based on somewhat arbitrary conditions (in this case the ivar names). Same for the selectors/functions. While the other way (from Cocoa keys to JS names) is never used. Very bad design, and very inefficient. They should have had methods isWebScriptNameExcluded: and keyForWebScriptName:, which would have made much more sense and would be able to work correctly with KVC. Christiaan