From 7b7ba99e348efe0888b1c9afd1e94e7cc5a4a583 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 2 Sep 2024 15:06:40 -0700 Subject: [PATCH 1/2] Rename type_generation module to gen --- tool/src/js/{type_generation => gen}/converter.rs | 0 tool/src/js/{type_generation => gen}/mod.rs | 0 tool/src/js/mod.rs | 6 +++--- 3 files changed, 3 insertions(+), 3 deletions(-) rename tool/src/js/{type_generation => gen}/converter.rs (100%) rename tool/src/js/{type_generation => gen}/mod.rs (100%) diff --git a/tool/src/js/type_generation/converter.rs b/tool/src/js/gen/converter.rs similarity index 100% rename from tool/src/js/type_generation/converter.rs rename to tool/src/js/gen/converter.rs diff --git a/tool/src/js/type_generation/mod.rs b/tool/src/js/gen/mod.rs similarity index 100% rename from tool/src/js/type_generation/mod.rs rename to tool/src/js/gen/mod.rs diff --git a/tool/src/js/mod.rs b/tool/src/js/mod.rs index 5dfd1522f..e1aa9b66d 100644 --- a/tool/src/js/mod.rs +++ b/tool/src/js/mod.rs @@ -1,6 +1,6 @@ //! JS code generation backend. //! -//! This module specifically handles the overview of generating all the necessary `.mjs` and `.d.ts` files that [`type_generation`] creates content for. +//! This module specifically handles the overview of generating all the necessary `.mjs` and `.d.ts` files that [`gen`] creates content for. use std::collections::BTreeSet; use std::{borrow::Cow, cell::RefCell}; @@ -12,8 +12,8 @@ use askama::Template; pub(crate) mod formatter; use formatter::JSFormatter; -mod type_generation; -use type_generation::{MethodsInfo, TyGenContext}; +mod gen; +use gen::{MethodsInfo, TyGenContext}; mod layout; From fc0693b7fd8d94a421bacb8fe3bcc51b4e693969 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 2 Sep 2024 15:17:04 -0700 Subject: [PATCH 2/2] Flatten out converter from gen --- tool/src/js/{gen => }/converter.rs | 4 ++-- tool/src/js/{gen/mod.rs => gen.rs} | 37 +++++++++++++++--------------- tool/src/js/mod.rs | 1 + 3 files changed, 21 insertions(+), 21 deletions(-) rename tool/src/js/{gen => }/converter.rs (99%) rename tool/src/js/{gen/mod.rs => gen.rs} (96%) diff --git a/tool/src/js/gen/converter.rs b/tool/src/js/converter.rs similarity index 99% rename from tool/src/js/gen/converter.rs rename to tool/src/js/converter.rs index 03b08755e..ce72e6702 100644 --- a/tool/src/js/gen/converter.rs +++ b/tool/src/js/converter.rs @@ -10,7 +10,7 @@ use diplomat_core::hir::{ }; use std::fmt::Write; -use super::TyGenContext; +use super::gen::TyGenContext; /// Check if an enum's values are consecutive. (i.e., if we start at 1, the next value is 2). fn is_contiguous_enum(ty: &hir::EnumDef) -> bool { @@ -369,7 +369,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> { /// We access [`super::MethodInfo`] to handle allocation and cleanup. pub(super) fn gen_c_to_js_for_return_type( &self, - method_info: &mut super::MethodInfo, + method_info: &mut super::gen::MethodInfo, method: &Method, ) -> Option> { let return_type = &method.output; diff --git a/tool/src/js/gen/mod.rs b/tool/src/js/gen.rs similarity index 96% rename from tool/src/js/gen/mod.rs rename to tool/src/js/gen.rs index 43b030519..7999d4491 100644 --- a/tool/src/js/gen/mod.rs +++ b/tool/src/js/gen.rs @@ -19,8 +19,7 @@ use askama::{self, Template}; use super::formatter::JSFormatter; use crate::ErrorStore; -mod converter; -use converter::{ForcePaddingStatus, JsToCConversionContext, StructBorrowContext}; +use super::converter::{ForcePaddingStatus, JsToCConversionContext, StructBorrowContext}; /// Represents context for generating a Javascript class. /// @@ -425,7 +424,7 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { let struct_borrow_info = if let ParamBorrowInfo::Struct(param_info) = param_borrow_kind { - Some(converter::StructBorrowContext { + Some(super::converter::StructBorrowContext { use_env: &method.lifetime_env, param_info, is_method: true, @@ -499,7 +498,7 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> { /// Represents a parameter of a method. Used as part of [`MethodInfo`], exclusively in the method definition. #[derive(Default)] -struct ParamInfo<'a> { +pub(super) struct ParamInfo<'a> { ty: Cow<'a, str>, name: Cow<'a, str>, } @@ -511,7 +510,7 @@ struct ParamInfo<'a> { /// [`ParamInfo`] represents the conversion of the slice into C-friendly terms. This just represents an extra stage for Diplomat to convert whatever slice type we're given into a type that returns a `.ptr` and `.size` field. /// /// See `DiplomatBuf` in `runtime.mjs` for more. -struct SliceParam<'a> { +pub(super) struct SliceParam<'a> { name: Cow<'a, str>, /// How to convert the JS type into a C slice. slice_expr: String, @@ -524,41 +523,41 @@ struct SliceParam<'a> { #[template(path = "js/method.js.jinja", escape = "none")] pub(super) struct MethodInfo<'info> { /// Do we return the `()` type? - method_output_is_ffi_unit: bool, + pub method_output_is_ffi_unit: bool, /// The declaration signature. Something like `static functionName() { /* ... */ }` versus `functionName() { /* ... */ }` - method_decl: String, + pub method_decl: String, /// Native C method name - abi_name: String, + pub abi_name: String, /// If we need to create a `CleanupArena` (see `runtime.mjs`) to free any [`SliceParam`]s that are present. - needs_slice_cleanup: bool, + pub needs_slice_cleanup: bool, /// For calling .releaseToGarbageCollector on slices. - needs_slice_collection: bool, + pub needs_slice_collection: bool, pub typescript: bool, /// Represents all the parameters in the method definition (mostly for `.d.ts` generation, showing names and types). - parameters: Vec>, + pub parameters: Vec>, /// See [`SliceParam`] for info on how this array is used. - slice_params: Vec>, + pub slice_params: Vec>, /// Represents the Javascript needed to take the parameters from the method definition into C-friendly terms. See [`TyGenContext::gen_js_to_c_for_type`] for more. - param_conversions: Vec>, + pub param_conversions: Vec>, /// The return type, for `.d.ts` files. - return_type: String, + pub return_type: String, /// The JS expression used when this method returns. - return_expression: Option>, + pub return_expression: Option>, /// Used for generating edge information when constructing items like Slices, Structs, and Opaque types. See [hir::methods::borrowing_param::BorrowedLifetimeInfo] for more. - method_lifetimes_map: BTreeMap>, + pub method_lifetimes_map: BTreeMap>, /// We use this to access individual [`hir::Lifetimes`], which we then use to access the [`MethodInfo::method_lifetimes_map`]. - lifetimes: Option<&'info LifetimeEnv>, + pub lifetimes: Option<&'info LifetimeEnv>, /// Anything we need to allocate for [`MethodInfo::param_conversions`] - alloc_expressions: Vec>, + pub alloc_expressions: Vec>, /// Anything from [`MethodInfo::alloc_expressions`] we need to clean up afterwards. - cleanup_expressions: Vec>, + pub cleanup_expressions: Vec>, } /// See [`TyGenContext::generate_special_method`]. diff --git a/tool/src/js/mod.rs b/tool/src/js/mod.rs index e1aa9b66d..24a20f0a0 100644 --- a/tool/src/js/mod.rs +++ b/tool/src/js/mod.rs @@ -14,6 +14,7 @@ use formatter::JSFormatter; mod gen; use gen::{MethodsInfo, TyGenContext}; +mod converter; mod layout;