Roll your own JavaScript runtime, pt. 3
deno.com
deno.com
v8 is a JS runtime. JSC is a JS runtime. SpiderMonkey is a JS runtime.
All of these are embeddable and have usable APIs. If all you are doing, is linking to a JS runtime, and then using it, you aren't "rolling your own runtime".
If you want to roll your own JS runtime go look at how the LibJS folk in serenity did it - they did it without corporate backing and despite that I believe LibJS is fairly complete even with the new draft language features, albeit lacking a decade or so of performance optimizations.
I guess the difference is runtime vs perhaps interpreter, but it's definitely ambiguous.
Adding a couple of functions to an existing runtime is not rolling your own runtime.
Runtime: - Chrome - Deno - Node - Bun
Engine: - V8 - JSC - SpiderMonkey - LibJS
Part 2 of this series, for instance, shows the instantiator of the V8 engine (via deno_core, a runtime-less wrapper as explained in Part 1) implementing a simplified fetch API with some trivial JS and the bulk of the logic in Rust: https://deno.com/blog/roll-your-own-javascript-runtime-pt2#i... - this would then be available to any code executing in their custom JS environment.
This is useful even outside of creating a full Node-style implementation; for instance, Cloudflare created a locked-down runtime (a reasonable subset of the browser runtime) for v8 isolates for their workers: https://developers.cloudflare.com/workers/runtime-apis/
It's super cool to know this stuff as it lets you consider (when appropriate!) using JS as a way to accept Turing-complete user-submitted logic rather than just accepting, say, JSON configs, while limiting the surface with which it can interact with your system.
All of which is provided by V8 in this example. The global object, math object, "Object" in general, Arrays, etc are all the runtime. All this tutorial series on embedding V8 is doing is instantiating an existing runtime environment and adding using the APIs to insert a few new APIs.
All together you might say you've got a custom runtime as you have the baseline "JS runtime" + some new APIs and that's clearly a new runtime environment that is distinct from the JS environment on a web page, vs. the one in a worker, vs. in node, etc. But that is at best "extending a runtime", not "rolling your own".
Using the embedding APIs provided by a JS runtime as documented and intended, is not "rolling your own". By that definition I could make an app, embed a WebView, and claim I rolled my own browser runtime, which I would hope is more clearly absurd. Or I could "roll my own Command-line" by reading a string from a user, prepending some commands, and then passing it to system().
The only bit of the old Edge that had its source emancipated. It was/is? quite performant.
[0]: https://en.wikipedia.org/wiki/Runtime_systemhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
For example, in the case of node, it's a runtime built around the v8 engine, using libuv for bindings.
The runtime is the combination of the execution engine, the garbage collector, and the environment.
V8, JSC, SpiderMonkey, LibJS, etc all provide all of those.
They all have APIs that let you (the embedder) a few new objects and functions, as it would be a useless embedding API otherwise. But that's all you're doing: adding some glorified callbacks to an existing runtime. You are in no way "rolling your own JS runtime".
To break it down:
Execution Engine: the part of the runtime that evaluates JS, an interpreter, jit, or some combination. That would be "Ignition" and "TurboFan" in V8, "LLInt" and "FTL" in JSC, "WarpMonkey" in SpiderMonkey.
Garbage Collector: the part of the runtime that supports object allocation and reclamation. "Orinoco" in V8, but not sure if given a separate marketing name in other runtimes.
The environment: this is all the builtin objects and functionality, things like the global object, the regex engine (note it's not a regex runtime because all it does find the start and stop sections of matches, the embedding environment is responsible for everything else), all those core things like the Object, Array, Math, Number, etc types, and all of their implementations and runtime functions. The Execution engine does not need any of those to be implemented, as to the engine there is essentially no distinction between those builtin things and anything else written in JS.
When you embed V8, JSC, LibJS, etc you are getting a full runtime, that can do a huge amount. You _might_ choose to use there APIs to add some new objects or or functions, but what you are doing is negligible, and certainly not "a runtime".
Node uses the V8 C++ APIs to add additional functions and objects to that runtime.
So Node has a specific JS runtime environment, just as browsers have a specific JS runtime environment (and Workers have another), etc.
All of these environments are extending the runtime environment provided by their underlying JS runtime, they're not all providing their own stdlib implementations, they're not providing their own implementations of arrays, objects, global object, etc.
V8, JSC and SpiderMonkey have no way to interact with the outside world. Without some supporting structure, running JavaScript does anything other than being an over-engineered space heater. It's that supporting structure — the runtime — that allows you to do anything useful.
Visual Studio Redistributables for C++ is a famous case of a runtime that doesn't involve an interpreter.
The lack of direct IO is irrelevant. You can take any of these libraries, and execute arbitrary JS, and then display the output (Serenity's spreadsheet uses LibJS for equation cells IIRC). E.g. JS that runs and uses the "runtime environment" to do things.
Your particular use case may benefit from exposing some additional APIs to JS, and all these libraries allow you to do that. But exposing, for example, printf to a full JS runtime environment does not mean you've made a runtime. The belief that for something to be a "runtime" it must have built in IO routines me that no generally usable scripting environment could be a runtime.
You can argue you don't need those things, but their lack isn't a feature either. If you're not coupling to C's memory model or if for some reason someone has already done the work of building and debugging the integration... you gain nothing by using lua over js.
I've done professional work in lua and it's highly overrated imo. For integrated languages Tcl or janet is a better choice, unless your use case is extremely extremely simple and then forth is a better choice. The only time I would choose lua is if I'm integrating with C, the integration language tasks are very simple but also requires coroutines. Lua's coroutines are genuinely good.
Lua is technically superior, but JavaScript is practically superior due to its ubiquitous platform the web browser.
Similar but different to C "beating" Lisp.
JavaScript as it is today is a vastly more ergonomic and fluid experience, and I am saying this as someone that has written tons of both.
The embedding experience with Lua is likely much more form fit than JavaScript (sandboxing, etc). I’m strictly speaking about syntax and language features.
0-index arrays express offsets, 1-index arrays express index selection. In C (and family) arrays start at zero because they offset, `a[i]` literally means `* (a+i)` (you actually can write `i[a]`, it will work and be valid). For other language selecting 1 base indexing is not a weird option, I'd argue.
With Deno I know I can quickly spawn lots of isolated instances at very low overhead.
If I really want to push it far, there's tools for loading frozen snapshots that have lots of code loaded.
I've never seen similar from Lua & I'm not sure it exists. Seems like a huge advantage.
Err..new to Deno - how does one do this ?
There are a lot of browser API's. They're portable, designed with security in mind (at least somewhat), usually well documented, reasonably easy to understand, and people already know them. So, even if it's not a browser, you may prefer JavaScript so you don't reinvent the relevant API's and there's less to learn.
Deno itself is a good example of this.
Because I like TypeScript and don't like Lua.
JavaScript, I don't like. It's in the same bucket to me as Python, Ruby, and Lua - things I might have to use sometimes but I'm holding my nose. But TypeScript? I like TypeScript and want to use it more.
This series is effectively about doing the same, but embedded Deno instead of Lua. It's showing how to launch Deno runtime, and how to seed those runtime with various hooks to access & manipulate the program it's embedded in.