Skip to content
This repository was archived by the owner on Jul 12, 2024. It is now read-only.

Implement support for classOf and getClass. #19

Merged
merged 4 commits into from
Mar 18, 2024

Conversation

sjrd
Copy link
Collaborator

@sjrd sjrd commented Mar 15, 2024

Including elementary support of methods of java.lang.Class.

We implement this in a way that is similar to the JavaScript back-end: we have an internal typeData structure, which contains the raw metadata that will be needed by a java.lang.Class instance. It also contains a lazily evaluated pointer to the unique java.lang.Class and the typeData of an array of that type. If we need the typeData of a multi-dimensional array, we follow the chain of arrayOf pointers.

The name is initially stored as an (array u16) so that it can be initialized as a constant expression in globals. The string value is lazily initialized from that raw data the first time it is requested.

This machinery requires quite a bit of run-time helper functions. These are hard-coded in HelperFunctions.scala. Again, this is similar to the JS back-end, which defines a bunch of helper JS functions in its CoreJSLib.scala.

The typeData of classes that have a vtable are inserted into the vtable itself, making the latter a subtype of typeData. This is used for the support of GetClass: we fetch the vtable field of the object as the typeData from which to extract the java.lang.Class instance. The typeData of other classes are stored as independent globals.

@sjrd sjrd requested a review from tanishiking March 15, 2024 10:00
@sjrd sjrd changed the title Implement support for classOf. Implement support for classOf and getClass. Mar 15, 2024
Copy link
Owner

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much @sjrd !
One question is that I'm not sure why do we need element section for getComponentType, but otherwise looks great! (+ I left some small questions)

sjrd added 4 commits March 18, 2024 14:15
* Handling of the function signature and signature type.
* Building of the `WasmFunction` and registration to the context.
* Small DSL for emitting structured control flow instructions.
Mostly for forward declarations of functions to be used in
`ref.func` instructions.
Including elementary support of methods of `java.lang.Class`.

We implement this in a way that is similar to the JavaScript
back-end: we have an internal `typeData` structure, which contains
the raw metadata that will be needed by a `java.lang.Class`
instance. It also contains a lazily evaluated pointer to the
unique `java.lang.Class` and the `typeData` of an array of that
type. If we need the `typeData` of a multi-dimensional array, we
follow the chain of `arrayOf` pointers.

The name is initially stored as an `(array u16)` so that it can be
initialized as a constant expression in globals. The `string`
value is lazily initialized from that raw data the first time it
is requested.

This machinery requires quite a bit of run-time helper functions.
These are hard-coded in `HelperFunctions.scala`. Again, this is
similar to the JS back-end, which defines a bunch of helper JS
functions in its `CoreJSLib.scala`.

The `typeData` of classes that have a `vtable` are inserted into
the `vtable` itself, making the latter a subtype of `typeData`.
This is done in anticipation of the support for `GetClass`: it
will be able to fetch the `vtable` field of the object as the
`typeData` from which to extract the `java.lang.Class` instance.
The `typeData` of other classes are stored as independent
globals.
When the value is one of our objects, we look at its `vtable`
field, which is also the appropriate `typeData` reference.

When it is a JS value, we implement the dispatch in a dedicated
helper function.
Copy link
Owner

@tanishiking tanishiking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you very much for working on this quite difficult part of new backend!

@tanishiking tanishiking merged commit 3635d44 into tanishiking:main Mar 18, 2024
1 check passed
@sjrd sjrd deleted the classof branch March 18, 2024 16:54
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants