Struct OnBeforeParse

Source
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>

Source

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,
  };
}
Source

pub fn path(&self) -> PluginResult<Cow<'_, str>>

Source

pub fn namespace(&self) -> PluginResult<Cow<'_, str>>

Source

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;
    },
};
Source

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.

Source

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.

Source

pub fn set_output_source_code(&mut self, source: String, loader: BunLoader)

Set the output source code for the current file.

Source

pub fn set_output_loader(&self, loader: BunLoader)

Set the output loader for the current file.

Source

pub fn output_loader(&self) -> BunLoader

Get the output loader for the current file.

Source

pub fn log_error(&self, message: &str)

Log an error message.

Source

pub fn log(&self, message: &str, level: BunLogLevel)

Log a message with the given level.

Auto Trait Implementations§

§

impl<'a> Freeze for OnBeforeParse<'a>

§

impl<'a> RefUnwindSafe for OnBeforeParse<'a>

§

impl<'a> !Send for OnBeforeParse<'a>

§

impl<'a> !Sync for OnBeforeParse<'a>

§

impl<'a> Unpin for OnBeforeParse<'a>

§

impl<'a> UnwindSafe for OnBeforeParse<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.