fix(plugin): Allow testing (#3542)

This commit is contained in:
Donny/강동윤 2022-02-12 15:53:41 +09:00 committed by GitHub
parent 72b31977e4
commit fbe91963bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -1,5 +1,6 @@
use swc_common::plugin::Serialized;
#[cfg(target_arch = "wasm32")] // Allow testing
extern "C" {
fn __emit_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32);
fn __free(bytes_ptr: i32, size: i32) -> i32;
@ -13,14 +14,16 @@ extern "C" {
pub struct PluginDiagnosticsEmitter {}
impl swc_common::errors::Emitter for PluginDiagnosticsEmitter {
#[cfg_attr(not(target_arch = "wasm32"), allow(unused))]
fn emit(&mut self, db: &swc_common::errors::DiagnosticBuilder<'_>) {
let diag =
Serialized::serialize(&*db.diagnostic).expect("Should able to serialize Diagnostic");
let diag_ref = diag.as_ref();
let ptr = diag_ref.as_ptr() as _;
let ptr = diag_ref.as_ptr() as i32;
let len = diag_ref.len() as i32;
#[cfg(target_arch = "wasm32")] // Allow testing
unsafe {
__emit_diagnostics(ptr, len);
}

View File

@ -26,6 +26,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
// Declaration for imported function from swc host.
// Refer swc_plugin_runner for the actual implementation.
#[cfg(target_arch = "wasm32")] // Allow testing
extern "C" {
fn __set_transform_result(bytes_ptr: i32, bytes_ptr_len: i32);
fn __free(bytes_ptr: i32, size: i32) -> i32;
@ -36,6 +37,7 @@ fn handle_func(func: ItemFn) -> TokenStream {
/// When guest calls __set_transform_result host should've completed read guest's memory and allocates its byte
/// into host's enviroment so guest can free its memory later.
fn set_transform_result_volatile(bytes_ptr: i32, bytes_ptr_len: i32) {
#[cfg(target_arch = "wasm32")] // Allow testing
unsafe {
__set_transform_result(bytes_ptr, bytes_ptr_len);
__free(bytes_ptr, bytes_ptr_len);