Skip to content

Commit

Permalink
move santize to one-time init
Browse files Browse the repository at this point in the history
  • Loading branch information
robtfm committed Jul 17, 2023
1 parent d1a0497 commit 63bc729
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/compose/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl ErrSource {
pub fn source<'a>(&'a self, composer: &'a Composer) -> Cow<'a, String> {
match self {
ErrSource::Module { name, defs, .. } => {
let raw_source = &composer.module_sets.get(name).unwrap().raw_source;
let raw_source = &composer.module_sets.get(name).unwrap().sanitized_source;
let Ok(PreprocessOutput {
preprocessed_source: source,
meta: PreprocessorMetaData { imports, .. },
Expand All @@ -51,7 +51,7 @@ impl ErrSource {
};

let Ok(source) = composer
.sanitize_and_substitute_shader_string(&source, &imports)
.substitute_shader_string(&source, &imports)
else { return Default::default() };

Cow::Owned(source)
Expand Down
43 changes: 24 additions & 19 deletions src/compose/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ pub struct ComposableModule {
pub struct ComposableModuleDefinition {
pub name: String,
// shader text (with auto bindings replaced - we do this on module add as we only want to do it once to avoid burning slots)
pub raw_source: String,
pub sanitized_source: String,
// language
pub language: ShaderLanguage,
// source path for error display
Expand Down Expand Up @@ -421,7 +421,12 @@ impl Composer {
undecor.to_string()
}

fn set_auto_bindings(&mut self, source: &str) -> String {
fn sanitize_and_set_auto_bindings(&mut self, source: &str) -> String {
let mut substituted_source = source.replace("\r\n", "\n").replace('\r', "\n");
if !substituted_source.ends_with('\n') {
substituted_source.push('\n');
}

// replace @binding(auto) with an incrementing index
struct AutoBindingReplacer<'a> {
auto: &'a mut u32,
Expand All @@ -435,7 +440,7 @@ impl Composer {
}

let substituted_source = self.auto_binding_regex.replace_all(
source,
&substituted_source,
AutoBindingReplacer {
auto: &mut self.auto_binding_index,
},
Expand All @@ -444,21 +449,17 @@ impl Composer {
substituted_source.into_owned()
}

fn sanitize_and_substitute_shader_string(
fn substitute_shader_string(
&self,
source: &str,
imports: &[ImportDefWithOffset],
) -> Result<String, ComposerErrorInner> {
let mut substituted_source = source.replace("\r\n", "\n").replace('\r', "\n");
if !substituted_source.ends_with('\n') {
substituted_source.push('\n');
}

// sort imports by decreasing length so we don't accidentally replace substrings of a longer import
let mut imports = imports.to_vec();
imports.sort_by_key(|import| usize::MAX - import.definition.as_name().len());

let mut imported_items = HashMap::new();
let mut substituted_source = source.to_owned();

for import in imports {
match import.definition.items {
Expand Down Expand Up @@ -907,11 +908,15 @@ impl Composer {
meta: PreprocessorMetaData { imports, .. },
} = self
.preprocessor
.preprocess(&module_definition.raw_source, shader_defs, self.validate)
.preprocess(
&module_definition.sanitized_source,
shader_defs,
self.validate,
)
.map_err(wrap_err)?;

let source = self
.sanitize_and_substitute_shader_string(&source, &imports)
.substitute_shader_string(&source, &imports)
.map_err(wrap_err)?;

let mut imports: Vec<_> = imports
Expand Down Expand Up @@ -1349,7 +1354,7 @@ impl Composer {
) -> Result<ComposableModule, ComposerError> {
let imports = self
.preprocessor
.preprocess(&module_set.raw_source, shader_defs, self.validate)
.preprocess(&module_set.sanitized_source, shader_defs, self.validate)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Module {
Expand Down Expand Up @@ -1527,7 +1532,7 @@ impl Composer {
}),
);

let substituted_source = self.set_auto_bindings(source);
let substituted_source = self.sanitize_and_set_auto_bindings(source);

let mut effective_defs = HashSet::new();
for import in &imports {
Expand Down Expand Up @@ -1567,7 +1572,7 @@ impl Composer {

let module_set = ComposableModuleDefinition {
name: module_name.clone(),
raw_source: substituted_source,
sanitized_source: substituted_source,
file_path: file_path.to_owned(),
language,
effective_defs: effective_defs.into_iter().collect(),
Expand Down Expand Up @@ -1649,9 +1654,9 @@ impl Composer {
})?;

let name = name.unwrap_or_default();
let substituted_source = self.set_auto_bindings(source);
let substituted_source = self.sanitize_and_set_auto_bindings(source);
let substituted_source = self
.sanitize_and_substitute_shader_string(&substituted_source, &imports)
.substitute_shader_string(&substituted_source, &imports)
.map_err(|inner| ComposerError {
inner,
source: ErrSource::Constructing {
Expand Down Expand Up @@ -1704,7 +1709,7 @@ impl Composer {

let definition = ComposableModuleDefinition {
name,
raw_source: substituted_source,
sanitized_source: substituted_source,
language: shader_type.into(),
file_path: file_path.to_owned(),
module_index: 0,
Expand All @@ -1722,7 +1727,7 @@ impl Composer {
inner: e.inner,
source: ErrSource::Constructing {
path: definition.file_path.to_owned(),
source: definition.raw_source.to_owned(),
source: definition.sanitized_source.to_owned(),
offset: e.source.offset(),
},
})?;
Expand Down Expand Up @@ -1815,7 +1820,7 @@ impl Composer {
match module_index {
0 => ErrSource::Constructing {
path: file_path.to_owned(),
source: definition.raw_source,
source: definition.sanitized_source,
offset: composable.start_offset,
},
_ => {
Expand Down

0 comments on commit 63bc729

Please sign in to comment.