Skip to content

Commit

Permalink
Internal cleanup: Simplify string handling when accessing compiler-em…
Browse files Browse the repository at this point in the history
…bedded files

For loading images that are included in the widget library that's included in turn
in the compiler binary, we need to create ImageInner::EmbeddedData
with &'static data and &'static file extension. The latter was
created using string interning, but we can also access the path of the
widget library data structure.
  • Loading branch information
tronical committed Oct 5, 2021
1 parent df9e9dd commit 4b267a8
Showing 6 changed files with 10 additions and 22 deletions.
1 change: 1 addition & 0 deletions sixtyfps_compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
@@ -605,6 +605,7 @@ pub fn generate(doc: &Document, diag: &mut BuildDiagnostics) -> Option<impl std:
.into(),
Some(builtin) => crate::library::load_file(std::path::Path::new(builtin))
.expect("non-existent internal file referenced")
.contents
.into(),
};

2 changes: 1 addition & 1 deletion sixtyfps_compiler/generator/rust.rs
Original file line number Diff line number Diff line change
@@ -2091,7 +2091,7 @@ fn embedded_file_tokens(path: &str) -> TokenStream {
Some(builtin) => {
let data = crate::library::load_file(std::path::Path::new(builtin))
.expect("non-existent internal file referenced");
let literal = proc_macro2::Literal::byte_string(data);
let literal = proc_macro2::Literal::byte_string(data.contents);
quote!(#literal)
}
}
4 changes: 2 additions & 2 deletions sixtyfps_compiler/lib.rs
Original file line number Diff line number Diff line change
@@ -136,7 +136,7 @@ pub async fn compile_syntax_node(
pub mod library {
include!(env!("SIXTYFPS_WIDGETS_LIBRARY"));

pub fn load_file(builtin_path: &std::path::Path) -> Option<&'static [u8]> {
pub fn load_file(builtin_path: &std::path::Path) -> Option<&'static VirtualFile<'static>> {
let mut components = vec![];
for part in builtin_path.iter() {
if part == ".." {
@@ -147,7 +147,7 @@ pub mod library {
}
if let &[folder, file] = components.as_slice() {
let library = widget_library().iter().find(|x| x.0 == folder)?.1;
library.iter().find_map(|vf| if vf.path == file { Some(vf.contents) } else { None })
library.iter().find_map(|vf| if vf.path == file { Some(*vf) } else { None })
} else {
None
}
5 changes: 2 additions & 3 deletions sixtyfps_compiler/typeloader.rs
Original file line number Diff line number Diff line change
@@ -388,9 +388,8 @@ impl<'a> TypeLoader<'a> {
let candidate = include_dir.join(file_to_import);
match candidate.strip_prefix("builtin:/") {
Err(_) => candidate.exists().then(|| (candidate, None)),
Ok(builtin) => {
crate::library::load_file(builtin).map(|data| (candidate, Some(data)))
}
Ok(builtin) => crate::library::load_file(builtin)
.map(|virtual_file| (candidate, Some(virtual_file.contents))),
}
})
}
1 change: 0 additions & 1 deletion sixtyfps_runtime/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -30,7 +30,6 @@ derive_more = "0.99.5"
generativity = "1"
once_cell = "1.5"
thiserror = "1"
lazy_static = "1"

[dependencies.spin_on]
version = "0.1"
19 changes: 4 additions & 15 deletions sixtyfps_runtime/interpreter/eval.rs
Original file line number Diff line number Diff line change
@@ -485,15 +485,17 @@ pub fn eval_expression(expression: &Expression, local_context: &mut EvalLocalCon
let extra_data = toplevel_instance.component_type.extra_data_offset.apply(toplevel_instance.as_ref());
let path = extra_data.embedded_file_resources.get(resource_id).expect("internal error: invalid resource id");

let data = match path.strip_prefix("builtin:/") {
let virtual_file = match path.strip_prefix("builtin:/") {
None => unimplemented!(),
Some(builtin) => {
sixtyfps_compilerlib::library::load_file(std::path::Path::new(builtin))
.expect("non-existent internal file referenced")
}
};
let virtual_file_extension = std::path::Path::new(virtual_file.path).extension().unwrap().to_str().unwrap();
debug_assert_eq!(virtual_file_extension, extension);
Ok(corelib::graphics::Image::from(
corelib::graphics::ImageInner::EmbeddedData{ data: corelib::slice::Slice::from_slice(data), format: corelib::slice::Slice::from_slice(interned_str(extension).as_bytes()) }
corelib::graphics::ImageInner::EmbeddedData{ data: corelib::slice::Slice::from_slice(virtual_file.contents), format: corelib::slice::Slice::from_slice(virtual_file_extension.as_bytes()) }
))
}
}.unwrap_or_else(|_| {
@@ -1097,16 +1099,3 @@ pub fn default_value_for_type(ty: &Type) -> Value {
}
}
}

lazy_static::lazy_static! {
static ref INTERNED_STRINGS: std::sync::Mutex<std::collections::HashSet<String>> = Default::default();
}

pub fn interned_str(s: &str) -> &'static str {
let mut cache = INTERNED_STRINGS.lock().unwrap();
if !cache.contains(s) {
cache.insert(s.into());
}
let s: &str = cache.get(s).unwrap();
return unsafe { std::mem::transmute(s) };
}

0 comments on commit 4b267a8

Please sign in to comment.