Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors = ["@rscarson"]
description = "Effortless JS Integration for Rust"
edition = "2021"
license = "MIT OR Apache-2.0"
version = "0.12.2"
version = "0.12.3"
repository = "https://github.com/rscarson/rustyscript"

keywords = ["rust", "javascript", "deno", "runtime", "embedding"]
Expand Down
110 changes: 79 additions & 31 deletions src/ext/broadcast_channel/mod.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,79 @@
use deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_core::{extension, Extension};

use super::ExtensionTrait;

mod wrapper;
pub use wrapper::BroadcastChannelWrapper;

extension!(
init_broadcast_channel,
deps = [rustyscript],
esm_entry_point = "ext:init_broadcast_channel/init_broadcast_channel.js",
esm = [ dir "src/ext/broadcast_channel", "init_broadcast_channel.js" ],
);
impl ExtensionTrait<()> for init_broadcast_channel {
fn init((): ()) -> Extension {
init_broadcast_channel::init()
}
}
impl ExtensionTrait<InMemoryBroadcastChannel> for deno_broadcast_channel::deno_broadcast_channel {
fn init(channel: InMemoryBroadcastChannel) -> Extension {
deno_broadcast_channel::deno_broadcast_channel::init(channel)
}
}

pub fn extensions(channel: InMemoryBroadcastChannel, is_snapshot: bool) -> Vec<Extension> {
vec![
deno_broadcast_channel::deno_broadcast_channel::build(channel, is_snapshot),
init_broadcast_channel::build((), is_snapshot),
]
}
use deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_core::{extension, Extension};

use super::ExtensionTrait;

mod wrapper;
pub use wrapper::BroadcastChannelWrapper;

extension!(
init_broadcast_channel,
deps = [rustyscript],
esm_entry_point = "ext:init_broadcast_channel/init_broadcast_channel.js",
esm = [ dir "src/ext/broadcast_channel", "init_broadcast_channel.js" ],
);
impl ExtensionTrait<()> for init_broadcast_channel {
fn init((): ()) -> Extension {
init_broadcast_channel::init()
}
}
impl ExtensionTrait<InMemoryBroadcastChannel> for deno_broadcast_channel::deno_broadcast_channel {
fn init(channel: InMemoryBroadcastChannel) -> Extension {
deno_broadcast_channel::deno_broadcast_channel::init(channel)
}
}

pub fn extensions(channel: InMemoryBroadcastChannel, is_snapshot: bool) -> Vec<Extension> {
vec![
deno_broadcast_channel::deno_broadcast_channel::build(channel, is_snapshot),
init_broadcast_channel::build((), is_snapshot),
]
}

#[cfg(test)]
mod test {
use deno_core::PollEventLoopOptions;

use crate::{module, BroadcastChannelWrapper, Module, Runtime, RuntimeOptions};

static TEST_MOD: Module = module!(
"test.js",
"
const channel = new BroadcastChannel('my_channel');
channel.onmessage = (event) => {
channel.postMessage('Received: ' + event.data);
};
"
);

#[test]
fn test_broadcast_channel() {
let options = RuntimeOptions::default();
let channel = options.extension_options.broadcast_channel.clone();

let mut runtime = Runtime::new(options).unwrap();
let tokio_runtime = runtime.tokio_runtime();

let channel = BroadcastChannelWrapper::new(&channel, "my_channel").unwrap();

tokio_runtime
.block_on(runtime.load_module_async(&TEST_MOD))
.unwrap();

channel.send_sync(&mut runtime, "foo").unwrap();

runtime
.block_on_event_loop(
PollEventLoopOptions::default(),
Some(std::time::Duration::from_secs(1)),
)
.unwrap();

let value = channel
.recv_sync::<String>(&mut runtime, Some(std::time::Duration::from_secs(1)))
.unwrap()
.unwrap();

assert_eq!(value, "Received: foo");
}
}
3 changes: 2 additions & 1 deletion src/inner_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
path::{Path, PathBuf},
pin::Pin,
rc::Rc,
sync::Arc,
task::Poll,
time::Duration,
};
Expand Down Expand Up @@ -265,7 +266,7 @@ impl<RT: RuntimeTrait> InnerRuntime<RT> {
.rt_mut()
.op_state()
.borrow_mut()
.put(feature_checker);
.put(Arc::new(feature_checker));

// Add a callback to terminate the runtime if the max_heap_size limit is approached
if options.max_heap_size.is_some() {
Expand Down