Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 3 commits into from

Conversation

Sticky-fingerz
Copy link

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;

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @Sticky-fingerz! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16535"

@dkorpel
Copy link
Contributor

dkorpel commented May 27, 2024

WebAssembly doesn't define these C types or malloc, are you using WASI? In that case, compiling with -mtriple=wasm32-unknown-unknown-wasi should work, as there are version(WASI) blocks. If not, then where did you get these type definitons from?

@Sticky-fingerz
Copy link
Author

Sticky-fingerz commented May 27, 2024

In fact, I don't really understand,but I wanted to develop wasm game with raylib and dlang.
At first, to use ldc2, my game source compile to wasm object(index.o) ,
and to link index.o and libraylib.a by emscripten created index.wasm.
This approach worked for zig,odin and rust.
this is my command.

ldc2 -c -betterC -mtriple=wasm32-unknown-unknown-wasm src/hello.d  --od=dist
emcc -o dist/index.html dist/index.o libs/libraylib.a -s USE_GLFW=3 -s ASYNCIFY --preload-file resources

This approach seems to be working in my game.
Is emscripten's malloc being called?

@Sticky-fingerz
Copy link
Author

By the way,I didn't realize it until you mentioned it.
I tried "-mtriple=wasm32-unknown-unknown-wasi" and it worked. 
Maybe it's better to remove this PR?

@dkorpel
Copy link
Contributor

dkorpel commented May 27, 2024

Is emscripten's malloc being called?

Yes, emscripten provides a C runtime with the right JavaScript hooks IIRC

Maybe it's better to remove this PR?

While I do wish D supported WebAssembly better out of the box, I don't think this PR is correct, so I'll close it.

@dkorpel dkorpel closed this May 27, 2024
@JohanEngelen
Copy link
Contributor

@Sticky-fingerz Note -mtriple=wasm32-unknown-unknown-emscripten does not set version(WASI) but does set version(Emscripten), so I think still this work needed (with adjustment).

@Sticky-fingerz
Copy link
Author

Sticky-fingerz commented May 27, 2024

I tried the above commit with changeing version(WebAssembly) → version(Emscripten) and build with
"-mtriple=wasm32-unknown-unknown-emscripten",
but it didn't work with same errors.
It's a mystery,when I build
"-mtriple=wasm32-unknown-unknown-emscripten"
with the above commit(version(WebAssembly)), it worked.
If I develop game with wasi and a problem occurs, i might commit version(Emscripten), but I don't know if I can solve it.

@JohanEngelen
Copy link
Contributor

@kassane Can you give a hand here? :) thanks!

@Sticky-fingerz -mtriple=wasm32-unknown-unknown-emscripten requires LDC master, it's not released yet.

@kassane
Copy link
Contributor

kassane commented May 27, 2024

@kassane Can you give a hand here?

Hi @Sticky-fingerz,

WiP ldc2-1.39/2.109.0: wasm32-unknown-emscripten simplifies linking with emcc-libc, preferably with LTO enabled.

You could also try using wasm32-unknown-unknown-wasm with tinyd-rt (based on arsd-webassembly).

References

@Sticky-fingerz
Copy link
Author

@JohanEngelen I understood.thanks.
@kassane  Thank you for the information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants