Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation options for extension!() #232

Merged
merged 24 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
23af4da
Add documentation option for extension!
rscarson Sep 22, 2023
223cde5
Merge pull request #1 from denoland/main
rscarson Sep 22, 2023
f5594ef
Fix fmt
rscarson Sep 22, 2023
003073f
Merge branch 'main' of https://github.com/rscarson/deno_core into main
rscarson Sep 22, 2023
c2cc557
Fix fmt
rscarson Sep 22, 2023
34bb3d4
Fix fmt
rscarson Sep 22, 2023
04b9d30
Fix fmt
rscarson Sep 22, 2023
fb405e8
Merge pull request #2 from denoland/main
rscarson Sep 22, 2023
1b65766
give more details in the docs
rscarson Sep 22, 2023
f3b8fc6
Merge branch 'main' of https://github.com/rscarson/deno_core into main
rscarson Sep 22, 2023
72cb806
format didn't like the space in my docblock
rscarson Sep 22, 2023
41c45df
Update core/examples/op2.rs
rscarson Sep 23, 2023
bd7a87f
Update docblock as per recommendation
rscarson Sep 23, 2023
3a5f8bd
Merge branch 'main' of https://github.com/rscarson/deno_core into main
rscarson Sep 23, 2023
4ccb502
update docblock, fix fmt
rscarson Sep 25, 2023
6a1d029
Update core/extensions.rs
rscarson Sep 25, 2023
c2d298b
Update core/examples/op2.rs
rscarson Sep 25, 2023
1964647
Update extensions.rs
rscarson Sep 25, 2023
9d4df85
Update extensions.rs
rscarson Sep 25, 2023
7a5df1b
Update extensions.rs
rscarson Sep 25, 2023
331f2c7
Fix example in extensions
rscarson Sep 25, 2023
468006e
Update core/extensions.rs
rscarson Sep 26, 2023
5e16d1f
Update core/extensions.rs
rscarson Sep 26, 2023
977acd1
Mark js_only as depr
rscarson Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/examples/op2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
globalThis.op2_sample = {
"use_state": (f) => Deno.core.ops.op_use_state(f),
};
68 changes: 68 additions & 0 deletions core/examples/op2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::Context;
use deno_core::anyhow::Error;
use deno_core::extension;
use deno_core::op2;
use deno_core::resolve_path;
use deno_core::v8;
use deno_core::FsModuleLoader;
use deno_core::JsRuntime;
use deno_core::OpState;
use std::rc::Rc;

#[op2]
fn op_use_state(
state: &mut OpState,
#[global] callback: v8::Global<v8::Function>,
) -> Result<(), Error> {
state.put(callback);
Ok(())
}

extension!(
op2_sample,
ops = [op_use_state],
esm_entry_point = "ext:op2_sample/op2.js",
esm = [ dir "examples", "op2.js" ],
docs = "A small example demonstrating op2 usage", "Contains one op"
rscarson marked this conversation as resolved.
Show resolved Hide resolved
);

fn main() -> Result<(), Error> {
let module_name = "test.js";
let module_code = "
op2_sample.use_state(() => {
console.log('Hello World');
});
"
.to_string();

let mut js_runtime = JsRuntime::new(deno_core::RuntimeOptions {
module_loader: Some(Rc::new(FsModuleLoader)),
extensions: vec![op2_sample::init_ops_and_esm()],
..Default::default()
});

let main_module = resolve_path(
module_name,
&std::env::current_dir()
.context("Unable to get current working directory")?,
)?;

let future = async move {
let mod_id = js_runtime
.load_main_module(
&main_module,
Some(deno_core::FastString::from(module_code)),
)
.await?;

let result = js_runtime.mod_evaluate(mod_id);
js_runtime.run_event_loop(false).await?;
result.await?
};

tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(future)
}
32 changes: 29 additions & 3 deletions core/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ macro_rules! or {
/// my_extension,
/// ops = [ op_xyz ],
/// esm = [ "my_script.js" ],
/// docs = "A small sample extension"
/// );
/// ```
///
Expand All @@ -249,6 +250,7 @@ macro_rules! or {
/// * event_loop_middleware: an event-loop middleware function (see [`ExtensionBuilder::event_loop_middleware`])
/// * global_template_middleware: a global template middleware function (see [`ExtensionBuilder::global_template_middleware`])
/// * global_object_middleware: a global object middleware function (see [`ExtensionBuilder::global_object_middleware`])
/// * docs: comma separated list of toplevel #[doc=...] tags to be applied to the extension's resulting struct
#[macro_export]
macro_rules! extension {
(
Expand All @@ -269,11 +271,20 @@ macro_rules! extension {
$(, global_object_middleware = $global_object_middleware_fn:expr )?
$(, external_references = [ $( $external_reference:expr ),* $(,)? ] )?
$(, customizer = $customizer_fn:expr )?
$(, docs = $($docblocks:expr),+)?
$(,)?
) => {
/// Extension struct for
#[doc = stringify!($name)]
/// .
$( $(#[doc = $docblocks])+ )?
/// An extension for use with the Deno JS runtime
/// To use it, provide it as an argument when instantiating your runtime:
///
/// ```rust
#[doc = concat!("let mut extensions = vec![", stringify!($name), "::init_ops_and_esm()];")]
rscarson marked this conversation as resolved.
Show resolved Hide resolved
/// let mut js_runtime = JsRuntime::new(RuntimeOptions {
/// extensions
/// })
/// ```
///
#[allow(non_camel_case_types)]
pub struct $name {
}
Expand Down Expand Up @@ -358,6 +369,11 @@ macro_rules! extension {
}

#[allow(dead_code)]
/// Initialize this extension for use with CommonJS
/// For modules, use init_ops_and_esm instead
///
/// # Returns
/// an Extension object that can be used during instantiation of a JsRuntime
nayeemrmn marked this conversation as resolved.
Show resolved Hide resolved
pub fn init_js_only $( < $( $param : $type + 'static ),* > )? () -> $crate::Extension
Copy link
Collaborator

@nayeemrmn nayeemrmn Sep 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should leave this one undocumented, or even mark it as deprecated. We don't use it in CLI or anywhere I know of. And I don't know when you'd use it. It might be a legacy thing. It seems to just not execute the state fn and middleware.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      /// Legacy function for extension instantiation.
      /// Please use `init_ops_and_esm` or `init_ops` instead
      ///
      /// # Returns
      /// an Extension object that can be used during instantiation of a JsRuntime
      #[deprecated(since="0.216.0", note="please use `init_ops_and_esm` or `init_ops` instead")]

Added deprecation notice, and altered documentation to reflect the change

$( where $( $bound : $bound_type ),+ )?
{
Expand All @@ -368,6 +384,11 @@ macro_rules! extension {
}

#[allow(dead_code)]
/// Initialize this extension for use with ES modules
/// For CommonJS, use init_js_only instead
rscarson marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Returns
/// an Extension object that can be used during instantiation of a JsRuntime
pub fn init_ops_and_esm $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension
$( where $( $bound : $bound_type ),+ )?
{
Expand All @@ -379,6 +400,11 @@ macro_rules! extension {
}

#[allow(dead_code)]
/// Initialize this extension's OPs
/// See[OP2](https://docs.rs/deno_core/latest/deno_core/attr.op2.html)
rscarson marked this conversation as resolved.
Show resolved Hide resolved
///
/// # Returns
/// an Extension object that can be used during instantiation of a JsRuntime
pub fn init_ops $( < $( $param : $type + 'static ),+ > )? ( $( $( $options_id : $options_type ),* )? ) -> $crate::Extension
$( where $( $bound : $bound_type ),+ )?
{
Expand Down