DEV Community

Cover image for The experience of creating my own JavaScript runtime - #1

Posted on

The experience of creating my own JavaScript runtime - #1

Hello everyone, for a while (a few months) I came up with the idea of developing my own JavaScript runtime, written in C# using Jint. At first it seemed unfeasible, or at least difficult enough, but I had already tried to assemble a project (for a hobby, like this one) using its own JavaScript engine, HTAPPS, whose focus was to do the same thing as Electron, however, in a lightweight way (and only for windows, as it used the standard IE frame to execute).

The current development repository can be accessed here.

But why? I decided to start this project as a pure hobby, and among the main reasons why I started the project are:

  • I thought about how Node.js was created in its beginnings, it was the opportunity to build something similar in CSharp, my specialty
  • I needed my own automation tool, I'm not very into Python but TypeScript/JavaScript would be a perfect thing to do it. Node can handle it, but there are some "raw" things that make me very uncomfortable
  • I need something to present as my college graduation project (yes)

First steps

MelonRuntime is based on Jint, a JavaScript interpreter created by SΓ©bastien Ros, software developer in the ASP.NET team at Microsoft. Melon works from bindings and functionalities injected into this interpreter together with external typing (provided by another package that comes with the default project generated by the command npx melon new) and built-in tools.

The first days were very fun, I had to learn how to inject functionality into the engine (binding what is C# and what is JavaScript) and being able to call them from within the code. For that I had (and still have the help) of a library that I created myself, called CLI.NET, to create command interfaces quickly.

  • Tip: I do not recommend using CLI.NET for production, there is no Nuget package and I had to perform some optimizations, the original repository needs attention

The first binding was to implement a basic console tool, and the first function was obviously log:

First console.log example

The implementation was a mess (I think it still), but I like it, and I have to improve it more and more. Currently, there is the implementation:

public static void Write(object obj, int color)
    CLNConsole.Write("< ", ConsoleColor.Red);

        JsonSerializerOptions options = new()
            ReferenceHandler = ReferenceHandler.IgnoreCycles,
            WriteIndented = true

        var serialized = JsonSerializer.Serialize(obj, options);
        CLNConsole.Write(serialized, color);
        CLNConsole.Write(obj.ToString() ?? "null", color);

Enter fullscreen mode Exit fullscreen mode

After that, I was focused in turning the errors more readable, like that:

First exception message implementation

There is still a long way to go.

Discussion (7)

andrewbaisden profile image
Andrew Baisden

So interesting seems like a lot of work keep it up!

fabiocosta89 profile image
Fabio Costa

Great job, for a powerful language

otaviosc profile image

Great job

vit0rr profile image

Congrats Vic

sofavalentinus profile image
Valentinus Sofa

Amazing job.

aviavinav profile image
Avi Avinav


drazisil profile image
Drazi Crendraven

Love it! Please keep us updated. πŸ’œ