-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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: unify JavaScript script execution method #11043
refactor: unify JavaScript script execution method #11043
Conversation
worker.execute("window.dispatchEvent(new Event('load'))")?; | ||
worker.execute_script( | ||
"[native code]", | ||
"window.dispatchEvent(new Event('load'))", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of something like std::file!()
for JS inlined in rust?
Edit: Highlighted wrong line...
"window.dispatchEvent(new Event('load'))", | |
&format!("[deno:{}].js", std::file!()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that was my first thought but I didn't want to over do it.
I'll be fine even with copy-pasting the strings, don't think a couple dozen call sites across 3 modules need a macro
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a good idea maybe something like
format!("[deno-internal:{}]", std::file!())
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
execute_script could even become a macro, so this code wasn't repeated everywhere...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
format!("[deno-internal:{}]", std::file!())
It can just be deno:
, the situation matches exactly how we use this protocol: deno:<repo-relative path to source file>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked and
std::file!()
appends leading slash:let a = format!("deno:/{}", std::file!());
Looks like the leading slash is yours? 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I reworked it to use std::file!()
haven't added curly braces. I'm open to doing that, @ry wanna break the tie on this one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm open to that, though it doesn't bother me that much - in either situation you won't be able to lookup this code using column and line anyway (line will always be 1, as we only execute onliners).
That's why I think it shouldn't mislead with a connected line number.
However, I think we can give useful location info. I didn't suggest it because it seemed like a long line to repeat but we can do it with a macro:
macro_rules! located_script_name {
() => {
format!("[deno:{}:{}:{}]", std::file!(), std::line!(), std::column!());
};
}
// ...
worker.execute_script(
&located_script_name!(),
"window.dispatchEvent(new Event('load'))"
);
The location points to the initializer of the script name instead of the code but it's genuinely useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I added this macro to deno_core
and reexported it from there so both deno_runtime
and deno_cli
crates can use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
maybe this should wait for 1.12?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit renames "JsRuntime::execute" to "JsRuntime::execute_script". Additionally
same renames were applied to methods on "deno_runtime::Worker" and
"deno_runtime::WebWorker".
A new macro was added to "deno_core" called "located_script_name" which
returns the name of Rust file alongside line no and col no of that call site.
This macro is useful in combination with "JsRuntime::execute_script"
and allows to provide accurate place where "one-off" JavaScript scripts
are executed for internal runtime functions.
Co-authored-by: Nayeem Rahman [email protected]