December 01, 2022

On Thursday, 1 December 2022 at 05:49:56 UTC, cc wrote:

>

I imagine some stubbed toes writing if (ret.Error) without the ? for a while, and potential unnoticed bugs when it works by accident. I like the recentish trend of D compiler errors suggesting "Did you mean to use..." but would that be suitable for a runtime error message?

Nevermind, I see now it's just a straight up termination with no message. Ouch.

December 01, 2022
On Wednesday, 30 November 2022 at 20:54:26 UTC, H. S. Teoh wrote:
> On Wed, Nov 30, 2022 at 08:32:26PM +0000, Hipreme via Digitalmars-d wrote:
>> On Wednesday, 30 November 2022 at 20:01:47 UTC, Guillaume Piolat wrote:
>> > On Tuesday, 29 November 2022 at 06:26:20 UTC, Walter Bright wrote:
>> > > Go ahead, Make My Day! Destroy!
>> > > 
>> > > https://github.com/WalterBright/DIPs/blob/sumtypes/DIPs/1NNN-(wgb).md
>> > 
>> > Would prefer WASM and named arguments to sum types.
>> 
>> Would love to implement Web support for my engine too :D
>
> 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?  How complete/incomplete are they?  How much can you do in them? How close/far are they to a comprehensive WASM support package for D?
>
> I think we should evaluate what we currently have, and lay out a rough plan for how to move forward.
>
> My ideal dream is a WASM support package (build system, helper scripts, HTML/JS generators, etc) that lets me take an existing D program, complete with GC and main(), and compile it into a web app (WASM modules + HTML + JS glue, the latter preferably completely auto-generated, that can be copied into my Apache web directory and it would Just Work(tm)), with minimal / no changes to the code itself.  Fiddly details like passing strings to/from JS, interfacing with web APIs or WASI or whatever, should just be completely automated away by scripts or build system or whatever.
>
> Of course, I think we still have a way to go before we get to this point.  But it would be good to evaluate just where exactly we are right now wrt this ideal dream, so that we can make meaningful steps forward.
>
>
> T

Adam's WASM is a lot easier to a mere mortal to use, it is only a matter of including it to the project and build it as ldc wasm triple and you're done.
I think the problem Adam's project has is of it being a custom druntime so you'll have a lot of pitfalls there and must be careful in which features you're using, but it is quite usable small projects IMO.

The other is from Sebastian Koppe, which he attempts to port WASI as the libc (and thus, making the runtime just work). The problem is that you will need to actually build the druntime and wasi itself which I wasn't able to get it done for reasons like: WASI wasn't building in my PC so I wasn't able to try the runtime.


From what I remember that Adam said, there was a problem on the garbage collection being executed while wasm was running its code. IIRC, he said something about deferring the GC, didn't quite get the details but he seemed really sure that would solve the problems.


>Fiddly details like passing strings to/from JS interfacing with web APIs or WASI or whatever, should just be completely automated away by scripts or build system or whatever.

IIRC, Adam's project had an `eval!` which would be able to do that. But I guess having real compiler support and marking the function as `extern(System)` could do that. Executing JS defined code could even be: `@wasm("the.javascript.function")` or even `extern(WebAssembly, "the.javascript.function")` as it would use the same syntax from the C++ namespace

December 01, 2022
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.

December 01, 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

// 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:

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

December 01, 2022

On Thursday, 1 December 2022 at 12:48:04 UTC, ryuukk_ wrote:

Forgot to add, good ressources for WASM without emscripten, that's what i followed basically:

https://schellcode.github.io/webassembly-without-emscripten

Also an example of a language that follows this principle: Odin:

https://github.com/odin-lang/Odin/tree/master/vendor/wasm

This is imo the proper way to do it

And you can still use emscripten btw: https://theartofmachinery.com/2018/12/20/emscripten_d.html

So druntime/phobos might be working out of the box, but will produce rather large wasm files

I'll give that a try some day

December 01, 2022

On Thursday, 1 December 2022 at 12:55:17 UTC, ryuukk_ wrote:

>

On Thursday, 1 December 2022 at 12:48:04 UTC, ryuukk_ wrote:

Forgot to add, good ressources for WASM without emscripten, that's what i followed basically:

https://schellcode.github.io/webassembly-without-emscripten

Also an example of a language that follows this principle: Odin:

https://github.com/odin-lang/Odin/tree/master/vendor/wasm

This is imo the proper way to do it

And you can still use emscripten btw: https://theartofmachinery.com/2018/12/20/emscripten_d.html

So druntime/phobos might be working out of the box, but will produce rather large wasm files

I'll give that a try some day

Actually it dont: Instead of porting the D runtime, it uses a lightweight, runtimeless -betterC build.

Doing a simple game without GC is possible, but when you wish to use GC and standard library this is the problem.

If one wishes to offer a not bloated solution, one can use betterC, having the option for me is the best thing to do. I have done my own standard library to use in D, which avoids many templates, many imports depending from the libc, still, it is quite far from having real support on WASM.

Though I think the main problem it has is using associative arrays, I have my own implementation of ref counted associative arrays, but I'm not using. I should consider a bit about this.

December 01, 2022
On 11/29/2022 10:52 AM, ryuukk_ wrote:
> About the ? query thing, why not at the end?

Because then it would make parsing ?: more difficult.

December 01, 2022
On 11/29/2022 1:37 AM, Paulo Pinto wrote:
> On Tuesday, 29 November 2022 at 06:26:20 UTC, Walter Bright wrote:
>> Go ahead, Make My Day! Destroy!
>>
>> https://github.com/WalterBright/DIPs/blob/sumtypes/DIPs/1NNN-(wgb).md
> 
> What if I game the compiler changing the declaration order?
> 
> sumtype Pointer
> {
>      int* Ptr,
>      Null,
> }

That shouldn't make any difference.

> or if I have multiple pointer based cases,

For multiple non-null pointers:

sumtype Pointer { Null, int* Ptr }

sumtype TwoPointers { Pointer Ptr1, Pointer Ptr2 }
December 01, 2022
On 11/30/2022 6:43 AM, Adam D Ruppe wrote:
> This phenomenon is what prompted my newest blog post:
> http://dpldocs.info/this-week-in-d/Blog.Posted_2022_11_28.html#dip-dip,-part-2
> 
> I'd formalize some of the feedback stuff to ensure discussions happen before feedback and to remove duplicates.

Not so easy to do :-/
December 01, 2022
On 11/30/2022 12:49 AM, Andrey Zherikov wrote:
> Please consider moving `?` to be after identifier so the following can be allowed:

Problems with:

   a?

and:

   a?b:c

It's not impossible to do, but it requires arbitrary lookahead to resolve and I like to keep the parser simple and fast.