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

Extract generated class constructor hook into handwritten functions #31571

Open
Tracked by #31570
jdm opened this issue Mar 7, 2024 · 17 comments · May be fixed by #31949
Open
Tracked by #31570

Extract generated class constructor hook into handwritten functions #31571

jdm opened this issue Mar 7, 2024 · 17 comments · May be fixed by #31949
Labels
A-content/bindings The DOM bindings E-more-complex Variable effort required; may require a mentor. Recommended solution is clearly described in the iss I-cleanup No impact; the issue is one of maintainability or tidiness.

Comments

@jdm
Copy link
Member

jdm commented Mar 7, 2024

The output of CGClassConstructHook is difficult to read. By following the model of #31569 and creating two generic helper functions that cover both cases (isHTMLConstructor is true/false), it will be easier to read and modify this code in the future.

Part of #31570.

@jdm jdm added A-content/bindings The DOM bindings E-more-complex Variable effort required; may require a mentor. Recommended solution is clearly described in the iss I-cleanup No impact; the issue is one of maintainability or tidiness. labels Mar 7, 2024
@rohansx
Copy link

rohansx commented Mar 11, 2024

@jdm @mrobinson would like to take on this task

@jdm
Copy link
Member Author

jdm commented Mar 11, 2024

@rohansx Ok! Take a look at the model PR in #31569 and ask any questions about the task here!

@MunishMummadi
Copy link
Contributor

@servo-highfive assign me

@servo-highfive
Copy link

Hey @MunishMummadi! Thanks for your interest in working on this issue. It's now assigned to you!

@servo-highfive servo-highfive added the C-assigned There is someone working on resolving the issue label Mar 21, 2024
@MunishMummadi
Copy link
Contributor

Hey @jdm . I would love to work on this issue. Can you mentor me for this issue. I understood what to do. But it seems little overwhelming.

@jdm
Copy link
Member Author

jdm commented Mar 22, 2024

Yes, I'm happy to answer questions about it

@MunishMummadi
Copy link
Contributor

MunishMummadi commented Mar 23, 2024

@jdm . I am sorry to say this. I tried my best. But I failed. Thank you for the heads up. You can unassign me.

@jdm jdm removed the C-assigned There is someone working on resolving the issue label Mar 23, 2024
@rohansx
Copy link

rohansx commented Mar 28, 2024

@jdm Went through the issue and need a few clarifications:
The PR states
output of CGClassConstructHook (Present on Line 6111)
Does this class code needs to be changed for better readability ?

Because the highlighted code section in (difficult to read) shows code line numbers from 6175-6212

@jdm
Copy link
Member Author

jdm commented Mar 28, 2024

Thanks for checking!

class CGClassConstructHook(CGAbstractExternMethod):
"""
JS-visible constructor for our objects
"""
def __init__(self, descriptor, constructor=None):
args = [Argument('*mut JSContext', 'cx'), Argument('u32', 'argc'), Argument('*mut JSVal', 'vp')]
name = CONSTRUCT_HOOK_NAME
if constructor:
name += "_" + constructor.identifier.name
else:
constructor = descriptor.interface.ctor()
assert constructor
CGAbstractExternMethod.__init__(self, descriptor, name, 'bool', args)
self.constructor = constructor
self.exposureSet = descriptor.interface.exposureSet
def definition_body(self):
preamble = """let cx = SafeJSContext::from_ptr(cx);
let args = CallArgs::from_vp(vp, argc);
let global = GlobalScope::from_object(JS_CALLEE(*cx, vp).to_object());
"""
if len(self.exposureSet) == 1:
preamble += """\
let global = DomRoot::downcast::<dom::types::%s>(global).unwrap();
""" % list(self.exposureSet)[0]
if self.constructor.isHTMLConstructor():
signatures = self.constructor.signatures()
assert len(signatures) == 1
constructorCall = CGGeneric("""dom::bindings::htmlconstructor::call_html_constructor::<dom::types::%s>(
cx,
&args,
&global,
PrototypeList::ID::%s,
CreateInterfaceObjects,
)""" % (self.descriptor.name, MakeNativeName(self.descriptor.name)))
else:
ctorName = GetConstructorNameForReporting(self.descriptor, self.constructor)
preamble += """
if !callargs_is_constructing(&args) {
throw_constructor_without_new(*cx, "%s");
return false;
}
rooted!(in(*cx) let mut desired_proto = ptr::null_mut::<JSObject>());
let proto_result = get_desired_proto(
cx,
&args,
PrototypeList::ID::%s,
CreateInterfaceObjects,
desired_proto.handle_mut(),
);
assert!(proto_result.is_ok());
if proto_result.is_err() {
return false;
}
""" % (ctorName, MakeNativeName(self.descriptor.name))
name = self.constructor.identifier.name
nativeName = MakeNativeName(self.descriptor.binaryNameFor(name))
args = ["&global", "Some(desired_proto.handle())"]
constructorCall = CGMethodCall(args, nativeName, True,
self.descriptor, self.constructor)
return CGList([CGGeneric(preamble), constructorCall])
is the intended class.

@rohansx
Copy link

rohansx commented Mar 29, 2024

@servo-highfive @jdm Just one more additional help needed here. I have performed some code changes for this PR.
However can you guide me as to how do I test this function while running this application in local setup? (For debugging purpose)

Screenshot from 2024-03-29 16-35-31

@jdm
Copy link
Member Author

jdm commented Mar 29, 2024

If the page loads like it did before, I would expect that's good enough for a PR. We can run the full automated test suite to verify the changes.

@rohansx rohansx linked a pull request Mar 30, 2024 that will close this issue
3 tasks
@rohansx
Copy link

rohansx commented Mar 30, 2024

Hello @jdm attempted this issue and created PR #31949 but would like a litte guidance .
I'm struggling with interop (running rust code within python file codegenrust.py
My rust code is in a separate file called construct_hook.rs
Added the necessary import of crate in import.rs
Facing the below interop errors:
error[E0424]: expected value, found module 'self' --> /home/rsx/Desktop/outreachy/servo/target/debug/build/script-30067a5bb009356d/out/Bindings/FileReaderSyncBinding.rs:515:93 | 512 | unsafe extern fn _constructor(cx: *mut JSContext, argc: u32, vp: *mut JSVal) -> bool { | ------------ this function can't have aselfparameter ... 515 | let global = get_global_scope(cx, vp, argc);handle_constructor(cx, &argc, &global, &self.descriptor, &self.constructor); | ^^^^selfvalue is a keyword only available in methods with aself parameter

@sagudev
Copy link
Member

sagudev commented Mar 31, 2024

self.constructor is only available in python, rust cannot use python's variables.

@rohansx
Copy link

rohansx commented Mar 31, 2024

Hello Thank you for the guidance made a bit more changes can you help me with this one last query
I noticed I can't find use crate import statements mentioned in PR https://github.com/servo/servo/pull/31569/files
I need to add this similar line to make my code work. Has the location been changed to import.rs

use crate::construct_hook::construct_if_block;

Screenshot 2024-03-31 at 5 07 29 PM

@sagudev
Copy link
Member

sagudev commented Mar 31, 2024

There were some changes to how importing is done. All you need to to is add proper use statements to import.rs. #31569 was made before this was introduced in #31711.

@rohansx
Copy link

rohansx commented Mar 31, 2024

Hello @sagudev a bit more help please, struggling to make build here . In my rust code (which I have separated in construct_hook.rs facing the below error for PrototypeList::ID::"%s"
Do I have to explicitly define the type?
error: expected identifier, found "%s" --> components/script/dom/bindings/construct_hook.rs:18:28 | 18 | PrototypeList: id: "%s", // Adjust as needed | ^^^^ expected identifier Building [=======================> ] 762/768: script

@sagudev
Copy link
Member

sagudev commented Mar 31, 2024

I am not sure if I understand error, it better to use ``` blocks when having code of multiple lines. From what I see in PR you need to be more careful what is python and what is rust. Codegen (python code) generates rust code (this is usually done with strings manipulation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-content/bindings The DOM bindings E-more-complex Variable effort required; may require a mentor. Recommended solution is clearly described in the iss I-cleanup No impact; the issue is one of maintainability or tidiness.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants