From 4f77f34ea0cc09dca96fa40a620cd3c0acc66a0b Mon Sep 17 00:00:00 2001 From: z Date: Tue, 29 Jul 2025 16:23:06 +1200 Subject: [PATCH 1/5] added runtime flag --- Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index db8d5e3..18ef7d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,8 @@ lto = true opt-level = 3 [features] -default = ["crypto", "fetch", "llm", "wasip1"] +default = ["runtime", "crypto", "fetch", "llm", "wasip1"] +runtime = [] crypto = [] fetch = ["blockless-sdk/http"] llm = ["blockless-sdk/llm"] From 8d925af2ca9532a7a9bfdee20be1cc614e279001 Mon Sep 17 00:00:00 2001 From: z Date: Tue, 29 Jul 2025 16:23:23 +1200 Subject: [PATCH 2/5] crate-type can also be used as rust lib --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 18ef7d3..761e320 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/blessnetwork/javy-bless-plugins" [lib] name = "bless_plugins" -crate-type = ["cdylib"] +crate-type = ["cdylib", "rlib"] [dependencies] anyhow = "1.0.95" From ef5f4ce5b555350150813e0be08266ada537c806 Mon Sep 17 00:00:00 2001 From: z Date: Tue, 29 Jul 2025 16:24:58 +1200 Subject: [PATCH 3/5] removed standard build as it matches full build --- .github/workflows/release.yml | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 411869e..36e9042 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -13,11 +13,10 @@ jobs: strategy: matrix: build: [ - { name: 'standard', features: '' }, - { name: 'crypto', features: '--no-default-features --features crypto' }, - { name: 'fetch', features: '--no-default-features --features fetch' }, - { name: 'llm', features: '--no-default-features --features llm' }, - { name: 'wasip1', features: '--no-default-features --features wasip1' }, + { name: 'crypto', features: '--no-default-features --features runtime,crypto' }, + { name: 'fetch', features: '--no-default-features --features runtime,fetch' }, + { name: 'wasip1', features: '--no-default-features --features runtime,wasip1' }, + { name: 'llm', features: '--no-default-features --features runtime,llm' }, { name: 'full', features: '--all-features' }, ] @@ -46,7 +45,7 @@ jobs: id: prepare run: | VERSION=${{ github.event.release.tag_name }} - SUFFIX="${{ matrix.build.name != 'standard' && format('-{0}', matrix.build.name) || '' }}" + SUFFIX="${{ format('-{0}', matrix.build.name) || '' }}" ARTIFACT_NAME="bless-plugins${SUFFIX}-${VERSION}.wasm" mv bless_plugins.wasm "$ARTIFACT_NAME" echo "artifact_name=$ARTIFACT_NAME" >> "$GITHUB_OUTPUT" From 561f0c5c13e6c187ea3df741caa0da435835cfa1 Mon Sep 17 00:00:00 2001 From: z Date: Tue, 29 Jul 2025 16:25:43 +1200 Subject: [PATCH 4/5] introduced runtime flag to codebase --- src/lib.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6885c03..e280f1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,17 +17,10 @@ pub mod llm; #[cfg(feature = "wasip1")] pub mod wasi; -#[cfg(feature = "crypto")] -use crypto::bless_get_random_values; - -#[cfg(feature = "fetch")] -use fetch::bless_fetch_request; - -#[cfg(feature = "llm")] -use llm::bless_llm_plugin; - +#[cfg(feature = "runtime")] import_namespace!("bless_core_plugins"); +#[cfg(feature = "runtime")] #[export_name = "initialize_runtime"] pub extern "C" fn initialize_runtime() { let mut config = Config::default(); From cbe9b4bfdaccd0adc8d89518a78f73f21809009e Mon Sep 17 00:00:00 2001 From: z Date: Tue, 29 Jul 2025 16:26:23 +1200 Subject: [PATCH 5/5] refactor modules to make them importable with external runtimes --- src/lib.rs | 176 +++++++++++++++++++++++++++++------------------------ 1 file changed, 95 insertions(+), 81 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e280f1c..29e304c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ +#[allow(unused_imports)] use javy_plugin_api::{ import_namespace, javy::{ hold, hold_and_release, - quickjs::{prelude::MutFn, Function}, + quickjs::{prelude::MutFn, Ctx, Function, Result}, to_js_error, Args, }, Config, @@ -28,94 +29,19 @@ pub extern "C" fn initialize_runtime() { config.javy_stream_io(true); config.text_encoding(true); + #[allow(unused_variables)] javy_plugin_api::initialize_runtime(config, |runtime| { runtime .context() .with(|ctx| { #[cfg(feature = "crypto")] - ctx.globals().set( - "__javy_crypto_get_random_values", - Function::new( - ctx.clone(), - MutFn::new(move |cx, args| { - let (cx, args) = hold_and_release!(cx, args); - bless_get_random_values(hold!(cx.clone(), args)) - .map_err(|e| to_js_error(cx, e)) - }), - )?, - )?; - + set_crypto_globals(&ctx)?; #[cfg(feature = "fetch")] - ctx.globals().set( - "__javy_fetchio_request", - Function::new( - ctx.clone(), - MutFn::new(move |cx, args| { - let (cx, args) = hold_and_release!(cx, args); - bless_fetch_request(hold!(cx.clone(), args)) - .map_err(|e| to_js_error(cx, e)) - }), - )?, - )?; - + set_fetch_globals(&ctx)?; #[cfg(feature = "wasip1")] - { - macro_rules! bind { - (function, $l: ident) => { - let name = concat!("__javy_", stringify!($l)); - ctx.globals().set( - name, - Function::new( - ctx.clone(), - MutFn::new(move |cx, args| { - let (cx, args) = hold_and_release!(cx, args); - wasi::$l(hold!(cx.clone(), args)) - .map_err(|e| to_js_error(cx, e)) - }), - )?, - )?; - }; - } - bind!(function, wasi_preview1_open); - bind!(function, wasi_preview1_fd_prestat_dir_name); - bind!(function, wasi_preview1_path_create_directory); - bind!(function, wasi_preview1_path_remove_directory); - bind!(function, wasi_preview1_path_unlink_file); - bind!(function, wasi_preview1_close); - bind!(function, wasi_preview1_path_symlink); - bind!(function, wasi_preview1_path_link); - bind!(function, wasi_preview1_path_rename); - bind!(function, wasi_preview1_path_filestat_get); - } - + set_wasi_globals(&ctx)?; #[cfg(feature = "llm")] - ctx.globals().set( - "BlessLLM", - Function::new( - ctx.clone(), - MutFn::new(move |cx, args| { - let (cx, args) = hold_and_release!(cx, args); - bless_llm_plugin(hold!(cx.clone(), args)) - .map_err(|e| to_js_error(cx, e)) - }), - )?, - )?; - - // Expose the suppported models object globally for JS - #[cfg(feature = "llm")] - ctx.globals().set( - "MODELS", - javy_plugin_api::javy::quickjs::Value::from_object( - llm::supported_models_object(&ctx)?, - ), - )?; - - #[cfg(feature = "crypto")] - ctx.eval::<(), _>(include_str!("crypto/crypto.js"))?; - #[cfg(feature = "fetch")] - ctx.eval::<(), _>(include_str!("fetch/fetch.js"))?; - #[cfg(feature = "wasip1")] - ctx.eval::<(), _>(include_str!("wasi/preview_1.js"))?; + set_llm_globals(&ctx)?; Ok::<_, anyhow::Error>(()) }) .unwrap(); @@ -124,3 +50,91 @@ pub extern "C" fn initialize_runtime() { }) .unwrap(); } + +#[cfg(feature = "crypto")] +pub fn set_crypto_globals(ctx: &Ctx<'_>) -> Result<()> { + ctx.globals().set( + "__javy_crypto_get_random_values", + Function::new( + ctx.clone(), + MutFn::new(move |cx, args| { + let (cx, args) = hold_and_release!(cx, args); + crypto::bless_get_random_values(hold!(cx.clone(), args)) + .map_err(|e| to_js_error(cx, e)) + }), + )?, + )?; + ctx.eval::<(), _>(include_str!("crypto/crypto.js"))?; + Ok(()) +} + +#[cfg(feature = "fetch")] +pub fn set_fetch_globals(ctx: &Ctx<'_>) -> Result<()> { + ctx.globals().set( + "__javy_fetchio_request", + Function::new( + ctx.clone(), + MutFn::new(move |cx, args| { + let (cx, args) = hold_and_release!(cx, args); + fetch::bless_fetch_request(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e)) + }), + )?, + )?; + ctx.eval::<(), _>(include_str!("fetch/fetch.js"))?; + Ok(()) +} + +#[cfg(feature = "wasip1")] +pub fn set_wasi_globals(ctx: &Ctx<'_>) -> Result<()> { + macro_rules! bind { + (function, $l: ident) => { + let name = concat!("__javy_", stringify!($l)); + ctx.globals().set( + name, + Function::new( + ctx.clone(), + MutFn::new(move |cx, args| { + let (cx, args) = hold_and_release!(cx, args); + wasi::$l(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e)) + }), + )?, + )?; + }; + } + bind!(function, wasi_preview1_open); + bind!(function, wasi_preview1_fd_prestat_dir_name); + bind!(function, wasi_preview1_path_create_directory); + bind!(function, wasi_preview1_path_remove_directory); + bind!(function, wasi_preview1_path_unlink_file); + bind!(function, wasi_preview1_close); + bind!(function, wasi_preview1_path_symlink); + bind!(function, wasi_preview1_path_link); + bind!(function, wasi_preview1_path_rename); + bind!(function, wasi_preview1_path_filestat_get); + ctx.eval::<(), _>(include_str!("wasi/preview_1.js"))?; + Ok(()) +} + +#[cfg(feature = "llm")] +pub fn set_llm_globals(ctx: &Ctx<'_>) -> Result<()> { + ctx.globals().set( + "BlessLLM", + Function::new( + ctx.clone(), + MutFn::new(move |cx, args| { + let (cx, args) = hold_and_release!(cx, args); + llm::bless_llm_plugin(hold!(cx.clone(), args)).map_err(|e| to_js_error(cx, e)) + }), + )?, + )?; + + // Expose the suppported models object globally for JS + let ctx_clone = ctx.clone(); + ctx.globals().set( + "MODELS", + javy_plugin_api::javy::quickjs::Value::from_object( + llm::supported_models_object(ctx).map_err(|e| to_js_error(ctx_clone, e))?, + ), + )?; + Ok(()) +}