ldc-developers/ldc

core.stdc.stdlib.d is missing some type definitions for wasm32-unknown-unknown-wasm target

Sticky-fingerz opened this issue · 5 comments

When import core.stdc.stdlib.d (for example,to use malloc) in wasm project and build next command

ldc2 -c -betterC -mtriple=wasm32-unknown-unknown-wasm src/wasmproject.d  

these errors ocuured.

C:\ldc2\bin\..\import\core\stdc\stdlib.d(73): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(74): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(112): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(121): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(125): Error: undefined identifier `c_ulong`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(202): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(209): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(209): Error: undefined identifier `c_long`
C:\ldc2\bin\..\import\core\stdc\stdlib.d(217): Error: undefined identifier `wchar_t`, did you mean `dchar`? 
C:\ldc2\bin\..\import\core\stdc\stdlib.d(221): Error: undefined identifier `wchar_t`, did you mean `dchar`? 
C:\ldc2\bin\..\import\core\stdc\stdlib.d(223): Error: undefined identifier `wchar_t`, did you mean `dchar`? 

These problems solved by adding type definitions to the three files (config.d,stddef.d,stdlib.d) in the binary directory(ldc\import\core\stdc).

config.d

Add to line 135 
else version (WebAssembly)
{
    static if ( (void*).sizeof > int.sizeof )
    {
        enum __c_longlong  : long;
        enum __c_ulonglong : ulong;

        alias long  c_long;
        alias ulong c_ulong;

        alias long   cpp_long;
        alias ulong  cpp_ulong;

        alias __c_longlong  cpp_longlong;
        alias __c_ulonglong cpp_ulonglong;
    }
    else
    {
        enum __c_long  : int;
        enum __c_ulong : uint;

        alias int   c_long;
        alias uint  c_ulong;

        alias __c_long   cpp_long;
        alias __c_ulong  cpp_ulong;

        alias long  cpp_longlong;
        alias ulong cpp_ulonglong;
    }
}

stddef.d

Add to line 32
else version (WebAssembly)
{
    ///
    alias dchar wchar_t;
}

stdlib.d

Add to line 105
else version (WebAssembly) enum RAND_MAX = 0x7fffffff;

It's working fine for now.

please submit a PR upstream to dlang/dmd to add these definitions

I have not my local repository.
Is it okay to post the same issue to dmd?

Please post a PR, you already have the solution, you can open an issue at issues.dlang.org if you feel inclined.
You will need to clone https://github.com/dlang/dmd/ and apply your changes to https://github.com/dlang/dmd/tree/master/druntime/src/core/stdc

To fork dlang/dmd repository,I posted pull request.
Git clone and push approach is mission impossible for my PC.
Please check it just in case.
dlang/dmd#16535

When I posted to dlang/dmd, I found out that the same thing can be achieved with "-mtriple=wasm32-unknown-unknown-wasi", and that the above type is actually not implemented in webassembly and the emscripten type is called, so I closed this issue as incorrect.