[Dojo-interest] Can anyone explain what dojo's eval method is supposed to do?

David Mark dmark at cinsoft.net
Thu Feb 25 20:34:25 EST 2010

Can anyone explain what dojo's eval method is supposed to do?  Not what it
actually does, but what it was designed to do.  Barring that, what are the
popular beliefs associated with its functionality.

JFTR, here is how I see it.

dojo["eval"] = function(/*String*/ scriptFragment){
		//	summary:
		//		Perform an evaluation in the global scope. Use this rather than
		//		calling 'eval()' directly.

First of all, it does not perform an evaluation in the global scope.  It may
do that in some cases, but clearly it does not do that in others.  And why
should it be used in lieu of eval when eval does not do what this method is
described to do (but does match what it actually does in at least some
cases?)  There seems to be some confusion about this very pivotal method
(which is used to evaluate scripts downloaded by Dojo).

		//	description:
		//		Placed in a separate function to minimize size of trapped
		//		exceptions. Calling eval() directly from some other scope may
		//		complicate tracebacks on some platforms.

The function itself is exactly one line of code, but it is not at all clear
what that one line of code is supposed (or expected) to do.

What is placed in a separate function?  The aside about scope just confuses
the issue further.  Just what does this one line of code do to simplify
error reporting?

		//	returns:
		//		The result of the evaluation. Often `undefined`

What does that mean?  It clearly returns the result of the evaluation.  The
frequency with which that might be the undefined value (not "undefined",
which is a string) is indeterminate, so this is just more muddying. 
Deleting every comment up to this point would make things much clearer
(hence my original question about what people think this is supposed to do).

		// note:
		//	 - JSC eval() takes an optional second argument which can be 'unsafe'.

That note is clearly out of place (i.e. what does it have to do with this

		//	 - Mozilla/SpiderMonkey eval() takes an optional second argument which
is the
		//  	 scope object for new symbols.

Same here.  So far we have what seems to be folklore and a couple of
seemingly random observations about JS engines, but nothing that indicates
what this method is supposed to do.

		// FIXME: investigate Joseph Smarr's technique for IE:

It's irrelevant (and not a technique per se) as it recommends IE's
execScript method, which does not return a value.  The goal of the cited
page is also different in that the author seeks to create globals, which
Dojo's eval does not aspire to do (or at the very least it isn't something
it can do reliably at the moment).  That comes back to the original question
of what is this supposed to do.  In reality, this is used in Dojo in
circumstances where the return value is irrelevant and in others where it is
required by the caller.  Clearly it would make more sense to split this up
into two methods (one that returns a value and one that does not).  The one
that does not require a return value should clearly use script injection as
that technique does provide the global scoping that seems to be the aim of
this method.  The one that does require a return value should simply use
eval (so as to be consistent).

		//	see also:
		// 		http://trac.dojotoolkit.org/ticket/744
		return d.global.eval ? d.global.eval(scriptFragment) :
eval(scriptFragment); 	// Object

This is it as it sits and it doesn't make sense.  The assumption seems to be
if an eval method exists on the Global Object (i.e. on Object.prototype), it
will evaluate in the global scope (and with the - this - identifier set to
the Global object).  This is definitely not the case (it does in some
versions of major browsers, but not others and is also proprietary and
deprecated at this time--clearly not something to hang your hat on).  But
even if it were a valid assumption, the logic doesn't make any sense as the
fall back is to use eval in the scope of a function that is inside an
anonymous function with tons of scope chain baggage.  Furthermore, the -
this - identifier (which is a separate issue from scope) will reference -
dojo - rather than the Global object when this is called as a method.  The
version in the featuredetection branch (which I see is finally being
scavenged by those who nastily dismissed it last fall) deals with these
latter issues, but doesn't go as far as to split the method into two.


In short, it should look something like this:-

return eval(arguments[0]); // Look ma, (almost) no scope chain augmentation!

...and the function should not be declared inside another, but in the Global
execution context.

It should be documented that to ensure that the - this - identifier is set
to the Global object (when needed), it should be called like this:-

var myEval = dojo.eval;

...as then the - this - identifier for the myEval function call will be the
Global object (and so to for the evaluated script).  It's really nowhere
near as complicated (and doesn't need to be inconsistent) as the current
implementation makes it.

If this seems like deja vu, it is because we already had this discussion
four months ago.  I was very surprised to see that this same dubious (one
line) pattern is present in a version released after that discussion. 
Perhaps there was some misunderstanding about what this code was supposed to
do?  There couldn't possibly have been confusion about how to fix it as that
was spelled out in no uncertain terms (see the aforementioned branch). 
IIRC, there was a lot of talk about filing tickets, but very little about
what the code was supposed (and expected) to do.  As I said then, the
tickets can wait until everyone is on the same page concerning this very
critical low-level function (and clearly we are not at this point, else this
code would not persist).%-|

Next up, attr/removeAttr.  But not until this one (critical) line is finally
put to bed.  At this rate, it will take forever to get through the core, let
alone Dijit and DojoX.  I know as I've already been through them all.  ;-)
View this message in context: http://n3.nabble.com/Can-anyone-explain-what-dojo-s-eval-method-is-supposed-to-do-tp416373p416373.html
Sent from the Dojo Toolkit mailing list archive at Nabble.com.

More information about the Dojo-interest mailing list