Skip to content

Commit

Permalink
Change default access modifier to internal (#69)
Browse files Browse the repository at this point in the history
Access modifier may be configured via `access_modifier`
configuration value.

Signed-off-by: Kristupas Antanavičius <kristupas.antanavicius@nordsec.com>
  • Loading branch information
arg0d authored Feb 9, 2024
1 parent ce23ac2 commit 861f4cb
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 26 deletions.
16 changes: 12 additions & 4 deletions bindgen/src/gen_cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct Config {
#[serde(default)]
external_packages: HashMap<String, String>,
global_methods_class_name: Option<String>,
access_modifier: Option<String>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -77,6 +78,13 @@ impl Config {
.expect("`cdylib_name` not specified")
.clone()
}

pub fn access_modifier(&self) -> String {
match self.access_modifier.as_ref() {
Some(value) => value.clone(),
None => "internal".to_string(),
}
}
}

// Generate C# bindings for the given ComponentInterface, as a string.
Expand All @@ -93,7 +101,7 @@ pub fn generate_bindings(config: &Config, ci: &ComponentInterface) -> Result<Str
#[derive(Template)]
#[template(syntax = "cs", escape = "none", path = "Types.cs")]
pub struct TypeRenderer<'a> {
cs_config: &'a Config,
config: &'a Config,
ci: &'a ComponentInterface,
// Track included modules for the `include_once()` macro
include_once_names: RefCell<HashSet<String>>,
Expand All @@ -110,9 +118,9 @@ pub struct TypeAlias {
}

impl<'a> TypeRenderer<'a> {
fn new(cs_config: &'a Config, ci: &'a ComponentInterface) -> Self {
fn new(config: &'a Config, ci: &'a ComponentInterface) -> Self {
Self {
cs_config,
config,
ci,
include_once_names: RefCell::new(HashSet::new()),
imports: RefCell::new(BTreeSet::new()),
Expand All @@ -124,7 +132,7 @@ impl<'a> TypeRenderer<'a> {
fn external_type_package_name(&self, module_path: &str, namespace: &str) -> String {
// config overrides are keyed by the crate name, default fallback is the namespace.
let crate_name = module_path.split("::").next().unwrap();
match self.cs_config.external_packages.get(crate_name) {
match self.config.external_packages.get(crate_name) {
Some(name) => name.clone(),
// unreachable in library mode - all deps are in our config with correct namespace.
None => format!("uniffi.{namespace}"),
Expand Down
2 changes: 1 addition & 1 deletion bindgen/templates/CallbackInterfaceTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{% if self.include_once_check("CallbackInterfaceRuntime.cs") %}{% include "CallbackInterfaceRuntime.cs" %}{% endif %}

{%- call cs::docstring(cbi, 0) %}
public interface {{ type_name }} {
{{ config.access_modifier() }} interface {{ type_name }} {
{%- for meth in cbi.methods() %}
{%- call cs::docstring(meth, 4) %}
{%- call cs::method_throws_annotation(meth.throws_type()) %}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/templates/CustomTypeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */#}

{%- match cs_config.custom_types.get(name.as_str()) %}
{%- match config.custom_types.get(name.as_str()) %}
{%- when None %}
{#- Define the type using typealiases to the builtin #}
/**
Expand Down
4 changes: 2 additions & 2 deletions bindgen/templates/EnumTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{%- if e.is_flat() %}

{%- call cs::docstring(e, 0) %}
public enum {{ type_name }}: int {
{{ config.access_modifier() }} enum {{ type_name }}: int {
{% for variant in e.variants() -%}
{%- call cs::docstring(variant, 4) %}
{{ variant.name()|enum_variant }}{% if !loop.last %},{% endif %}
Expand Down Expand Up @@ -41,7 +41,7 @@ public override void Write({{ type_name }} value, BigEndianStream stream) {
{% else %}

{%- call cs::docstring(e, 0) %}
public record {{ type_name }}{% if contains_object_references %}: IDisposable {% endif %} {
{{ config.access_modifier() }} record {{ type_name }}{% if contains_object_references %}: IDisposable {% endif %} {
{% for variant in e.variants() -%}
{%- call cs::docstring(variant, 4) %}
{% if !variant.has_fields() -%}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/templates/ErrorTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

{% if e.is_flat() %}
{%- call cs::docstring(e, 0) %}
public class {{ type_name }}: UniffiException {
{{ config.access_modifier() }} class {{ type_name }}: UniffiException {
{{ type_name }}(string message): base(message) {}

// Each variant is a nested class
Expand Down Expand Up @@ -54,7 +54,7 @@ public override void Write({{ type_name }} value, BigEndianStream stream) {

{%- else %}
{%- call cs::docstring(e, 0) %}
public class {{ type_name }}: UniffiException{% if contains_object_references %}, IDisposable {% endif %} {
{{ config.access_modifier() }} class {{ type_name }}: UniffiException{% if contains_object_references %}, IDisposable {% endif %} {
// Each variant is a nested class
{% for variant in e.variants() -%}
{%- call cs::docstring(variant, 4) %}
Expand Down
16 changes: 8 additions & 8 deletions bindgen/templates/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,38 @@ public bool IsPanic() {
}

// Base class for all uniffi exceptions
public class UniffiException: Exception {
{{ config.access_modifier() }} class UniffiException: Exception {
public UniffiException(): base() {}
public UniffiException(string message): base(message) {}
}

public class UndeclaredErrorException: UniffiException {
{{ config.access_modifier() }} class UndeclaredErrorException: UniffiException {
public UndeclaredErrorException(string message): base(message) {}
}

public class PanicException: UniffiException {
{{ config.access_modifier() }} class PanicException: UniffiException {
public PanicException(string message): base(message) {}
}

public class AllocationException: UniffiException {
{{ config.access_modifier() }} class AllocationException: UniffiException {
public AllocationException(string message): base(message) {}
}

public class InternalException: UniffiException {
{{ config.access_modifier() }} class InternalException: UniffiException {
public InternalException(string message): base(message) {}
}

public class InvalidEnumException: InternalException {
{{ config.access_modifier() }} class InvalidEnumException: InternalException {
public InvalidEnumException(string message): base(message) {
}
}

public class UniffiContractVersionException: UniffiException {
{{ config.access_modifier() }} class UniffiContractVersionException: UniffiException {
public UniffiContractVersionException(string message): base(message) {
}
}

public class UniffiContractChecksumException: UniffiException {
{{ config.access_modifier() }} class UniffiContractChecksumException: UniffiException {
public UniffiContractChecksumException(string message): base(message) {
}
}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/templates/ObjectRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// https://github.com/mozilla/uniffi-rs/blob/0dc031132d9493ca812c3af6e7dd60ad2ea95bf0/uniffi_bindgen/src/bindings/kotlin/templates/ObjectRuntime.kt#L31
// https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.criticalhandle

public abstract class FFIObject<THandle>: IDisposable where THandle : FFISafeHandle {
{{ config.access_modifier() }} abstract class FFIObject<THandle>: IDisposable where THandle : FFISafeHandle {
private THandle handle;

public FFIObject(THandle handle) {
Expand All @@ -23,7 +23,7 @@ public void Dispose() {
}
}

public abstract class FFISafeHandle: SafeHandle {
{{ config.access_modifier() }} abstract class FFISafeHandle: SafeHandle {
public FFISafeHandle(): base(new IntPtr(0), true) {
}

Expand Down
6 changes: 3 additions & 3 deletions bindgen/templates/ObjectTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
{%- if self.include_once_check("ObjectRuntime.cs") %}{% include "ObjectRuntime.cs" %}{% endif %}

{%- call cs::docstring(obj, 0) %}
public interface I{{ type_name }} {
{{ config.access_modifier() }} interface I{{ type_name }} {
{% for meth in obj.methods() -%}
{%- call cs::docstring(meth, 4) %}
{%- call cs::method_throws_annotation(meth.throws_type()) %}
{% match meth.return_type() -%} {%- when Some with (return_type) -%} {{ return_type|type_name }} {%- when None %}void{%- endmatch %} {{ meth.name()|fn_name }}({% call cs::arg_list_decl(meth) %});
{% endfor %}
}

public class {{ safe_handle_type }}: FFISafeHandle {
{{ config.access_modifier() }} class {{ safe_handle_type }}: FFISafeHandle {
public {{ safe_handle_type }}(): base() {
}
public {{ safe_handle_type }}(IntPtr pointer): base(pointer) {
Expand All @@ -29,7 +29,7 @@ override protected bool ReleaseHandle() {
}

{%- call cs::docstring(obj, 0) %}
public class {{ type_name }}: FFIObject<{{ safe_handle_type }}>, I{{ type_name }} {
{{ config.access_modifier() }} class {{ type_name }}: FFIObject<{{ safe_handle_type }}>, I{{ type_name }} {
public {{ type_name }}({{ safe_handle_type }} pointer): base(pointer) {}

{%- match obj.primary_constructor() %}
Expand Down
2 changes: 1 addition & 1 deletion bindgen/templates/RecordTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/// future ordering changes.
/// </remarks>
{%- endif %}
public record {{ type_name }} (
{{ config.access_modifier() }} record {{ type_name }} (
{%- for field in ordered_fields %}
{%- call cs::docstring(field, 4) %}
{{ field|type_name }} {{ field.name()|var_name -}}
Expand Down
4 changes: 2 additions & 2 deletions bindgen/templates/wrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ namespace {{ config.namespace() }};

{%- match config.global_methods_class_name %}
{%- when Some(class_name) %}
public static class {{ class_name }} {
{{ config.access_modifier() }} static class {{ class_name }} {
{%- when None %}
public static class {{ ci.namespace().to_upper_camel_case() }}Methods {
{{ config.access_modifier() }} static class {{ ci.namespace().to_upper_camel_case() }}Methods {
{%- endmatch %}
{%- for func in ci.function_definitions() %}
{%- include "TopLevelFunctionTemplate.cs" %}
Expand Down
2 changes: 2 additions & 0 deletions docs/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ uniffi-bindgen-cs path/to/definitions.udl --config path/to/uniffi.toml
[bindings.csharp]
namespace = "LibGreeter"
```

- `access_modifier` - override the default `internal` access modifier for "exported" uniffi symbols.

0 comments on commit 861f4cb

Please sign in to comment.