You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
functiongetStringValue(name,fullExpression){// From the JavaScript docs:// Property names must be strings. This means that non-string objects cannot be used// as keys in an object. Any non-string object, including a number, is typecasted// into a string via the toString method.//// So, to ensure that we are checking the same `name` that JavaScript would use,// we cast it to a string, if possible.// Doing `name + ''` can cause a repl error if the result to `toString` is not a string,// this is, this will handle objects that misbehave.name=name+'';if(!isString(name)){throw$parseMinErr('iseccst','Cannot convert object to primitive value! '+'Expression: {0}',fullExpression);}returnname;}
What caught my attention is that executing this line name = name + ''; can have only one of these two effects:
either a string is assigned to the name variable, or
an exception (TypeError: Cannot convert object to primitive value) is thrown by the JS engine if name.toString is 'broken' (doesn't return a string, isn't a function, etc.).
It's even mentioned in the comment in this code. This means this check if (!isString(name)) never passes, so $parseMinErr('iseccst', ...) is never thrown.
This is some leftover from the time that we used to do name = toString(name) (that had other issues and was changed to the current implementation). In this case, even the comment does not reflect how the method actually works.
The if block should go away and the comment reflect what is going on and when it is expected this to throw. No try...catch as is check is in the critical path of $parse
I accidentally stumbled upon this code in
$parse
:What caught my attention is that executing this line
name = name + '';
can have only one of these two effects:name
variable, orTypeError: Cannot convert object to primitive value
) is thrown by the JS engine ifname.toString
is 'broken' (doesn't return a string, isn't a function, etc.).It's even mentioned in the comment in this code. This means this check
if (!isString(name))
never passes, so$parseMinErr('iseccst', ...)
is never thrown.Plunker: http://plnkr.co/edit/xWFZbUxTeKszPd28apSf?p=preview
The text was updated successfully, but these errors were encountered: