feat(plugin): Implement proxy for dummy_with_cmt (#4268)

This commit is contained in:
OJ Kwon 2022-04-06 22:37:12 -07:00 committed by GitHub
parent 92186d0407
commit b15e984317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

@ -6,7 +6,7 @@ use std::{
hash::{Hash, Hasher},
ops::{Add, Sub},
path::PathBuf,
sync::atomic::{AtomicU32, Ordering},
sync::atomic::AtomicU32,
};
#[cfg(feature = "parking_lot")]
@ -68,6 +68,7 @@ pub const DUMMY_SP: Span = Span {
#[derive(Default)]
pub struct Globals {
hygiene_data: Mutex<hygiene::HygieneData>,
#[allow(unused)]
dummy_cnt: AtomicU32,
}
@ -199,6 +200,10 @@ pub struct MultiSpan {
span_labels: Vec<(Span, String)>,
}
extern "C" {
fn __span_dummy_with_cmt_proxy() -> u32;
}
impl Span {
#[inline]
pub fn lo(self) -> BytePos {
@ -438,14 +443,30 @@ impl Span {
/// Dummy span, both position are extremely large numbers so they would be
/// ignore by sourcemap, but can still have comments
pub fn dummy_with_cmt() -> Self {
GLOBALS.with(|globals| {
let lo = BytePos(globals.dummy_cnt.fetch_add(1, Ordering::SeqCst));
#[cfg(all(feature = "plugin-mode", target_arch = "wasm32"))]
{
let lo = BytePos(unsafe { __span_dummy_with_cmt_proxy() });
return Span {
lo,
hi: lo,
ctxt: SyntaxContext::empty(),
};
}
#[cfg(not(all(feature = "plugin-mode", target_arch = "wasm32")))]
return GLOBALS.with(|globals| {
let lo = BytePos(
globals
.dummy_cnt
.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
);
Span {
lo,
hi: lo,
ctxt: SyntaxContext::empty(),
}
})
});
}
}

View File

@ -57,6 +57,7 @@ use crate::{
take_leading_comments_proxy, take_trailing_comments_proxy, CommentHostEnvironment,
},
set_transform_result::{set_transform_result, TransformResultHostEnvironment},
span::span_dummy_with_cmt_proxy,
},
};
@ -64,6 +65,7 @@ mod comments;
mod handler;
mod hygiene;
mod set_transform_result;
mod span;
use handler::*;
use hygiene::*;
@ -114,6 +116,9 @@ pub(crate) fn build_import_object(
let syntax_context_outer_fn_decl =
Function::new_native(wasmer_store, syntax_context_outer_proxy);
// Span
let span_dummy_with_cmt_fn_decl = Function::new_native(wasmer_store, span_dummy_with_cmt_proxy);
// comments
let comment_buffer = Arc::new(Mutex::new(vec![]));
@ -209,6 +214,8 @@ pub(crate) fn build_import_object(
"__syntax_context_apply_mark_proxy" => syntax_context_apply_mark_fn_decl,
"__syntax_context_remove_mark_proxy" => syntax_context_remove_mark_fn_decl,
"__syntax_context_outer_proxy" => syntax_context_outer_fn_decl,
// span
"__span_dummy_with_cmt_proxy" => span_dummy_with_cmt_fn_decl,
// comments
"__copy_comment_to_host_env" => copy_comment_to_host_env_fn_decl,
"__add_leading_comment_proxy" => add_leading_comment_fn_decl,

View File

@ -0,0 +1,4 @@
pub fn span_dummy_with_cmt_proxy() -> u32 {
// Instead of trying to serialize whole span, send bytepos only
swc_common::Span::dummy_with_cmt().lo.0
}