pub struct OnBeforeParse<'a> {
pub args_raw: *mut OnBeforeParseArguments,
/* private fields */
}
Expand description
A safe handle for the arguments + result struct for the
OnBeforeParse
bundler lifecycle hook.
This struct acts as a safe wrapper around the raw C API structs
(sys::OnBeforeParseArguments
/sys::OnBeforeParseResult
) needed to
implement the OnBeforeParse
bundler lifecycle hook.
To initialize this struct, see the from_raw
method.
Fields§
§args_raw: *mut OnBeforeParseArguments
Implementations§
Source§impl<'a> OnBeforeParse<'a>
impl<'a> OnBeforeParse<'a>
Sourcepub fn from_raw(
args: *mut OnBeforeParseArguments,
result: *mut OnBeforeParseResult,
) -> PluginResult<Self>
pub fn from_raw( args: *mut OnBeforeParseArguments, result: *mut OnBeforeParseResult, ) -> PluginResult<Self>
Initialize this struct from references to their raw counterparts.
This function will do a versioning check to ensure that the plugin is compatible with the current version of Bun. If the plugin is not compatible, it will log an error and return an error result.
§Example
extern "C" fn on_before_parse_impl(args: *const sys::OnBeforeParseArguments, result: *mut sys::OnBeforeParseResult) {
let args = unsafe { &*args };
let result = unsafe { &mut *result };
let handle = match OnBeforeParse::from_raw(args, result) {
Ok(handle) => handle,
Err(()) => return,
};
}
pub fn path(&self) -> PluginResult<Cow<'_, str>>
pub fn namespace(&self) -> PluginResult<Cow<'_, str>>
Sourcepub unsafe fn external<'b, T: 'static + Sync>(
&self,
from_raw: unsafe fn(*mut c_void) -> Option<&'b T>,
) -> PluginResult<Option<&'b T>>
pub unsafe fn external<'b, T: 'static + Sync>( &self, from_raw: unsafe fn(*mut c_void) -> Option<&'b T>, ) -> PluginResult<Option<&'b T>>
§Safety
This is unsafe as you must ensure that no other invocation of the plugin (or JS!) simultaneously holds a mutable reference to the external.
Get the external object from the OnBeforeParse
arguments.
The external object is set by the plugin definition inside of JS:
await Bun.build({
plugins: [
{
name: "my-plugin",
setup(builder) {
const native_plugin = require("./native_plugin.node");
const external = native_plugin.createExternal();
builder.external({ napiModule: native_plugin, symbol: 'onBeforeParse', external });
},
},
],
});
The external object must be created from NAPI for this function to be safe!
This function will return an error if the external object is not a valid tagged object for the given type.
This function will return Ok(None)
if there is no external object
set.
§Example
The code to create the external from napi-rs:
#[no_mangle]
#[napi]
pub fn create_my_external() -> External<MyStruct> {
let external = External::new(MyStruct::new());
external
}
The code to extract the external:
let external = match handle.external::<MyStruct>() {
Ok(Some(external)) => external,
_ => {
handle.log_error("Could not get external object.");
return;
},
};
Sourcepub unsafe fn external_mut<'b, T: 'static + Sync>(
&mut self,
from_raw: unsafe fn(*mut c_void) -> Option<&'b mut T>,
) -> PluginResult<Option<&'b mut T>>
pub unsafe fn external_mut<'b, T: 'static + Sync>( &mut self, from_raw: unsafe fn(*mut c_void) -> Option<&'b mut T>, ) -> PluginResult<Option<&'b mut T>>
The same as [crate::bun_native_plugin::OnBeforeParse::external
], but returns a mutable reference.
§Safety
This is unsafe as you must ensure that no other invocation of the plugin (or JS!) simultaneously holds a mutable reference to the external.
Sourcepub fn input_source_code(&self) -> PluginResult<Cow<'_, str>>
pub fn input_source_code(&self) -> PluginResult<Cow<'_, str>>
Get the input source code for the current file.
On Windows, this function may return an Err(Error::Utf8(...))
if the
source code contains invalid UTF-8.
Sourcepub fn set_output_source_code(&mut self, source: String, loader: BunLoader)
pub fn set_output_source_code(&mut self, source: String, loader: BunLoader)
Set the output source code for the current file.
Sourcepub fn set_output_loader(&self, loader: BunLoader)
pub fn set_output_loader(&self, loader: BunLoader)
Set the output loader for the current file.
Sourcepub fn output_loader(&self) -> BunLoader
pub fn output_loader(&self) -> BunLoader
Get the output loader for the current file.
Sourcepub fn log(&self, message: &str, level: BunLogLevel)
pub fn log(&self, message: &str, level: BunLogLevel)
Log a message with the given level.