Algorithm for javascript pre-defined functions (parseInt, parseFloat, isNaN, etc.)
Asked Answered
C

3

9

What is the best way, if even possible, to see the underlying code for the predefined functions in Javascript. Is there documentation that shows how these were coded, or an easy way to actually view the underlying code?

  • parseInt
  • parseFloat
  • isNaN
Calyx answered 20/3, 2013 at 20:37 Comment(0)
C
3

After looking further I found this in the ECMAScript specification. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

When the parseInt function is called, the following steps are taken:

  1. Let inputString be ToString(string).
  2. Let S be a newly created substring of inputString consisting of the first character that is not a StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white space.) If inputString does not contain any such characters, let S be the empty string.
  3. Let sign be 1.
  4. If S is not empty and the first character of S is a minus sign -, let sign be 1.
  5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
  6. Let R = ToInt32(radix).
  7. Let stripPrefix be true.
  8. If R  0, then© Ecma International 2011 105 a. If R < 2 or R > 36, then return NaN. b. If R  16, let stripPrefix be false.
  9. Else, R = 0 a. Let R = 10.
  10. If stripPrefix is true, then a. If the length of S is at least 2 and the first two characters of S are either ―0x‖ or ―0X‖, then remove the first two characters from S and let R = 16.
  11. If S contains any character that is not a radix-R digit, then let Z be the substring of S consisting of all characters before the first such character; otherwise, let Z be S.
  12. If Z is empty, return NaN.
  13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix-R notation.)
    1. Let number be the Number value for mathInt.
    2. Return sign  number. NOTE parseInt may interpret only a leading portion of string as an integer value; it ignores any characters that cannot be interpreted as part of the notation of an integer, and no indication is given that any such characters were ignored.

When the parseFloat function is called, the following steps are taken:

  1. Let inputString be ToString(string).
  2. Let trimmedString be a substring of inputString consisting of the leftmost character that is not a StrWhiteSpaceChar and all characters to the right of that character. (In other words, remove leading white space.) If inputString does not contain any such characters, let trimmedString be the empty string.
  3. If neither trimmedString nor any prefix of trimmedString satisfies the syntax of a StrDecimalLiteral (see 9.3.1), return NaN.
  4. Let numberString be the longest prefix of trimmedString, which might be trimmedString itself, that satisfies the syntax of a StrDecimalLiteral.
  5. Return the Number value for the MV of numberString.

NOTE parseFloat may interpret only a leading portion of string as a Number value; it ignores any characters that cannot be interpreted as part of the notation of an decimal literal, and no indication is given that any such characters were ignored.

Returns true if the argument coerces to NaN, and otherwise returns false.

  1. If ToNumber(number) is NaN, return true.
  2. Otherwise, return false. NOTE A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.
Calyx answered 20/3, 2013 at 20:43 Comment(1)
Note that this is the algorithm to follow, and each browser/JS engine separately natively implements it as the browser/engine creators saw fit in a chosen language.Margo
P
7

They are native functions, and maybe coded in the language your JS engine was written in - you'd need to contact it's source.

However, you probably are more interested in the EcmaScript specification that describes how the algorithms work.

And if you're lucky, for some of the functions you even might find an JS equivalent. You'll find them mostly on pages that test ES implementations against the standard.

Proser answered 20/3, 2013 at 20:40 Comment(3)
Yep this. It's important to note that those functions are not implemented in JavaScript. console.log(parseInt) //=> function parseInt() { [native code] }Laurenlaurena
@AlexWayne: Actually, some of them are implemented with JS: code.google.com/p/v8/source/browse/trunk/src/v8natives.jsProser
I suppose "some" or "most" would have been more accurate :)Laurenlaurena
C
3

After looking further I found this in the ECMAScript specification. http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

When the parseInt function is called, the following steps are taken:

  1. Let inputString be ToString(string).
  2. Let S be a newly created substring of inputString consisting of the first character that is not a StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white space.) If inputString does not contain any such characters, let S be the empty string.
  3. Let sign be 1.
  4. If S is not empty and the first character of S is a minus sign -, let sign be 1.
  5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
  6. Let R = ToInt32(radix).
  7. Let stripPrefix be true.
  8. If R  0, then© Ecma International 2011 105 a. If R < 2 or R > 36, then return NaN. b. If R  16, let stripPrefix be false.
  9. Else, R = 0 a. Let R = 10.
  10. If stripPrefix is true, then a. If the length of S is at least 2 and the first two characters of S are either ―0x‖ or ―0X‖, then remove the first two characters from S and let R = 16.
  11. If S contains any character that is not a radix-R digit, then let Z be the substring of S consisting of all characters before the first such character; otherwise, let Z be S.
  12. If Z is empty, return NaN.
  13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the mathematical integer value that is represented by Z in radix-R notation.)
    1. Let number be the Number value for mathInt.
    2. Return sign  number. NOTE parseInt may interpret only a leading portion of string as an integer value; it ignores any characters that cannot be interpreted as part of the notation of an integer, and no indication is given that any such characters were ignored.

When the parseFloat function is called, the following steps are taken:

  1. Let inputString be ToString(string).
  2. Let trimmedString be a substring of inputString consisting of the leftmost character that is not a StrWhiteSpaceChar and all characters to the right of that character. (In other words, remove leading white space.) If inputString does not contain any such characters, let trimmedString be the empty string.
  3. If neither trimmedString nor any prefix of trimmedString satisfies the syntax of a StrDecimalLiteral (see 9.3.1), return NaN.
  4. Let numberString be the longest prefix of trimmedString, which might be trimmedString itself, that satisfies the syntax of a StrDecimalLiteral.
  5. Return the Number value for the MV of numberString.

NOTE parseFloat may interpret only a leading portion of string as a Number value; it ignores any characters that cannot be interpreted as part of the notation of an decimal literal, and no indication is given that any such characters were ignored.

Returns true if the argument coerces to NaN, and otherwise returns false.

  1. If ToNumber(number) is NaN, return true.
  2. Otherwise, return false. NOTE A reliable way for ECMAScript code to test if a value X is a NaN is an expression of the form X !== X. The result will be true if and only if X is a NaN.
Calyx answered 20/3, 2013 at 20:43 Comment(1)
Note that this is the algorithm to follow, and each browser/JS engine separately natively implements it as the browser/engine creators saw fit in a chosen language.Margo
N
0

Those functions are implementation specific depending on browser, and are not written in JS (unless somebody's decided to write a browser engine in JS). The code is not guaranteed to be the same across environments, though they do have to (in theory) adhere to the ECMAScript specification for their behavior.

Navigation answered 20/3, 2013 at 20:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.