Building a JavaScript Runtime using C

7 days ago (devlogs.xyz)

For those who would like a true "from scratch" implementation of JavaScript, Fabrice Bellard's QuickJS [1] is clean, readable and approachable. It's a full implementation of modern JavaScript in a straightforward project, not nearly as complex or difficult as V8.

[1] https://bellard.org/quickjs/

This is not "Building from scratch" This is just using.

  • Runtime is the glue between JS Engine and OS which is from scratch. Runtime embeds engine and lets engine talk to the outside world.

I guess I would like to see defining your global object in a real use case and adding some functions to your global object that make sense, which admittedly once you ask someone to do the creative work of making a use case that is sensible as they start implementing it they might find it is more useful to complete the implementation of that use case rather than releasing a starter tutorial.

I was a little disappointed that this was “just” a wrapper for JavaScriptCore.

  • Agreed. It contradicts the whole "from scratch" idea. The article even has an engine implementation section where it just calls JavaScriptCore as you mentioned. It's a cool wrapper, but a misleading and disappointing article

    • Yup. I clicked on it, based on the title, and expected a long-form article. Not a simple library utilisation post.

  • To be fair, all commercial non-browser runtimes (node, bun, deno) are "just" wrappers of V8 or JSC. Some more experimental ones are "just" wrappers of QuickJS and other less known engines.

    • iiuc its a runtime because the engine just dispatches one javascript microtask and returns to the runtime with a stack of remaining microtasks

  • Yeah, I was expecting a lot more than "I glued some libraries together in C!", especially when the author is claiming 'from scratch'. Seems like a somewhat disingenuous title if you ask me..

    • I suppose when your accustomed level of abstraction is interpreted languages like JavaScript, and "the web", "gluing some libraries together in C" is a somewhat novel and interesting endeavor.

      2 replies →

How about a formally verified runtime that takes the JS spec & constructs a runtime by converting the spec w/ incremental & verifiable transformations into an executable runtime?

unpopular comment : v8 > JavaScriptCore.

  • In general, yes, although it's nice to have more than one javascript implementation. And one advantage of JSC is that it implements tail call optimization (per the ES6 spec).

    I wrote my own language that targeted javascript. When I made my language self-hosting, I initially used `bun` (based on JSC), so I wouldn't have to implement a tail call transformation myself. It was expedient.

    My goal was to run in a browser, so I did eventually add that transformation and found that my compiler was 40% faster running in node (based on v8).