fix(es/plugin): Print more details on pointer conversion failures (#6378)

This commit is contained in:
Andreas 2022-11-11 23:47:44 +01:00 committed by GitHub
parent 8d8f058d36
commit b6c1cc49e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 98 additions and 95 deletions

View File

@ -123,7 +123,7 @@ impl PluginSerializedBytes {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub unsafe fn deserialize_from_ptr<W>( pub unsafe fn deserialize_from_ptr<W>(
raw_allocated_ptr: *const u8, raw_allocated_ptr: *const u8,
raw_allocated_ptr_len: i32, raw_allocated_ptr_len: u32,
) -> Result<W, Error> ) -> Result<W, Error>
where where
W: rkyv::Archive, W: rkyv::Archive,
@ -147,7 +147,7 @@ where
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub unsafe fn deserialize_from_ptr_into_fallible<W>( pub unsafe fn deserialize_from_ptr_into_fallible<W>(
raw_allocated_ptr: *const u8, raw_allocated_ptr: *const u8,
raw_allocated_ptr_len: i32, raw_allocated_ptr_len: u32,
) -> Result<W, Error> ) -> Result<W, Error>
where where
W: rkyv::Archive, W: rkyv::Archive,

View File

@ -31,9 +31,9 @@ fn handle_func(func: ItemFn) -> TokenStream {
// Refer swc_plugin_runner for the actual implementation. // Refer swc_plugin_runner for the actual implementation.
#[cfg(target_arch = "wasm32")] // Allow testing #[cfg(target_arch = "wasm32")] // Allow testing
extern "C" { extern "C" {
fn __set_transform_result(bytes_ptr: i32, bytes_ptr_len: i32); fn __set_transform_result(bytes_ptr: u32, bytes_ptr_len: u32);
fn __set_transform_plugin_core_pkg_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); fn __set_transform_plugin_core_pkg_diagnostics(bytes_ptr: u32, bytes_ptr_len: u32);
fn __emit_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); fn __emit_diagnostics(bytes_ptr: u32, bytes_ptr_len: u32);
} }
/// An emitter for the Diagnostic in plugin's context by borrowing host's /// An emitter for the Diagnostic in plugin's context by borrowing host's
@ -52,7 +52,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
#[cfg(target_arch = "wasm32")] // Allow testing #[cfg(target_arch = "wasm32")] // Allow testing
unsafe { unsafe {
__emit_diagnostics(ptr as i32, len as i32); __emit_diagnostics(ptr as u32, len as u32);
} }
} }
} }
@ -60,7 +60,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
/// Call hosts's imported fn to set transform results. /// Call hosts's imported fn to set transform results.
/// __set_transform_result is host side imported fn, which read and copies guest's byte into host. /// __set_transform_result is host side imported fn, which read and copies guest's byte into host.
fn send_transform_result_to_host(bytes_ptr: i32, bytes_ptr_len: i32) { fn send_transform_result_to_host(bytes_ptr: u32, bytes_ptr_len: u32) {
#[cfg(target_arch = "wasm32")] // Allow testing #[cfg(target_arch = "wasm32")] // Allow testing
unsafe { unsafe {
__set_transform_result(bytes_ptr, bytes_ptr_len); __set_transform_result(bytes_ptr, bytes_ptr_len);
@ -68,19 +68,19 @@ fn handle_func(func: ItemFn) -> TokenStream {
} }
/// Internal function plugin_macro uses to create ptr to PluginError. /// Internal function plugin_macro uses to create ptr to PluginError.
fn construct_error_ptr(plugin_error: swc_core::common::plugin::serialized::PluginError) -> i32 { fn construct_error_ptr(plugin_error: swc_core::common::plugin::serialized::PluginError) -> u32 {
let ret = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); let ret = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError");
let (ptr, len) = ret.as_ptr(); let (ptr, len) = ret.as_ptr();
send_transform_result_to_host( send_transform_result_to_host(
ptr as _, ptr as _,
len as i32 len as u32
); );
1 1
} }
#[no_mangle] #[no_mangle]
pub fn #transform_core_pkg_diag_ident() -> i32 { pub fn #transform_core_pkg_diag_ident() -> u32 {
let schema_version = swc_core::common::plugin::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION; let schema_version = swc_core::common::plugin::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION;
let core_pkg_diag = swc_core::diagnostics::get_core_engine_diagnostics(); let core_pkg_diag = swc_core::diagnostics::get_core_engine_diagnostics();
@ -99,7 +99,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
#[cfg(target_arch = "wasm32")] // Allow testing #[cfg(target_arch = "wasm32")] // Allow testing
unsafe { unsafe {
__set_transform_plugin_core_pkg_diagnostics(serialized_result_ptr as _, serialized_result_ptr_len as i32); __set_transform_plugin_core_pkg_diagnostics(serialized_result_ptr as _, serialized_result_ptr_len as u32);
} }
0 0
} }
@ -110,8 +110,8 @@ fn handle_func(func: ItemFn) -> TokenStream {
// serialization of PluginError itself should succeed. // serialization of PluginError itself should succeed.
#[no_mangle] #[no_mangle]
pub fn #transform_process_impl_ident( pub fn #transform_process_impl_ident(
ast_ptr: *const u8, ast_ptr_len: i32, ast_ptr: *const u8, ast_ptr_len: u32,
unresolved_mark: i32, should_enable_comments_proxy: i32) -> i32 { unresolved_mark: u32, should_enable_comments_proxy: i32) -> u32 {
// Reconstruct `Program` & config string from serialized program // Reconstruct `Program` & config string from serialized program
// Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin.
let program = unsafe { swc_core::common::plugin::serialized::deserialize_from_ptr(ast_ptr, ast_ptr_len) }; let program = unsafe { swc_core::common::plugin::serialized::deserialize_from_ptr(ast_ptr, ast_ptr_len) };
@ -160,7 +160,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
let serialized_result = serialized_result.expect("Should be a realized transformed program"); let serialized_result = serialized_result.expect("Should be a realized transformed program");
let (serialized_result_ptr, serialized_result_ptr_len) = serialized_result.as_ptr(); let (serialized_result_ptr, serialized_result_ptr_len) = serialized_result.as_ptr();
send_transform_result_to_host(serialized_result_ptr as _, serialized_result_ptr_len as i32); send_transform_result_to_host(serialized_result_ptr as _, serialized_result_ptr_len as u32);
0 0
} }
}; };

View File

@ -14,19 +14,19 @@ use crate::memory_interop::read_returned_result_from_host;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
extern "C" { extern "C" {
fn __copy_comment_to_host_env(bytes_ptr: i32, bytes_ptr_len: i32); fn __copy_comment_to_host_env(bytes_ptr: u32, bytes_ptr_len: u32);
fn __add_leading_comment_proxy(byte_pos: u32); fn __add_leading_comment_proxy(byte_pos: u32);
fn __add_leading_comments_proxy(byte_pos: u32); fn __add_leading_comments_proxy(byte_pos: u32);
fn __has_leading_comments_proxy(byte_pos: u32) -> i32; fn __has_leading_comments_proxy(byte_pos: u32) -> u32;
fn __move_leading_comments_proxy(from_byte_pos: u32, to_byte_pos: u32); fn __move_leading_comments_proxy(from_byte_pos: u32, to_byte_pos: u32);
fn __take_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; fn __take_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32;
fn __get_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; fn __get_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32;
fn __add_trailing_comment_proxy(byte_pos: u32); fn __add_trailing_comment_proxy(byte_pos: u32);
fn __add_trailing_comments_proxy(byte_pos: u32); fn __add_trailing_comments_proxy(byte_pos: u32);
fn __has_trailing_comments_proxy(byte_pos: u32) -> i32; fn __has_trailing_comments_proxy(byte_pos: u32) -> u32;
fn __move_trailing_comments_proxy(from_byte_pos: u32, to_byte_pos: u32); fn __move_trailing_comments_proxy(from_byte_pos: u32, to_byte_pos: u32);
fn __take_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; fn __take_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32;
fn __get_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; fn __get_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32;
fn __add_pure_comment_proxy(byte_pos: u32); fn __add_pure_comment_proxy(byte_pos: u32);
} }
@ -65,7 +65,7 @@ impl PluginCommentsProxy {
// CommentHostEnvironment's buffer, subsequent proxy call will read & // CommentHostEnvironment's buffer, subsequent proxy call will read &
// deserialize it. // deserialize it.
__copy_comment_to_host_env( __copy_comment_to_host_env(
serialized_comment_ptr as i32, serialized_comment_ptr as u32,
serialized_comment_ptr_len serialized_comment_ptr_len
.try_into() .try_into()
.expect("Should able to convert ptr length"), .expect("Should able to convert ptr length"),

View File

@ -11,7 +11,7 @@ use swc_common::plugin::serialized::{
feature = "__rkyv", feature = "__rkyv",
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)] )]
pub struct AllocatedBytesPtr(pub i32, pub i32); pub struct AllocatedBytesPtr(pub u32, pub u32);
#[cfg(not(feature = "__rkyv"))] #[cfg(not(feature = "__rkyv"))]
fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> { fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> {
@ -31,7 +31,7 @@ fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr>
where where
F: FnOnce(i32) -> i32, F: FnOnce(u32) -> u32,
{ {
// Allocate AllocatedBytesPtr to get return value from the host // Allocate AllocatedBytesPtr to get return value from the host
let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); let allocated_bytes_ptr = AllocatedBytesPtr(0, 0);
@ -78,7 +78,7 @@ pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R> {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R> pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R>
where where
F: FnOnce(i32) -> i32, F: FnOnce(u32) -> u32,
R: rkyv::Archive, R: rkyv::Archive,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>, R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
{ {
@ -110,7 +110,7 @@ pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R> {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R> pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R>
where where
F: FnOnce(i32) -> i32, F: FnOnce(u32) -> u32,
R: rkyv::Archive, R: rkyv::Archive,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>, R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
{ {
@ -138,7 +138,7 @@ where
let allocated_returned_value_ptr: AllocatedBytesPtr = unsafe { let allocated_returned_value_ptr: AllocatedBytesPtr = unsafe {
deserialize_from_ptr( deserialize_from_ptr(
serialized_allocated_bytes_raw_ptr, serialized_allocated_bytes_raw_ptr,
serialized_allocated_bytes_raw_ptr_size as i32, serialized_allocated_bytes_raw_ptr_size as u32,
) )
.expect("Should able to deserialize AllocatedBytesPtr") .expect("Should able to deserialize AllocatedBytesPtr")
}; };

View File

@ -31,11 +31,11 @@ pub struct TransformPluginProgramMetadata {
#[cfg(target_arch = "wasm32")] // Allow testing #[cfg(target_arch = "wasm32")] // Allow testing
extern "C" { extern "C" {
fn __copy_context_key_to_host_env(bytes_ptr: i32, bytes_ptr_len: i32); fn __copy_context_key_to_host_env(bytes_ptr: u32, bytes_ptr_len: u32);
fn __get_transform_plugin_config(allocated_ret_ptr: i32) -> i32; fn __get_transform_plugin_config(allocated_ret_ptr: u32) -> u32;
fn __get_transform_context(key: u32, allocated_ret_ptr: i32) -> i32; fn __get_transform_context(key: u32, allocated_ret_ptr: u32) -> u32;
fn __get_experimental_transform_context(allocated_ret_ptr: i32) -> i32; fn __get_experimental_transform_context(allocated_ret_ptr: u32) -> u32;
fn __get_raw_experiemtal_transform_context(allocated_ret_ptr: i32) -> i32; fn __get_raw_experiemtal_transform_context(allocated_ret_ptr: u32) -> u32;
} }
#[cfg(feature = "__plugin_mode")] #[cfg(feature = "__plugin_mode")]
@ -86,7 +86,7 @@ impl TransformPluginProgramMetadata {
) )
.expect("Should be serializable"); .expect("Should be serializable");
let (key_ptr, key_ptr_len) = serialized.as_ptr(); let (key_ptr, key_ptr_len) = serialized.as_ptr();
__copy_context_key_to_host_env(key_ptr as i32, key_ptr_len as i32); __copy_context_key_to_host_env(key_ptr as u32, key_ptr_len as u32);
__get_experimental_transform_context(serialized_ptr) __get_experimental_transform_context(serialized_ptr)
}); });

View File

@ -22,8 +22,8 @@ extern "C" {
fn __lookup_char_pos_source_map_proxy( fn __lookup_char_pos_source_map_proxy(
byte_pos: u32, byte_pos: u32,
should_include_source_file: i32, should_include_source_file: i32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32; ) -> u32;
fn __doctest_offset_line_proxy(orig: u32) -> u32; fn __doctest_offset_line_proxy(orig: u32) -> u32;
fn __merge_spans_proxy( fn __merge_spans_proxy(
lhs_lo: u32, lhs_lo: u32,
@ -32,28 +32,28 @@ extern "C" {
rhs_lo: u32, rhs_lo: u32,
rhs_hi: u32, rhs_hi: u32,
rhs_ctxt: u32, rhs_ctxt: u32,
allocated_ptr: i32, allocated_ptr: u32,
) -> i32; ) -> u32;
fn __span_to_string_proxy( fn __span_to_string_proxy(
span_lo: u32, span_lo: u32,
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32; ) -> u32;
fn __span_to_filename_proxy( fn __span_to_filename_proxy(
span_lo: u32, span_lo: u32,
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32; ) -> u32;
fn __span_to_lines_proxy( fn __span_to_lines_proxy(
span_lo: u32, span_lo: u32,
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
should_request_source_file: i32, should_request_source_file: i32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32; ) -> u32;
fn __lookup_byte_offset_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; fn __lookup_byte_offset_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32;
} }
#[cfg(feature = "__plugin_mode")] #[cfg(feature = "__plugin_mode")]

View File

@ -20,7 +20,7 @@ pub struct CommentHostEnvironment {
/// Attached imported fn `__alloc` to the hostenvironment to allow any other /// Attached imported fn `__alloc` to the hostenvironment to allow any other
/// imported fn can allocate guest's memory space from host runtime. /// imported fn can allocate guest's memory space from host runtime.
#[wasmer(export(name = "__alloc"))] #[wasmer(export(name = "__alloc"))]
pub alloc_guest_memory: LazyInit<NativeFunc<u32, i32>>, pub alloc_guest_memory: LazyInit<NativeFunc<u32, u32>>,
/// A buffer to `Comment`, or `Vec<Comment>` plugin need to pass to the host /// A buffer to `Comment`, or `Vec<Comment>` plugin need to pass to the host
/// to perform mutable comment operations like `add_leading, or /// to perform mutable comment operations like `add_leading, or
/// add_leading_comments`. This is vec to serialized bytes, doesn't /// add_leading_comments`. This is vec to serialized bytes, doesn't
@ -42,7 +42,7 @@ impl CommentHostEnvironment {
/// Copy given serialized byte into host's comment buffer, subsequent proxy call /// Copy given serialized byte into host's comment buffer, subsequent proxy call
/// in the host can read it. /// in the host can read it.
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn copy_comment_to_host_env(env: &CommentHostEnvironment, bytes_ptr: i32, bytes_ptr_len: i32) { pub fn copy_comment_to_host_env(env: &CommentHostEnvironment, bytes_ptr: u32, bytes_ptr_len: u32) {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
(*env.mutable_comment_buffer.lock()) = (*env.mutable_comment_buffer.lock()) =
copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len);
@ -90,7 +90,7 @@ where
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
fn unwrap_comments_storage_with_env<F, R>(env: &CommentHostEnvironment, f: F, default: R) -> R fn unwrap_comments_storage_with_env<F, R>(env: &CommentHostEnvironment, f: F, default: R) -> R
where where
F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc<u32, i32>) -> R, F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc<u32, u32>) -> R,
{ {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() {
@ -162,7 +162,7 @@ pub fn move_leading_comments_proxy(from_byte_pos: u32, to_byte_pos: u32) {
pub fn take_leading_comments_proxy( pub fn take_leading_comments_proxy(
env: &CommentHostEnvironment, env: &CommentHostEnvironment,
byte_pos: u32, byte_pos: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
unwrap_comments_storage_with_env( unwrap_comments_storage_with_env(
env, env,
@ -197,7 +197,7 @@ pub fn take_leading_comments_proxy(
pub fn get_leading_comments_proxy( pub fn get_leading_comments_proxy(
env: &CommentHostEnvironment, env: &CommentHostEnvironment,
byte_pos: u32, byte_pos: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
unwrap_comments_storage_with_env( unwrap_comments_storage_with_env(
env, env,
@ -266,7 +266,7 @@ pub fn move_trailing_comments_proxy(from_byte_pos: u32, to_byte_pos: u32) {
pub fn take_trailing_comments_proxy( pub fn take_trailing_comments_proxy(
env: &CommentHostEnvironment, env: &CommentHostEnvironment,
byte_pos: u32, byte_pos: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
unwrap_comments_storage_with_env( unwrap_comments_storage_with_env(
env, env,
@ -296,7 +296,7 @@ pub fn take_trailing_comments_proxy(
pub fn get_trailing_comments_proxy( pub fn get_trailing_comments_proxy(
env: &CommentHostEnvironment, env: &CommentHostEnvironment,
byte_pos: u32, byte_pos: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
unwrap_comments_storage_with_env( unwrap_comments_storage_with_env(
env, env,

View File

@ -26,8 +26,8 @@ impl DiagnosticContextHostEnvironment {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn set_plugin_core_pkg_diagnostics( pub fn set_plugin_core_pkg_diagnostics(
env: &DiagnosticContextHostEnvironment, env: &DiagnosticContextHostEnvironment,
bytes_ptr: i32, bytes_ptr: u32,
bytes_ptr_len: i32, bytes_ptr_len: u32,
) { ) {
let memory = env.memory_ref().expect("Memory should be initialized"); let memory = env.memory_ref().expect("Memory should be initialized");
(*env.core_diag_buffer.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); (*env.core_diag_buffer.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len);

View File

@ -6,7 +6,7 @@ use swc_common::{
use crate::{host_environment::BaseHostEnvironment, memory_interop::copy_bytes_into_host}; use crate::{host_environment::BaseHostEnvironment, memory_interop::copy_bytes_into_host};
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len: i32) { pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: u32, bytes_ptr_len: u32) {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
if HANDLER.is_set() { if HANDLER.is_set() {
HANDLER.with(|handler| { HANDLER.with(|handler| {

View File

@ -33,7 +33,7 @@ pub fn mark_is_descendant_of_proxy(
env: &BaseHostEnvironment, env: &BaseHostEnvironment,
self_mark: u32, self_mark: u32,
ancestor: u32, ancestor: u32,
allocated_ptr: i32, allocated_ptr: u32,
) { ) {
let self_mark = Mark::from_u32(self_mark); let self_mark = Mark::from_u32(self_mark);
let ancestor = Mark::from_u32(ancestor); let ancestor = Mark::from_u32(ancestor);
@ -50,7 +50,7 @@ pub fn mark_is_descendant_of_proxy(
} }
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn mark_least_ancestor_proxy(env: &BaseHostEnvironment, a: u32, b: u32, allocated_ptr: i32) { pub fn mark_least_ancestor_proxy(env: &BaseHostEnvironment, a: u32, b: u32, allocated_ptr: u32) {
let a = Mark::from_u32(a); let a = Mark::from_u32(a);
let b = Mark::from_u32(b); let b = Mark::from_u32(b);
@ -76,7 +76,7 @@ pub fn syntax_context_apply_mark_proxy(self_syntax_context: u32, mark: u32) -> u
pub fn syntax_context_remove_mark_proxy( pub fn syntax_context_remove_mark_proxy(
env: &BaseHostEnvironment, env: &BaseHostEnvironment,
self_mark: u32, self_mark: u32,
allocated_ptr: i32, allocated_ptr: u32,
) { ) {
let mut self_mark = SyntaxContext::from_u32(self_mark); let mut self_mark = SyntaxContext::from_u32(self_mark);

View File

@ -16,7 +16,7 @@ pub struct MetadataContextHostEnvironment {
/// Attached imported fn `__alloc` to the hostenvironment to allow any other /// Attached imported fn `__alloc` to the hostenvironment to allow any other
/// imported fn can allocate guest's memory space from host runtime. /// imported fn can allocate guest's memory space from host runtime.
#[wasmer(export(name = "__alloc"))] #[wasmer(export(name = "__alloc"))]
pub alloc_guest_memory: LazyInit<NativeFunc<u32, i32>>, pub alloc_guest_memory: LazyInit<NativeFunc<u32, u32>>,
pub metadata_context: Arc<TransformPluginMetadataContext>, pub metadata_context: Arc<TransformPluginMetadataContext>,
pub transform_plugin_config: Option<serde_json::Value>, pub transform_plugin_config: Option<serde_json::Value>,
/// A buffer to string key to the context plugin need to pass to the host. /// A buffer to string key to the context plugin need to pass to the host.
@ -44,8 +44,8 @@ impl MetadataContextHostEnvironment {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn copy_context_key_to_host_env( pub fn copy_context_key_to_host_env(
env: &MetadataContextHostEnvironment, env: &MetadataContextHostEnvironment,
bytes_ptr: i32, bytes_ptr: u32,
bytes_ptr_len: i32, bytes_ptr_len: u32,
) { ) {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
(*env.mutable_context_key_buffer.lock()) = (*env.mutable_context_key_buffer.lock()) =
@ -56,7 +56,7 @@ pub fn copy_context_key_to_host_env(
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn get_transform_plugin_config( pub fn get_transform_plugin_config(
env: &MetadataContextHostEnvironment, env: &MetadataContextHostEnvironment,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() {
@ -88,7 +88,7 @@ pub fn get_transform_plugin_config(
pub fn get_transform_context( pub fn get_transform_context(
env: &MetadataContextHostEnvironment, env: &MetadataContextHostEnvironment,
key: u32, key: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() {
@ -117,7 +117,7 @@ pub fn get_transform_context(
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn get_experimental_transform_context( pub fn get_experimental_transform_context(
env: &MetadataContextHostEnvironment, env: &MetadataContextHostEnvironment,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() {
@ -153,7 +153,7 @@ pub fn get_experimental_transform_context(
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn get_raw_experiemtal_transform_context( pub fn get_raw_experiemtal_transform_context(
env: &MetadataContextHostEnvironment, env: &MetadataContextHostEnvironment,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let experimental_context = &env.metadata_context.experimental; let experimental_context = &env.metadata_context.experimental;

View File

@ -33,8 +33,8 @@ impl TransformResultHostEnvironment {
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn set_transform_result( pub fn set_transform_result(
env: &TransformResultHostEnvironment, env: &TransformResultHostEnvironment,
bytes_ptr: i32, bytes_ptr: u32,
bytes_ptr_len: i32, bytes_ptr_len: u32,
) { ) {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
(*env.transform_result.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); (*env.transform_result.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len);

View File

@ -19,7 +19,7 @@ pub struct SourceMapHostEnvironment {
/// Attached imported fn `__alloc` to the hostenvironment to allow any other /// Attached imported fn `__alloc` to the hostenvironment to allow any other
/// imported fn can allocate guest's memory space from host runtime. /// imported fn can allocate guest's memory space from host runtime.
#[wasmer(export(name = "__alloc"))] #[wasmer(export(name = "__alloc"))]
pub alloc_guest_memory: LazyInit<NativeFunc<u32, i32>>, pub alloc_guest_memory: LazyInit<NativeFunc<u32, u32>>,
pub source_map: Arc<Mutex<Arc<SourceMap>>>, pub source_map: Arc<Mutex<Arc<SourceMap>>>,
/// A buffer to non-determined size of return value from the host. /// A buffer to non-determined size of return value from the host.
pub mutable_source_map_buffer: Arc<Mutex<Vec<u8>>>, pub mutable_source_map_buffer: Arc<Mutex<Vec<u8>>>,
@ -47,7 +47,7 @@ pub fn lookup_char_pos_proxy(
env: &SourceMapHostEnvironment, env: &SourceMapHostEnvironment,
byte_pos: u32, byte_pos: u32,
should_include_source_file: i32, should_include_source_file: i32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let original_loc = (env.source_map.lock()).lookup_char_pos(BytePos(byte_pos)); let original_loc = (env.source_map.lock()).lookup_char_pos(BytePos(byte_pos));
@ -96,7 +96,7 @@ pub fn merge_spans_proxy(
rhs_lo: u32, rhs_lo: u32,
rhs_hi: u32, rhs_hi: u32,
rhs_ctxt: u32, rhs_ctxt: u32,
allocated_ptr: i32, allocated_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let sp_lhs = Span { let sp_lhs = Span {
@ -132,7 +132,7 @@ pub fn span_to_lines_proxy(
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
should_request_source_file: i32, should_request_source_file: i32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let span = Span { let span = Span {
@ -175,7 +175,7 @@ pub fn span_to_lines_proxy(
pub fn lookup_byte_offset_proxy( pub fn lookup_byte_offset_proxy(
env: &SourceMapHostEnvironment, env: &SourceMapHostEnvironment,
byte_pos: u32, byte_pos: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let byte_pos = BytePos(byte_pos); let byte_pos = BytePos(byte_pos);
@ -206,7 +206,7 @@ pub fn span_to_string_proxy(
span_lo: u32, span_lo: u32,
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let span = Span { let span = Span {
@ -240,7 +240,7 @@ pub fn span_to_filename_proxy(
span_lo: u32, span_lo: u32,
span_hi: u32, span_hi: u32,
span_ctxt: u32, span_ctxt: u32,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
) -> i32 { ) -> i32 {
if let Some(memory) = env.memory_ref() { if let Some(memory) = env.memory_ref() {
let span = Span { let span = Span {

View File

@ -3,7 +3,7 @@ use swc_plugin_proxy::AllocatedBytesPtr;
use wasmer::{Array, Memory, NativeFunc, WasmPtr}; use wasmer::{Array, Memory, NativeFunc, WasmPtr};
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn copy_bytes_into_host(memory: &Memory, bytes_ptr: i32, bytes_ptr_len: i32) -> Vec<u8> { pub fn copy_bytes_into_host(memory: &Memory, bytes_ptr: u32, bytes_ptr_len: u32) -> Vec<u8> {
let ptr: WasmPtr<u8, Array> = WasmPtr::new(bytes_ptr as _); let ptr: WasmPtr<u8, Array> = WasmPtr::new(bytes_ptr as _);
// Deref & read through plugin's wasm memory space via returned ptr // Deref & read through plugin's wasm memory space via returned ptr
@ -25,19 +25,22 @@ pub fn write_into_memory_view<F>(
memory: &Memory, memory: &Memory,
serialized_bytes: &PluginSerializedBytes, serialized_bytes: &PluginSerializedBytes,
get_allocated_ptr: F, get_allocated_ptr: F,
) -> (i32, i32) ) -> (u32, u32)
where where
F: Fn(usize) -> i32, F: Fn(usize) -> u32,
{ {
let serialized_len = serialized_bytes.as_ptr().1; let serialized_len = serialized_bytes.as_ptr().1;
let ptr_start = get_allocated_ptr(serialized_len); let ptr_start: u32 = get_allocated_ptr(serialized_len);
let ptr_start_size = ptr_start let ptr_start_size: u32 = ptr_start
.try_into() .try_into()
.expect("Should be able to convert to usize"); .unwrap_or_else(|_| panic!("Should be able to convert the value {} to u32", ptr_start));
let serialized_len_size: u32 = serialized_len let serialized_len_size: u32 = serialized_len.try_into().unwrap_or_else(|_| {
.try_into() panic!(
.expect("Should be able to convert to u32"); "Should be able to convert the value {} to u32",
serialized_len
)
});
// Note: it's important to get a view from memory _after_ alloc completes // Note: it's important to get a view from memory _after_ alloc completes
let view = memory.view::<u8>(); let view = memory.view::<u8>();
@ -69,11 +72,11 @@ where
#[tracing::instrument(level = "info", skip_all)] #[tracing::instrument(level = "info", skip_all)]
pub fn allocate_return_values_into_guest( pub fn allocate_return_values_into_guest(
memory: &Memory, memory: &Memory,
alloc_guest_memory: &NativeFunc<u32, i32>, alloc_guest_memory: &NativeFunc<u32, u32>,
allocated_ret_ptr: i32, allocated_ret_ptr: u32,
serialized_bytes: &PluginSerializedBytes, serialized_bytes: &PluginSerializedBytes,
) { ) {
let serialized_bytes_len = serialized_bytes.as_ptr().1; let serialized_bytes_len: usize = serialized_bytes.as_ptr().1;
let (allocated_ptr, allocated_ptr_len) = let (allocated_ptr, allocated_ptr_len) =
write_into_memory_view(memory, serialized_bytes, |_| { write_into_memory_view(memory, serialized_bytes, |_| {

View File

@ -21,16 +21,16 @@ use crate::memory_interop::write_into_memory_view;
/// A struct encapsule executing a plugin's transform interop to its teardown /// A struct encapsule executing a plugin's transform interop to its teardown
pub struct TransformExecutor { pub struct TransformExecutor {
// Main transform interface plugin exports // Main transform interface plugin exports
exported_plugin_transform: wasmer::NativeFunc<(i32, i32, u32, i32), i32>, exported_plugin_transform: wasmer::NativeFunc<(u32, u32, u32, u32), u32>,
// `__free` function automatically exported via swc_plugin sdk to allow deallocation in guest // `__free` function automatically exported via swc_plugin sdk to allow deallocation in guest
// memory space // memory space
exported_plugin_free: wasmer::NativeFunc<(i32, i32), i32>, exported_plugin_free: wasmer::NativeFunc<(u32, u32), u32>,
// `__alloc` function automatically exported via swc_plugin sdk to allow allocation in guest // `__alloc` function automatically exported via swc_plugin sdk to allow allocation in guest
// memory space // memory space
exported_plugin_alloc: wasmer::NativeFunc<u32, i32>, exported_plugin_alloc: wasmer::NativeFunc<u32, u32>,
instance: Instance, instance: Instance,
// Reference to the pointers successfully allocated which'll be freed by Drop. // Reference to the pointers successfully allocated which'll be freed by Drop.
allocated_ptr_vec: Vec<(i32, i32)>, allocated_ptr_vec: Vec<(u32, u32)>,
transform_result: Arc<Mutex<Vec<u8>>>, transform_result: Arc<Mutex<Vec<u8>>>,
// diagnostic metadata for the swc_core plugin binary uses. // diagnostic metadata for the swc_core plugin binary uses.
pub plugin_core_diag: PluginCorePkgDiagnostics, pub plugin_core_diag: PluginCorePkgDiagnostics,
@ -81,15 +81,15 @@ impl TransformExecutor {
let tracker = TransformExecutor { let tracker = TransformExecutor {
exported_plugin_transform: instance exported_plugin_transform: instance
.exports .exports
.get_native_function::<(i32, i32, u32, i32), i32>( .get_native_function::<(u32, u32, u32, u32), u32>(
"__transform_plugin_process_impl", "__transform_plugin_process_impl",
)?, )?,
exported_plugin_free: instance exported_plugin_free: instance
.exports .exports
.get_native_function::<(i32, i32), i32>("__free")?, .get_native_function::<(u32, u32), u32>("__free")?,
exported_plugin_alloc: instance exported_plugin_alloc: instance
.exports .exports
.get_native_function::<u32, i32>("__alloc")?, .get_native_function::<u32, u32>("__alloc")?,
instance, instance,
allocated_ptr_vec: Vec::with_capacity(3), allocated_ptr_vec: Vec::with_capacity(3),
transform_result, transform_result,
@ -105,7 +105,7 @@ impl TransformExecutor {
fn write_bytes_into_guest( fn write_bytes_into_guest(
&mut self, &mut self,
serialized_bytes: &PluginSerializedBytes, serialized_bytes: &PluginSerializedBytes,
) -> Result<(i32, i32), Error> { ) -> Result<(u32, u32), Error> {
let memory = self.instance.exports.get_memory("memory")?; let memory = self.instance.exports.get_memory("memory")?;
let ptr = write_into_memory_view(memory, serialized_bytes, |serialized_len| { let ptr = write_into_memory_view(memory, serialized_bytes, |serialized_len| {
@ -122,7 +122,7 @@ impl TransformExecutor {
/// bytes. /// bytes.
fn read_transformed_result_bytes_from_guest( fn read_transformed_result_bytes_from_guest(
&mut self, &mut self,
returned_ptr_result: i32, returned_ptr_result: u32,
) -> Result<PluginSerializedBytes, Error> { ) -> Result<PluginSerializedBytes, Error> {
let transformed_result = &(*self.transform_result.lock()); let transformed_result = &(*self.transform_result.lock());
let ret = PluginSerializedBytes::from_slice(&transformed_result[..]); let ret = PluginSerializedBytes::from_slice(&transformed_result[..]);
@ -187,7 +187,7 @@ impl TransformExecutor {
unresolved_mark: swc_common::Mark, unresolved_mark: swc_common::Mark,
should_enable_comments_proxy: bool, should_enable_comments_proxy: bool,
) -> Result<PluginSerializedBytes, Error> { ) -> Result<PluginSerializedBytes, Error> {
let should_enable_comments_proxy = i32::from(should_enable_comments_proxy); let should_enable_comments_proxy = u32::from(should_enable_comments_proxy);
let guest_program_ptr = self.write_bytes_into_guest(program)?; let guest_program_ptr = self.write_bytes_into_guest(program)?;
let result = self.exported_plugin_transform.call( let result = self.exported_plugin_transform.call(