File tree Expand file tree Collapse file tree 7 files changed +122
-1
lines changed
tests/runtime/c/autodrop-borrows Expand file tree Collapse file tree 7 files changed +122
-1
lines changed Original file line number Diff line number Diff line change @@ -1295,7 +1295,8 @@ void {ns}_{snake}_drop_own({own} handle) {{
1295
1295
"\n typedef struct {borrow} {{\n int32_t __handle;\n }} {borrow};\n "
1296
1296
) ) ;
1297
1297
1298
- if self . autodrop_enabled ( ) {
1298
+ // Explicit borrow dropping is not required if autodrop is enabled.
1299
+ if !self . autodrop_enabled ( ) {
1299
1300
// As we have two different types for owned vs borrowed resources,
1300
1301
// but owns and borrows are dropped using the same intrinsic we
1301
1302
// also generate a version of the drop function for borrows that we
Original file line number Diff line number Diff line change
1
+ //@ args = '--autodrop-borrows=yes'
2
+
3
+ #include "autodropper.h"
4
+ #include <assert.h>
5
+
6
+ void exports_test_resource_borrow_imported_autodrop_borrow_thing_do_borrow (
7
+ test_resource_borrow_imported_test_borrow_thing_t thing ) {
8
+ uint32_t result = test_resource_borrow_imported_test_method_thing_get_int (thing );
9
+ assert (result == 42 );
10
+ // Intentionally do not drop the borrow, as it will be done automatically
11
+ }
Original file line number Diff line number Diff line change
1
+ //@ args = '--autodrop-borrows=no'
2
+
3
+ #include "borrower.h"
4
+ #include <assert.h>
5
+
6
+ void exports_test_resource_borrow_imported_borrow_thing_do_borrow (
7
+ test_resource_borrow_imported_test_borrow_thing_t thing ) {
8
+ uint32_t result = test_resource_borrow_imported_test_method_thing_get_int (thing );
9
+ assert (result == 42 );
10
+ // We must explicitly drop the borrow, because autodrop borrows is turned off
11
+ test_resource_borrow_imported_test_thing_drop_borrow (thing );
12
+ }
Original file line number Diff line number Diff line change
1
+ package example:composition;
2
+
3
+ let exporter = new test:exporter { ... };
4
+ let borrower = new test:borrower {
5
+ test: exporter.test,
6
+ ...
7
+ };
8
+ let autodropper = new test:autodropper {
9
+ test: exporter.test,
10
+ ...
11
+ };
12
+ let runner = new test:runner {
13
+ test: exporter.test,
14
+ borrow-thing: borrower.borrow-thing,
15
+ autodrop-borrow-thing: autodropper.autodrop-borrow-thing,
16
+ ...
17
+ };
18
+
19
+ export runner...;
Original file line number Diff line number Diff line change
1
+ #include "exporter.h"
2
+ #include <stdlib.h>
3
+
4
+ struct exports_test_resource_borrow_imported_test_thing_t {
5
+ uint32_t my_state ;
6
+ };
7
+
8
+ uint32_t exports_test_resource_borrow_imported_test_method_thing_get_int (
9
+ exports_test_resource_borrow_imported_test_borrow_thing_t thing ) {
10
+ return thing -> my_state ;
11
+ }
12
+
13
+ void exports_test_resource_borrow_imported_test_thing_destructor (
14
+ exports_test_resource_borrow_imported_test_thing_t * rep ) {
15
+ free (rep );
16
+ }
17
+
18
+ exports_test_resource_borrow_imported_test_own_thing_t exports_test_resource_borrow_imported_test_constructor_thing (void ) {
19
+ exports_test_resource_borrow_imported_test_thing_t * rep = malloc (sizeof (exports_test_resource_borrow_imported_test_thing_t ));
20
+ rep -> my_state = 42 ;
21
+ return exports_test_resource_borrow_imported_test_thing_new (rep );
22
+ }
Original file line number Diff line number Diff line change
1
+ #include <assert.h>
2
+ #include "runner.h"
3
+
4
+ int main () {
5
+ test_resource_borrow_imported_test_own_thing_t thing = test_resource_borrow_imported_test_constructor_thing ();
6
+ assert (thing .__handle != 0 );
7
+
8
+ test_resource_borrow_imported_borrow_thing_do_borrow (
9
+ test_resource_borrow_imported_test_borrow_thing (thing )
10
+ );
11
+
12
+ test_resource_borrow_imported_autodrop_borrow_thing_do_borrow (
13
+ test_resource_borrow_imported_test_borrow_thing (thing )
14
+ );
15
+
16
+ test_resource_borrow_imported_test_thing_drop_own (thing );
17
+ }
Original file line number Diff line number Diff line change
1
+ //@ dependencies = ['exporter', 'borrower', 'autodropper']
2
+ //@ wac = 'compose.wac'
3
+
4
+ package test : resource-borrow-imported ;
5
+
6
+ interface test {
7
+ resource thing {
8
+ constructor ();
9
+ get-int : func () -> u32 ;
10
+ }
11
+ }
12
+
13
+ interface borrow-thing {
14
+ use test . {thing };
15
+ do-borrow : func (handle : borrow <thing >);
16
+ }
17
+
18
+ interface autodrop-borrow-thing {
19
+ use test . {thing };
20
+ do-borrow : func (handle : borrow <thing >);
21
+ }
22
+
23
+ world exporter {
24
+ export test ;
25
+ }
26
+
27
+ world borrower {
28
+ export borrow-thing ;
29
+ }
30
+
31
+ world autodropper {
32
+ export autodrop-borrow-thing ;
33
+ }
34
+
35
+ world runner {
36
+ import test ;
37
+ import autodrop-borrow-thing ;
38
+ import borrow-thing ;
39
+ }
You can’t perform that action at this time.
0 commit comments