Thread: What does it mean when people say that Python and JS Libraries are "written in C"?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    45

    What does it mean when people say that Python and JS Libraries are "written in C"?

    I've heard this said before and it half makes intuitive sense. One example is this video at the 1 minute mark: Why C Programming Is Awesome - YouTube

    I think it might have something to do with encapsulation (information hiding) and/or compiling to byte code, which is what all languages are at the end of the day.

    Is it that when you execute a JavaScript program, it actually calls down to C function's to accomplish some of the tasks?

    I was hoping someone could explain this.

    Also of tangential note: is there any way to look "under the hood" of JavaScript or Python programs to see the C code that is involved? All the C will probably have been compiled, but are there any resources that show the source code?
    Last edited by potomac; 12-28-2016 at 11:23 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Not C necessarily, but yeah, interpreted languages such as JS and Py use... well... an interpreter. That interpreter needs to be as efficient as possible, so it's only logical that efficient, close-to-the-metal languages such as C or C++ would be used for their implementation.

    For example, here's Spider Monkey, a JS engine for firefox.
    SpiderMonkey - Mozilla | MDN
    Last edited by GReaper; 12-29-2016 at 02:32 AM.
    Devoted my life to programming...

  3. #3
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Encapsulation is one thing in C++ that C just can't do.

    Programs compile to machine code, byte code is often used as an intermediate step for interpreted languages which don't run in machine code but in an interpreter such as a Java virtual machine.

    Python was written in C, it's also open-source so you can download the code if you wish. I think most javascript under the hood is C++ but I am less sure about that.

  4. #4
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    Could you say that the Python code "received" by interpreter will always match up with a line/several lines of C or C++ code?

    What would be the best way to look at that open source Python code and see how it matches up with the C/C++? (Or Javascript) Is there a way to easily view a side-by-side comparison?

  5. #5
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    At your coding level I doubt that you would understand it to be honest. No there's not really any easy side by side comparison, and besides you would need to learn the basics of Python and you are doing well with C at the moment so I would just stick with that and become more comfortable with it before moving in a different direction. Python is an extensible language, so libraries can be provided for python in a few different languages.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    45
    I don't intend to do anything with that knowledge. Just a curiosity.

    I was particularly interested in just seeing if a single line of JavaScript can sometimes be equivalent to a ton of lines of C. I figured this would have to be the case since it's a higher level language.

  7. #7
    Old Took
    Join Date
    Nov 2016
    Location
    Londonistan
    Posts
    121
    Most JS runs in a browser so you will need to dig through the source code of browsers to find how the developers have implemented JS. Most browsers are written in C++.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    To answer your main question:
    "Calling down to C to accomplish some tasks" refers to when interpreted languages call functions which are implemented in C instead of that language. So in Python, the interpreter is made to know that when it sees those particular functions, to look at the attached compiled binaries instead of the python source. In javascript it's similar except the C functions must be part of the browser, so you can't just attach any c code. Technically the function aren't necessarily written in C, but they must be made to look like they were by having "C linkage".

    It's also true that interpreters are themselves written in native languages like C. This is technically separate from "calling down to c", except that standard library functions may call down to C, and are effectively part of the interpreter. Unlike calling down to c, interpreters themselves do not need C linkage, and so are more easily written in other languages.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-19-2016, 12:07 PM
  2. Replies: 3
    Last Post: 07-07-2013, 01:41 AM
  3. "The memory could not be written" error
    By elmvin_s in forum C++ Programming
    Replies: 9
    Last Post: 12-13-2009, 04:56 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread