From CommonJS Spec Wiki
Jump to: navigation, search

For C/C++ based JavaScript interpreters, being able to interface easily with C libraries is a big win because of all of the available functionality. Each JavaScript interpreter has its own bridge to C, but if there is some common API (possibly exposed at the JavaScript level as in ctypes), then this will make it much easier to share work between interpreters.

Prior Art


The authors of Lua make a convincing argument for the advantages of the Lua 5 C API, which evolved considerably and incompatibly from Lua 1. They compare it with Python's API, Java's JNI API, and others. The main idea is that using a stack to communicate between Lua and C solves 2 "impedance mismatches" between Lua and C:

  • Converting between the C type system and the Lua type system
  • Manual memory management in C and automatic memory management in Lua (garbage collection). For example, the Python API is very concerned about the details of garbage collection, which Lua avoids. v8's API is also influenced strongly by its particular implementation of garbage collection.

Using the stack for communication also allows for a much smaller API, and they compare the sizes of the various APIs.