Skip to content

Commit 5b2f63c

Browse files
committed
Refactor and optimize code across multiple modules
- Improved import statement parsing in the module system to handle quoted strings more efficiently. - Removed unnecessary Arc wrappers around RwLock in V8 callback registry for better performance. - Enhanced the BrowserEngine to create layout trees with a more structured approach, including new methods for building and extracting layout nodes. - Updated signal handling in the main runtime to ensure proper task management. - Reworked the Linux window implementation to utilize winit for event handling, supporting both X11 and Wayland. - Simplified cache control parsing in the PWA cache manager for better readability. - Streamlined cacheability checks in the PWA strategy executor using pattern matching. - Refined permission management in the sandbox to utilize type aliases for clarity and maintainability. - Improved Vulkan renderer code for better resource management and command buffer handling. - Updated shader management to use simpler string handling for entry points. - Enhanced security policy validation logic for better clarity and efficiency.
1 parent 32ed82c commit 5b2f63c

File tree

20 files changed

+721
-363
lines changed

20 files changed

+721
-363
lines changed

src/core/layout/grid.rs

Lines changed: 79 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ pub struct GridLayout {
204204
cache: Arc<dashmap::DashMap<NodeId, GridContainer>>,
205205
}
206206

207+
struct GridSizingContext<'a> {
208+
constraints: &'a LayoutConstraints,
209+
document: &'a Document,
210+
style_engine: &'a StyleEngine,
211+
layout_engine: &'a LayoutEngine,
212+
generation: u64,
213+
}
214+
207215
impl Default for GridLayout {
208216
fn default() -> Self {
209217
Self::new()
@@ -241,16 +249,16 @@ impl GridLayout {
241249

242250
self.place_grid_items(&mut grid_container, &mut grid_items)?;
243251

244-
self.size_grid_tracks(
245-
&mut grid_container,
246-
&grid_items,
247-
&constraints,
252+
let sizing_context = GridSizingContext {
253+
constraints: &constraints,
248254
document,
249255
style_engine,
250256
layout_engine,
251257
generation,
252-
)
253-
.await?;
258+
};
259+
260+
self.size_grid_tracks(&mut grid_container, &grid_items, sizing_context)
261+
.await?;
254262

255263
self.align_and_position_items(
256264
&grid_container,
@@ -286,37 +294,43 @@ impl GridLayout {
286294
&self,
287295
styles: &ComputedStyles,
288296
) -> std::result::Result<GridContainer, LayoutError> {
289-
let mut container = GridContainer::default();
290-
291-
container.row_gap = match styles.get_computed_value("row-gap") {
297+
let base_row_gap = match styles.get_computed_value("row-gap") {
292298
Ok(ComputedValue::Length(v)) => v,
293299
_ => 0.0,
294300
};
295-
container.column_gap = match styles.get_computed_value("column-gap") {
301+
302+
let base_column_gap = match styles.get_computed_value("column-gap") {
296303
Ok(ComputedValue::Length(v)) => v,
297304
_ => 0.0,
298305
};
299306

300-
if let Ok(gap) = styles.get_computed_value("gap") {
301-
if let ComputedValue::Length(gap_val) = gap {
302-
if container.row_gap == 0.0 {
303-
container.row_gap = gap_val;
304-
}
305-
if container.column_gap == 0.0 {
306-
container.column_gap = gap_val;
307-
}
308-
}
309-
}
310-
311-
container.justify_items = self.parse_justify_items(styles)?;
312-
container.align_items = self.parse_align_items(styles)?;
313-
container.justify_content = self.parse_justify_content(styles)?;
314-
container.align_content = self.parse_align_content(styles)?;
315-
316-
container.auto_flow = self.parse_grid_auto_flow(styles)?;
317-
container.dense = self.parse_grid_auto_flow_dense(styles);
307+
let (row_gap, column_gap) = match styles.get_computed_value("gap") {
308+
Ok(ComputedValue::Length(gap_val)) => (
309+
if base_row_gap == 0.0 {
310+
gap_val
311+
} else {
312+
base_row_gap
313+
},
314+
if base_column_gap == 0.0 {
315+
gap_val
316+
} else {
317+
base_column_gap
318+
},
319+
),
320+
_ => (base_row_gap, base_column_gap),
321+
};
318322

319-
Ok(container)
323+
Ok(GridContainer {
324+
row_gap,
325+
column_gap,
326+
justify_items: self.parse_justify_items(styles)?,
327+
align_items: self.parse_align_items(styles)?,
328+
justify_content: self.parse_justify_content(styles)?,
329+
align_content: self.parse_align_content(styles)?,
330+
auto_flow: self.parse_grid_auto_flow(styles)?,
331+
dense: self.parse_grid_auto_flow_dense(styles),
332+
..GridContainer::default()
333+
})
320334
}
321335

322336
fn parse_justify_items(
@@ -508,11 +522,11 @@ impl GridLayout {
508522
}
509523

510524
if let Ok(value) = styles.get_computed_value("grid-auto-rows") {
511-
container.implicit_row_size = self.parse_track_size(&value)?;
525+
container.implicit_row_size = Self::parse_track_size(&value)?;
512526
}
513527

514528
if let Ok(value) = styles.get_computed_value("grid-auto-columns") {
515-
container.implicit_column_size = self.parse_track_size(&value)?;
529+
container.implicit_column_size = Self::parse_track_size(&value)?;
516530
}
517531

518532
Ok(())
@@ -527,15 +541,15 @@ impl GridLayout {
527541
match value {
528542
ComputedValue::List(values) => {
529543
for val in values {
530-
let size = self.parse_track_size(val)?;
544+
let size = Self::parse_track_size(val)?;
531545
tracks.push(GridTrack {
532546
size,
533547
..GridTrack::default()
534548
});
535549
}
536550
}
537551
_ => {
538-
let size = self.parse_track_size(value)?;
552+
let size = Self::parse_track_size(value)?;
539553
tracks.push(GridTrack {
540554
size,
541555
..GridTrack::default()
@@ -546,10 +560,7 @@ impl GridLayout {
546560
Ok(tracks)
547561
}
548562

549-
fn parse_track_size(
550-
&self,
551-
value: &ComputedValue,
552-
) -> std::result::Result<TrackSize, LayoutError> {
563+
fn parse_track_size(value: &ComputedValue) -> std::result::Result<TrackSize, LayoutError> {
553564
match value {
554565
ComputedValue::Length(length) => Ok(TrackSize::Length(*length)),
555566
ComputedValue::Percentage(percentage) => Ok(TrackSize::Percentage(*percentage)),
@@ -572,16 +583,16 @@ impl GridLayout {
572583
ComputedValue::Function { name, args } => match name.as_str() {
573584
"minmax" => {
574585
if args.len() == 2 {
575-
let min_size = self.parse_track_size(&args[0])?;
576-
let max_size = self.parse_track_size(&args[1])?;
586+
let min_size = Self::parse_track_size(&args[0])?;
587+
let max_size = Self::parse_track_size(&args[1])?;
577588
Ok(TrackSize::MinMax(Box::new(min_size), Box::new(max_size)))
578589
} else {
579590
Ok(TrackSize::Auto)
580591
}
581592
}
582593
"fit-content" => {
583594
if args.len() == 1 {
584-
let size = self.parse_track_size(&args[0])?;
595+
let size = Self::parse_track_size(&args[0])?;
585596
Ok(TrackSize::FitContent(Box::new(size)))
586597
} else {
587598
Ok(TrackSize::Auto)
@@ -618,16 +629,20 @@ impl GridLayout {
618629
container: &GridContainer,
619630
_placement_grid: &PlacementGrid,
620631
) -> std::result::Result<ResolvedGridArea, LayoutError> {
621-
let mut resolved = ResolvedGridArea::default();
622-
623-
resolved.row_start =
624-
self.resolve_grid_line(&area.row_start, container.row_tracks.len(), true)?;
625-
resolved.row_end =
626-
self.resolve_grid_line(&area.row_end, container.row_tracks.len(), true)?;
627-
resolved.column_start =
628-
self.resolve_grid_line(&area.column_start, container.column_tracks.len(), false)?;
629-
resolved.column_end =
630-
self.resolve_grid_line(&area.column_end, container.column_tracks.len(), false)?;
632+
let mut resolved = ResolvedGridArea {
633+
row_start: self.resolve_grid_line(&area.row_start, container.row_tracks.len(), true)?,
634+
row_end: self.resolve_grid_line(&area.row_end, container.row_tracks.len(), true)?,
635+
column_start: self.resolve_grid_line(
636+
&area.column_start,
637+
container.column_tracks.len(),
638+
false,
639+
)?,
640+
column_end: self.resolve_grid_line(
641+
&area.column_end,
642+
container.column_tracks.len(),
643+
false,
644+
)?,
645+
};
631646

632647
if resolved.row_start >= resolved.row_end {
633648
resolved.row_end = resolved.row_start + 1;
@@ -682,27 +697,19 @@ impl GridLayout {
682697
&self,
683698
container: &mut GridContainer,
684699
items: &[GridItem],
685-
constraints: &LayoutConstraints,
686-
document: &Document,
687-
style_engine: &StyleEngine,
688-
layout_engine: &LayoutEngine,
689-
generation: u64,
700+
context: GridSizingContext<'_>,
690701
) -> std::result::Result<(), LayoutError> {
691-
let available_width = constraints.available_width.unwrap_or(f32::INFINITY);
692-
let available_height = constraints.available_height.unwrap_or(f32::INFINITY);
702+
let available_width = context.constraints.available_width.unwrap_or(f32::INFINITY);
703+
let available_height = context
704+
.constraints
705+
.available_height
706+
.unwrap_or(f32::INFINITY);
693707

694708
self.initialize_track_sizes(&mut container.column_tracks, available_width);
695709
self.initialize_track_sizes(&mut container.row_tracks, available_height);
696710

697-
self.resolve_intrinsic_track_sizes(
698-
container,
699-
items,
700-
document,
701-
style_engine,
702-
layout_engine,
703-
generation,
704-
)
705-
.await?;
711+
self.resolve_intrinsic_track_sizes(container, items, &context)
712+
.await?;
706713

707714
self.maximize_tracks(&mut container.column_tracks);
708715
self.maximize_tracks(&mut container.row_tracks);
@@ -760,22 +767,20 @@ impl GridLayout {
760767
&self,
761768
container: &mut GridContainer,
762769
items: &[GridItem],
763-
document: &Document,
764-
style_engine: &StyleEngine,
765-
layout_engine: &LayoutEngine,
766-
generation: u64,
770+
context: &GridSizingContext<'_>,
767771
) -> std::result::Result<(), LayoutError> {
768772
// Simplified intrinsic sizing - real implementation would be more comprehensive
769773
for item in items {
770774
let constraints = LayoutConstraints::default();
771775

772-
if let Ok(layout_result) = layout_engine
776+
if let Ok(layout_result) = context
777+
.layout_engine
773778
.layout_node_public(
774779
item.node_id,
775780
constraints,
776-
document,
777-
style_engine,
778-
generation,
781+
context.document,
782+
context.style_engine,
783+
context.generation,
779784
)
780785
.await
781786
{

src/core/network/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,12 @@ impl SecurityPolicy {
514514
ipv4.is_private() || ipv4.is_loopback() || ipv4.is_link_local()
515515
}
516516
std::net::IpAddr::V6(ipv6) => {
517-
ipv6.is_loopback() || ipv6.is_unique_local() || ipv6.is_unicast_link_local()
517+
let octets = ipv6.octets();
518+
let prefix = u16::from_be_bytes([octets[0], octets[1]]);
519+
let is_unique_local = (prefix & 0xfe00) == 0xfc00;
520+
let is_link_local = (prefix & 0xffc0) == 0xfe80;
521+
522+
ipv6.is_loopback() || is_unique_local || is_link_local
518523
}
519524
}
520525
}

src/js_engine/jit/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,16 @@ impl ProfilerData {
196196
}
197197

198198
pub struct JITCompiler {
199-
module: Arc<Mutex<JITModule>>,
200-
builder_context: Arc<Mutex<FunctionBuilderContext>>,
201-
cranelift_context: Arc<Mutex<CraneliftContext>>,
202-
compiled_functions: Arc<DashMap<String, CompiledFunction>>,
199+
module: Mutex<JITModule>,
200+
builder_context: Mutex<FunctionBuilderContext>,
201+
cranelift_context: Mutex<CraneliftContext>,
202+
compiled_functions: DashMap<String, CompiledFunction>,
203203
optimization_level: OptimizationLevel,
204-
profiler_data: Arc<ProfilerData>,
205-
runtime_helpers: Arc<DashMap<String, *const u8>>,
206-
type_specializations: Arc<DashMap<String, HashMap<String, FuncId>>>,
204+
profiler_data: ProfilerData,
205+
runtime_helpers: DashMap<String, *const u8>,
206+
type_specializations: DashMap<String, HashMap<String, FuncId>>,
207207
#[allow(dead_code)]
208-
deoptimization_stubs: Arc<DashMap<String, FuncId>>,
208+
deoptimization_stubs: DashMap<String, FuncId>,
209209
}
210210

211211
impl JITCompiler {
@@ -246,15 +246,15 @@ impl JITCompiler {
246246
let module = JITModule::new(builder);
247247

248248
let compiler = Self {
249-
module: Arc::new(Mutex::new(module)),
250-
builder_context: Arc::new(Mutex::new(FunctionBuilderContext::new())),
251-
cranelift_context: Arc::new(Mutex::new(CraneliftContext::new())),
252-
compiled_functions: Arc::new(DashMap::new()),
249+
module: Mutex::new(module),
250+
builder_context: Mutex::new(FunctionBuilderContext::new()),
251+
cranelift_context: Mutex::new(CraneliftContext::new()),
252+
compiled_functions: DashMap::new(),
253253
optimization_level,
254-
profiler_data: Arc::new(ProfilerData::new()),
255-
runtime_helpers: Arc::new(DashMap::new()),
256-
type_specializations: Arc::new(DashMap::new()),
257-
deoptimization_stubs: Arc::new(DashMap::new()),
254+
profiler_data: ProfilerData::new(),
255+
runtime_helpers: DashMap::new(),
256+
type_specializations: DashMap::new(),
257+
deoptimization_stubs: DashMap::new(),
258258
};
259259

260260
compiler.setup_runtime_helpers().await?;

src/js_engine/mod.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ unsafe impl Sync for RuntimeCore {}
191191

192192
pub struct JSRuntime {
193193
core: Arc<Mutex<RuntimeCore>>,
194-
jit_compiler: Arc<JITCompiler>,
194+
jit_compiler: JITCompiler,
195195
garbage_collector: Arc<AsyncMutex<GarbageCollector>>,
196196
#[allow(dead_code)]
197197
heap_manager: Arc<HeapManager>,
@@ -225,11 +225,9 @@ impl JSRuntime {
225225
OptimizationLevel::None
226226
};
227227

228-
let jit_compiler = Arc::new(
229-
JITCompiler::new(optimization_level)
230-
.await
231-
.map_err(|e| JSError::JIT(format!("JIT compiler initialization failed: {}", e)))?,
232-
);
228+
let jit_compiler = JITCompiler::new(optimization_level)
229+
.await
230+
.map_err(|e| JSError::JIT(format!("JIT compiler initialization failed: {}", e)))?;
233231

234232
let heap_manager = Arc::new(HeapManager::new());
235233
let garbage_collector = Arc::new(AsyncMutex::new(GarbageCollector::new()));
@@ -324,16 +322,14 @@ impl JSRuntime {
324322
.await;
325323
}
326324

327-
let result = {
328-
let mut core = self.core.lock();
329-
330-
if let Some(jit_function) = self.get_jit_compiled_function(script_hash).await {
331-
self.execute_jit_function(&jit_function, context_id).await
332-
} else {
333-
core.v8_runtime
334-
.execute(script)
335-
.map_err(|e| JSError::Execution(e.to_string()))
336-
}
325+
let result = if let Some(jit_function) = self.get_jit_compiled_function(script_hash).await {
326+
self.execute_jit_function(&jit_function, context_id).await
327+
} else {
328+
self.core
329+
.lock()
330+
.v8_runtime
331+
.execute(script)
332+
.map_err(|e| JSError::Execution(e.to_string()))
337333
}?;
338334

339335
self.update_context_usage(context_id).await;

src/js_engine/modules/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ impl ModuleSystem {
121121
if line.starts_with("import ") {
122122
if let Some(from_pos) = line.find(" from ") {
123123
let spec_part = &line[from_pos + 6..].trim();
124-
if spec_part.starts_with('"') && spec_part.ends_with('"') {
125-
return Some(spec_part[1..spec_part.len() - 1].to_string());
126-
} else if spec_part.starts_with('\'') && spec_part.ends_with('\'') {
127-
return Some(spec_part[1..spec_part.len() - 1].to_string());
124+
if spec_part.len() >= 2 {
125+
let bytes = spec_part.as_bytes();
126+
let quote = bytes[0];
127+
if (quote == b'"' || quote == b'\'') && bytes.last() == Some(&quote) {
128+
return Some(spec_part[1..spec_part.len() - 1].to_string());
129+
}
128130
}
129131
}
130132
}

0 commit comments

Comments
 (0)