Skip to content

Conversation

@ZZZank
Copy link
Contributor

@ZZZank ZZZank commented Jun 27, 2025

The intention of this PR is the same as #1948 : to make code gen works even after a package renaming (aka relocating)

In order to handle such a large amount of method references to ScriptRuntime, a new enum class ScriptRuntimeMethodSig is introduced, with each enum value holding code gen related information for a method in ScriptRuntime.

See javadoc for ScriptRuntimeMethodSig for more info: link

In theory this PR is completed and ready for review, but I dont know how to suppress the "ImmutableEnum" error when doing gradle check, so draft for now

@gbrail
Copy link
Collaborator

gbrail commented Jun 28, 2025

I haven't had a chance to read this yet, but you can check here for the warning:

https://errorprone.info/bugpattern/[ImmutableEnumChecker](https://errorprone.info/bugpattern/ImmutableEnumChecker)

@ZZZank ZZZank marked this pull request as ready for review June 30, 2025 14:17
@rPraml
Copy link
Contributor

rPraml commented Jun 30, 2025

This is really cool. But please take a look if this will work when method with different signatures are here as in this PR https://github.com/mozilla/rhino/pull/1957/files

@ZZZank
Copy link
Contributor Author

ZZZank commented Jul 1, 2025

But please take a look if this will work when method with different signatures

Yes it will work. Take memberRef as an example:

memberRef_member,
memberRef_namespaceMember,
memberRef_name,
memberRef_namespaceName,

There're 4 memberRef methods with the same name but different signatures in ScriptRuntime, and they are linked to 4 enum values presented above.

ScriptRuntime.class.getName().replace('.', '/');

static {
// init
Copy link
Contributor

Choose a reason for hiding this comment

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

This init code is really "java-magic"

  • the ScriptRuntime-class needs the MethodSig-enum in annotations
  • the static initialzer of the MethodSig-enum needs the annotations in ScriptRuntime.

First I thought, that this will not work, because we have a cycle (but somehow the JVM can handle this and probably in some JLS the behavior is described exactly)
if we are sure, that this will work on all VMs etc, all good
...otherwise I would suggest to move the init code to the BodyCodeGen

Copy link
Contributor Author

@ZZZank ZZZank Jul 14, 2025

Choose a reason for hiding this comment

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

I briefly walked through JLS for enum class, and unfortunately there doesn't seem to be a behavior definition for what we're doing here. But I do found out that:

enum SomeEnumClass {
    static {
        SomeEnumClass.values();
    }
}

is valid, which indicates that enum members are already initialized when doing class static initialization, so what we're doing here should be fine

@gbrail
Copy link
Collaborator

gbrail commented Jul 2, 2025

This is a clever solution, barring @rbri 's comments about static initialization order.

Is it your intention to see this through to the end? The bootstrap method for INDY instructions, and everything in OptRuntime, for example, still aren't replaced using this mechanism.

@ZZZank
Copy link
Contributor Author

ZZZank commented Jul 14, 2025

Is it your intention to see this through to the end?

My plan is to, at least, replace all method references in BodyCodeGen, so that even if I dont have much time for it, other volunteers can know how this works and continue working on it. I will certainly see this through to the end, if time permitted

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

Successfully merging this pull request may close these issues.

3 participants