-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Working, with tests. Depends on local changes to uniffi.toml * Now with some uniffi-extras.toml * Add to the readme * Update test to set things * Get rid of Disposable * PR review suggestions --------- Co-authored-by: Murph Murphy <[email protected]>
- Loading branch information
Showing
11 changed files
with
447 additions
and
64 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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/. */ | ||
|
||
use super::CodeType; | ||
use crate::ComponentInterface; | ||
|
||
#[derive(Debug)] | ||
pub struct CustomCodeType { | ||
name: String, | ||
} | ||
|
||
impl CustomCodeType { | ||
pub fn new(name: String) -> Self { | ||
CustomCodeType { name } | ||
} | ||
} | ||
|
||
impl CodeType for CustomCodeType { | ||
fn type_label(&self, ci: &ComponentInterface) -> String { | ||
super::JavaCodeOracle.class_name(ci, &self.name) | ||
} | ||
|
||
fn canonical_name(&self) -> String { | ||
format!("Type{}", self.name) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
{%- let package_name = config.package_name() %} | ||
{%- let ffi_type_name=builtin|ffi_type|ffi_type_name_by_value %} | ||
{%- match config.custom_types.get(name.as_str()) %} | ||
{%- when None %} | ||
{#- Define a newtype record that delegates to the builtin #} | ||
|
||
package {{ package_name }}; | ||
|
||
public record {{ type_name }}( | ||
{{ builtin|type_name(ci) }} value | ||
) { | ||
} | ||
|
||
package {{ package_name }}; | ||
import java.nio.ByteBuffer; | ||
|
||
public enum {{ ffi_converter_name }} implements FfiConverter<{{ type_name }}, {{ ffi_type_name}}> { | ||
INSTANCE; | ||
@Override | ||
public {{ type_name }} lift({{ ffi_type_name }} value) { | ||
var builtinValue = {{ builtin|lift_fn }}(value); | ||
return new {{ type_name }}(builtinValue); | ||
} | ||
@Override | ||
public {{ ffi_type_name }} lower({{ type_name }} value) { | ||
var builtinValue = value.value(); | ||
return {{ builtin|lower_fn }}(builtinValue); | ||
} | ||
@Override | ||
public {{ type_name }} read(ByteBuffer buf) { | ||
var builtinValue = {{ builtin|read_fn }}(buf); | ||
return new {{ type_name }}(builtinValue); | ||
} | ||
@Override | ||
public long allocationSize({{ type_name }} value) { | ||
var builtinValue = value.value(); | ||
return {{ builtin|allocation_size_fn }}(builtinValue); | ||
} | ||
@Override | ||
public void write({{ type_name }} value, ByteBuffer buf) { | ||
var builtinValue = value.value(); | ||
{{ builtin|write_fn }}(builtinValue, buf); | ||
} | ||
} | ||
|
||
{%- when Some with (config) %} | ||
|
||
{# | ||
When the config specifies a different type name, use that other type inside our newtype. | ||
Lift/lower using their configured code. | ||
#} | ||
{%- match config.type_name %} | ||
{%- when Some(concrete_type_name) %} | ||
|
||
package {{ package_name }}; | ||
|
||
{%- match config.imports %} | ||
{%- when Some(imports) %} | ||
{%- for import_name in imports %} | ||
import {{ import_name }}; | ||
{%- endfor %} | ||
{%- else %} | ||
{%- endmatch %} | ||
|
||
public record {{ type_name }}( | ||
{{ concrete_type_name }} value | ||
) {} | ||
|
||
{%- else %} | ||
{%- endmatch %} | ||
|
||
package {{ package_name }}; | ||
import java.nio.ByteBuffer; | ||
|
||
{%- match config.imports %} | ||
{%- when Some(imports) %} | ||
{%- for import_name in imports %} | ||
import {{ import_name }}; | ||
{%- endfor %} | ||
{%- else %} | ||
{%- endmatch %} | ||
// FFI converter with custom code. | ||
public enum {{ ffi_converter_name }} implements FfiConverter<{{ type_name }}, {{ ffi_type_name }}> { | ||
INSTANCE; | ||
@Override | ||
public {{ type_name }} lift({{ ffi_type_name }} value) { | ||
var builtinValue = {{ builtin|lift_fn }}(value); | ||
try{ | ||
return new {{ type_name}}({{ config.into_custom.render("builtinValue") }}); | ||
} catch(Exception e){ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
@Override | ||
public {{ ffi_type_name }} lower({{ type_name }} value) { | ||
try{ | ||
var builtinValue = {{ config.from_custom.render("value.value()") }}; | ||
return {{ builtin|lower_fn }}(builtinValue); | ||
} catch(Exception e){ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
@Override | ||
public {{ type_name }} read(ByteBuffer buf) { | ||
try{ | ||
var builtinValue = {{ builtin|read_fn }}(buf); | ||
return new {{ type_name }}({{ config.into_custom.render("builtinValue") }}); | ||
} catch(Exception e){ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
@Override | ||
public long allocationSize({{ type_name }} value) { | ||
try { | ||
var builtinValue = {{ config.from_custom.render("value.value()") }}; | ||
return {{ builtin|allocation_size_fn }}(builtinValue); | ||
} catch(Exception e){ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
@Override | ||
public void write({{ type_name }} value, ByteBuffer buf) { | ||
try { | ||
var builtinValue = {{ config.from_custom.render("value.value()") }}; | ||
{{ builtin|write_fn }}(builtinValue, buf); | ||
} catch(Exception e){ | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
{%- endmatch %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import customtypes.*; | ||
|
||
public class TestCustomTypes { | ||
|
||
public static void main(String[] args) throws MalformedURLException { | ||
// Get the custom types and check their data | ||
CustomTypesDemo demo = CustomTypes.getCustomTypesDemo(null); | ||
// URL is customized on the bindings side | ||
assert demo.url().equals(new Url(new URL("http://example.com/"))); | ||
// Handle isn't, but because java doesn't have type aliases it's still wrapped. | ||
assert demo.handle().equals(new Handle(123L)); | ||
|
||
// // Change some data and ensure that the round-trip works | ||
demo.setUrl(new Url(new URL("http://new.example.com/"))); | ||
demo.setHandle(new Handle(456L)); | ||
assert demo.equals(CustomTypes.getCustomTypesDemo(demo)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[bindings.java] | ||
package_name = "customtypes" | ||
|
||
[bindings.java.custom_types.Url] | ||
# Name of the type in the Kotlin code | ||
type_name = "URL" | ||
# Classes that need to be imported | ||
imports = ["java.net.URI", "java.net.URL"] | ||
# Functions to convert between strings and URLs | ||
into_custom = "new URI({}).toURL()" | ||
from_custom = "{}.toString()" |