Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demo Owning Type Arguments #714

Merged
merged 8 commits into from
Oct 29, 2024
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
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ feature_tests/dotnet/Lib/** linguist-generated=true
feature_tests/js/api/** linguist-generated=true
feature_tests/js/docs/source/** linguist-generated=true
feature_tests/kotlin/somelib/** linguist-generated=true
feature_tests/js/api/** linguist-generated=true
feature_tests/js/api/** linguist-generated=true
feature_tests/demo_gen/demo/** linguist-generated=true
8 changes: 4 additions & 4 deletions feature_tests/demo_gen/demo/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 27 additions & 9 deletions tool/src/demo_gen/terminus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct MethodDependency {

/// Parameters to pass into the method.
params: Vec<ParamInfo>,

/// The Rust parameter that we're attempting to construct with this method. Currently used by [`OutParam`] for better default parameter names.
owning_param: Option<String>,
}

pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
Expand All @@ -56,10 +59,11 @@ pub(super) struct RenderTerminusContext<'ctx, 'tcx> {
}

impl MethodDependency {
pub fn new(method_js: String) -> Self {
pub fn new(method_js: String, owning_param: Option<String>) -> Self {
MethodDependency {
method_js,
params: Vec::new(),
owning_param,
}
}
}
Expand Down Expand Up @@ -156,7 +160,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
// Not making this as part of the RenderTerminusContext because we want each evaluation to have a specific node,
// which I find easier easier to represent as a parameter to each function than something like an updating the current node in the struct.
let mut root =
MethodDependency::new(self.get_constructor_js(type_name.to_string(), method));
MethodDependency::new(self.get_constructor_js(type_name.clone(), method), None);

// And then we just treat the terminus as a regular constructor method:
self.terminus_info.node_call_stack = self.evaluate_constructor(method, &mut root);
Expand Down Expand Up @@ -189,7 +193,17 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
let attrs_default = attrs.unwrap_or_default();
// This only works for enums, since otherwise we break the type into its component parts.
let label = if attrs_default.input_cfg.label.is_empty() {
heck::AsUpperCamelCase(param_name.clone()).to_string()
let owning_str = node
.owning_param
.as_ref()
.map(|p| format!("{}:", heck::AsUpperCamelCase(p)))
.unwrap_or_default();
format!(
"{}{}",
owning_str,
heck::AsUpperCamelCase(param_name.clone())
)
.to_string()
} else {
attrs_default.input_cfg.label
};
Expand Down Expand Up @@ -287,7 +301,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
return;
}

self.evaluate_op_constructors(op, type_name.to_string(), node);
self.evaluate_op_constructors(op, type_name.to_string(), param_name, node);
}
Type::Struct(s) => {
let st = s.resolve(self.tcx);
Expand All @@ -298,7 +312,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
.push_error(format!("Found usage of disabled type {type_name}"))
}

self.evaluate_struct_fields(st, type_name.to_string(), node);
self.evaluate_struct_fields(st, type_name.to_string(), param_name, node);
}
Type::DiplomatOption(ref inner) => {
self.evaluate_param(inner, param_name, node, param_attrs)
Expand Down Expand Up @@ -342,6 +356,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
&mut self,
op: &OpaqueDef,
type_name: String,
param_name: String,
node: &mut MethodDependency,
) {
let mut usable_constructor = false;
Expand All @@ -367,8 +382,10 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
self.relative_import_path.clone(),
));

let mut child =
MethodDependency::new(self.get_constructor_js(type_name.to_string(), method));
let mut child = MethodDependency::new(
self.get_constructor_js(type_name.to_string(), method),
Some(param_name),
);

let call = self.evaluate_constructor(method, &mut child);
node.params.push(ParamInfo { js: call });
Expand Down Expand Up @@ -402,6 +419,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
&mut self,
st: &StructDef,
type_name: String,
param_name: String,
node: &mut MethodDependency,
) {
self.terminus_info
Expand All @@ -412,7 +430,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
self.relative_import_path.clone(),
));

let mut child = MethodDependency::new("".to_string());
let mut child = MethodDependency::new("".to_string(), Some(param_name));

#[derive(Template)]
#[template(path = "demo_gen/struct.js.jinja", escape = "none")]
Expand All @@ -430,7 +448,7 @@ impl<'ctx, 'tcx> RenderTerminusContext<'ctx, 'tcx> {
}

child.method_js = StructInfo {
type_name: type_name.to_string(),
type_name: type_name.clone(),
}
.render()
.unwrap();
Expand Down