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

Dan Wood dw43031apple at karelia.com
Sat Jul 3 21:45:59 PDT 2010

Ah, it's very helpful to have jsc.  At least now I can see that there were errors loading the code, which for some reason I wasn't able to get when loading from Cocoa.

So according to the logic in this javascript file (excerpted below), if there is a window variable passed in, then try to make use of all the window properties; if it's undefined, then assume it's loading under node.js and attach to the "exports" object.

> var less, tree;
> if (typeof(window) === 'undefined') {
>     less = exports,
>     tree = require('less/tree');
> } else {
>     less = window.less = {},
>     tree = window.less.tree = {};
> }

What's strange is that if I don't define window={} as you did, and just load the file wish jsc, I get the following error:

Exception: ReferenceError: Can't find variable: window

Um, is this what I would expect?  Isn't the javascript 'undefined' type exactly for this kind of purpose?  Do I need to pre-define "window" as an undefined object somehow?

Or maybe where it's failing is in line 8 of the file where it's referencing window somehow?

On Jul 3, 2010, at 12:02 PM, Alexey Proskuryakov wrote:

> 02.07.2010, в 22:58, Dan Wood написал(а):
>> * 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
> less.js project page says "will be able to run directly in the browser, as well as on the server, with node.js". Assuming it's currently capable of the latter, it sounds like you need to implement whatever node.js requires from its environment, evaluate node.js, and then evaluate less.js without adding a window object. Or it could be easier to just run it in a WebView.
> When window is not undefined, less.js starts using things that are only available in browser DOM, such as XMLHttpRequest or document.getElementsByTagName.
>> 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.
> The reason JavaScript code is often wrapped this way is to avoid defining functions and variables in global scope.
> I'm not sure why exactly you are not getting a "less" property added to your stub object. It seems that it should be added unless an uncaught exception were raised (and then you'd get null from JSEvaluateScript, not undefined).
> In fact, I cannot replicate your results with jsc command line tool:
> $ WebKitBuild/Debug/jsc
>> window={}
> [object Object]
>> load("/Users/ap/Desktop/less-1.0.30.js");
> Exception: ReferenceError: Can't find variable: location
> - WBR, Alexey Proskuryakov

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

He who passively accepts evil is as much involved in it as he who helps to perpetrate it. He who accepts evil without protesting against it is really cooperating with it. — Martin Luther King, Jr.

More information about the webkit-help mailing list