swc_plugin_runner/
plugin_module_bytes.rs

1use serde::{Deserialize, Serialize};
2
3use crate::runtime;
4
5// A trait abstracts plugin's wasm compilation and instantiation.
6// Depends on the caller, this could be a simple clone from existing module, or
7// load from file system cache.
8pub trait PluginModuleBytes {
9    // Returns a name to the module, typically either path to the plugin or its
10    // package name.
11    fn get_module_name(&self) -> &str;
12    // Returns a compiled wasmer::Module for the plugin module.
13    fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module;
14}
15
16/// A struct for the plugin contains raw bytes can be compiled into Wasm Module.
17#[derive(Debug, Clone, Eq, Serialize, Deserialize, PartialEq)]
18pub struct RawPluginModuleBytes {
19    plugin_name: String,
20    bytes: Vec<u8>,
21}
22
23impl PluginModuleBytes for RawPluginModuleBytes {
24    fn get_module_name(&self) -> &str {
25        &self.plugin_name
26    }
27
28    fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module {
29        let cache = rt.prepare_module(&self.bytes).unwrap();
30        runtime::Module::Cache(cache)
31    }
32}
33
34impl RawPluginModuleBytes {
35    pub fn new(identifier: String, bytes: Vec<u8>) -> Self {
36        Self {
37            plugin_name: identifier,
38            bytes,
39        }
40    }
41}
42
43/// A struct for the plugin contains pre-compiled binary.
44/// This is for the cases would like to reuse the compiled module, or either
45/// load from FileSystemCache.
46#[derive(Serialize, Deserialize)]
47pub struct CompiledPluginModuleBytes {
48    plugin_name: String,
49    #[serde(skip)]
50    cache: Option<runtime::ModuleCache>,
51}
52
53impl CompiledPluginModuleBytes {
54    pub fn new(identifier: String, cache: runtime::ModuleCache) -> Self {
55        Self {
56            plugin_name: identifier,
57            cache: Some(cache),
58        }
59    }
60
61    // Allow to `pre` compile wasm module when there is a raw bytes, want to avoid
62    // to skip the compilation step per each trasform.
63    pub fn from_raw_module(rt: &dyn runtime::Runtime, raw: RawPluginModuleBytes) -> Self {
64        let cache = rt.prepare_module(&raw.bytes).unwrap();
65        CompiledPluginModuleBytes {
66            plugin_name: raw.plugin_name,
67            cache: Some(cache),
68        }
69    }
70
71    pub fn clone_module(&self, builder: &dyn runtime::Runtime) -> Self {
72        CompiledPluginModuleBytes {
73            plugin_name: self.plugin_name.clone(),
74            cache: self
75                .cache
76                .as_ref()
77                .map(|cache| builder.clone_cache(cache).unwrap()),
78        }
79    }
80}
81
82impl PluginModuleBytes for CompiledPluginModuleBytes {
83    fn get_module_name(&self) -> &str {
84        &self.plugin_name
85    }
86
87    fn compile_module(&self, rt: &dyn runtime::Runtime) -> runtime::Module {
88        let cache = self.cache.as_ref().unwrap();
89        let cache = rt.clone_cache(cache).unwrap();
90        runtime::Module::Cache(cache)
91    }
92}