janet-lang / janet

A dynamic language and bytecode vm
https://janet-lang.org
MIT License
3.45k stars 223 forks source link

Embedding and AOT #1119

Open glyh opened 1 year ago

glyh commented 1 year ago

I am trying to embedding janet into Zig, and I succeeded with embedding tutorial.

However, there's an overhead using the dostring/dobytes that I want to avoid. Is it possible to call compile functions in zig's compile time, so I can have bytes code embedded in my executable instead of having source code being compiled multiple times whenever I try to run the executable?

Thank you!

CosmicToast commented 1 year ago

Disclaimer: I'm still relatively new to Janet specifics, so what I say below might not be perfectly accurate.

You can do whatever dobytes does: https://github.com/janet-lang/janet/blob/master/src/core/run.c#L30 The short version is to parse using janet_parser_*, then compile individual forms using janet_compile.

Those forms can then be used at runtime by putting them into a fiber and janet_continueing it.

This all, of course, depends on you knowing exactly what you want to run ahead of time. It may also interact poorly with imports and environments (you'll need to handle those either at runtime or build time somehow), though the approach of creating the fiber at runtime means that you can set the environment to whatever you want.

sogaiu commented 1 year ago

On a side note, if you haven't checked out @greenfork's jzignet yet, may be it's worth a look. There is specific mention of embedding in the README:

You can:

Embed Janet programs into Zig

There is also: https://hg.sr.ht/~paulsnar/zig-janet which is apparently based on the above (at least according to this).

...and there is this project which seems to be written in Zig with some Janet bits inside. No clue about specifics though (^^;

glyh commented 1 year ago

@sogaiu I did check jzignet, it's not compiling on my version of zig so I bascially rewrite my own. I don't think they have a solution for my problem, tho.

omasanori commented 1 year ago

I think https://github.com/janet-lang/janet/discussions/999 would answer some of your questions. Also, here are some code snippets.

https://github.com/janet-lang/janet/blob/4ff81a5a25cb3e398bd7cd1d76283e1e2b503820/src/boot/boot.janet#L4126-L4136 https://github.com/janet-lang/janet/blob/4ff81a5a25cb3e398bd7cd1d76283e1e2b503820/src/boot/boot.janet#L4229-L4237 https://github.com/janet-lang/janet/blob/4ff81a5a25cb3e398bd7cd1d76283e1e2b503820/src/core/corelib.c#L1254-L1264

omasanori commented 1 year ago

Searching janet_core_image in the amalgamated janet.c may also be helpful.

iacore commented 11 months ago

One way is to run (make-image (curenv)) and load the image at runtime. Modules imported will be cached by module/cache, and will be in the image.

cfunction can't be included this way by default. You can modify janet_core_env() (add cfunction to it) before make-image.