
Mjs – An interpreter for ECMAScript 1st edition written in C++17 - eatonphil
https://github.com/mras0/mjs
======
austincheney
Pretty ambitious for a simple educational project.

I suspect the hardest part is getting garbage collection correct around
lexical scope. The browsers struggled with this for years.

~~~
chrisseaton
> The browsers struggled with this for years.

Multiple browsers had bugs with garbage collection of a basic part of
JavaScript for years? What was the problem? I wonder what made them specific
to lexical scopes, because a lexical scope is either just more entries on the
stack, which is scanned for local variables in the same way, or objects on the
heap, which is scanned for fields in the same way.

~~~
gsnedders
My understanding of the things that catch people out are things like:

    
    
        function f(){
            let a = {};
            return function(){};
        }
    

In all (IIRC) major implementations, the object allocated for `a` will be held
on to after the function returns because it exists in the lexical scope of the
function returned (even though it isn't—and never can be—reached, as static
analysis can prove).

Basically all the other bugs I'm aware of are cases of C++ code not treating
things as roots when necessary, mostly in DOM code.

~~~
ZenPsycho
V8 does actually check if the outer variables are referenced by the inner
function. There's a misconception amongst a disturbing number of programmers
that it's impossible to memory leak in a garbage collected language. For some
reason I've found this particularly prevalent amongst C# developers, but no
language community is immune from it.

In truth there are myriad ways to very casually and accidentally create pools
of references that can't be garbage collected. I'll leave finding these as an
exercise for the reader to google.

here's an old article by Douglas Crockford about a particular memory leak in
IE's garbage collector that persisted for a very long time:

[https://crockford.com/javascript/memory/leak.html](https://crockford.com/javascript/memory/leak.html)

~~~
earenndil
What if the internal function calls eval?

~~~
gsnedders
In the simple case in my parent comment? If it's a direct eval call, then the
VM has to assume it'll reference every possible variable, and has to keep them
around as a result.

------
snek
It's pretty cool to see all the different ways ECMA-262 has been implemented.
I've been working on my own implementation[1] and seeing how other
implementations like v8, prepack, mongoosejs, and now this one solve things
has been super helpful.

1:
[https://github.com/devsnek/engine262](https://github.com/devsnek/engine262)

~~~
mncharity
> pretty cool to see all the different ways ECMA-262 has been implemented

My favorite is that the language spec was written as quite regular pseudo-
code... so at least as of some years ago, and after some manual massage to
clean up irregularities, one could automagically transliterate the text of the
spec into code. And if the target language had label gotos (because the spec
was full of 'then go to step N'), and permited arbitrary identifiers, then the
code could read just like the spec. Making code-review proof reading much
easier.

That's one of my canaries for detecting when a non-crippling programming
language finally exists - someone generates a not-necessarily performant but
fully spec-compliant js implementation with some strikingly tiny effort. A few
days.

------
SlowRobotAhead
Edit: Apparently this project is a JS interpreter for C++ ... while mJS that I
wrote about is a JS interpreter for... C and C++

The one I was talking about is
[https://github.com/cesanta/mjs](https://github.com/cesanta/mjs)

... yea, two JS interpreters for C++ with the same name. How could that be
confusing?

~~~
eatonphil
Not sure this is the same project? This is MIT licensed and says it's a hobby
implementation. I don't see anything about a company or about ARM support.

~~~
tiles
Looks like it's similarly named:
[https://github.com/cesanta/mjs](https://github.com/cesanta/mjs)

~~~
SlowRobotAhead
Exact same name, nearly the same purpose except that Cesanta’s is not limited
to C++ only. My mistake.

