Halo, hello, I’m 132, and I have a big announcement to make today, which is, I finally made a good joke
The day before yesterday I wrote a very straightforward article about how compilation works
Based on my own thinking, I came up with an idea that could implement a language (engine) in less than a thousand lines of code, and since some people were interested in the comments, I also struck while the iron was hot and worked it out immediately
Github is here: github.com/yisar/joke
Then, follow the steps:
Use cases
console.log(123)
Copy the code
Yes, you read that right, that’s the most common line of code, but it’s actually a member of the console method, the log method, and then you call the log method and you pass in a 123 argument, and then you print out 123
lexer
The simplest thing Lexer does is to break down strings and get tokens.
["console"."."."log"."(".123.")"]
Copy the code
parser
And what the Parser does is not that difficult, it’s basically iterate over tokens and get an AST tree that looks like this:
[NodeList([Call(Member(Identifier("console"), "log"),Number(123.0)]])]Copy the code
This is the way rust describes trees, using structures that are not as straightforward as JS objects, but are actually much easier to manipulate
All that is left is to traverse the tree. It is worth noting that rust traversal is very interesting. There are usually two ways to traverse the tree. The other is impl struct, like class, which typically implements iterator methods like next()…
I chose the latter, because the future iteration is going to be a big iteration, and the impL struct approach is easier to expand
codegen
Codegen is used to generate bytecodes. What it does is convert AST to Bytecodes. Why do I need to convert bytecodes
But at least for Joke, what it does for now is it takes apart the AST into bytecodes for the VM to run and states for the execution stack and GC
The bytecode looks like this, in hexadecimal:
[1.0.0.0.0.0.0.0.0.4.0.0.0.0.2.0.0.0.0.4.1.0.0.0.3.5.1.0]
Copy the code
vm
All the VM has to do to run the code, as per the use case, is to maintain a state stack globally with the console.log method hanging from it
There will be scopes, prototype chains, closures, event loops…
thinking
I had been writing JS for a long time, but ACTUALLY I didn’t know much about its internal execution principle, but suddenly I wrote JS engine, I had to think about these things that I didn’t pay attention to before
- gc
To be honest, GC was my original inspiration, why would I write a JS engine in Rust in the first place
We all know rust’s proprietary ability to manage memory securely, so I wondered if it would be possible to implement a completely secure GC with this feature.
If you can do that, it will be different from v8 and other engines that are prone to memory leaks…
The answer is yes. Deno provides an isolate mentality, which is safe, although I don’t know if deno can do that, with V8
- Scope, prototype chain, closure, Event loop
These things are the basic JS, in the past every interview, was asked about these content, very pull points
Now it’s my turn to write it myself, and this is the most important part, and I need to meditate, and try to design the simplest mental model
The important thing is the prototype chain. With this done, basically all userland apis (map, some, bind, etc.) don’t need to be built in, and can be polyfilled in the JS layer based on the prototype
- Deno
Many of you must be wondering, where is Deno’s hidden global variable
In addition to isolate, Deno Core also includes binding and shareQueue, which are very important. Although it binds V8 and Rust, it can still bring new inspiration to Joke
Eventually a similar mechanism could be implemented so that all the built-in apis could be externalized (Array, Ojbect, Date, Math, Josn, etc.)
conclusion
By clever design, Joke Core can be minimized to less than a thousand lines
You can also take advantage of rust’s features to solve V8’s memory problems
Incidentally, you can also externalize all apis
To be honest, every time I write something, I get mixed reviews. Many people say, it can’t be used, no one uses it, it doesn’t work…
But WHEN I write something, I don’t really write it to make it work. I don’t care about performance or standards
I’m more interested in a new way of thinking, clever design…
Finally, put the group no. : 813783512, welcome to play Then, put the github:https://github.com/yisar/joke, welcome star, welcome pr ~Copy the code