alef 0.68.0

Opinionated polyglot binding generator for Rust libraries
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use alef::backends::jni::JniBackend;
use alef::backends::kotlin_android::KotlinAndroidBackend;
use alef::core::backend::{Backend, GeneratedFile};
use alef::core::config::{NewAlefConfig, ResolvedCrateConfig};
use alef::core::ir::{ApiSurface, FunctionDef, MethodDef, ReceiverKind, TypeDef, TypeRef};

const CAPSULE_CONFIG: &str = r#"
[workspace]
languages = ["kotlin_android", "jni", "ffi"]

[[crates]]
name = "demo"
sources = ["src/lib.rs"]
version_from = "/nonexistent/Cargo.toml"

[crates.kotlin_android]
package = "dev.sample"

[crates.kotlin_android.capsule_types.Language]
host_type = "dev.runtime.Language"
construct_expr = "dev.runtime.Language({ptr})"
pointer_ownership = "borrowed_static"
host_destructor = "none"
abi_compatible = true

[crates.ffi.capsule_types.Language]
into_raw_type = "tree_sitter::ffi::TSLanguage"
c_return_type = "TSLanguage"

[crates.package_metadata]
repository = "https://github.com/example/demo"
license = "MIT"
"#;

fn capsule_config() -> ResolvedCrateConfig {
    let config: NewAlefConfig = toml::from_str(CAPSULE_CONFIG).expect("capsule config should parse");
    config.resolve().expect("capsule config should resolve").remove(0)
}

fn unsafe_owned_capsule_config() -> ResolvedCrateConfig {
    let unsafe_config = CAPSULE_CONFIG
        .replace("pointer_ownership = \"borrowed_static\"\n", "")
        .replace("host_destructor = \"none\"\n", "")
        .replace("abi_compatible = true\n", "");
    let config: NewAlefConfig = toml::from_str(&unsafe_config).expect("unsafe capsule config should parse");
    config
        .resolve()
        .expect("unsafe capsule config should resolve")
        .remove(0)
}

fn capsule_method_api(return_type: TypeRef) -> ApiSurface {
    let get_language = MethodDef {
        name: "get_language".into(),
        return_type,
        receiver: Some(ReceiverKind::Ref),
        cfg: None,
        ..Default::default()
    };
    ApiSurface {
        crate_name: "demo".into(),
        version: "0.1.0".into(),
        types: vec![
            TypeDef {
                name: "DefaultClient".into(),
                methods: vec![get_language],
                is_opaque: true,
                ..Default::default()
            },
            TypeDef {
                name: "Language".into(),
                is_opaque: true,
                ..Default::default()
            },
        ],
        ..Default::default()
    }
}

fn capsule_function_api() -> ApiSurface {
    ApiSurface {
        crate_name: "demo".into(),
        version: "0.1.0".into(),
        functions: vec![
            FunctionDef {
                name: "language_sample".into(),
                rust_path: "demo::language_sample".into(),
                return_type: TypeRef::Named("Language".into()),
                ..Default::default()
            },
            FunctionDef {
                name: "optional_language_sample".into(),
                rust_path: "demo::optional_language_sample".into(),
                return_type: TypeRef::Optional(Box::new(TypeRef::Named("Language".into()))),
                ..Default::default()
            },
        ],
        types: vec![TypeDef {
            name: "Language".into(),
            is_opaque: true,
            ..Default::default()
        }],
        ..Default::default()
    }
}

fn ffi_only_capsule_api() -> ApiSurface {
    let mut api = capsule_method_api(TypeRef::Named("Language".into()));
    api.functions.extend([
        FunctionDef {
            name: "language_sample".into(),
            rust_path: "demo::language_sample".into(),
            return_type: TypeRef::Named("Language".into()),
            ..Default::default()
        },
        FunctionDef {
            name: "optional_language_sample".into(),
            rust_path: "demo::optional_language_sample".into(),
            return_type: TypeRef::Optional(Box::new(TypeRef::Named("Language".into()))),
            ..Default::default()
        },
    ]);
    api
}

fn ffi_only_capsule_method_api() -> ApiSurface {
    let mut api = capsule_method_api(TypeRef::Named("Language".into()));
    let client = api
        .types
        .iter_mut()
        .find(|type_def| type_def.name == "DefaultClient")
        .expect("fixture should contain DefaultClient");
    client.methods.push(MethodDef {
        name: "find_language".into(),
        return_type: TypeRef::Optional(Box::new(TypeRef::Named("Language".into()))),
        receiver: Some(ReceiverKind::Ref),
        cfg: None,
        ..Default::default()
    });
    api
}

fn file_name(file: &GeneratedFile) -> String {
    file.path
        .file_name()
        .and_then(|name| name.to_str())
        .expect("generated file should have a UTF-8 name")
        .to_owned()
}

#[test]
fn should_generate_host_capsule_for_opaque_client_method_return() {
    let files = KotlinAndroidBackend
        .generate_bindings(
            &capsule_method_api(TypeRef::Named("Language".into())),
            &capsule_config(),
        )
        .expect("Kotlin Android bindings should generate");
    let client = files
        .iter()
        .find(|file| file_name(file) == "DefaultClient.kt")
        .expect("DefaultClient.kt should be generated");
    let signature = client.content.lines().find(|line| line.contains("fun getLanguage("));
    let construction = client
        .content
        .lines()
        .find(|line| line.trim_start().starts_with("return ") && line.contains("Language("));
    let language_files: Vec<_> = files
        .iter()
        .map(file_name)
        .filter(|name| name == "Language.kt")
        .collect();
    let free_bridge_files: Vec<_> = files
        .iter()
        .filter(|file| file.content.contains("nativeFreeLanguage"))
        .map(file_name)
        .collect();

    assert_eq!(
        (signature, construction, language_files, free_bridge_files),
        (
            Some("    fun getLanguage(): dev.runtime.Language {"),
            Some("        return dev.runtime.Language(capsulePtr)"),
            Vec::<String>::new(),
            Vec::<String>::new(),
        )
    );
}

#[test]
fn should_generate_nullable_host_capsule_for_optional_method_return() {
    let return_type = TypeRef::Optional(Box::new(TypeRef::Named("Language".into())));
    let files = KotlinAndroidBackend
        .generate_bindings(&capsule_method_api(return_type), &capsule_config())
        .expect("Kotlin Android bindings should generate");
    let client = files
        .iter()
        .find(|file| file_name(file) == "DefaultClient.kt")
        .expect("DefaultClient.kt should be generated");
    let signature = client.content.lines().find(|line| line.contains("fun getLanguage("));
    let construction = client
        .content
        .lines()
        .find(|line| line.trim_start().starts_with("return if (capsulePtr"));

    assert_eq!(
        (signature, construction),
        (
            Some("    fun getLanguage(): dev.runtime.Language? {"),
            Some("        return if (capsulePtr == 0L) null else dev.runtime.Language(capsulePtr)"),
        )
    );
}

#[test]
fn should_declare_configured_capsule_method_as_long_without_ir_type_definition() {
    let mut api = capsule_method_api(TypeRef::Named("Language".into()));
    api.types.retain(|type_def| type_def.name != "Language");
    let files = KotlinAndroidBackend
        .generate_bindings(&api, &capsule_config())
        .expect("Kotlin Android bindings should generate");
    let bridge = files
        .iter()
        .find(|file| file_name(file) == "DemoBridge.kt")
        .expect("DemoBridge.kt should be generated");
    let declaration = bridge
        .content
        .lines()
        .find(|line| line.contains("nativeDefaultClientGetLanguage"));

    assert_eq!(
        declaration,
        Some("    external fun nativeDefaultClientGetLanguage(handle: Long): Long")
    );
}

#[test]
fn should_generate_direct_and_optional_host_capsule_free_functions() {
    let api = capsule_function_api();
    let kotlin_files = KotlinAndroidBackend
        .generate_bindings(&api, &capsule_config())
        .expect("Kotlin Android capsule functions should generate");
    let facade = kotlin_files
        .iter()
        .find(|file| file.content.contains("fun languageSample("))
        .expect("module facade should contain capsule functions");
    let bridge = kotlin_files
        .iter()
        .find(|file| file_name(file) == "DemoBridge.kt")
        .expect("DemoBridge.kt should be generated");
    let jni_files = JniBackend
        .generate_bindings(&api, &capsule_config())
        .expect("JNI capsule functions should generate");
    let jni = &jni_files[0].content;

    assert!(facade.content.contains("fun languageSample(): dev.runtime.Language {"));
    assert!(
        facade
            .content
            .contains("fun optionalLanguageSample(): dev.runtime.Language? {")
    );
    assert!(
        facade
            .content
            .contains("return if (capsulePtr == 0L) null else dev.runtime.Language(capsulePtr)")
    );
    assert!(
        bridge
            .content
            .contains("external fun nativeOptionalLanguageSample(): Long")
    );
    assert!(jni.contains("v.into_raw() as jlong"));
    assert!(jni.contains("Some(inner) => inner.into_raw() as jlong"));
    assert!(!jni.contains("nativeFreeLanguage"));
}

#[test]
fn should_reject_host_capsule_without_matching_ffi_definition() {
    let mut config = capsule_config();
    config
        .ffi
        .as_mut()
        .expect("fixture should configure FFI")
        .capsule_types
        .clear();

    let error = KotlinAndroidBackend
        .generate_bindings(&capsule_method_api(TypeRef::Named("Language".into())), &config)
        .expect_err("host-only capsule configuration must fail closed");

    assert_eq!(
        error.to_string(),
        "kotlin_android capsule types require matching FFI capsule definitions: Language; \
         add each type under `[crates.ffi.capsule_types.<Type>]`"
    );
}

#[test]
fn should_generate_ordinary_owned_handle_for_ffi_only_capsule_definition() {
    let mut config = capsule_config();
    config
        .kotlin_android
        .as_mut()
        .expect("fixture should configure Kotlin Android")
        .capsule_types
        .clear();

    let api = ffi_only_capsule_api();
    let kotlin_files = KotlinAndroidBackend
        .generate_bindings(&api, &config)
        .expect("FFI-only capsule should use ordinary Kotlin handles");
    let client = kotlin_files
        .iter()
        .find(|file| file_name(file) == "DefaultClient.kt")
        .expect("DefaultClient.kt should be generated");
    let facade = kotlin_files
        .iter()
        .find(|file| file.content.contains("fun languageSample("))
        .expect("module facade should contain ordinary free function");
    let jni_files = JniBackend
        .generate_bindings(&api, &config)
        .expect("FFI-only capsule should use ordinary JNI handles");
    assert_ffi_only_owned_shape(&kotlin_files, &client.content, &facade.content, &jni_files[0].content);
}

#[test]
fn should_declare_destructor_for_ffi_only_capsule_returned_only_by_methods() {
    let mut config = capsule_config();
    config
        .kotlin_android
        .as_mut()
        .expect("fixture should configure Kotlin Android")
        .capsule_types
        .clear();
    let api = ffi_only_capsule_method_api();
    let files = KotlinAndroidBackend
        .generate_bindings(&api, &config)
        .expect("FFI-only method returns should use ordinary Kotlin handles");
    let bridge = files
        .iter()
        .find(|file| file_name(file) == "DemoBridge.kt")
        .expect("DemoBridge.kt should be generated");
    assert!(bridge.content.contains("external fun nativeFreeLanguage(handle: Long)"));
    assert!(
        bridge
            .content
            .contains("nativeDefaultClientGetLanguage(handle: Long): Long")
    );
    assert!(
        bridge
            .content
            .contains("nativeDefaultClientFindLanguage(handle: Long): Long")
    );
}

fn assert_ffi_only_owned_shape(kotlin_files: &[GeneratedFile], client: &str, facade: &str, jni: &str) {
    assert_ffi_only_direct_kotlin_shape(kotlin_files, client, facade);
    assert_ffi_only_optional_kotlin_shape(kotlin_files, facade);
    assert_ffi_only_jni_shape(jni);
}

fn assert_ffi_only_direct_kotlin_shape(kotlin_files: &[GeneratedFile], client: &str, facade: &str) {
    assert_eq!(
        (
            client.lines().find(|line| line.contains("fun getLanguage(")),
            client.contains("return Language(handle)"),
            facade.contains("fun languageSample(): Language"),
            facade.contains("Language(DemoBridge.nativeLanguageSample())"),
            kotlin_files.iter().any(|file| file_name(file) == "Language.kt"),
            kotlin_files
                .iter()
                .any(|file| file.content.contains("nativeFreeLanguage")),
        ),
        (Some("    fun getLanguage(): Language {"), true, true, true, true, true,)
    );
}

fn assert_ffi_only_optional_kotlin_shape(kotlin_files: &[GeneratedFile], facade: &str) {
    assert!(facade.contains("fun optionalLanguageSample(): Language?"), "{facade}");
    assert!(
        facade.contains("DemoBridge.nativeOptionalLanguageSample().takeIf { it != 0L }?.let(::Language)"),
        "{facade}"
    );
    assert!(kotlin_files.iter().any(|file| {
        file.content
            .contains("external fun nativeOptionalLanguageSample(): Long")
    }));
}

fn assert_ffi_only_jni_shape(jni: &str) {
    assert_eq!(jni.matches("Box::into_raw(Box::new(v)) as jlong").count(), 2);
    assert!(jni.contains("Some(inner) => Box::into_raw(Box::new(inner)) as jlong"));
    assert!(jni.contains("None => 0"));
    assert!(jni.contains("nativeFreeLanguage"));
    assert!(!jni.contains("v.into_raw() as jlong"));
}

#[test]
fn should_reject_owned_host_capsule_without_shared_native_runtime() {
    let error = KotlinAndroidBackend
        .generate_bindings(
            &capsule_method_api(TypeRef::Named("Language".into())),
            &unsafe_owned_capsule_config(),
        )
        .expect_err("owned host capsules require a shared native runtime");

    assert_eq!(
        error.to_string(),
        "capsule configuration in backend `kotlin_android` cannot safely wrap native pointers: \
         capsule type `Language`: `pointer_ownership = \"borrowed_static\"` is required, \
         `abi_compatible = true` is required, `host_destructor = \"none\"` or \
         `host_destructor = \"abi_noop\"` is required; declare a complete borrowed-static \
         ABI-compatible no-destructor contract for every listed capsule, or set \
         `[crates.kotlin_android].shares_native_runtime = true` only when every configured host \
         wrapper uses the exact same native runtime and ownership contract"
    );
}