Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding and AOT #1119

Open
glyh opened this issue May 4, 2023 · 6 comments
Open

Embedding and AOT #1119

glyh opened this issue May 4, 2023 · 6 comments

Comments

@glyh
Copy link

glyh commented May 4, 2023

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
Copy link
Contributor

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
Copy link
Contributor

sogaiu commented May 4, 2023

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
Copy link
Author

glyh commented May 4, 2023

@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
Copy link

I think #999 would answer some of your questions. Also, here are some code snippets.

janet/src/boot/boot.janet

Lines 4126 to 4136 in 4ff81a5

(def image
(let [env-pairs (pairs (env-lookup root-env))
essential-pairs (filter (fn [[k v]] (or (cfunction? v) (abstract? v))) env-pairs)
lookup (table ;(mapcat identity essential-pairs))
reverse-lookup (invert lookup)]
# Check no duplicate values
(def temp @{})
(eachp [k v] lookup
(if (in temp v) (errorf "duplicate value: %v" v))
(put temp v k))
(marshal root-env reverse-lookup)))

janet/src/boot/boot.janet

Lines 4229 to 4237 in 4ff81a5

(print "static const unsigned char janet_core_image_bytes[] = {")
(loop [line :in (partition 16 image)]
(prin " ")
(each b line
(prinf "0x%.2X, " b))
(print))
(print " 0\n};\n")
(print "const unsigned char *janet_core_image = janet_core_image_bytes;")
(print "size_t janet_core_image_size = sizeof(janet_core_image_bytes);"))

janet/src/core/corelib.c

Lines 1254 to 1264 in 4ff81a5

Janet marsh_out = janet_unmarshal(
janet_core_image,
janet_core_image_size,
0,
dict,
NULL);
/* Memoize */
janet_gcroot(marsh_out);
JanetTable *env = janet_unwrap_table(marsh_out);
janet_vm.core_env = env;

@omasanori
Copy link

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

@iacore
Copy link
Contributor

iacore commented Sep 20, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants