From b6b1821a32c99db3881dc36dd38171ce0df83363 Mon Sep 17 00:00:00 2001 From: "Join.G" Date: Mon, 26 Aug 2024 17:30:52 +0800 Subject: [PATCH 1/5] feature: use the stdout as output when using console.log api. --- crates/cli/benches/benchmark.rs | 1 + crates/cli/src/bytecode.rs | 1 + crates/cli/tests/dylib_test.rs | 38 ++++--- crates/cli/tests/dynamic_linking_test.rs | 23 ++-- crates/cli/tests/integration_test.rs | 20 ++-- crates/core/src/runtime.rs | 2 +- crates/javy/src/alloc.rs | 4 +- crates/quickjs-wasm-rs/src/lib.rs | 6 +- download.sh | 129 +++++++++++++++++++++++ 9 files changed, 192 insertions(+), 32 deletions(-) create mode 100755 download.sh diff --git a/crates/cli/benches/benchmark.rs b/crates/cli/benches/benchmark.rs index f522995e..aa77ffa2 100644 --- a/crates/cli/benches/benchmark.rs +++ b/crates/cli/benches/benchmark.rs @@ -84,6 +84,7 @@ impl FunctionCase { Ok(function_case) } + #[allow(clippy::needless_borrows_for_generic_args)] pub fn run(&self, linker: &mut Linker, mut store: &mut Store) -> Result<()> { let js_module = match &self.precompiled_elf_bytes { Some(bytes) => unsafe { Module::deserialize(&self.engine, bytes) }?, diff --git a/crates/cli/src/bytecode.rs b/crates/cli/src/bytecode.rs index abe4ec4e..a099a0f0 100644 --- a/crates/cli/src/bytecode.rs +++ b/crates/cli/src/bytecode.rs @@ -26,6 +26,7 @@ fn create_wasm_env() -> Result<(Store, Instance, Memory)> { Ok((store, instance, memory)) } +#[allow(clippy::needless_borrows_for_generic_args)] fn copy_source_code_into_instance( js_source_code: &[u8], mut store: &mut Store, diff --git a/crates/cli/tests/dylib_test.rs b/crates/cli/tests/dylib_test.rs index 851d62f3..84c87dfa 100644 --- a/crates/cli/tests/dylib_test.rs +++ b/crates/cli/tests/dylib_test.rs @@ -11,7 +11,7 @@ mod common; fn test_dylib() -> Result<()> { let js_src = "console.log(42);"; let stderr = WritePipe::new_in_memory(); - run_js_src(js_src, &stderr)?; + run_js_src(js_src, &stderr, false)?; let output = stderr.try_into_inner().unwrap().into_inner(); assert_eq!("42\n", str::from_utf8(&output)?); @@ -23,7 +23,7 @@ fn test_dylib() -> Result<()> { fn test_dylib_with_error() -> Result<()> { let js_src = "function foo() { throw new Error('foo error'); } foo();"; let stderr = WritePipe::new_in_memory(); - let result = run_js_src(js_src, &stderr); + let result = run_js_src(js_src, &stderr, true); assert!(result.is_err()); let output = stderr.try_into_inner().unwrap().into_inner(); @@ -37,17 +37,21 @@ fn test_dylib_with_error() -> Result<()> { #[test] fn test_dylib_with_exported_func() -> Result<()> { let js_src = "export function foo() { console.log('In foo'); }; console.log('Toplevel');"; - let stderr = WritePipe::new_in_memory(); - run_invoke(js_src, "foo", &stderr)?; + let stdout = WritePipe::new_in_memory(); + run_invoke(js_src, "foo", &stdout)?; - let output = stderr.try_into_inner().unwrap().into_inner(); + let output = stdout.try_into_inner().unwrap().into_inner(); assert_eq!("Toplevel\nIn foo\n", str::from_utf8(&output)?); Ok(()) } -fn run_js_src(js_src: &str, stderr: &T) -> Result<()> { - let (instance, mut store) = create_wasm_env(stderr)?; +fn run_js_src( + js_src: &str, + stderr: &T, + is_stderr: bool, +) -> Result<()> { + let (instance, mut store) = create_wasm_env(stderr, is_stderr)?; let eval_bytecode_func = instance.get_typed_func::<(u32, u32), ()>(&mut store, "eval_bytecode")?; @@ -59,9 +63,9 @@ fn run_js_src(js_src: &str, stderr: &T) -> Result fn run_invoke( js_src: &str, fn_to_invoke: &str, - stderr: &T, + stdout: &T, ) -> Result<()> { - let (instance, mut store) = create_wasm_env(stderr)?; + let (instance, mut store) = create_wasm_env(stdout, false)?; let invoke_func = instance.get_typed_func::<(u32, u32, u32, u32), ()>(&mut store, "invoke")?; let (bytecode_ptr, bytecode_len) = compile_src(js_src.as_bytes(), &instance, &mut store)?; @@ -75,13 +79,20 @@ fn run_invoke( fn create_wasm_env( stderr: &T, + is_stderr: bool, ) -> Result<(Instance, Store)> { let engine = Engine::default(); let mut linker = Linker::new(&engine); wasmtime_wasi::add_to_linker(&mut linker, |s| s)?; - let wasi = WasiCtxBuilder::new() - .stderr(Box::new(stderr.clone())) - .build(); + let wasi = if is_stderr { + WasiCtxBuilder::new() + .stderr(Box::new(stderr.clone())) + .build() + } else { + WasiCtxBuilder::new() + .stdout(Box::new(stderr.clone())) + .build() + }; let module = common::create_quickjs_provider_module(&engine)?; let mut store = Store::new(&engine, wasi); @@ -90,6 +101,7 @@ fn create_wasm_env( Ok((instance, store)) } +#[allow(clippy::needless_borrows_for_generic_args)] fn compile_src( js_src: &[u8], instance: &Instance, @@ -110,6 +122,7 @@ fn compile_src( Ok((bytecode_ptr, bytecode_len)) } +#[allow(clippy::needless_borrows_for_generic_args)] fn copy_func_name( fn_name: &str, instance: &Instance, @@ -123,6 +136,7 @@ fn copy_func_name( Ok((fn_name_ptr, fn_name_bytes.len().try_into()?)) } +#[allow(clippy::needless_borrows_for_generic_args)] fn allocate_memory( instance: &Instance, mut store: &mut Store, diff --git a/crates/cli/tests/dynamic_linking_test.rs b/crates/cli/tests/dynamic_linking_test.rs index 4f4b194b..22581a67 100644 --- a/crates/cli/tests/dynamic_linking_test.rs +++ b/crates/cli/tests/dynamic_linking_test.rs @@ -13,7 +13,7 @@ mod common; #[test] pub fn test_dynamic_linking() -> Result<()> { let js_src = "console.log(42);"; - let log_output = invoke_fn_on_generated_module(js_src, "_start", None)?; + let log_output = invoke_fn_on_generated_module(js_src, "_start", None, false)?; assert_eq!("42\n", &log_output); Ok(()) } @@ -28,7 +28,8 @@ pub fn test_dynamic_linking_with_func() -> Result<()> { export foo-bar: func() } "; - let log_output = invoke_fn_on_generated_module(js_src, "foo-bar", Some((wit, "foo-test")))?; + let log_output = + invoke_fn_on_generated_module(js_src, "foo-bar", Some((wit, "foo-test")), false)?; assert_eq!("Toplevel\nIn foo\n", &log_output); Ok(()) } @@ -36,7 +37,7 @@ pub fn test_dynamic_linking_with_func() -> Result<()> { #[test] pub fn test_dynamic_linking_with_func_without_flag() -> Result<()> { let js_src = "export function foo() { console.log('In foo'); }; console.log('Toplevel');"; - let res = invoke_fn_on_generated_module(js_src, "foo", None); + let res = invoke_fn_on_generated_module(js_src, "foo", None, true); assert_eq!( "failed to find function export `foo`", res.err().unwrap().to_string() @@ -49,7 +50,7 @@ pub fn check_for_new_imports() -> Result<()> { // If you need to change this test, then you've likely made a breaking change. let js_src = "console.log(42);"; let wasm = create_dynamically_linked_wasm_module(js_src, None)?; - let (engine, _linker, _store) = create_wasm_env(WritePipe::new_in_memory())?; + let (engine, _linker, _store) = create_wasm_env(WritePipe::new_in_memory(), false)?; let module = Module::from_binary(&engine, &wasm)?; for import in module.imports() { match (import.module(), import.name(), import.ty()) { @@ -78,7 +79,7 @@ pub fn check_for_new_imports_for_exports() -> Result<()> { } "; let wasm = create_dynamically_linked_wasm_module(js_src, Some((wit, "foo-test")))?; - let (engine, _linker, _store) = create_wasm_env(WritePipe::new_in_memory())?; + let (engine, _linker, _store) = create_wasm_env(WritePipe::new_in_memory(), false)?; let module = Module::from_binary(&engine, &wasm)?; for import in module.imports() { match (import.module(), import.name(), import.ty()) { @@ -110,7 +111,7 @@ pub fn test_dynamic_linking_with_arrow_fn() -> Result<()> { } "; let log_output = - invoke_fn_on_generated_module(js_src, "default", Some((wit, "exported-arrow")))?; + invoke_fn_on_generated_module(js_src, "default", Some((wit, "exported-arrow")), false)?; assert_eq!("42\n", log_output); Ok(()) } @@ -166,11 +167,12 @@ fn invoke_fn_on_generated_module( js_src: &str, func: &str, wit: Option<(&str, &str)>, + is_stderr: bool, ) -> Result { let js_wasm = create_dynamically_linked_wasm_module(js_src, wit)?; let stderr = WritePipe::new_in_memory(); - let (engine, mut linker, mut store) = create_wasm_env(stderr.clone())?; + let (engine, mut linker, mut store) = create_wasm_env(stderr.clone(), is_stderr)?; let quickjs_provider_module = common::create_quickjs_provider_module(&engine)?; let js_module = Module::from_binary(&engine, &js_wasm)?; @@ -192,11 +194,16 @@ fn invoke_fn_on_generated_module( fn create_wasm_env( stderr: WritePipe>>, + is_stderr: bool, ) -> Result<(Engine, Linker, Store)> { let engine = Engine::new(Config::new().wasm_multi_memory(true))?; let mut linker = Linker::new(&engine); wasmtime_wasi::add_to_linker(&mut linker, |s| s)?; - let wasi = WasiCtxBuilder::new().stderr(Box::new(stderr)).build(); + let wasi = if is_stderr { + WasiCtxBuilder::new().stderr(Box::new(stderr)).build() + } else { + WasiCtxBuilder::new().stdout(Box::new(stderr)).build() + }; let store = Store::new(&engine, wasi); Ok((engine, linker, store)) } diff --git a/crates/cli/tests/integration_test.rs b/crates/cli/tests/integration_test.rs index e07ac541..eb7fd311 100644 --- a/crates/cli/tests/integration_test.rs +++ b/crates/cli/tests/integration_test.rs @@ -62,10 +62,12 @@ fn test_encoding() { fn test_logging() { let mut runner = Runner::new("logging.js"); - let (_output, logs, fuel_consumed) = run(&mut runner, &[]); + let (output, logs, fuel_consumed) = run_fn(&mut runner, "_start", &[]); + let mut output = String::from_utf8(output).unwrap(); + output.push_str(&logs); assert_eq!( "hello world from console.log\nhello world from console.error\n", - logs.as_str(), + output.as_str(), ); assert_fuel_consumed_within_threshold(22_296, fuel_consumed); } @@ -104,10 +106,10 @@ fn test_promises() { #[test] fn test_exported_functions() { let mut runner = Runner::new_with_exports("exported-fn.js", "exported-fn.wit", "exported-fn"); - let (_, logs, fuel_consumed) = run_fn(&mut runner, "foo", &[]); + let (logs, _, fuel_consumed) = run_fn_stdout(&mut runner, "foo", &[]); assert_eq!("Hello from top-level\nHello from foo\n", logs); assert_fuel_consumed_within_threshold(54610, fuel_consumed); - let (_, logs, _) = run_fn(&mut runner, "foo-bar", &[]); + let (logs, _, _) = run_fn_stdout(&mut runner, "foo-bar", &[]); assert_eq!("Hello from top-level\nHello from fooBar\n", logs); } @@ -178,7 +180,7 @@ fn test_exported_default_arrow_fn() { "exported-default-arrow-fn.wit", "exported-arrow", ); - let (_, logs, fuel_consumed) = run_fn(&mut runner, "default", &[]); + let (logs, _, fuel_consumed) = run_fn_stdout(&mut runner, "default", &[]); assert_eq!(logs, "42\n"); assert_fuel_consumed_within_threshold(48_628, fuel_consumed); } @@ -190,7 +192,7 @@ fn test_exported_default_fn() { "exported-default-fn.wit", "exported-default", ); - let (_, logs, fuel_consumed) = run_fn(&mut runner, "default", &[]); + let (logs, _, fuel_consumed) = run_fn_stdout(&mut runner, "default", &[]); assert_eq!(logs, "42\n"); assert_fuel_consumed_within_threshold(49_748, fuel_consumed); } @@ -211,6 +213,12 @@ fn run_fn(r: &mut Runner, func: &str, stdin: &[u8]) -> (Vec, String, u64) { (output, logs, fuel_consumed) } +fn run_fn_stdout(r: &mut Runner, func: &str, stdin: &[u8]) -> (String, Vec, u64) { + let (output, logs, fuel_consumed) = r.exec_func(func, stdin).unwrap(); + let output = String::from_utf8(output).unwrap(); + (output, logs, fuel_consumed) +} + /// Used to detect any significant changes in the fuel consumption when making changes in Javy. /// /// A threshold is used here so that we can decide how much of a change is acceptable. The threshold value needs to be sufficiently large enough to account for fuel differences between different operating systems. diff --git a/crates/core/src/runtime.rs b/crates/core/src/runtime.rs index a2d5bcbb..65edfa01 100644 --- a/crates/core/src/runtime.rs +++ b/crates/core/src/runtime.rs @@ -4,6 +4,6 @@ use javy_apis::{APIConfig, LogStream, RuntimeExt}; pub(crate) fn new_runtime() -> Result { let mut api_config = APIConfig::default(); - api_config.log_stream(LogStream::StdErr); + api_config.log_stream(LogStream::StdOut); Runtime::new_with_apis(Config::default(), api_config) } diff --git a/crates/javy/src/alloc.rs b/crates/javy/src/alloc.rs index 706e8b13..ee6c23ad 100644 --- a/crates/javy/src/alloc.rs +++ b/crates/javy/src/alloc.rs @@ -18,8 +18,8 @@ const ZERO_SIZE_ALLOCATION_PTR: *mut u8 = 1 as _; /// /// 1. Allocate memory of new_size with alignment. /// 2. If original_ptr != 0. -/// a. copy min(new_size, original_size) bytes from original_ptr to new memory. -/// b. de-allocate original_ptr. +/// a. copy min(new_size, original_size) bytes from original_ptr to new memory. +/// b. de-allocate original_ptr. /// 3. Return new memory ptr. /// /// # Safety diff --git a/crates/quickjs-wasm-rs/src/lib.rs b/crates/quickjs-wasm-rs/src/lib.rs index 0120aed9..9df8ed05 100644 --- a/crates/quickjs-wasm-rs/src/lib.rs +++ b/crates/quickjs-wasm-rs/src/lib.rs @@ -50,9 +50,9 @@ //! serialization formats and `JSValueRef`: //! - `messagepack` provides `quickjs_wasm_rs::messagepack` for msgpack, using `rmp_serde`. //! - `json` provides `quickjs_wasm_rs::json` for JSON, using `serde_json`. - -//! msgpack example: - +//! +//! msgpack example: +//! //! ```rust //! use quickjs_wasm_rs::{messagepack, JSContextRef, JSValueRef}; diff --git a/download.sh b/download.sh new file mode 100755 index 00000000..005c5092 --- /dev/null +++ b/download.sh @@ -0,0 +1,129 @@ +#!/bin/bash +TMP_DIR="/tmp/blsjavy" +function cleanup { + rm -rf $TMP_DIR > /dev/null +} +function fail { + cleanup + msg=$1 + echo "============" + echo "Error: $msg" 1>&2 + exit 1 +} +function install { + #settings + USER="blocklessnetwork" + PROG='blsjavy' + MOVE="true" + RELEASE="latest" + INSECURE="false" + OUT_DIR="$HOME/.bin" + GH="https://github.com" + #bash check + [ ! "$BASH_VERSION" ] && fail "Please use bash instead" + [ ! -d $OUT_DIR ] && mkdir -p $OUT_DIR + #dependency check, assume we are a standard POSIX machine + which find > /dev/null || fail "find not installed" + which xargs > /dev/null || fail "xargs not installed" + which sort > /dev/null || fail "sort not installed" + which tail > /dev/null || fail "tail not installed" + which cut > /dev/null || fail "cut not installed" + which du > /dev/null || fail "du not installed" + GET="" + if which curl > /dev/null; then + GET="curl" + if [[ $INSECURE = "true" ]]; then GET="$GET --insecure"; fi + GET="$GET --fail -# -L" + elif which wget > /dev/null; then + GET="wget" + if [[ $INSECURE = "true" ]]; then GET="$GET --no-check-certificate"; fi + GET="$GET -qO-" + else + fail "neither wget/curl are installed" + fi + #find OS #TODO BSDs and other posixs + case `uname -s` in + Darwin) OS="darwin";; + Linux) OS="linux";; + *) fail "unknown os: $(uname -s)";; + esac + #find ARCH + if uname -m | grep 64 | grep arm > /dev/null; then + ARCH="arm64" + elif uname -m | grep 64 | grep aarch > /dev/null; then + ARCH="arm64" + elif uname -m | grep 64 > /dev/null; then + ARCH="amd64" + elif uname -m | grep arm > /dev/null; then + ARCH="arm" #TODO armv6/v7 + elif uname -m | grep 386 > /dev/null; then + ARCH="386" + else + fail "unknown arch: $(uname -m)" + fi + + #choose from asset list + URL="" + FTYPE="" + DEFAULT_VERSION="v1.4.0" + VERSION=${1:-$DEFAULT_VERSION} + case "${OS}_${ARCH}" in + "darwin_amd64") + URL="https://github.com/blocklessnetwork/bls-javy/releases/download/${VERSION}/javy-x86_64-macos-${VERSION}.gz" + FTYPE=".gz" + ;; + "darwin_arm64") + URL="https://github.com/blocklessnetwork/bls-javy/releases/download/${VERSION}/javy-arm-macos-${VERSION}.gz" + FTYPE=".gz" + ;; + "linux_amd64") + URL="https://github.com/blocklessnetwork/bls-javy/releases/download/${VERSION}/javy-x86_64-linux-${VERSION}.gz" + FTYPE=".gz" + ;; + "linux_arm64") + URL="https://github.com/blocklessnetwork/bls-javy/releases/download/${VERSION}/javy-arm-linux-${VERSION}.gz" + FTYPE=".gz" + ;; + *) fail "No asset for platform ${OS}-${ARCH}";; + esac + #echo the URL + echo "Download URL: $URL" + #got URL! download it... + echo -n "Installing $PROG $VERSION" + + echo "....." + + #enter tempdir + mkdir -p $TMP_DIR + cd $TMP_DIR + if [[ $FTYPE = ".gz" ]]; then + which gzip > /dev/null || fail "gzip is not installed" + #gzipped binary + NAME="${PROG}_${OS}_${ARCH}.gz" + #gz download! + bash -c "$GET $URL" | gzip -d - > $PROG || fail "download failed" + elif [[ $FTYPE = "" ]]; then + bash -c "$GET $URL" > "b7s_${OS}_${ARCH}" || fail "download failed" + else + fail "unknown file type: $FTYPE" + fi + #search subtree largest file (bin) + TMP_BIN=$(find . -type f | xargs du | sort -n | tail -n 1 | cut -f 2) + if [ ! -f "$TMP_BIN" ]; then + fail "could not find binary (largest file)" + fi + #ensure its larger than 1MB + if [[ $(du -m $TMP_BIN | cut -f1) -lt 1 ]]; then + fail "no binary found ($TMP_BIN is not larger than 1MB)" + fi + #move into PATH or cwd + chmod +x $TMP_BIN || fail "chmod +x failed" + + mv $TMP_BIN $OUT_DIR/$PROG || fail "mv failed" #FINAL STEP! + echo "Installed $PROG $VERSION at $OUT_DIR/$PROG" + #done + cleanup + echo "Please add the following line to your .bashrc or .zshrc:" + echo 'export PATH="$HOME/.bin:$PATH"' +} +install $1 \ No newline at end of file From c2ec15081f97ccd4b0b44ba92a25a05d64546b49 Mon Sep 17 00:00:00 2001 From: "Join.Gong" Date: Mon, 26 Aug 2024 13:29:38 +0000 Subject: [PATCH 2/5] change the ci for core --- .github/workflows/ci.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b233d4a..5fa704ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,7 +32,12 @@ jobs: run: cargo build -p javy-core --release --target=wasm32-wasi - name: Test - run: cargo hack wasi test --workspace --exclude=javy-cli --each-feature -- --nocapture + run: | + cargo wasi test --manifest-path crates/quickjs-wasm-rs/Cargo.toml --all-features -- --nocapture + cargo wasi test --manifest-path crates/apis/Cargo.toml --all-features -- --nocapture + cargo wasi test --manifest-path crates/quickjs-wasm-sys/Cargo.toml --all-features -- --nocapture + cargo wasi test --manifest-path crates/javy/Cargo.toml --all-features -- --nocapture + - name: Lint run: cargo clippy --workspace --exclude=javy-cli --target=wasm32-wasi --all-targets -- -D warnings From e3e712635c39a36c67ce98ea9e4231441bf7a7e1 Mon Sep 17 00:00:00 2001 From: "Join.Gong" Date: Mon, 26 Aug 2024 13:51:46 +0000 Subject: [PATCH 3/5] fix lint issue. --- crates/apis/src/fetch_io/blockless.rs | 1 + crates/apis/src/fetch_io/mod.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/apis/src/fetch_io/blockless.rs b/crates/apis/src/fetch_io/blockless.rs index e0fdb161..138817bf 100644 --- a/crates/apis/src/fetch_io/blockless.rs +++ b/crates/apis/src/fetch_io/blockless.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use std::cmp::Ordering; use serde_json::{json, Value}; use crate::fetch_io::FetchOptions; diff --git a/crates/apis/src/fetch_io/mod.rs b/crates/apis/src/fetch_io/mod.rs index 46a312d6..5362461f 100644 --- a/crates/apis/src/fetch_io/mod.rs +++ b/crates/apis/src/fetch_io/mod.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] mod blockless; use std::collections::HashMap; @@ -23,12 +24,14 @@ impl FetchOptions { method: method.into() } } - + + #[allow(clippy::inherent_to_string)] pub fn to_string(&self) -> String { serde_json::to_string(&self).unwrap() } } + impl JSApiSet for FetchIO { fn register(&self, runtime: &Runtime, _config: &APIConfig) -> Result<()> { let context = runtime.context(); From 34ef74732b35ab8774c41ceffec7f210e42924d6 Mon Sep 17 00:00:00 2001 From: "Join.Gong" Date: Mon, 26 Aug 2024 14:11:02 +0000 Subject: [PATCH 4/5] fix fmt issue. --- crates/apis/src/crypto/mod.rs | 4 ++-- crates/apis/src/fetch_io/blockless.rs | 6 +++--- crates/apis/src/fetch_io/mod.rs | 16 ++++++++-------- crates/apis/src/lib.rs | 9 +++++---- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/crates/apis/src/crypto/mod.rs b/crates/apis/src/crypto/mod.rs index 4cc44487..fbff9f75 100644 --- a/crates/apis/src/crypto/mod.rs +++ b/crates/apis/src/crypto/mod.rs @@ -1,9 +1,9 @@ use anyhow::Result; -use javy::{quickjs::JSValue, Runtime}; use javy::quickjs::{JSContextRef, JSValueRef}; +use javy::{quickjs::JSValue, Runtime}; -use rand::RngCore; use crate::{APIConfig, JSApiSet}; +use rand::RngCore; pub struct Crypto; diff --git a/crates/apis/src/fetch_io/blockless.rs b/crates/apis/src/fetch_io/blockless.rs index 138817bf..3e9ff329 100644 --- a/crates/apis/src/fetch_io/blockless.rs +++ b/crates/apis/src/fetch_io/blockless.rs @@ -1,7 +1,7 @@ #![allow(dead_code)] -use std::cmp::Ordering; -use serde_json::{json, Value}; use crate::fetch_io::FetchOptions; +use serde_json::{json, Value}; +use std::cmp::Ordering; pub type Handle = u32; @@ -227,4 +227,4 @@ extern "C" { #[link_name = "http_close"] pub(crate) fn http_close(handle: u32) -> u32; -} \ No newline at end of file +} diff --git a/crates/apis/src/fetch_io/mod.rs b/crates/apis/src/fetch_io/mod.rs index 5362461f..74a8114e 100644 --- a/crates/apis/src/fetch_io/mod.rs +++ b/crates/apis/src/fetch_io/mod.rs @@ -1,15 +1,15 @@ #![allow(dead_code)] mod blockless; -use std::collections::HashMap; use anyhow::{anyhow, Result}; use javy::{quickjs::JSValue, Runtime}; -use serde_json::{from_slice}; use serde::{Deserialize, Serialize}; +use serde_json::from_slice; +use std::collections::HashMap; +use crate::fetch_io::blockless::BlocklessHttp; use crate::{APIConfig, JSApiSet}; use javy::quickjs::{JSContextRef, JSValueRef}; -use crate::fetch_io::blockless::BlocklessHttp; pub(super) struct FetchIO; @@ -21,17 +21,16 @@ pub struct FetchOptions { impl FetchOptions { pub fn new(method: &str) -> Self { FetchOptions { - method: method.into() + method: method.into(), } } - + #[allow(clippy::inherent_to_string)] pub fn to_string(&self) -> String { serde_json::to_string(&self).unwrap() } } - impl JSApiSet for FetchIO { fn register(&self, runtime: &Runtime, _config: &APIConfig) -> Result<()> { let context = runtime.context(); @@ -48,7 +47,8 @@ impl JSApiSet for FetchIO { } } -fn fetchio_request() -> impl FnMut(&JSContextRef, JSValueRef, &[JSValueRef]) -> anyhow::Result { +fn fetchio_request( +) -> impl FnMut(&JSContextRef, JSValueRef, &[JSValueRef]) -> anyhow::Result { move |_ctx: &JSContextRef, _this: JSValueRef, args: &[JSValueRef]| { if args.len() != 4 { return Err(anyhow!("Expecting 4 arguments, received {}", args.len())); @@ -80,4 +80,4 @@ fn fetchio_request() -> impl FnMut(&JSContextRef, JSValueRef, &[JSValueRef]) -> Ok(JSValue::Object(response)) } -} \ No newline at end of file +} diff --git a/crates/apis/src/lib.rs b/crates/apis/src/lib.rs index a5cc74bc..8493d96b 100644 --- a/crates/apis/src/lib.rs +++ b/crates/apis/src/lib.rs @@ -48,11 +48,16 @@ use javy::Runtime; pub use api_config::APIConfig; #[cfg(feature = "console")] pub use console::LogStream; + pub use runtime_ext::RuntimeExt; mod api_config; #[cfg(feature = "console")] mod console; +#[cfg(feature = "crypto")] +mod crypto; +#[cfg(feature = "fetch_io")] +mod fetch_io; #[cfg(feature = "random")] mod random; mod runtime_ext; @@ -60,10 +65,6 @@ mod runtime_ext; mod stream_io; #[cfg(feature = "text_encoding")] mod text_encoding; -#[cfg(feature = "fetch_io")] -mod fetch_io; -#[cfg(feature = "crypto")] -mod crypto; pub(crate) trait JSApiSet { fn register(&self, runtime: &Runtime, config: &APIConfig) -> Result<()>; From 61fa8470e8000e325190d958aaf6cd37523185f7 Mon Sep 17 00:00:00 2001 From: "Join.Gong" Date: Mon, 26 Aug 2024 14:30:01 +0000 Subject: [PATCH 5/5] fix fmt issue. --- crates/cli/src/bytecode.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/cli/src/bytecode.rs b/crates/cli/src/bytecode.rs index a099a0f0..3c7c3c03 100644 --- a/crates/cli/src/bytecode.rs +++ b/crates/cli/src/bytecode.rs @@ -49,6 +49,7 @@ fn copy_source_code_into_instance( Ok((js_source_ptr, js_src_len)) } +#[allow(clippy::needless_borrows_for_generic_args)] fn call_compile( js_src_ptr: u32, js_src_len: u32, @@ -62,6 +63,7 @@ fn call_compile( Ok(ret_ptr) } +#[allow(clippy::needless_borrows_for_generic_args)] fn copy_bytecode_from_instance( ret_ptr: u32, mut store: &mut Store,