Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/plotnik-lib/src/bytecode/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,18 @@ impl<'a> TypesView<'a> {
let count = def.count as usize;
(0..count).map(move |i| self.get_member(start + i))
}

/// Unwrap Optional wrapper and return (inner_type, is_optional).
/// If not Optional, returns (type_id, false).
pub fn unwrap_optional(&self, type_id: QTypeId) -> (QTypeId, bool) {
let Some(type_def) = self.get(type_id) else {
return (type_id, false);
};
if !type_def.is_optional() {
return (type_id, false);
}
(QTypeId(type_def.data), true)
}
}

/// View into entrypoints.
Expand Down
6 changes: 6 additions & 0 deletions crates/plotnik-lib/src/bytecode/type_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl TypeDef {
TypeKind::from_u8(self.kind).is_some_and(|k| k.is_alias())
}

/// Whether this is an Optional wrapper type.
#[inline]
pub fn is_optional(&self) -> bool {
TypeKind::from_u8(self.kind).is_some_and(|k| k == TypeKind::Optional)
}

/// For alias types, get the target type.
#[inline]
pub fn alias_target(&self) -> Option<QTypeId> {
Expand Down
2 changes: 1 addition & 1 deletion crates/plotnik-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

pub mod analyze;
pub mod bytecode;
pub mod typegen;
pub mod compile;
pub mod diagnostics;
pub mod emit;
pub mod parser;
pub mod query;
pub mod type_system;
pub mod typegen;

/// Result type for analysis passes that produce both output and diagnostics.
///
Expand Down
17 changes: 3 additions & 14 deletions crates/plotnik-lib/src/typegen/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<'a> Emitter<'a> {
contexts.entry(type_id).or_insert_with(|| ctx.clone());
for member in self.types.members_of(&type_def) {
let field_name = self.strings.get(member.name);
let (inner_type, _) = self.unwrap_optional(member.type_id);
let (inner_type, _) = self.types.unwrap_optional(member.type_id);
let field_ctx = NamingContext {
def_name: ctx.def_name.clone(),
field_name: Some(field_name.to_string()),
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<'a> Emitter<'a> {
.members_of(type_def)
.map(|member| {
let field_name = self.strings.get(member.name).to_string();
let (inner_type, optional) = self.unwrap_optional(member.type_id);
let (inner_type, optional) = self.types.unwrap_optional(member.type_id);
(field_name, inner_type, optional)
})
.collect();
Expand Down Expand Up @@ -660,7 +660,7 @@ impl<'a> Emitter<'a> {
.members_of(type_def)
.map(|member| {
let field_name = self.strings.get(member.name).to_string();
let (inner_type, optional) = self.unwrap_optional(member.type_id);
let (inner_type, optional) = self.types.unwrap_optional(member.type_id);
(field_name, inner_type, optional)
})
.collect();
Expand Down Expand Up @@ -712,17 +712,6 @@ impl<'a> Emitter<'a> {
}
}

/// Unwrap Optional wrappers and return (inner_type, is_optional).
fn unwrap_optional(&self, type_id: QTypeId) -> (QTypeId, bool) {
let Some(type_def) = self.types.get(type_id) else {
return (type_id, false);
};
if type_def.type_kind() != Some(TypeKind::Optional) {
return (type_id, false);
}
(QTypeId(type_def.data), true)
}

fn needs_generated_name(&self, type_def: &TypeDef) -> bool {
matches!(
type_def.type_kind(),
Expand Down