Skip to content

Commit 8017f19

Browse files
WASI support (#75)
Adds support for WASI/`wasi-libc` to Phobos.
1 parent 57477cf commit 8017f19

30 files changed

Lines changed: 788 additions & 249 deletions

etc/c/curl.d

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
module etc.c.curl;
3737

38+
// WASIp1's networking is extremely limited
39+
version (WASIp1) {}
40+
else:
41+
3842
import core.stdc.config;
3943
import core.stdc.time;
4044
import std.socket;

std/concurrency.d

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ import std.range.interfaces : InputRange;
8888
import std.traits;
8989

9090
///
91-
@system unittest
91+
version (WASI) {} // WASI is single-threaded
92+
else @system unittest
9293
{
9394
__gshared string received;
9495
static void spawnedFunc(Tid ownerTid)
@@ -413,7 +414,8 @@ public:
413414
}
414415

415416
// https://issues.dlang.org/show_bug.cgi?id=21512
416-
@system unittest
417+
version (WASI) {} // WASI is single-threaded
418+
else @system unittest
417419
{
418420
import std.format : format;
419421

@@ -452,7 +454,8 @@ public:
452454
return thisInfo.owner;
453455
}
454456

455-
@system unittest
457+
version (WASI) {} // WASI is single-threaded
458+
else @system unittest
456459
{
457460
import std.exception : assertThrown;
458461

@@ -526,6 +529,8 @@ if (isSpawnable!(F, T))
526529
}
527530

528531
///
532+
version (WASI) {} // WASI is single-threaded
533+
else
529534
@system unittest
530535
{
531536
static void f(string msg)
@@ -537,7 +542,8 @@ if (isSpawnable!(F, T))
537542
}
538543

539544
/// Fails: char[] has mutable aliasing.
540-
@system unittest
545+
version (WASI) {} // WASI is single-threaded
546+
else @system unittest
541547
{
542548
string msg = "Hello, World!";
543549

@@ -551,15 +557,17 @@ if (isSpawnable!(F, T))
551557
}
552558

553559
/// New thread with anonymous function
554-
@system unittest
560+
version (WASI) {} // WASI is single-threaded
561+
else @system unittest
555562
{
556563
spawn({
557564
ownerTid.send("This is so great!");
558565
});
559566
assert(receiveOnly!string == "This is so great!");
560567
}
561568

562-
@system unittest
569+
version (WASI) {} // WASI is single-threaded
570+
else @system unittest
563571
{
564572
import core.thread : thread_joinAll;
565573

@@ -630,7 +638,8 @@ if (isSpawnable!(F, T))
630638
return spawnTid;
631639
}
632640

633-
@system unittest
641+
version (WASI) {} // WASI is single-threaded
642+
else @system unittest
634643
{
635644
void function() fn1;
636645
void function(int) fn2;
@@ -757,7 +766,8 @@ do
757766
}
758767

759768
///
760-
@system unittest
769+
version (WASI) {} // WASI is single-threaded
770+
else @system unittest
761771
{
762772
import std.variant : Variant;
763773

@@ -895,7 +905,8 @@ do
895905
}
896906

897907
///
898-
@system unittest
908+
version (WASI) {} // WASI is single-threaded
909+
else @system unittest
899910
{
900911
auto tid = spawn(
901912
{
@@ -905,7 +916,8 @@ do
905916
}
906917

907918
///
908-
@system unittest
919+
version (WASI) {} // WASI is single-threaded
920+
else @system unittest
909921
{
910922
auto tid = spawn(
911923
{
@@ -915,7 +927,8 @@ do
915927
}
916928

917929
///
918-
@system unittest
930+
version (WASI) {} // WASI is single-threaded
931+
else @system unittest
919932
{
920933
struct Record { string name; int age; }
921934

@@ -930,7 +943,8 @@ do
930943
send(tid, 0.5, Record("Alice", 31));
931944
}
932945

933-
@system unittest
946+
version (WASI) {} // WASI is single-threaded
947+
else @system unittest
934948
{
935949
static void t1(Tid mainTid)
936950
{
@@ -1244,7 +1258,8 @@ void join(Tid tid)
12441258
}
12451259

12461260
///
1247-
@system unittest
1261+
version (WASI) {} // WASI is single-threaded
1262+
else @system unittest
12481263
{
12491264
import core.time : msecs;
12501265
import core.thread : Thread;
@@ -1478,7 +1493,8 @@ class ThreadScheduler : Scheduler
14781493
* This is an example scheduler that creates a new `Fiber` per call to spawn
14791494
* and multiplexes the execution of all fibers within the main thread.
14801495
*/
1481-
class FiberScheduler : Scheduler
1496+
version (WebAssembly) {} // No Fiber support on Wasm yet
1497+
else class FiberScheduler : Scheduler
14821498
{
14831499
/**
14841500
* This creates a new `Fiber` for the supplied op and then starts the
@@ -1669,7 +1685,8 @@ private:
16691685
size_t m_pos;
16701686
}
16711687

1672-
@system unittest
1688+
version (WebAssembly) {} // No Fiber support on Wasm yet
1689+
else @system unittest
16731690
{
16741691
static void receive(Condition cond, ref size_t received)
16751692
{
@@ -1729,16 +1746,23 @@ __gshared Scheduler scheduler;
17291746
*/
17301747
void yield() nothrow
17311748
{
1732-
auto fiber = Fiber.getThis();
1733-
if (!(cast(IsGenerator) fiber))
1749+
version (WebAssembly) // No Fiber support on Wasm yet
1750+
{
1751+
scheduler.yield();
1752+
}
1753+
else
17341754
{
1735-
if (scheduler is null)
1755+
auto fiber = Fiber.getThis();
1756+
if (!(cast(IsGenerator) fiber))
17361757
{
1737-
if (fiber)
1738-
return Fiber.yield();
1758+
if (scheduler is null)
1759+
{
1760+
if (fiber)
1761+
return Fiber.yield();
1762+
}
1763+
else
1764+
scheduler.yield();
17391765
}
1740-
else
1741-
scheduler.yield();
17421766
}
17431767
}
17441768

@@ -1751,7 +1775,8 @@ private interface IsGenerator {}
17511775
* that periodically returns values of type `T` to the
17521776
* caller via `yield`. This is represented as an InputRange.
17531777
*/
1754-
class Generator(T) :
1778+
version (WebAssembly) {} // No Fiber support on Wasm yet
1779+
else class Generator(T) :
17551780
Fiber, IsGenerator, InputRange!T
17561781
{
17571782
/**
@@ -1934,7 +1959,8 @@ private:
19341959
}
19351960

19361961
///
1937-
@system unittest
1962+
version (WebAssembly) {} // No Fiber support on Wasm yet
1963+
else @system unittest
19381964
{
19391965
auto tid = spawn({
19401966
int i;
@@ -1962,7 +1988,8 @@ private:
19621988
* Params:
19631989
* value = The value to yield.
19641990
*/
1965-
void yield(T)(ref T value)
1991+
version (WebAssembly) {} // No Fiber support on Wasm yet
1992+
else void yield(T)(ref T value)
19661993
{
19671994
Generator!T cur = cast(Generator!T) Fiber.getThis();
19681995
if (cur !is null && cur.state == Fiber.State.EXEC)
@@ -1974,12 +2001,14 @@ void yield(T)(ref T value)
19742001
}
19752002

19762003
/// ditto
1977-
void yield(T)(T value)
2004+
version (WebAssembly) {} // No Fiber support on Wasm yet
2005+
else void yield(T)(T value)
19782006
{
19792007
yield(value);
19802008
}
19812009

1982-
@system unittest
2010+
version (WebAssembly) {} // No Fiber support on Wasm yet
2011+
else @system unittest
19832012
{
19842013
import core.exception;
19852014
import std.exception;
@@ -2033,7 +2062,8 @@ void yield(T)(T value)
20332062
scheduler = null;
20342063
}
20352064
///
2036-
@system unittest
2065+
version (WebAssembly) {} // No Fiber support on Wasm yet
2066+
else @system unittest
20372067
{
20382068
import std.range;
20392069

@@ -2670,7 +2700,8 @@ private
26702700
}
26712701
}
26722702

2673-
@system unittest
2703+
version (WASI) {} // WASI is single-threaded
2704+
else @system unittest
26742705
{
26752706
import std.typecons : tuple, Tuple;
26762707

@@ -2763,7 +2794,8 @@ auto ref initOnce(alias var)(lazy typeof(var) init)
27632794
assert(MySingleton.instance !is null);
27642795
}
27652796

2766-
@system unittest
2797+
version (WASI) {} // WASI is single-threaded
2798+
else @system unittest
27672799
{
27682800
static class MySingleton
27692801
{
@@ -2832,7 +2864,8 @@ auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex)
28322864
}
28332865

28342866
/// Use a separate mutex when init blocks on another thread that might also call initOnce.
2835-
@system unittest
2867+
version (WASI) {} // WASI is single-threaded
2868+
else @system unittest
28362869
{
28372870
import core.sync.mutex : Mutex;
28382871

@@ -2864,7 +2897,8 @@ auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex)
28642897
}
28652898

28662899
// test ability to send shared arrays
2867-
@system unittest
2900+
version (WASI) {} // WASI is single-threaded
2901+
else @system unittest
28682902
{
28692903
static shared int[] x = new shared(int)[1];
28702904
auto tid = spawn({
@@ -2878,15 +2912,17 @@ auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex)
28782912
}
28792913

28802914
// https://issues.dlang.org/show_bug.cgi?id=13930
2881-
@system unittest
2915+
version (WASI) {} // WASI is single-threaded
2916+
else @system unittest
28822917
{
28832918
immutable aa = ["0":0];
28842919
thisTid.send(aa);
28852920
receiveOnly!(immutable int[string]); // compile error
28862921
}
28872922

28882923
// https://issues.dlang.org/show_bug.cgi?id=19345
2889-
@system unittest
2924+
version (WASI) {} // WASI is single-threaded
2925+
else @system unittest
28902926
{
28912927
static struct Aggregate { const int a; const int[5] b; }
28922928
static void t1(Tid mainTid)
@@ -2916,7 +2952,10 @@ auto ref initOnce(alias var)(lazy typeof(var) init, Mutex mutex)
29162952
static assert(__traits(compiles, receiveOnly!noreturn() ));
29172953
static assert(__traits(compiles, send(Tid.init, noreturn.init) ));
29182954
static assert(__traits(compiles, prioritySend(Tid.init, noreturn.init) ));
2919-
static assert(__traits(compiles, yield(noreturn.init) ));
2955+
2956+
version (WebAssembly) {}
2957+
else
2958+
static assert(__traits(compiles, yield(noreturn.init) ));
29202959

29212960
static assert(__traits(compiles, {
29222961
__gshared noreturn n;

0 commit comments

Comments
 (0)