Skip to content

Name mangling: demangler and/or annotations #69

Open

Description

@LDemetrios

Hi! So I started trying the compiled jars, and came up with a few proposals to improve your beautiful compiler.

First of them would be the name mangling rules. Rust famously produces turbofish names for its functions and they must be mangled into valid jvm names, that's understandable. However, I see that algorithm you use for that is just replacing all weird symbols with underscores, so it becomes quite hard to figure out what happens in the types. Here's one from the project you already know: Result_alloc_vec_Vec_u8_alloc_alloc_Global_typst_library_diag_FileError. It arguably is still readable if you are familiar with the source, after some time I was able to deduce Result<alloc::vec::Vec<u8, alloc::alloc::Global>, typst_library::diag::FileError> from that. However, there is no automatic way to do that.

So I would propose

  1. Incorporating different mangling algorithms.
    1a) Yours does have an upside: it is a simple identifier, which probably is valid in all JVM languages. Plus, some projects might already rely on it, so it is reasonable to leave it as an option, maybe even default one.
    1b) Use standard mangler:
    1b.i) There is a Rust one, which is not exactly readable but is common and has and a reverse.
    1b.ii) Clojure one -- clojure is a jvm language which allows any kind of crazy names, it just replaces any weird symbols with _SYMBOLNAME_:
> (println +)
#object[clojure.core$_PLUS_ 0x791d1f8b clojure.core$_PLUS_@791d1f8b]
> (println any?)
#object[clojure.core$any_QMARK_ 0x93081b6 clojure.core$any_QMARK_@93081b6]

Can't exactly recommend that but I should've mentioned it
1c) Make your own mangler with minimal disruptions: there are only a 6 of symbols that is forbidden in jvm names

. / < > : ;

and 6 more symbols usage of which is discouraged

? * " | % and a backtick I can't just use in this markdown

That means all this: !@#$^&()№', is a fair game. Dollar is already used in a lot of places by jvm itself, so I wouldn't. But since it's only angle brackets and colons that are issue, I would just use pars and bang instead of them: Result(alloc!!vec!!Vec(u8, alloc!!alloc!!Global), typst_library!!diag!!FileError).

  1. Add annotations with runtime retain that holds the original name of the function or the type. That would be useful for reflective analysis, for example, modifying the stacktrace so it uses correct names.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions