Skip to content

Commit 3849c13

Browse files
tests through records and fixes
1 parent 0478bc7 commit 3849c13

14 files changed

Lines changed: 390 additions & 40 deletions

File tree

crates/d/src/lib.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,7 +1280,7 @@ impl<'a> DInterfaceGenerator<'a> {
12801280
self.src.push_str("static ");
12811281
}
12821282
self.src.push_str(&format!(
1283-
"{} {}({}) @nogc nothrow {{\n",
1283+
"{} {}({}) @trusted nothrow {{\n",
12841284
d_sig.result,
12851285
d_sig.name,
12861286
d_sig
@@ -1353,7 +1353,7 @@ impl<'a> DInterfaceGenerator<'a> {
13531353
self.src.push_str("static ");
13541354
}
13551355
self.src.push_str(&format!(
1356-
"private extern(C) {} __import_{}({}) @nogc nothrow;\n",
1356+
"private extern(C) {} __import_{}({}) nothrow;\n",
13571357
match wasm_sig.results.len() {
13581358
0 => "void",
13591359
1 => wasm_type(wasm_sig.results[0]),
@@ -1727,11 +1727,9 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
17271727

17281728
self.src.push_str(&format!(
17291729
"struct {escaped_name} {{
1730-
@nogc nothrow:
1731-
17321730
package({}) uint __handle = 0;
17331731
1734-
package({0}) this(uint handle) {{
1732+
package({0}) this(uint handle) @safe @nogc nothrow {{
17351733
__handle = handle;
17361734
}}
17371735
@@ -1782,8 +1780,9 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
17821780
}
17831781
}
17841782

1785-
self.src
1786-
.push_str("\nvoid drop() {\n__import_drop(__handle);\n}\n");
1783+
self.src.push_str(
1784+
"\nvoid drop() @trusted @nogc nothrow {\n__import_drop(__handle);\n}\n",
1785+
);
17871786
self.src.push_str(&format!(
17881787
"@wasmImport!(\"{}\", \"[resource-drop]{}\")\n",
17891788
self.wasm_import_module.unwrap(),
@@ -1797,8 +1796,9 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
17971796
.replace("-", "_"),
17981797
name.replace("-", "_")
17991798
));
1800-
self.src
1801-
.push_str("static private extern(C) void __import_drop(uint);\n\n");
1799+
self.src.push_str(
1800+
"static private extern(C) void __import_drop(uint) @nogc nothrow;\n\n",
1801+
);
18021802
self.src.push_str("alias witFree = drop;\n");
18031803

18041804
self.src.push_str(&format!(
@@ -1808,18 +1808,16 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
18081808
alias borrow this;
18091809
18101810
struct Borrow {{
1811-
@nogc nothrow:
1812-
18131811
package({}) uint __handle = 0;
18141812
1815-
package({0}) this(uint handle) {{
1813+
package({0}) this(uint handle) @safe @nogc nothrow {{
18161814
__handle = handle;
18171815
}}
18181816
18191817
@disable this();
18201818
1821-
void witFree() {{}}
1822-
Borrow witClone() const {{ return Borrow(__handle); }}
1819+
void witFree() @safe @nogc nothrow {{}}
1820+
Borrow witClone() const @safe @nogc nothrow {{ return Borrow(__handle); }}
18231821
",
18241822
self.r#gen.root_pkg
18251823
));
@@ -1883,11 +1881,9 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
18831881

18841882
self.src.push_str(&format!(
18851883
"struct {escaped_name} {{
1886-
@nogc nothrow:
1887-
18881884
package({}) uint __handle = 0;
18891885
1890-
package({0}) this(uint handle) {{
1886+
package({0}) this(uint handle) @safe @nogc nothrow {{
18911887
__handle = handle;
18921888
}}
18931889
@@ -1899,6 +1895,8 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
18991895
self.src.push_str(&format!(
19001896
"
19011897
static {escaped_name} makeNew(T)(scope void delegate(out T) dg) if (is(T == struct)) {{
1898+
if (dg is null) return {escaped_name}.init;
1899+
19021900
auto ptr = cast(T*)malloc(T.sizeof);
19031901
if (ptr is null) return {escaped_name}.init;
19041902
@@ -1908,7 +1906,7 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
19081906
",
19091907
));
19101908
self.src.push_str(&format!(
1911-
"@wasmImport!(\"{}\", \"[resource-new]{}\")\n",
1909+
"@wasmImport!(\"[export]{}\", \"[resource-new]{}\")\n",
19121910
self.wasm_import_module.unwrap(),
19131911
name
19141912
));
@@ -1924,9 +1922,9 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
19241922
.push_str("static private extern(C) uint __import_makeNew(void*);\n\n");
19251923

19261924
self.src
1927-
.push_str("T* rep(T)() if (is(T == struct)) {\nreturn cast(T*)__import_rep(__handle);\n}\n");
1925+
.push_str("T* rep(T)() @nogc nothrow if (is(T == struct)) {\nreturn cast(T*)__import_rep(__handle);\n}\n");
19281926
self.src.push_str(&format!(
1929-
"@wasmImport!(\"{}\", \"[resource-rep]{}\")\n",
1927+
"@wasmImport!(\"[export]{}\", \"[resource-rep]{}\")\n",
19301928
self.wasm_import_module.unwrap(),
19311929
name
19321930
));
@@ -1941,10 +1939,11 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
19411939
self.src
19421940
.push_str("static private extern(C) void __import_rep(uint);\n\n");
19431941

1944-
self.src
1945-
.push_str("void drop() {\n__import_drop(__handle);\n}\n");
1942+
self.src.push_str(
1943+
"void drop() @trusted @nogc nothrow {\n__import_drop(__handle);\n}\n",
1944+
);
19461945
self.src.push_str(&format!(
1947-
"@wasmImport!(\"{}\", \"[resource-drop]{}\")\n",
1946+
"@wasmImport!(\"[export]{}\", \"[resource-drop]{}\")\n",
19481947
self.wasm_import_module.unwrap(),
19491948
name
19501949
));
@@ -1956,28 +1955,27 @@ impl<'a> InterfaceGenerator<'a> for DInterfaceGenerator<'a> {
19561955
.replace("-", "_"),
19571956
name.replace("-", "_")
19581957
));
1959-
self.src
1960-
.push_str("static private extern(C) void __import_drop(uint);\n\n");
1958+
self.src.push_str(
1959+
"static private extern(C) void __import_drop(uint) @nogc nothrow;\n\n",
1960+
);
19611961
self.src.push_str("alias witFree = drop;\n");
19621962

19631963
self.src.push_str(&format!(
19641964
"// TODO: make RAII? disable copy for the own
1965-
Borrow borrow() => Borrow(__handle);
1965+
Borrow borrow() @safe @nogc nothrow => Borrow(__handle);
19661966
alias borrow this;
19671967
19681968
struct Borrow {{
1969-
@nogc nothrow:
1970-
19711969
package({}) uint __handle = 0;
19721970
1973-
package({0}) this(uint handle) {{
1971+
package({0}) this(uint handle) @safe @nogc nothrow {{
19741972
__handle = handle;
19751973
}}
19761974
19771975
@disable this();
19781976
1979-
void witFree() {{}}
1980-
Borrow witClone() const {{ return Borrow(__handle); }}
1977+
void witFree() @safe @nogc nothrow {{}}
1978+
Borrow witClone() const @safe @nogc nothrow {{ return Borrow(__handle); }}
19811979
19821980
",
19831981
self.r#gen.root_pkg
@@ -3106,9 +3104,9 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
31063104
bool {is_some} = ({op0}) != 0;
31073105
if ({is_some}) {{
31083106
{some}
3109-
{resultname} = {type_name}.some({some_value});
3107+
{resultname} = {type_name}.makeSome({some_value});
31103108
}} else {{
3111-
{resultname} = {type_name}.none;
3109+
{resultname} = {type_name}.makeNone;
31123110
}}
31133111
"
31143112
));

crates/d/src/wit_common.d

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ private:
145145
_value = value;
146146
}
147147
public:
148-
static inout(Option) some(inout T value) @safe @nogc nothrow {
148+
static inout(Option) makeSome(inout T value) @safe @nogc nothrow {
149149
return inout Option(true, value);
150150
}
151151

152-
static Option none() @safe @nogc nothrow {
152+
static Option makeNone() @safe @nogc nothrow {
153153
return Option(false, T.init);
154154
}
155155

@@ -169,11 +169,11 @@ public:
169169
}
170170

171171
auto some(T)(inout T value) @safe @nogc nothrow {
172-
return Option!T.some(value);
172+
return Option!T.makeSome(value);
173173
}
174174

175175
auto none(T)() @safe @nogc nothrow {
176-
return Option!T.none;
176+
return Option!T.makeNone;
177177
}
178178

179179
/// Based on Rust's Result
@@ -256,12 +256,12 @@ void witFree(T : Option!U, U)(scope ref T val) {
256256
T witClone(T : Option!U, U)(in T val) {
257257
if (val.isSome) {
258258
static if (!is(U == void)) {
259-
return T.some(val.unwrap.witClone);
259+
return T.makeSome(val.unwrap.witClone);
260260
} else {
261-
return T.some;
261+
return T.makeSome;
262262
}
263263
} else {
264-
return T.none;
264+
return T.makeNone;
265265
}
266266
}
267267

tests/runtime/lists-alias/runner.d

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import wit.my.lists.runner;
2+
import cat = wit.my.lists.runner.imports.cat;
3+
import wit.common;
4+
5+
@witExport("$root", "run")
6+
void run() {
7+
cat.foo((cast(immutable ubyte[])"hello").witList);
8+
9+
WitList!ubyte t = cat.bar();
10+
scope(exit) t.witFree;
11+
assert(t == (cast(immutable ubyte[])"world").witList);
12+
}
13+
14+
alias Exports = wit.my.lists.runner.Exports!(
15+
run
16+
);

tests/runtime/lists-alias/test.d

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import wit.my.lists.test;
2+
import wit.common;
3+
4+
@witExport("cat", "foo")
5+
void foo(in WitList!ubyte x) {
6+
assert(x == (cast(immutable ubyte[])"hello").witList);
7+
}
8+
9+
@witExport("cat", "bar")
10+
WitList!ubyte bar() {
11+
return (cast(immutable ubyte[])"world").witList.witClone;
12+
}
13+
14+
alias Exports = wit.my.lists.test.Exports!(
15+
foo,
16+
bar
17+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import wit.test.many_arguments.runner;
2+
import wit.common;
3+
4+
@witExport("$root", "run")
5+
void run() {
6+
manyArguments(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
7+
}
8+
9+
alias Exports = wit.test.many_arguments.runner.Exports!(
10+
run
11+
);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import wit.test.many_arguments.test;
2+
import wit.common;
3+
4+
import std.meta : Repeat, AliasSeq;
5+
6+
@witExport("test:many-arguments/to-test", "many-arguments")
7+
void manyArguments(Repeat!(16, ulong) args) {
8+
assert(args == AliasSeq!(
9+
1, 2, 3, 4, 5, 6, 7, 8,
10+
9, 10, 11, 12, 13, 14, 15, 16
11+
));
12+
}
13+
14+
alias Exports = wit.test.many_arguments.test.Exports!(
15+
manyArguments
16+
);

tests/runtime/numbers/runner.d

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import wit.test.numbers.runner;
2+
import wit.common;
3+
4+
void doAsserts(alias func)() {
5+
static if(is(typeof(func) P == function)) {
6+
alias T = P[0];
7+
static if (is(T == dchar)) {
8+
enum T a = 'a';
9+
enum T b = ' ';
10+
enum T c = '🚩';
11+
} else static if (__traits(isFloating, T)) {
12+
enum T a = 1.0;
13+
enum T b = -T.infinity;
14+
enum T c = T.infinity;
15+
} else {
16+
enum T a = 1;
17+
enum T b = T.min;
18+
enum T c = T.max;
19+
}
20+
}
21+
22+
assert(func(a) == a);
23+
assert(func(b) == b);
24+
assert(func(c) == c);
25+
}
26+
27+
@witExport("$root", "run")
28+
void run() {
29+
doAsserts!roundtripU8;
30+
doAsserts!roundtripS8;
31+
doAsserts!roundtripU16;
32+
doAsserts!roundtripS16;
33+
doAsserts!roundtripU32;
34+
doAsserts!roundtripS32;
35+
doAsserts!roundtripU64;
36+
doAsserts!roundtripS64;
37+
doAsserts!roundtripF32;
38+
doAsserts!roundtripF64;
39+
doAsserts!roundtripChar;
40+
41+
setScalar(2);
42+
assert(getScalar() == 2);
43+
44+
setScalar(4);
45+
assert(getScalar() == 4);
46+
}
47+
48+
alias Exports = wit.test.numbers.runner.Exports!(
49+
run
50+
);

tests/runtime/numbers/test.d

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import wit.test.numbers.test;
2+
import wit.common;
3+
4+
template roundtrip(T, string suffix) {
5+
@witExport("test:numbers/numbers", "roundtrip-"~suffix)
6+
T roundtrip(T val) => val;
7+
}
8+
9+
uint scalar;
10+
11+
@witExport("test:numbers/numbers", "get-scalar")
12+
auto getScalar() => scalar;
13+
14+
@witExport("test:numbers/numbers", "set-scalar")
15+
void setScalar(uint val) { scalar = val; }
16+
17+
alias Exports = wit.test.numbers.test.Exports!(
18+
roundtrip!(ubyte, "u8"),
19+
roundtrip!(byte, "s8"),
20+
roundtrip!(ushort, "u16"),
21+
roundtrip!(short, "s16"),
22+
roundtrip!(uint, "u32"),
23+
roundtrip!(int, "s32"),
24+
roundtrip!(ulong, "u64"),
25+
roundtrip!(long, "s64"),
26+
roundtrip!(float, "f32"),
27+
roundtrip!(double, "f64"),
28+
roundtrip!(dchar, "char"),
29+
30+
getScalar,
31+
setScalar
32+
);

0 commit comments

Comments
 (0)