draft proposal for Sum Types for D
ryuukk_
ryuukk.dev at gmail.com
Thu Dec 1 12:48:04 UTC 2022
On Thursday, 1 December 2022 at 11:51:23 UTC, Guillaume Piolat
wrote:
> On Wednesday, 30 November 2022 at 20:54:26 UTC, H. S. Teoh
> wrote:
>> I haven't had time to look at the two existing wasm projects
>> yet (one by Adam, and the other I forgot by who); have you
>> tried either of them out yet?
>
> With the LDC wasm triple, Phobos doesn't build on Windows, I
> didn't go much further. Beyond that, any C function will also
> not work.
>
>
>> I think we should evaluate what we currently have, and lay out
>> a rough plan for how to move forward.
>
> It seems to me there is few people that know what we do have,
> or can even envision the plan.
From a language PoV, WASM support is 100% complete
From a runtime/std PoV it's not.. but if you are making a game it
doesn't matter AT ALL!
In fact, i am even working on a game that supports WASM target
using WebGL: https://www.kdom.xyz/ (it is an online multiplayer
game)
I haven't updated the build on the website for a while since i'm
focusing on the art/backend side of things, but i'll soon make a
proper blog post about it once it's in a pleasant and playable
state
My only issue so far was when certain features didn't work, and
the compiler wouldn't help me with error messages, for example:
working with slices
I had to copy bits of druntime's object.d, i wish i didn't have
to do that
But i learned to like it and now i have a custom object.d and i
can use: `i32 u32 i64 u64`(etc) aliases globally! That's pretty
cool
Oh.. and allocating memory with WASM, this is why i wished there
was a proper Allocator API available that is minimal (meaning it
doesn't import anything from either druntime/phobos), so it's
available right away to kickstart projects that targets esoteric
platforms such as WASM
So i made one! it's basically a port of Zig's one, i yoinked it
since i didn't like the ergonomics of the language for their API
is cool, so i went a head and ported it, i might release a dub
package in the future
We have to be careful with WASM, we should not offer a bloated
solution
Writing your javascript glue code is more than enough to access
browser APIs, basic WASI support is all we really need if you
want to make a game
So a general purpose solution might not be the most useful one
Look at Blazor from .net and how they struggle to produce small
enough wasm files
My wasm file is just 700kb (and it includes builtin
textures/shaders), competitive advantage for D
My glue code for WebSocket is just this:
https://www.kdom.xyz/lib/net.js
```D
// websocket
alias ws_cb_t = extern(C) void delegate (int ws, int event, int
data);
int js_ws_create(const(char)[] url, void* bufferPtr, scope
ws_cb_t cb);
bool js_ws_send_data(int ws, const(void)* ptr, int len);
bool js_ws_send_str(int ws, const(char)* ptr, int len);
void js_ws_disconnect(int ws);
extern(C) export void js_cb_ws(uint ctx, uint fun, int ws, int
event, int data) {
static struct Handler {
union {
extern(C) void delegate(int, int, int) handle;
struct {
void* contextPtr;
void* funcPtr;
}
}
}
Handler c;
c.contextPtr = cast(void*) ctx;
c.funcPtr = cast(void*) fun;
c.handle(ws, event, data);
}
```
and i use it:
```D
state.ws_id = js_ws_create(host.strip, cast(void*)
state.ws_buffer.ptr, (ws, evt, data) {
if (evt == 1) // onopen
{
state.net_info.state = ConnectState.CONNECTED;
on_connected();
}
else if (evt == 2) // data str
{ }
else if (evt == 3) // data binary
{
decode_packet(state.ws_buffer[0 .. data]);
}
else if (evt == 4) // close
{
state.net_info.state = ConnectState.DISCONNECTED;
state.ws_id = 0;
on_disconnected();
}
else if (evt == 5) // error
{
LERRO("ws: error {} {} {}", ws, evt, data);
}
else
panic("shouldn't happen {} {} {}", ws, evt, data);
});
```
Full delegate support out of the box!
I need to become more responsible and provide blog posts,
resources and more examples on github to help people.. i got
this one example but no documentation and is a bit too old:
https://github.com/ryuukk/dasm
Hopefully this game i am making will be a good showcase for D
More information about the Digitalmars-d
mailing list