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: unify JavaScript script execution method #11043

Merged
merged 9 commits into from
Jun 21, 2021

Conversation

bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Jun 19, 2021

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]

@bartlomieju bartlomieju changed the title [WIP] refactor: unify JavaScript script execution method refactor: unify JavaScript script execution method Jun 19, 2021
worker.execute("window.dispatchEvent(new Event('load'))")?;
worker.execute_script(
"[native code]",
"window.dispatchEvent(new Event('load'))",
Copy link
Collaborator

@nayeemrmn nayeemrmn Jun 19, 2021

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...

Suggested change
"window.dispatchEvent(new Event('load'))",
&format!("[deno:{}].js", std::file!()),

image

Copy link
Member Author

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

Copy link
Member

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!())

Copy link
Member

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...

Copy link
Collaborator

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>.

Copy link
Collaborator

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? 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦

Copy link
Member Author

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?

Copy link
Collaborator

@nayeemrmn nayeemrmn Jun 19, 2021

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.

Copy link
Member Author

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.

@bartlomieju bartlomieju requested a review from ry June 19, 2021 14:45
core/lib.rs Show resolved Hide resolved
Copy link
Member

@ry ry left a 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?

Copy link
Collaborator

@nayeemrmn nayeemrmn left a comment

Choose a reason for hiding this comment

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

LGTM 👍

image
image

@bartlomieju
Copy link
Member Author

LGTM 👍

image
image

Yeah, I agree providing this path is much better than [native code]. Thanks for helping pan this out!

@bartlomieju bartlomieju added this to the 1.12.0 milestone Jun 19, 2021
@bartlomieju bartlomieju merged commit 9105892 into denoland:main Jun 21, 2021
@bartlomieju bartlomieju deleted the refactor_worker_execute branch June 21, 2021 23:45
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