Starlight-JS / starlight

JS engine in Rust
https://teletype.in/@starlight-js
Mozilla Public License 2.0
511 stars 9 forks source link

Codegen plugins #51

Open playXE opened 3 years ago

playXE commented 3 years ago

When I was implementing self-hosted builtins I added special functions prefixed with ___ that did emit raw bytecode instead of real calls to JS functions. This functionality is conditionally enabled when evaluating self-hosted builtins right now. And now I have great idea that will work like Rust's procedural macros but instead it will generate raw bytecode rather than new SWC AST. For example we could provide API like this:

runtime.register_codegen_plugin("MyOwnAddFn",  |compiler,this,call_args| 
{
   compiler.emit(call_args[0]);
   compiler.emit(call_args[1]);
   compiler.emit(OP_ADD);
});

runtime.eval("MyOwnAddFn(2,3)"); // => 5