From 5d229ff5521c8ae9289de9586fb4bf696b3f9e6d Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 31 Aug 2021 12:22:37 +0800 Subject: [PATCH] Added a workaround for rust-lang/rust#56639 --- crates/codegen/src/code.rs | 31 ++++++++++++++++++++++-------- crates/rune-cli/src/run/command.rs | 6 +++--- integration-tests/src/bin/main.rs | 13 ++++++++++++- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/crates/codegen/src/code.rs b/crates/codegen/src/code.rs index 072d4d6e6c..b7031eb671 100644 --- a/crates/codegen/src/code.rs +++ b/crates/codegen/src/code.rs @@ -72,7 +72,17 @@ fn embed_inline_resources( /// /// These resources are embedded in a WebAssembly custom section /// and located at runtime. - mod _embedded_default_resource_values { + /// + /// # Note to Implementors + /// + /// We put the `static` variables inside an exported function + /// instead of their own module because of [#56639 - *Custom section + /// generation under wasm32-unknown-unknown is inconsistent and + /// unintuitive*](https://github.com/rust-lang/rust/issues/56639) + #[allow(bad_style)] + #[doc(hidden)] + #[no_mangle] + pub extern "C" fn _embedded_default_resource_values() { use hotg_rune_core::InlineResource; #( #initializers )* @@ -99,8 +109,9 @@ fn inline_resource_from_disk( let path = path.display().to_string(); Ok(quote! { + #[used] #[link_section = #section] - pub(crate) static #ident: InlineResource<#name_len, #data_len> = InlineResource::new( + pub static #ident: InlineResource<#name_len, #data_len> = InlineResource::new( *#name_bytes, *include_bytes!(#path), ); @@ -117,8 +128,9 @@ fn inline_resource_from_literal(name: &str, data: &[u8]) -> TokenStream { let ident = variable_name(name); quote! { + #[used] #[link_section = #section] - pub(crate) static #ident: InlineResource<#name_len, #data_len> = InlineResource::new( + pub static #ident: InlineResource<#name_len, #data_len> = InlineResource::new( *#name_bytes, *#data_bytes, ); @@ -166,6 +178,7 @@ fn declare_resources(rune: &Rune, image_crate: &TokenStream) -> TokenStream { } else { quote! { /// Lazily loaded accessors for all resources used by this Rune. + #[allow(bad_style)] mod resources { lazy_static! { #( #initializers )* @@ -196,13 +209,14 @@ fn declare_models(rune: &Rune) -> TokenStream { TokenStream::new() } else { quote! { + #[allow(bad_style)] mod models { #( #initializers )* } } } } fn variable_name(name: impl Display) -> Ident { - let name = name.to_string().replace("-", "_").to_uppercase(); + let name = name.to_string().replace("-", "_"); Ident::new(&name, Span::call_site()) } @@ -617,7 +631,7 @@ fn quote_value(value: &Value) -> TokenStream { Value::String(ResourceOrString::String(s)) => quote!(#s), Value::String(ResourceOrString::Resource(resource)) => { let variable = variable_name(&resource.0); - quote!(&resources::#variable) + quote!(&*resources::#variable) }, Value::List(list) => { let values = list.iter().map(quote_value); @@ -883,7 +897,7 @@ mod tests { let should_be = quote! { let mut sine = runicos_base_wasm::Model::load( "application/tflite-model", - &models::SINE, + &models::sine, &[hotg_rune_core::Shape::new( hotg_rune_core::reflect::Type::f32, [1usize].as_ref() @@ -1214,7 +1228,7 @@ mod tests { let should_be = quote! { let mut sine = runicos_base_wasm::Model::load( #TFLITE_MIMETYPE, - &models::SINE, + &models::sine, &[ hotg_rune_core::Shape::new(hotg_rune_core::reflect::Type::u8, [18000usize].as_ref()), hotg_rune_core::Shape::new(hotg_rune_core::reflect::Type::f32, [1usize].as_ref()) @@ -1271,10 +1285,11 @@ mod tests { let should_be = quote! { /// Lazily loaded accessors for all resources used by this Rune. + #[allow(bad_style)] mod resources { lazy_static! { pub(crate) static ref SINE_MODEL: alloc::vec::Vec = hotg_runicos_base_wasm::Resource::read_to_end("SINE_MODEL"); - pub(crate) static ref SINE: &'static [u8] = include_bytes!("models/sine.tflite"); + pub(crate) static ref sine: &'static [u8] = include_bytes!("models/sine.tflite"); } } }; diff --git a/crates/rune-cli/src/run/command.rs b/crates/rune-cli/src/run/command.rs index 2009c80b13..effbe2177b 100644 --- a/crates/rune-cli/src/run/command.rs +++ b/crates/rune-cli/src/run/command.rs @@ -150,9 +150,9 @@ impl Run { /// capability. /// /// For example, imagine passing the path for 3 images to the `rune` CLI. -/// Inside the Rune, we'll instantiate a [`Capability`] object and set the -/// `"source"` parameter to `1`. This then tells the [`CapabilityFactory`] that -/// we want to read from the second image. +/// Inside the Rune, we'll instantiate a [`SourceBackedCapability`] object and +/// set the `"source"` parameter to `1`. This then tells the +/// [`CapabilityFactory`] that we want to read from the second image. fn capability_switcher( items: &[T], mut make_source: F, diff --git a/integration-tests/src/bin/main.rs b/integration-tests/src/bin/main.rs index a0f5949325..1cafd07e3f 100644 --- a/integration-tests/src/bin/main.rs +++ b/integration-tests/src/bin/main.rs @@ -9,7 +9,18 @@ use std::{ }; use env_logger::Env; -const DEFAULT_RUST_LOG: &str = "info"; +pub const DEFAULT_RUST_LOG: &str = concat!( + "info,", + "hotg_rune_cli=debug,", + "hotg_rune_codegen=debug,", + "hotg_rune_core=debug,", + "hotg_rune_runtime=debug,", + "hotg_rune_syntax=debug,", + "hotg_rune_wasmer_runtime=debug,", + "hotg_rune_wasm3_runtime=debug,", + "hotg_runicos_base_runtime=debug,", + "regalloc=warn,", +); fn main() -> Result<(), Error> { let env = Env::new().default_filter_or(DEFAULT_RUST_LOG);