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(common): Simplify SyntaxContext and Mark #9476

Merged
merged 5 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/clean-jobs-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_core: patch
swc_common: patch
swc_plugin_runner: patch
---

refactor(common): Simplify `SyntaxContext` and `Mark`
3 changes: 0 additions & 3 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ impl Globals {
hygiene_data: Mutex::new(hygiene::HygieneData::new()),
marks: Mutex::new(vec![MarkData {
parent: Mark::root(),
// If the root is opaque, then loops searching for an opaque mark
// will automatically stop after reaching it.
is_builtin: true,
}]),
dummy_cnt: AtomicU32::new(DUMMY_RESERVE),
}
Expand Down
57 changes: 2 additions & 55 deletions crates/swc_common/src/syntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ impl SyntaxContext {
struct SyntaxContextData {
outer_mark: Mark,
prev_ctxt: SyntaxContext,
// This context, but with all transparent and semi-transparent marks filtered away.
opaque: SyntaxContext,
// This context, but with all transparent marks filtered away.
opaque_and_semitransparent: SyntaxContext,
}

/// A mark is a unique id associated with a macro expansion.
Expand All @@ -83,7 +80,6 @@ pub struct Mark(u32);
#[derive(Clone, Debug)]
pub(crate) struct MarkData {
pub(crate) parent: Mark,
pub(crate) is_builtin: bool,
}

#[cfg_attr(
Expand All @@ -103,8 +99,6 @@ extern "C" {
// on their side.
fn __mark_fresh_proxy(mark: u32) -> u32;
fn __mark_parent_proxy(self_mark: u32) -> u32;
fn __mark_is_builtin_proxy(self_mark: u32) -> u32;
fn __mark_set_builtin_proxy(self_mark: u32, is_builtin: u32);
fn __syntax_context_apply_mark_proxy(self_syntax_context: u32, mark: u32) -> u32;
fn __syntax_context_outer_proxy(self_mark: u32) -> u32;

Expand Down Expand Up @@ -135,10 +129,7 @@ impl Mark {
// targeting wasm32-*.
#[cfg(not(all(feature = "__plugin_mode", target_arch = "wasm32")))]
return with_marks(|marks| {
marks.push(MarkData {
parent,
is_builtin: false,
});
marks.push(MarkData { parent });
Mark(marks.len() as u32 - 1)
});
}
Expand Down Expand Up @@ -169,33 +160,6 @@ impl Mark {
return with_marks(|marks| marks[self.0 as usize].parent);
}

#[inline]
pub fn is_builtin(self) -> bool {
#[cfg(all(feature = "__plugin_mode", target_arch = "wasm32"))]
return unsafe { __mark_is_builtin_proxy(self.0) != 0 };

#[cfg(not(all(feature = "__plugin_mode", target_arch = "wasm32")))]
{
assert_ne!(self, Mark::root());

with_marks(|marks| marks[self.0 as usize].is_builtin)
}
}

#[inline]
pub fn set_is_builtin(self, is_builtin: bool) {
#[cfg(all(feature = "__plugin_mode", target_arch = "wasm32"))]
unsafe {
__mark_set_builtin_proxy(self.0, is_builtin as u32)
}
#[cfg(not(all(feature = "__plugin_mode", target_arch = "wasm32")))]
{
assert_ne!(self, Mark::root());

with_marks(|marks| marks[self.0 as usize].is_builtin = is_builtin)
}
}

#[allow(unused_assignments)]
#[cfg(all(feature = "__plugin_mode", target_arch = "wasm32"))]
pub fn is_descendant_of(mut self, ancestor: Mark) -> bool {
Expand Down Expand Up @@ -321,7 +285,6 @@ impl HygieneData {
outer_mark: Mark::root(),
prev_ctxt: SyntaxContext(0),
opaque: SyntaxContext(0),
opaque_and_semitransparent: SyntaxContext(0),
}],
markings: HashMap::default(),
}
Expand Down Expand Up @@ -414,32 +377,16 @@ impl SyntaxContext {
HygieneData::with(|data| {
let syntax_contexts = &mut data.syntax_contexts;
let mut opaque = syntax_contexts[self.0 as usize].opaque;
let opaque_and_semitransparent =
syntax_contexts[self.0 as usize].opaque_and_semitransparent;

let prev_ctxt = opaque;
opaque = *data.markings.entry((prev_ctxt, mark)).or_insert_with(|| {
*data.markings.entry((prev_ctxt, mark)).or_insert_with(|| {
let new_opaque = SyntaxContext(syntax_contexts.len() as u32);
syntax_contexts.push(SyntaxContextData {
outer_mark: mark,
prev_ctxt,
opaque: new_opaque,
opaque_and_semitransparent: new_opaque,
});
new_opaque
});

let prev_ctxt = self;
*data.markings.entry((prev_ctxt, mark)).or_insert_with(|| {
let new_opaque_and_semitransparent_and_transparent =
SyntaxContext(syntax_contexts.len() as u32);
syntax_contexts.push(SyntaxContextData {
outer_mark: mark,
prev_ctxt,
opaque,
opaque_and_semitransparent,
});
new_opaque_and_semitransparent_and_transparent
})
})
}
Expand Down
8 changes: 0 additions & 8 deletions crates/swc_plugin_runner/src/imported_fn/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ pub fn mark_parent_proxy(self_mark: u32) -> u32 {
Mark::from_u32(self_mark).parent().as_u32()
}

pub fn mark_is_builtin_proxy(self_mark: u32) -> u32 {
Mark::from_u32(self_mark).is_builtin() as u32
}

pub fn mark_set_builtin_proxy(self_mark: u32, is_builtin: u32) {
Mark::from_u32(self_mark).set_is_builtin(is_builtin != 0);
}

/// A proxy to Mark::is_descendant_of_() that can be used in plugin.
/// Original call site have mutable param, which we'll pass over as return value
/// via serialized MutableMarkContext.
Expand Down
4 changes: 0 additions & 4 deletions crates/swc_plugin_runner/src/imported_fn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ pub(crate) fn build_import_object(
// hygiene
let mark_fresh_fn_decl = Function::new_typed(wasmer_store, mark_fresh_proxy);
let mark_parent_fn_decl = Function::new_typed(wasmer_store, mark_parent_proxy);
let mark_is_builtin_fn_decl = Function::new_typed(wasmer_store, mark_is_builtin_proxy);
let mark_set_builtin_fn_decl = Function::new_typed(wasmer_store, mark_set_builtin_proxy);
let mark_is_descendant_of_fn_decl =
Function::new_typed_with_env(wasmer_store, base_env, mark_is_descendant_of_proxy);

Expand Down Expand Up @@ -254,8 +252,6 @@ pub(crate) fn build_import_object(
// hygiene
"__mark_fresh_proxy" => mark_fresh_fn_decl,
"__mark_parent_proxy" => mark_parent_fn_decl,
"__mark_is_builtin_proxy" => mark_is_builtin_fn_decl,
"__mark_set_builtin_proxy" => mark_set_builtin_fn_decl,
"__mark_is_descendant_of_proxy" => mark_is_descendant_of_fn_decl,
"__mark_least_ancestor" => mark_least_ancestor_fn_decl,
"__syntax_context_apply_mark_proxy" => syntax_context_apply_mark_fn_decl,
Expand Down
Loading