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

refactor: CGClassConstructHook into Handwritten Helpers #31949

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rohansx
Copy link

@rohansx rohansx commented Mar 30, 2024

  • Extracted the constructor hook generated by CGClassConstructHook into two new, handwritten generic helper functions. This approach follows the model established in Extract generated finalizers into generic helper functions. #31569.

  • The new helper functions are designed to handle both cases of isHTMLConstructor being true or false, ensuring compatibility with the existing codebase while enhancing readability.

  • Removed the Python script responsible for generating the previous, less readable Rust code, replacing it with our new, cleaner solution.

  • ./mach build -d does not report any errors

  • ./mach test-tidy does not report any errors

  • These changes fix Extract generated class constructor hook into handwritten functions #31571

@servo-wpt-sync
Copy link
Collaborator

🛠 These changes could not be applied onto the latest upstream WPT. Servo's copy of the Web Platform Tests may be out of sync.

@servo-wpt-sync
Copy link
Collaborator

🛠 These changes could not be applied onto the latest upstream WPT. Servo's copy of the Web Platform Tests may be out of sync.

Copy link
Member

@jdm jdm left a comment

Choose a reason for hiding this comment

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

This is making good progress!

Comment on lines +12 to +15
pub unsafe fn construct_html_custom(global: &GlobalScope, cx: *mut JSContext, args: &CallArgs, descriptor_name: &str) -> bool {
let global = DomRoot::downcast::<dom::types::Global>(global).unwrap(); // Adjust type as needed
let prototype_id = match descriptor_name {
"HTMLDivElement" => PrototypeList::ID::HTMLDivElement,
Copy link
Member

Choose a reason for hiding this comment

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

Instead of taking string argument, we should take a PrototypeList::ID argument that we can use directly. The python code can construct the required value.

Comment on lines +30 to +32
pub unsafe fn construct_default_custom(global: &GlobalScope, cx: *mut JSContext, args: &CallArgs, descriptor_name: &str, ctor_name: &str) -> bool {
let prototype_id = match descriptor_name {
"HTMLDivElement" => PrototypeList::ID::HTMLDivElement,
Copy link
Member

Choose a reason for hiding this comment

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

Same comment about using a PrototypeList::ID argument instead of a string.

// Add cases for other descriptor names as needed
_ => panic!("Unsupported descriptor name: {}", descriptor_name),
};
htmlconstructor::call_html_constructor::<dom::types::Global>(
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using <dom::types::Global> here, we need to add a generic type to this function (pub unsafe fn construct_html_custom<T: DerivedFrom<Element> + DomObject>) and use that generic type (htmlconstructor::call_html_constructor::<T>).

use crate::dom::bindings::codegen::PrototypeList::ID;
// Function for HTML constructor case
pub unsafe fn construct_html_custom(global: &GlobalScope, cx: *mut JSContext, args: &CallArgs, descriptor_name: &str) -> bool {
let global = DomRoot::downcast::<dom::types::Global>(global).unwrap(); // Adjust type as needed
Copy link
Member

Choose a reason for hiding this comment

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

I believe this needs to be a downcast to Window, not Global, for the call to call_html_constructor to succeed.

args,
&global,
prototype_id, // Adjust as needed
descriptor_name,
Copy link
Member

Choose a reason for hiding this comment

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

We're missing the creator argument here. We should accept that as an argument in construct_html_custom and let the python code pass the appropriate function value like it does before the changes in this PR.

cx,
args,
prototype_id, // Adjust as needed
descriptor_name,
Copy link
Member

Choose a reason for hiding this comment

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

Same comment about the creator argument here as earlier. This should be passed in by the python code.

return false;
}

let nativeName = MakeNativeName(ctor_name);
Copy link
Member

Choose a reason for hiding this comment

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

MakeNativeName is a python function. This is going to be a bit tricky; I think we're going to need to leave the actual constructor call in the generated python code, because there's a lot of unique logic in there. What we can do is something like this:

pub unsafe fn construct_default_custom(..., constructor: impl FnOnce(*mut JSContext, &CallArgs, &GlobalScope) -> bool) -> bool {
  ...
  constructor(cx, args, global)
}

Then modify the python code to call f"construct_default_custom(&global, cx, &args, \"{ctorName}\", |cx, args, global| {{ {constructorInvoke} }});\n" where constructorInvoke is the result of calling .define() on the constructorCall CGMethodCall value.

@mrobinson
Copy link
Member

@rohansx Do you plan to keep working on this one?

@rohansx
Copy link
Author

rohansx commented Apr 22, 2024

@rohansx Do you plan to keep working on this one?

Yes.. I'll work on this issue @mrobinson

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.

Extract generated class constructor hook into handwritten functions
4 participants