Skip to content

Commit 9ce2b84

Browse files
committed
Prepare implementing file transfer for remote viewers.
1 parent c4ccf30 commit 9ce2b84

25 files changed

Lines changed: 236 additions & 46 deletions

File tree

api/rs/build/lib.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,12 @@ pub fn compile_with_output_path(
568568
let syntax_node = syntax_node.expect("diags contained no compilation errors");
569569

570570
// 'spin_on' is ok here because the compiler in single threaded and does not block if there is no blocking future
571-
let (doc, diag, loader) =
572-
spin_on::spin_on(i_slint_compiler::compile_syntax_node(syntax_node, diag, compiler_config));
571+
let (doc, diag, loader) = spin_on::spin_on(i_slint_compiler::compile_syntax_node(
572+
syntax_node,
573+
diag,
574+
compiler_config,
575+
(),
576+
));
573577

574578
if diag.has_errors()
575579
|| (!diag.is_empty() && std::env::var("SLINT_COMPILER_DENY_WARNINGS").is_ok())

api/rs/macros/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pub fn slint(stream: TokenStream) -> TokenStream {
372372
//println!("{syntax_node:#?}");
373373
compiler_config.translation_domain = std::env::var("CARGO_PKG_NAME").ok();
374374
let (root_component, diag, loader) =
375-
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config));
375+
spin_on::spin_on(compile_syntax_node(syntax_node, diag, compiler_config, ()));
376376
//println!("{tree:#?}");
377377
if diag.has_errors() {
378378
return diag.report_macro_diagnostic(&tokens);

internal/compiler/benches/semantic_analysis.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn compile_full(source: &str) -> (Document, BuildDiagnostics) {
209209
let config = CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Interpreter);
210210

211211
let (doc, diag, _loader) =
212-
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config));
212+
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config, ()));
213213
(doc, diag)
214214
}
215215

@@ -363,7 +363,7 @@ mod proc_macro_simulation {
363363
let config = CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Rust);
364364

365365
let (doc, diag, loader) =
366-
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config));
366+
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config, ()));
367367

368368
let rust_code = i_slint_compiler::generator::rust::generate(&doc, &loader.compiler_config)
369369
.expect("Rust code generation failed");
@@ -427,6 +427,7 @@ mod phase_breakdown {
427427
node,
428428
diagnostics,
429429
config,
430+
(),
430431
)));
431432
}
432433

@@ -438,7 +439,7 @@ mod phase_breakdown {
438439
let node = parse_source(EMPTY_COMPONENT);
439440
let config = CompilerConfiguration::new(i_slint_compiler::generator::OutputFormat::Rust);
440441
let (doc, _diag, loader) =
441-
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config));
442+
spin_on::spin_on(i_slint_compiler::compile_syntax_node(node, diagnostics, config, ()));
442443

443444
// Now benchmark just the code generation
444445
divan::black_box(
@@ -503,6 +504,7 @@ mod phase_breakdown {
503504
&mut loader,
504505
false,
505506
&mut diag,
507+
(),
506508
)));
507509
}
508510

internal/compiler/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ pub async fn compile_syntax_node(
297297
doc_node: parser::SyntaxNode,
298298
mut diagnostics: diagnostics::BuildDiagnostics,
299299
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
300+
resource_preloader: impl crate::passes::ResourcePreloader,
300301
) -> (object_tree::Document, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
301302
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
302303

@@ -316,7 +317,8 @@ pub async fn compile_syntax_node(
316317
);
317318

318319
if !diagnostics.has_errors() {
319-
passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics).await;
320+
passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics, resource_preloader)
321+
.await;
320322
} else {
321323
// Don't run all the passes in case of errors because because some invariants are not met.
322324
passes::run_import_passes(&doc, &loader, &mut diagnostics);
@@ -335,11 +337,13 @@ pub async fn load_root_file(
335337
source_code: String,
336338
mut diagnostics: diagnostics::BuildDiagnostics,
337339
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
340+
resource_preloader: impl passes::ResourcePreloader,
338341
) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
339342
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
340343

341-
let (path, _) =
342-
loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;
344+
let (path, _) = loader
345+
.load_root_file(path, source_path, source_code, false, &mut diagnostics, resource_preloader)
346+
.await;
343347

344348
(path, diagnostics, loader)
345349
}
@@ -356,6 +360,7 @@ pub async fn load_root_file_with_raw_type_loader(
356360
source_code: String,
357361
mut diagnostics: diagnostics::BuildDiagnostics,
358362
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
363+
resource_preloader: impl passes::ResourcePreloader,
359364
) -> (
360365
std::path::PathBuf,
361366
diagnostics::BuildDiagnostics,
@@ -364,8 +369,9 @@ pub async fn load_root_file_with_raw_type_loader(
364369
) {
365370
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
366371

367-
let (path, raw_type_loader) =
368-
loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;
372+
let (path, raw_type_loader) = loader
373+
.load_root_file(path, source_path, source_code, true, &mut diagnostics, resource_preloader)
374+
.await;
369375

370376
(path, diagnostics, loader, raw_type_loader)
371377
}

internal/compiler/passes.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use crate::expression_tree::Expression;
6161
use smol_str::SmolStr;
6262

6363
pub use binding_analysis::GlobalAnalysis;
64+
pub use embed_images::ResourcePreloader;
6465

6566
pub fn ignore_debug_hooks(expr: &Expression) -> &Expression {
6667
let mut expr = expr;
@@ -77,6 +78,7 @@ pub async fn run_passes(
7778
type_loader: &mut crate::typeloader::TypeLoader,
7879
keep_raw: bool,
7980
diag: &mut crate::diagnostics::BuildDiagnostics,
81+
resource_preloader: impl ResourcePreloader,
8082
) -> Option<crate::typeloader::TypeLoader> {
8183
let style_metrics = {
8284
// Ignore import errors
@@ -246,6 +248,7 @@ pub async fn run_passes(
246248
type_loader.compiler_config.embed_resources,
247249
type_loader.compiler_config.const_scale_factor.unwrap_or(1.),
248250
&type_loader.compiler_config.resource_url_mapper,
251+
resource_preloader,
249252
diag,
250253
)
251254
.await;

internal/compiler/passes/const_propagation.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ export component Foo {
356356
&mut test_diags,
357357
);
358358
let (doc, diag, _) =
359-
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config));
359+
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config, ()));
360360
assert!(!diag.has_errors(), "slint compile error {:#?}", diag.to_string_vec());
361361

362362
let expected_p = 3.0 * 2.0 + 15.0;
@@ -487,7 +487,7 @@ export component Foo inherits Window {{
487487
crate::CompilerConfiguration::new(crate::generator::OutputFormat::Interpreter);
488488
compiler_config.style = Some("fluent".into());
489489
let (doc, diag, _) =
490-
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config));
490+
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config, ()));
491491
assert!(!diag.has_errors(), "slint compile error {:#?}", diag.to_string_vec());
492492

493493
let bindings = &doc.inner_components.last().unwrap().root_element.borrow().bindings;
@@ -514,7 +514,7 @@ export component Foo inherits Window {
514514
compiler_config.style = Some("fluent".into());
515515
compiler_config.const_scale_factor = Some(2.);
516516
let (doc, diag, _) =
517-
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config));
517+
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config, ()));
518518
assert!(!diag.has_errors(), "slint compile error {:#?}", diag.to_string_vec());
519519

520520
let bindings = &doc.inner_components.last().unwrap().root_element.borrow().bindings;

internal/compiler/passes/default_geometry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ fn test_no_property_for_100pc() {
565565
&mut test_diags,
566566
);
567567
let (doc, diag, _) =
568-
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config));
568+
spin_on::spin_on(crate::compile_syntax_node(doc_node, test_diags, compiler_config, ()));
569569
assert!(!diag.has_errors(), "{:?}", diag.to_string_vec());
570570

571571
let root_elem = doc.inner_components.last().unwrap().root_element.borrow();

internal/compiler/passes/embed_images.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,37 @@ use std::collections::HashMap;
1414
use std::future::Future;
1515
use std::pin::Pin;
1616
use std::rc::Rc;
17+
use std::sync::Arc;
1718
use typed_index_collections::TiVec;
1819

20+
pub trait ResourcePreloader {
21+
fn load<'a>(
22+
&self,
23+
urls: impl Iterator<Item = &'a str>,
24+
push: impl FnMut(/* url */ &'a str, /* extension */ String, /* data */ Arc<[u8]>),
25+
) -> impl Future<Output = ()>;
26+
}
27+
28+
impl ResourcePreloader for () {
29+
fn load<'a>(
30+
&self,
31+
_urls: impl Iterator<Item = &'a str>,
32+
_push: impl FnMut(
33+
/* url */ &'a str,
34+
/* extension */ String,
35+
/* data */ Arc<[u8]>,
36+
),
37+
) -> impl Future<Output = ()> {
38+
std::future::ready(())
39+
}
40+
}
41+
1942
pub async fn embed_images(
2043
doc: &Document,
2144
embed_files: EmbedResourcesKind,
2245
scale_factor: f32,
2346
resource_url_mapper: &Option<Rc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = Option<String>>>>>>,
47+
resource_preloader: impl ResourcePreloader,
2448
diag: &mut BuildDiagnostics,
2549
) {
2650
if embed_files == EmbedResourcesKind::Nothing && resource_url_mapper.is_none() {
@@ -54,6 +78,20 @@ pub async fn embed_images(
5478
urls
5579
};
5680

81+
resource_preloader
82+
.load(
83+
mapped_urls.values().filter_map(|url| url.as_ref().map(SmolStr::as_str)),
84+
|url: &str, extension, data| {
85+
let mut resources = global_embedded_resources.borrow_mut();
86+
let id = resources.push_and_get_key(EmbeddedResources {
87+
path: Some(url.into()),
88+
kind: EmbeddedResourcesKind::DataUriPayload(data.to_vec(), extension),
89+
});
90+
path_to_id.insert(url.into(), id);
91+
},
92+
)
93+
.await;
94+
5795
// Use URLs (sync!):
5896
for component in &all_components {
5997
visit_all_expressions(component, |e, _| {

internal/compiler/tests/syntax_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ fn process_file_source(
447447
syntax_node.clone(),
448448
parse_diagnostics,
449449
compiler_config.clone(),
450+
(),
450451
));
451452
build_diags
452453
} else {
@@ -472,6 +473,7 @@ fn process_file_source(
472473
syntax_node,
473474
compile_diagnostics,
474475
compiler_config,
476+
(),
475477
));
476478
}
477479

internal/compiler/typeloader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ impl TypeLoader {
14681468
source_code: String,
14691469
keep_raw: bool,
14701470
diag: &mut BuildDiagnostics,
1471+
resource_preloader: impl crate::passes::ResourcePreloader,
14711472
) -> (PathBuf, Option<TypeLoader>) {
14721473
let path = crate::pathutils::clean_path(path);
14731474
let doc_node: syntax_nodes::Document =
@@ -1480,7 +1481,8 @@ impl TypeLoader {
14801481
let mut state = state.borrow_mut();
14811482
let state = &mut *state;
14821483
let raw_type_loader = if !state.diag.has_errors() {
1483-
crate::passes::run_passes(&mut doc, state.tl, keep_raw, state.diag).await
1484+
crate::passes::run_passes(&mut doc, state.tl, keep_raw, state.diag, resource_preloader)
1485+
.await
14841486
} else {
14851487
None
14861488
};

0 commit comments

Comments
 (0)