Skip to content

Commit

Permalink
Further renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Nov 30, 2024
1 parent ece6093 commit 7314eb0
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 69 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true

- name: Setup Rust
uses: dtolnay/rust-toolchain@315e265cd78dad1e1dcf3a5074f6d6c47029d5aa
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
publish:
if: github.repository == 'rquickjs/rquickjs-module'
if: github.repository == 'rquickjs/rquickjs-extension'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rquickjs-extension"
repository = "https://github.com/rquickjs/rquickjs-module"
repository = "https://github.com/rquickjs/rquickjs-extension"
description = "An extension system for rquickjs"
license = "MIT"
authors = [
Expand Down
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# rquickjs module
# rquickjs extension

[![github](https://img.shields.io/badge/github-rquickjs/rquickjs-module.svg?style=for-the-badge&logo=github)](https://github.com/rquickjs/rquickjs-module)
[![crates](https://img.shields.io/crates/v/rquickjs-module.svg?style=for-the-badge&color=fc8d62&logo=rust)](https://crates.io/crates/rquickjs-module)
[![github](https://img.shields.io/badge/github-rquickjs/rquickjs-extension.svg?style=for-the-badge&logo=github)](https://github.com/rquickjs/rquickjs-extension)
[![crates](https://img.shields.io/crates/v/rquickjs-extension.svg?style=for-the-badge&color=fc8d62&logo=rust)](https://crates.io/crates/rquickjs-extension)

This is an extension to [rquickjs](https://github.com/DelSkayn/rquickjs) to allow the ecosystem to create more unified Rust modules.
This is a complement to [rquickjs](https://github.com/DelSkayn/rquickjs) to allow the ecosystem to create more unified Rust extensions.

The goal was to create a better version of [`ModuleDef`](https://docs.rs/rquickjs/latest/rquickjs/module/trait.ModuleDef.html) that would allow it to have options as input and set global.
The goal was to create a more generic version of [`ModuleDef`](https://docs.rs/rquickjs/latest/rquickjs/module/trait.ModuleDef.html) that would allow it to have options and/or set global values.

For example, a `fetch` module using `ModuleDefExt` could set a global `fetch` function and have as options an allowlist of domains.
For example, a `fetch` extension could set a global `fetch` function and have as options an allowlist of domains.

## Using modules
## Using extensions

If you are a consumer of modules create using that crate, here is how you can import them in your runtime.
If you are a consumer of extensions created using that crate, here is how you can import them in your runtime.

```rust
use rquickjs::AsyncRuntime;
use rquickjs_module::ModuleLoader;
use rquickjs_extension::ExtensionBuilder;

#[tokio::main]
async fn main() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ModuleLoader::builder().with_module(MyModule).build();
let (loader, resolver, initalizer) = ExtensionBuilder::new().with_module(MyExtension).build();

rt.set_loader(resolver, loader).await;

Expand All @@ -36,37 +36,37 @@ async fn main() {
}
```

## Creating modules
## Creating extensions

For the base case, replace the `ModuleDef` by an implementation of `ModuleDefExt`.
For the base case, replace the `ModuleDef` by an implementation of `Extension`.

```rust
use rquickjs::{Ctx, JsLifetime, Object, Result};
use rquickjs_module::{ModuleDefExt, ModuleImpl};
use rquickjs_extension::{Extension, ModuleImpl};

#[derive(JsLifetime, Debug)]
struct MyModuleOptions {
struct MyExtensionOptions {
user: String,
}

struct MyModule {
options: MyModuleOptions,
struct MyExtension {
options: MyExtensionOptions,
}

impl MyModule {
impl MyExtension {
pub fn new<T: Into<String>>(user: T) -> Self {
Self {
options: MyModuleOptions {
options: MyExtensionOptions {
user: user.into(),
},
}
}
}

impl ModuleDefExt<MyModuleOptions> for MyModule {
impl Extension<MyExtensionOptions> for MyExtension {
// Use `ModuleImpl` when you want a Javascript module.
// The options generic is not required.
type Implementation = ModuleImpl<MyModuleOptions>;
type Implementation = ModuleImpl<MyExtensionOptions>;

fn implementation() -> &'static Self::Implementation {
// This is the same as the implementation of `ModuleDef`
Expand All @@ -83,34 +83,34 @@ impl ModuleDefExt<MyModuleOptions> for MyModule {
}
}

fn options(self) -> MyModuleOptions {
fn options(self) -> MyExtensionOptions {
self.options
}

fn globals(globals: &Object<'_>, options: &MyModuleOptions) -> Result<()> {
fn globals(globals: &Object<'_>, options: &MyExtensionOptions) -> Result<()> {
// Set your globals here
globals.set("global_user", options.user.clone())?;
Ok(())
}
}
```

At runtime, this module results in:
At runtime, this extension results in:

- A global variable named `global_user`
- An importable module `import { user } from "my-module"`

### Globals only

If you only need to set globals and do **NOT** want an actual module that the Javascript can import, use `GlobalsOnly` for the implementation.
If you only need to set globals and do **NOT** want a Javascript module, use `GlobalsOnly` for the implementation.

```rust
use rquickjs::{Object, Result};
use rquickjs_module::{ModuleDefExt, GlobalsOnly};
use rquickjs_extension::{Extension, GlobalsOnly};

struct MyModule;
struct MyExtension;

impl ModuleDefExt for MyModule {
impl Extension for MyExtension {
type Implementation = GlobalsOnly;

fn implementation() -> &'static Self::Implementation {
Expand All @@ -130,10 +130,10 @@ impl ModuleDefExt for MyModule {
You can also use the macro for a simpler experience:

```rust
use rquickjs_module::globals_only_module;
use rquickjs_module::globals_only;

struct MyModule;
globals_only_module!(MyModule, |globals| {
struct MyExtension;
globals_only!(MyExtension, |globals| {
// Set your globals here
globals.set("hello", "world".to_string())?;
Ok(())
Expand Down
11 changes: 5 additions & 6 deletions src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use rquickjs::{
Ctx, Object, Result,
};

/// A trait for defining Javascript module and globals in Rust
/// with options.
/// A trait for defining an extension with options.
///
/// # Example
///
Expand Down Expand Up @@ -59,7 +58,7 @@ use rquickjs::{
/// }
/// ```
pub trait Extension<O = ()> {
type Implementation: ModuleImplementationType<O>;
type Implementation: ExtensionType<O>;

fn globals(_globals: &Object<'_>, _options: &O) -> Result<()> {
Ok(())
Expand All @@ -71,11 +70,11 @@ pub trait Extension<O = ()> {
}

/// Marker trait for implementation types
pub trait ModuleImplementationType<T> {}
pub trait ExtensionType<T> {}

/// Implementation type when you only need to define globals
pub struct GlobalsOnly;
impl<T> ModuleImplementationType<T> for GlobalsOnly {}
impl<T> ExtensionType<T> for GlobalsOnly {}

/// Implementation type when you need to define a module and
/// optionally globals.
Expand All @@ -84,4 +83,4 @@ pub struct ModuleImpl<O = ()> {
pub evaluate: for<'js> fn(&Ctx<'js>, &Exports<'js>, &O) -> Result<()>,
pub name: &'static str,
}
impl<T> ModuleImplementationType<T> for ModuleImpl<T> {}
impl<T> ExtensionType<T> for ModuleImpl<T> {}
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! An improved module system for rquickjs
//! An extension system for rquickjs
//!
//! This is an extension to [rquickjs](https://github.com/DelSkayn/rquickjs)
//! to allow the ecosystem to create more unified Rust modules.
//! This is a complement to [rquickjs](https://github.com/DelSkayn/rquickjs)
//! to allow the ecosystem to create more unified Rust extensions.
//!
//! The goal was to create a better version of
//! The goal was to create a more generic version of
//! [`ModuleDef`](rquickjs::module::ModuleDef)
//! that would allow it to have options as input and set global.
//! that would allow it to have options and/or set global values.
pub use self::definition::{Extension, GlobalsOnly, ModuleImpl};
pub use self::loader::{ExtensionBuilder, GlobalInitializer, ModuleLoader, ModuleResolver};
Expand Down
6 changes: 2 additions & 4 deletions src/loader/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use rquickjs::{loader::Loader, Ctx, Error, Module, Result};

use super::ModuleLoadFn;

/// Rquickjs [`Loader`](rquickjs::loader::Loader) for Rust modules
/// defined using [`ModuleDefExt`](crate::ModuleDefExt).
///
/// See [`ModuleLoaderBuilder`] for usage.
/// Rquickjs [`Loader`](rquickjs::loader::Loader) for modules
/// defined using [`Extension`](crate::Extension).
pub struct ModuleLoader {
modules: HashMap<&'static str, ModuleLoadFn>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/loader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn load_module_func<D: ModuleDef>(ctx: Ctx<'_>, name: Vec<u8>) -> Result<Module<
///
/// # Example
/// ```rust
/// use rquickjs_module::{ModuleLoader, ModuleDefExt, ModuleImpl};
/// use rquickjs_extension::{Extension, ModuleImpl};
///
/// struct MyExtension;
///
Expand Down
2 changes: 1 addition & 1 deletion src/loader/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rquickjs::{
};

/// Rquickjs [`Resolver`](rquickjs::loader::Resolver) for modules
/// defined using [`ModuleDefExt`](crate::ModuleDefExt).
/// defined using [`Extension`](crate::Extension).
pub struct ModuleResolver {
inner: BuiltinResolver,
}
Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[macro_export]
macro_rules! globals_only_module {
macro_rules! globals_only {
($name:ident, |$globals:ident| { $($t:tt)* }) => {
impl Extension for $name {
type Implementation = GlobalsOnly;
Expand Down
2 changes: 1 addition & 1 deletion src/wrapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod module;
/// We use this trait to still access metadata once we have
/// converted it from an [`Extension`] to a [`ModuleDef`].
///
/// This is necessary for the loader to work.
/// This is necessary for the [`ExtensionBuilder`](crate::ExtensionBuilder) to work.
pub trait ModuleMeta {
fn name() -> &'static str;
fn is_module() -> bool;
Expand Down
14 changes: 7 additions & 7 deletions tests/globals.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use rquickjs::{async_with, AsyncContext, AsyncRuntime, CatchResultExt, Object, Result};
use rquickjs_extension::{globals_only_module, Extension, ExtensionBuilder, GlobalsOnly};
use rquickjs_extension::{globals_only, Extension, ExtensionBuilder, GlobalsOnly};

use self::common::{Printer, PrinterOptions};

mod common;

struct PrinterModule {
struct PrinterExtension {
options: PrinterOptions,
}

impl PrinterModule {
impl PrinterExtension {
pub fn new<T: Into<String>>(target: T) -> Self {
Self {
options: PrinterOptions {
Expand All @@ -19,7 +19,7 @@ impl PrinterModule {
}
}

impl Extension<PrinterOptions> for PrinterModule {
impl Extension<PrinterOptions> for PrinterExtension {
type Implementation = GlobalsOnly;

fn implementation() -> &'static Self::Implementation {
Expand All @@ -36,8 +36,8 @@ impl Extension<PrinterOptions> for PrinterModule {
}
}

struct PrinterModule2;
globals_only_module!(PrinterModule2, |globals| {
struct PrinterExtension2;
globals_only!(PrinterExtension2, |globals| {
globals.set("global_printer", Printer::new("emile".to_string()))?;
Ok(())
});
Expand All @@ -47,7 +47,7 @@ async fn test_global() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ExtensionBuilder::new()
.with_extension(PrinterModule::new("world"))
.with_extension(PrinterExtension::new("world"))
.build();

rt.set_loader(resolver, loader).await;
Expand Down
18 changes: 9 additions & 9 deletions tests/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use self::common::{Printer, PrinterOptions};

mod common;

struct PrinterModule {
struct PrinterExtension {
options: PrinterOptions,
}

impl PrinterModule {
impl PrinterExtension {
pub fn new<T: Into<String>>(target: T) -> Self {
Self {
options: PrinterOptions {
Expand All @@ -21,7 +21,7 @@ impl PrinterModule {
}
}

impl Extension<PrinterOptions> for PrinterModule {
impl Extension<PrinterOptions> for PrinterExtension {
type Implementation = ModuleImpl<PrinterOptions>;

fn implementation() -> &'static Self::Implementation {
Expand Down Expand Up @@ -49,11 +49,11 @@ impl Extension<PrinterOptions> for PrinterModule {
}

#[tokio::test]
async fn test_module() {
async fn test_extension() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ExtensionBuilder::new()
.with_extension(PrinterModule::new("john"))
.with_extension(PrinterExtension::new("john"))
.build();

rt.set_loader(resolver, loader).await;
Expand All @@ -77,11 +77,11 @@ async fn test_module() {
}

#[tokio::test]
async fn test_module_named() {
async fn test_extension_named() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ExtensionBuilder::new()
.with_extension_named(PrinterModule::new("arnold"), "custom_printer")
.with_extension_named(PrinterExtension::new("arnold"), "custom_printer")
.build();

rt.set_loader(resolver, loader).await;
Expand All @@ -105,11 +105,11 @@ async fn test_module_named() {
}

#[tokio::test]
async fn test_module_global() {
async fn test_extension_global() {
let rt = AsyncRuntime::new().unwrap();

let (loader, resolver, initalizer) = ExtensionBuilder::new()
.with_extension(PrinterModule::new("david"))
.with_extension(PrinterExtension::new("david"))
.build();

rt.set_loader(resolver, loader).await;
Expand Down

0 comments on commit 7314eb0

Please sign in to comment.