Skip to content

Commit

Permalink
Bytes type and coverall test fixture (#16)
Browse files Browse the repository at this point in the history
* Passing tests, coverall generation working

Still need to complete coverall tests

* Checkpointing because I don't want to lose it

* Finished all coverall tests

* rogue println
  • Loading branch information
skeet70 authored Jul 11, 2024
1 parent ea9f460 commit cd4eb0e
Show file tree
Hide file tree
Showing 19 changed files with 847 additions and 128 deletions.
45 changes: 28 additions & 17 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ uniffi-example-custom-types = { git = "https://github.com/mozilla/uniffi-rs.git"
uniffi-example-futures = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi-example-geometry = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi-example-rondpoint = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi-fixture-coverall = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi-fixture-futures = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi-fixture-time = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
uniffi_testing = { git = "https://github.com/mozilla/uniffi-rs.git", branch = "main" }
2 changes: 1 addition & 1 deletion src/gen_java/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ impl AsCodeType for Type {
Type::Float64 => Box::new(primitives::Float64CodeType),
Type::Boolean => Box::new(primitives::BooleanCodeType),
Type::String => Box::new(primitives::StringCodeType),
Type::Bytes => unimplemented!(), //Box::new(primitives::BytesCodeType),
Type::Bytes => Box::new(primitives::BytesCodeType),

Type::Timestamp => Box::new(miscellany::TimestampCodeType),
Type::Duration => Box::new(miscellany::DurationCodeType),
Expand Down
16 changes: 16 additions & 0 deletions src/gen_java/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ macro_rules! impl_code_type_for_primitive {
};
}

#[derive(Debug)]
pub struct BytesCodeType;
impl CodeType for BytesCodeType {
fn type_label(&self, _ci: &ComponentInterface) -> String {
"byte[]".to_string()
}

fn canonical_name(&self) -> String {
"ByteArray".to_string()
}

fn literal(&self, literal: &Literal, ci: &ComponentInterface) -> String {
render_literal(&literal, ci)
}
}

impl_code_type_for_primitive!(BooleanCodeType, "Boolean");
impl_code_type_for_primitive!(StringCodeType, "String");
impl_code_type_for_primitive!(Int8CodeType, "Byte");
Expand Down
26 changes: 26 additions & 0 deletions src/templates/ByteArrayHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package {{ config.package_name() }};

import java.nio.ByteBuffer;

public enum FfiConverterByteArray implements FfiConverterRustBuffer<byte[]>{
INSTANCE;

@Override
public byte[] read(ByteBuffer buf) {
int len = buf.getInt();
byte[] byteArr = new byte[len];
buf.get(byteArr);
return byteArr;
}

@Override
public long allocationSize(byte[] value) {
return 4L + (long)value.length;
}

@Override
public void write(byte[] value, ByteBuffer buf) {
buf.putInt(value.length);
buf.put(value);
}
}
18 changes: 12 additions & 6 deletions src/templates/CallbackInterfaceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
package {{ config.package_name() }};

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.List;
import com.sun.jna.*;
import com.sun.jna.ptr.*;

{%- let trait_impl=format!("UniffiCallbackInterface{}", name) %}

Expand Down Expand Up @@ -45,19 +49,20 @@ public static class {{ inner_method_class }} implements {{ ffi_callback.name()|f
{%- endif -%}
) {
var uniffiObj = {{ ffi_converter_name }}.INSTANCE.handleMap.get(uniffiHandle);
Supplier<{% if meth.is_async() %}{{ meth|async_return_type(ci) }}{% else %}{% match meth.return_type() %}{% when Some(return_type)%}{{ return_type|type_name(ci)}}{% when None %}Void{% endmatch %}{% endif %}> makeCall = () -> {
return uniffiObj.{{ meth.name()|fn_name() }}(
{% if !meth.is_async() && meth.throws_type().is_some() %}Callable{% else %}Supplier{%endif%}<{% if meth.is_async() %}{{ meth|async_return_type(ci) }}{% else %}{% match meth.return_type() %}{% when Some(return_type)%}{{ return_type|type_name(ci)}}{% when None %}Void{% endmatch %}{% endif %}> makeCall = () -> {
{% if meth.return_type().is_some() || meth.is_async() %}return {% endif %}uniffiObj.{{ meth.name()|fn_name() }}(
{%- for arg in meth.arguments() %}
{{ arg|lift_fn }}({{ arg.name()|var_name }}){% if !loop.last %},{% endif %}
{%- endfor %}
);
{% if meth.return_type().is_none() && !meth.is_async() %}return null;{% endif %}
};
{%- if !meth.is_async() %}
{%- match meth.return_type() %}
{%- when Some(return_type) %}
var writeReturn = ({{ return_type|type_name(ci) }} value) -> { uniffiOutReturn.setValue({{ return_type|lower_fn }}(value)); };
Consumer<{{ return_type|type_name(ci)}}> writeReturn = ({{ return_type|type_name(ci) }} value) -> { uniffiOutReturn.setValue({{ return_type|lower_fn }}(value)); };
{%- when None %}
var writeReturn = () -> {};
Consumer<Void> writeReturn = (nothing) -> {};
{%- endmatch %}

{%- match meth.throws_type() %}
Expand All @@ -68,8 +73,9 @@ public static class {{ inner_method_class }} implements {{ ffi_callback.name()|f
uniffiCallStatus,
makeCall,
writeReturn,
({{error_type|type_name(ci) }} e) -> { {{ error_type|lower_fn }}(e) }
)
({{error_type|type_name(ci) }} e) -> { return {{ error_type|lower_fn }}(e); },
{{error_type|type_name(ci)}}.class
);
{%- endmatch %}

{%- else %}
Expand Down
37 changes: 17 additions & 20 deletions src/templates/EnumTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,37 +57,34 @@ public void write({{ type_name }} value, ByteBuffer buf) {
{% else %}

{%- call java::docstring(e, 0) %}
public sealed interface {{ type_name }}{% if contains_object_references %} extends Disposable {% endif %} {
public sealed interface {{ type_name }}{% if contains_object_references %} extends AutoCloseable {% endif %} {
{% for variant in e.variants() -%}
{%- call java::docstring(variant, 4) %}
{% if !variant.has_fields() -%}
record {{ variant|type_name(ci)}}() implements {{ type_name }} {}
record {{ variant|type_name(ci)}}() implements {{ type_name }} {
{% if contains_object_references %}
@Override
public void close() {
// Nothing to destroy
}
{% endif %}
}
{% else -%}
record {{ variant|type_name(ci)}}(
{%- for field in variant.fields() -%}
{%- call java::docstring(field, 8) %}
{{ field|type_name(ci)}} {% call java::field_name(field, loop.index) %}{% if loop.last %}{% else %}, {% endif %}
{%- endfor -%}
) implements {{ type_name }} {}
{%- endif %}
{% endfor %}

{% if contains_object_references %}
@Override
void destroy() {
switch (this) {
{%- for variant in e.variants() %}
case {{ type_name }}.{{ variant|type_name(ci)}} -> {
{%- if variant.has_fields() %}
{% call java::destroy_fields(variant) %}
{% else -%}
// Nothing to destroy
{%- endif %}
}
{%- endfor %}
) implements {{ type_name }} {
{% if contains_object_references %}
@Override
public void close() {
{% call java::destroy_fields(variant) %}
}
{% endif %}
}
{% endif %}
{%- endif %}
{% endfor %}
}

package {{ config.package_name() }};
Expand Down
16 changes: 12 additions & 4 deletions src/templates/ErrorTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class {{ type_name }} extends Exception {

{% for variant in e.variants() -%}
{%- call java::docstring(variant, 4) %}
public static class {{ variant|error_variant_name }} extends {{ type_name }}{% if contains_object_references %}, Disposable{% endif %} {
public static class {{ variant|error_variant_name }} extends {{ type_name }}{% if contains_object_references %}, AutoCloseable{% endif %} {
public {{ variant|error_variant_name }}(String message) {
super(message);
}
Expand All @@ -32,7 +32,7 @@ public class {{ type_name }} extends Exception {
{% for variant in e.variants() -%}
{%- call java::docstring(variant, 4) %}
{%- let variant_name = variant|error_variant_name %}
public static class {{ variant_name }} extends {{ type_name }}{% if contains_object_references %}, Disposable{% endif %} {
public static class {{ variant_name }} extends {{ type_name }}{% if contains_object_references %}, AutoCloseable{% endif %} {
{% for field in variant.fields() -%}
{%- call java::docstring(field, 8) %}
{{ field|type_name(ci) }} {{ field.name()|var_name }};
Expand All @@ -43,7 +43,15 @@ public static class {{ variant_name }} extends {{ type_name }}{% if contains_obj
{{ field|type_name(ci)}} {{ field.name()|var_name }}{% if loop.last %}{% else %}, {% endif %}
{%- endfor -%}
) {
super("{%- for field in variant.fields() %}{{ field.name()|var_name|unquote }}=${ {{field.name()|var_name }} }{% if !loop.last %}, {% endif %}{% endfor %}");
super(new StringBuilder()
{%- for field in variant.fields() %}
.append("{{ field.name()|var_name|unquote }}=")
.append({{field.name()|var_name }})
{% if !loop.last %}
.append(", ")
{% endif %}
{% endfor %}
.toString());
{% for field in variant.fields() -%}
this.{{ field.name()|var_name }} = {{ field.name()|var_name }};
{% endfor -%}
Expand All @@ -57,7 +65,7 @@ public static class {{ variant_name }} extends {{ type_name }}{% if contains_obj

{% if contains_object_references %}
@Override
void destroy() {
void close() {
{%- if variant.has_fields() %}
{% call java::destroy_fields(variant) %}
{% else -%}
Expand Down
Loading

0 comments on commit cd4eb0e

Please sign in to comment.