Fix typeof evaluation for regex and function #181

Merged
merged 2 commits into from Apr 4, 2013

Conversation

Projects
None yet
3 participants
Contributor

candid82 commented Apr 4, 2013

Currently typeof /./ evaluates to specific string (on node it evaluates to "object"). This is incorrect since RegExp can be either "object" or "function" depending on whether or not it implements [[Call]]. This in particular breaks underscore's _.isFunction() on the platforms where RegExp is a function: https://github.com/documentcloud/underscore/blob/master/underscore.js#L992

Also, typeof function(){} currently evaluate to "object", which is obviously incorrect.

This PR fixes both issues.

+ e = typeof /./;
+ f = typeof false;
+ g = typeof function(){};
+ h = typeof undefined;
@michaelficarra

michaelficarra Apr 4, 2013

Contributor

This one's not appropriate. Should be typeof void 0. We can't statically determine the value of typeof undefined outside of strict mode in ES3. It's worrying if this test case is already passing.

@mishoo

mishoo Apr 4, 2013

Owner

UglifyJS assumes that the global undefined cannot be defined in any way, therefore I think it's safe to say that typeof undefined == "undefined". Is there a situation where this doesn't stand?

@michaelficarra

michaelficarra Apr 4, 2013

Contributor
undefined = true; // true
typeof undefined; // "boolean"

edit: Whoops, appears to be ES3 only.

@mishoo

mishoo Apr 4, 2013

Owner
$ node
> undefined = true
true
> undefined
undefined
> typeof undefined
'undefined'

Hmm, where do I find an ES3 engine? :-)

@mishoo mishoo merged commit 4b81805 into mishoo:master Apr 4, 2013

candid82 referenced this pull request in walmartlabs/lumbar Jul 18, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment