use of undefined naked identifier should throw exception

Oct 16, 2013 at 10:17 PM
Edited Oct 16, 2013 at 10:39 PM
The following code
var result = null; try { foo } catch (e) { result = e; } 
should leave result set to an exception object, however Jint does't currently throw an exception when it encounters 'foo'.
I'd think ExecutionVisitor.Visit(Identifier expression) should always throw an exception if the identifier isn't defined. If this is true, most (if not all) of the current calls to EnsureIdentifierIsDefined could be discarded. However there is ONE exception - which is when it's used immediately following typeof - i.e.

typeof foo

(assuming foo isn't defined) shouldn't throw an exception.
The only way I can see of achieving this is to have the code for typeof check if the following expression is an Identifier, and if so attempt to resolve it without checking if it's defined (i.e. don't use the Visit() function). Visit(Identifier) should be refactored to use the same function to resolve, but then check that the result is indeed defined.

Note that any expression following typeof other than a Identifier should still cause an exception to be thrown if it refers to an undefined identifier.