polyplug 0.1.1

Universal high-performance zero-overhead cross-language plugin runtime
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#![allow(clippy::expect_used)]

//! Integration tests for host contract registration and lookup in Runtime.
//!
//! Tests cover:
//! - Registration (register_host_contract)
//! - Lookup (get_host_contract)
//! - Version checking (min_version parameter)
//! - Thread safety (concurrent access)
//! - Missing contracts return None
//! - Unregister functionality

use std::sync::Arc;

use polyplug::runtime::Runtime;
use polyplug_abi::{
    DispatchMechanisms, DispatchType, HostContractInterface, NativeDispatch, Version,
};
use polyplug_utils::HostContractId;

// ─── Helper: Create a static host contract interface ─────────────────────────────

/// No-op create_instance callback.
unsafe extern "C" fn noop_create_instance(
    _this: *const HostContractInterface,
    _args: *const (),
    out_instance: *mut polyplug_abi::HostContractInstance,
) {
    if !out_instance.is_null() {
        // SAFETY: out_instance is non-null (just checked) and writable per the ABI contract.
        unsafe { out_instance.write(polyplug_abi::HostContractInstance::null()) };
    }
}

/// No-op destroy_instance callback.
unsafe extern "C" fn noop_destroy_instance(
    _this: *const HostContractInterface,
    _instance: polyplug_abi::HostContractInstance,
) {
}

/// Create a leaked (static) HostContractInterface for testing.
/// The interface is leaked and lives for the process lifetime.
fn create_static_interface(
    contract_id: u64,
    major: u32,
    minor: u32,
    singleton: bool,
) -> &'static HostContractInterface {
    // Create a dummy function pointer - use null for simplicity in tests
    // since we never actually call the functions in these tests.
    let interface: Box<HostContractInterface> = Box::new(HostContractInterface {
        contract_id: HostContractId::from(contract_id),
        contract_version: Version {
            major,
            minor,
            patch: 0,
        },
        singleton,
        dispatch_type: DispatchType::Native,
        runtime: core::ptr::null_mut(),
        user_data: core::ptr::null_mut(),
        create_instance: noop_create_instance,
        destroy_instance: noop_destroy_instance,
        dispatch: DispatchMechanisms {
            native: NativeDispatch {
                function_count: 0,
                functions: core::ptr::null(),
            },
        },
    });

    Box::leak(interface)
}

// ─── Registration Tests ───────────────────────────────────────────────────────

#[test]
fn register_host_contract_success() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x1234_5678_9ABC_DEF0;
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);

    let result: Result<(), polyplug::error::HostContractError> =
        runtime.register_host_contract(contract_id, interface);

    assert!(result.is_ok(), "registration should succeed: {result:?}");
}

#[test]
fn register_host_contract_duplicate_returns_error() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0xABCD_EF01_2345_6789;
    let interface1: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);
    let interface2: &'static HostContractInterface =
        create_static_interface(contract_id, 2, 0, true);

    // First registration succeeds
    let result1: Result<(), polyplug::error::HostContractError> =
        runtime.register_host_contract(contract_id, interface1);
    assert!(
        result1.is_ok(),
        "first registration should succeed: {result1:?}"
    );

    // Second registration fails with DuplicateContract
    let result2: Result<(), polyplug::error::HostContractError> =
        runtime.register_host_contract(contract_id, interface2);
    assert!(
        result2.is_err(),
        "duplicate registration should return error: {result2:?}"
    );
    match result2 {
        Err(polyplug::error::HostContractError::DuplicateContract { contract_id: id }) => {
            assert_eq!(id, contract_id, "error contract_id should match");
        }
        _ => panic!("error should be DuplicateContract variant, got: {result2:?}"),
    }
}

#[test]
fn register_multiple_contracts() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id1: u64 = 0x1111_1111_1111_1111;
    let contract_id2: u64 = 0x2222_2222_2222_2222;
    let contract_id3: u64 = 0x3333_3333_3333_3333;

    let interface1: &'static HostContractInterface =
        create_static_interface(contract_id1, 1, 0, true);
    let interface2: &'static HostContractInterface =
        create_static_interface(contract_id2, 1, 0, true);
    let interface3: &'static HostContractInterface =
        create_static_interface(contract_id3, 2, 0, true);

    assert!(
        runtime
            .register_host_contract(contract_id1, interface1)
            .is_ok()
    );
    assert!(
        runtime
            .register_host_contract(contract_id2, interface2)
            .is_ok()
    );
    assert!(
        runtime
            .register_host_contract(contract_id3, interface3)
            .is_ok()
    );
}

// ─── Lookup Tests ─────────────────────────────────────────────────────────────

#[test]
fn get_host_contract_found() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0xAAAA_AAAA_AAAA_AAAA;
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 5, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);

    assert!(result.is_some(), "lookup should return Some(interface)");
    let returned_interface: &HostContractInterface = result.expect("interface should be Some");
    assert_eq!(
        returned_interface.contract_id.id(),
        contract_id,
        "returned contract_id should match"
    );
    assert_eq!(
        returned_interface.contract_version.major, 1,
        "returned major version should match"
    );
    assert_eq!(
        returned_interface.contract_version.minor, 5,
        "returned minor version should match"
    );
}

#[test]
fn get_host_contract_not_found_returns_none() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let nonexistent_id: u64 = 0xFFFF_FFFF_FFFF_FFFF;
    let result: Option<&'static HostContractInterface> =
        runtime.get_host_contract(nonexistent_id, 0);

    assert!(
        result.is_none(),
        "lookup of nonexistent contract should return None"
    );
}

#[test]
fn get_host_contract_after_unregister() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0xBBBB_BBBB_BBBB_BBBB;
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Verify it's registered
    assert!(runtime.get_host_contract(contract_id, 0).is_some());

    // Unregister
    let removed: bool = runtime.unregister_host_contract(contract_id);
    assert!(
        removed,
        "unregister should return true for existing contract"
    );

    // Verify it's gone
    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);
    assert!(
        result.is_none(),
        "lookup after unregister should return None"
    );
}

#[test]
fn unregister_nonexistent_contract_returns_false() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let nonexistent_id: u64 = 0xEEEE_EEEE_EEEE_EEEE;
    let removed: bool = runtime.unregister_host_contract(nonexistent_id);

    assert!(
        !removed,
        "unregister should return false for nonexistent contract"
    );
}

// ─── Version Checking Tests ───────────────────────────────────────────────────

#[test]
fn get_host_contract_version_check_exact_match() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x1111_2222_3333_4444;
    // Register with version 1.5 (major=1, minor=5)
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 5, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Request exact version 1.5 (encoded as (1 << 16) | 5 = 65541)
    let min_version: u32 = (1 << 16) | 5;
    let result: Option<&'static HostContractInterface> =
        runtime.get_host_contract(contract_id, min_version);

    assert!(
        result.is_some(),
        "should find contract with exact version match"
    );
}

#[test]
fn get_host_contract_version_check_lower_minor_succeeds() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x2222_3333_4444_5555;
    // Register with version 2.10 (major=2, minor=10)
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 2, 10, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Request version 2.5 (lower minor) - should succeed
    let min_version: u32 = (2 << 16) | 5;
    let result: Option<&'static HostContractInterface> =
        runtime.get_host_contract(contract_id, min_version);

    assert!(
        result.is_some(),
        "should find contract when requesting lower minor version"
    );
}

#[test]
fn get_host_contract_version_check_higher_minor_fails() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x3333_4444_5555_6666;
    // Register with version 1.3 (major=1, minor=3)
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 3, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Request version 1.5 (higher minor) - should fail
    let min_version: u32 = (1 << 16) | 5;
    let result: Option<&'static HostContractInterface> =
        runtime.get_host_contract(contract_id, min_version);

    assert!(
        result.is_none(),
        "should NOT find contract when requesting higher minor version"
    );
}

#[test]
fn get_host_contract_version_check_higher_major_fails() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x4444_5555_6666_7777;
    // Register with version 1.0 (major=1, minor=0)
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Request version 2.0 (higher major) - should fail
    let min_version: u32 = 2 << 16;
    let result: Option<&'static HostContractInterface> =
        runtime.get_host_contract(contract_id, min_version);

    assert!(
        result.is_none(),
        "should NOT find contract when requesting higher major version"
    );
}

#[test]
fn get_host_contract_version_check_zero_succeeds() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x5555_6666_7777_8888;
    // Register with any version
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 3, 7, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // Request version 0 - should always succeed
    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);

    assert!(
        result.is_some(),
        "should find contract when requesting version 0"
    );
}

// ─── Thread Safety Tests ──────────────────────────────────────────────────────

#[test]
fn concurrent_register_and_lookup() {
    use std::thread;

    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x6666_7777_8888_9999;
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);

    // Register before spawning threads
    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    let runtime_clone1: Arc<Runtime> = Arc::clone(&runtime);
    let runtime_clone2: Arc<Runtime> = Arc::clone(&runtime);

    // Thread 1: Repeatedly lookup the contract
    let handle1: thread::JoinHandle<bool> = thread::spawn(move || {
        for _ in 0..100 {
            let result: Option<&'static HostContractInterface> =
                runtime_clone1.get_host_contract(contract_id, 0);
            if result.is_none() {
                return false;
            }
        }
        true
    });

    // Thread 2: Also lookup the contract
    let handle2: thread::JoinHandle<bool> = thread::spawn(move || {
        for _ in 0..100 {
            let result: Option<&'static HostContractInterface> =
                runtime_clone2.get_host_contract(contract_id, 0);
            if result.is_none() {
                return false;
            }
        }
        true
    });

    let success1: bool = handle1.join().expect("thread1 should not panic");
    let success2: bool = handle2.join().expect("thread2 should not panic");

    assert!(success1, "thread1 lookups should all succeed");
    assert!(success2, "thread2 lookups should all succeed");
}

#[test]
fn concurrent_lookups_multiple_contracts() {
    use std::thread;

    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    // Register multiple contracts
    let contract_ids: [u64; 5] = [
        0x1000_0000_0000_0001,
        0x1000_0000_0000_0002,
        0x1000_0000_0000_0003,
        0x1000_0000_0000_0004,
        0x1000_0000_0000_0005,
    ];

    for id in &contract_ids {
        let interface: &'static HostContractInterface = create_static_interface(*id, 1, 0, true);
        runtime
            .register_host_contract(*id, interface)
            .expect("registration should succeed");
    }

    let mut handles: Vec<thread::JoinHandle<bool>> = Vec::new();

    // Spawn 10 threads, each doing lookups on all contracts
    for thread_idx in 0..10 {
        let runtime_clone: Arc<Runtime> = Arc::clone(&runtime);
        let ids: [u64; 5] = contract_ids;

        let handle: thread::JoinHandle<bool> = thread::spawn(move || {
            for _ in 0..50 {
                for id in &ids {
                    let result: Option<&'static HostContractInterface> =
                        runtime_clone.get_host_contract(*id, 0);
                    if result.is_none() {
                        return false;
                    }
                }
            }
            // Each thread verifies it ran
            assert!(thread_idx < 10);
            true
        });

        handles.push(handle);
    }

    for (idx, handle) in handles.into_iter().enumerate() {
        let success: bool = handle.join().expect("thread should not panic");
        assert!(success, "thread {} lookups should all succeed", idx);
    }
}

#[test]
fn concurrent_register_different_contracts() {
    use std::thread;

    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_ids: [u64; 10] = [
        0x2000_0000_0000_0001,
        0x2000_0000_0000_0002,
        0x2000_0000_0000_0003,
        0x2000_0000_0000_0004,
        0x2000_0000_0000_0005,
        0x2000_0000_0000_0006,
        0x2000_0000_0000_0007,
        0x2000_0000_0000_0008,
        0x2000_0000_0000_0009,
        0x2000_0000_0000_000A,
    ];

    let mut handles: Vec<thread::JoinHandle<Result<(), polyplug::error::HostContractError>>> =
        Vec::new();

    // Spawn 10 threads, each registering a different contract
    for (idx, &id) in contract_ids.iter().enumerate() {
        let runtime_clone: Arc<Runtime> = Arc::clone(&runtime);
        let interface: &'static HostContractInterface = create_static_interface(id, 1, 0, true);

        let handle: thread::JoinHandle<Result<(), polyplug::error::HostContractError>> =
            thread::spawn(move || {
                // Small delay to increase race probability
                std::thread::sleep(core::time::Duration::from_millis(idx as u64));
                runtime_clone.register_host_contract(id, interface)
            });

        handles.push(handle);
    }

    // All registrations should succeed (different contract IDs)
    for (idx, handle) in handles.into_iter().enumerate() {
        let result: Result<(), polyplug::error::HostContractError> =
            handle.join().expect("thread should not panic");
        assert!(
            result.is_ok(),
            "registration in thread {} should succeed: {result:?}",
            idx
        );
    }

    // Verify all contracts are registered
    for id in &contract_ids {
        let result: Option<&'static HostContractInterface> = runtime.get_host_contract(*id, 0);
        assert!(
            result.is_some(),
            "contract 0x{id:016X} should be registered"
        );
    }
}

// ─── Edge Cases ───────────────────────────────────────────────────────────────

#[test]
fn get_host_contract_with_contract_id_helper() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    // Use the helper function to compute contract ID
    let contract_name: &str = "test.logger";
    let major: u32 = 1;
    let contract_id: u64 = HostContractId::new(contract_name, major).id();

    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, major, 0, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);

    assert!(
        result.is_some(),
        "should find contract registered with helper"
    );
}

#[test]
fn register_unregister_register_same_contract() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0x9999_AAAA_BBBB_CCCC;
    let interface1: &'static HostContractInterface =
        create_static_interface(contract_id, 1, 0, true);
    let interface2: &'static HostContractInterface =
        create_static_interface(contract_id, 2, 0, true);

    // Register v1
    assert!(
        runtime
            .register_host_contract(contract_id, interface1)
            .is_ok()
    );
    assert!(runtime.get_host_contract(contract_id, 0).is_some());

    // Unregister
    assert!(runtime.unregister_host_contract(contract_id));
    assert!(runtime.get_host_contract(contract_id, 0).is_none());

    // Register v2
    assert!(
        runtime
            .register_host_contract(contract_id, interface2)
            .is_ok()
    );

    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);
    assert!(result.is_some(), "should find re-registered contract");

    let returned_interface: &HostContractInterface = result.expect("interface should be Some");
    assert_eq!(
        returned_interface.contract_version.major, 2,
        "should return v2 after re-registration"
    );
}

#[test]
fn get_host_contract_min_version_zero_matches_all() {
    let runtime: Arc<Runtime> = Runtime::builder()
        .build()
        .expect("runtime build should succeed");

    let contract_id: u64 = 0xAAAA_BBBB_CCCC_DDDD;
    // Register with version 0.1 (very low version)
    let interface: &'static HostContractInterface =
        create_static_interface(contract_id, 0, 1, true);

    runtime
        .register_host_contract(contract_id, interface)
        .expect("registration should succeed");

    // min_version=0 should match any version
    let result: Option<&'static HostContractInterface> = runtime.get_host_contract(contract_id, 0);
    assert!(result.is_some(), "min_version=0 should match any version");
}