[webkit-help] Trouble figuring out JavaScriptCore way of getting into this JavaScript code

Dan Wood dw43031apple at karelia.com
Fri Jul 2 22:58:40 PDT 2010

Hi folks,

I'm trying out a little side project of loading a bit of JavaScript from a cocoa app -- directly, not from within a WebView.

I've figured out the basics of JavaScriptCore and I've played with JSInterpreter and JSPong a bit.  But I'm stuck, mostly trying to figure out how to get at the object that loading this JavaScript file is supposed to define.

I'm loading the "less.js" code ... you can see the file directly here http://github.com/cloudhead/less.js/raw/master/dist/less-1.0.30.js ; its home on GitHub is http://github.com/cloudhead/less.js .

The whole codebase is wrapped in (function (window, undefined) { ... })(window);  ... And alas, my JS-fu is not strong enough to comprehend what this is doing, why it is there, or how this is going to affect the global context that I'm passing in when I interpret this.

Around line 142, it's defining the variable "less", and I guess a property 'less' on the global 'window' object.  So I would think that if I pass in a global object "window" that I should get a property back on it?  But maybe this wrapper around all the code is making it not really appear where I'd think it would?

Here's what I'm doing in my JavaScriptCore code:

* Create a context with JSGlobalContextCreate
* Get the global object with JSContextGetGlobalObject 
* Create a generic object (to be the global 'window' object) with JSObjectMake(ctx, NULL, NULL)
* Set this object to be the property "window" of the global object with JSObjectSetProperty
* Load my string the source file using JSEvaluateScript, passing in thisObject=NULL

At this point, the JSValueRef returned is of type kJSTypeUndefined. I think that's OK.

But I try to get the object property of the global object called 'less', and also try to get the property of the reference to my 'window' object called 'less' but I get NULL in both cases.

So I'm kind of stuck.  I'm wondering if it's that mysterious wrapping around the source code that is confusing me enough to either pass in the wrong environment (e.g. a global 'window' object) or extract the results out incorrectly.

Any suggestions for how I might invoke this code and get out my "less" object so I can go onto the next step?

Also, are there any general suggestions for how to debug JavaScript?  Now that Drosera is no longer a stand-alone application, is there any way to debug JavaScriptCore-invoked JavaScript (i.e. not within a browser window context)? Any other suggestions for introspecting what is going on, e.g. what are all the objects and their properties?


Dan Wood
Twitter: http://twitter.com/danwood
Karelia Software — Sandvox for the Mac

"To strive, to seek, to find, and not to yield" — Tennyson, Ulysses

More information about the webkit-help mailing list