Macro deno_core::extension

source ·
macro_rules! extension {
    (
    $name:ident
    $(, deps = [ $( $dep:ident ),* ] )?
    $(, parameters = [ $( $param:ident : $type:ident ),+ ] )?
    $(, bounds = [ $( $bound:path : $bound_type:ident ),+ ] )?
    $(, ops_fn = $ops_symbol:ident $( < $ops_param:ident > )? )?
    $(, ops = [ $( $(#[$m:meta])* $( $op:ident )::+ $( < $( $op_param:ident ),* > )?  ),+ $(,)? ] )?
    $(, esm_entry_point = $esm_entry_point:expr )?
    $(, esm = [ $($esm:tt)* ] )?
    $(, lazy_loaded_esm = [ $($lazy_loaded_esm:tt)* ] )?
    $(, js = [ $($js:tt)* ] )?
    $(, options = { $( $options_id:ident : $options_type:ty ),* $(,)? } )?
    $(, middleware = $middleware_fn:expr )?
    $(, state = $state_fn:expr )?
    $(, global_template_middleware = $global_template_middleware_fn:expr )?
    $(, global_object_middleware = $global_object_middleware_fn:expr )?
    $(, external_references = [ $( $external_reference:expr ),* $(,)? ] )?
    $(, customizer = $customizer_fn:expr )?
    $(, docs = $($docblocks:expr),+)?
    $(,)?
  ) => { ... };
    (! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? config = { $( $options_id:ident : $options_type:ty ),* } $( state_fn = $state_fn:expr )? ) => { ... };
    (! __config__ $ext:ident $( parameters = [ $( $param:ident : $type:ident ),+ ] )? $( state_fn = $state_fn:expr )? ) => { ... };
    (! __ops__ $ext:ident __eot__) => { ... };
    (! __ops__ $ext:ident $ops_symbol:ident __eot__) => { ... };
    (! __ops__ $ext:ident $ops_symbol:ident < $ops_param:ident > __eot__) => { ... };
}
Expand description

Defines a Deno extension. The first parameter is the name of the extension symbol namespace to create. This is the symbol you will use to refer to the extension.

Most extensions will define a combination of ops and ESM files, like so:

#[op]
fn op_xyz() {
}

deno_core::extension!(
  my_extension,
  ops = [ op_xyz ],
  esm = [ "my_script.js" ],
  docs = "A small sample extension"
);

The following options are available for the [extension] macro:

  • deps: a comma-separated list of module dependencies, eg: deps = [ my_other_extension ]
  • parameters: a comma-separated list of parameters and base traits, eg: parameters = [ P: MyTrait ]
  • bounds: a comma-separated list of additional type bounds, eg: bounds = [ P::MyAssociatedType: MyTrait ]
  • ops: a comma-separated list of OpDecls to provide, eg: ops = [ op_foo, op_bar ]
  • esm: a comma-separated list of ESM module filenames (see [include_js_files]), eg: esm = [ dir "dir", "my_file.js" ]
  • lazy_loaded_esm: a comma-separated list of ESM module filenames (see [include_js_files]), that will be included in the produced binary, but not automatically evaluated. Eg: lazy_loaded_esm = [ dir "dir", "my_file.js" ]
  • js: a comma-separated list of JS filenames (see [include_js_files]), eg: js = [ dir "dir", "my_file.js" ]
  • config: a structure-like definition for configuration parameters which will be required when initializing this extension, eg: config = { my_param: Option<usize> }
  • middleware: an OpDecl middleware function with the signature fn (OpDecl) -> OpDecl
  • state: a state initialization function, with the signature fn (&mut OpState, ...) -> (), where ... are parameters matching the fields of the config struct
  • global_template_middleware: a global template middleware function (see Extension::global_template_middleware)
  • global_object_middleware: a global object middleware function (see Extension::global_object_middleware)
  • docs: comma separated list of toplevel #[doc=…] tags to be applied to the extension’s resulting struct