obj.h icon indicating copy to clipboard operation
obj.h copied to clipboard

How to use it in emscripten?

Open bumagh opened this issue 1 year ago • 13 comments

I want to use it in emscripten

bumagh avatar Dec 15 '22 11:12 bumagh

In theory, LLVM/Clang's C compiler can compile those examples fine, so there is no problem to compile them in Emscripten. But I'm not sure about they can run correctly, in machine code level.

You should use C++ instead.

nomi-san avatar Dec 15 '22 12:12 nomi-san

But in the obj.h it can not support the platform,

bumagh avatar Dec 22 '22 15:12 bumagh

Did you get compilation errors in this header?

nomi-san avatar Dec 22 '22 15:12 nomi-san

no compilation errors , just show not support

bumagh avatar Dec 22 '22 16:12 bumagh

#endif #define __OBJ_ACTIV(ptr, size)
(VirtualProtect(ptr, size, PAGE_EXECUTE_READWRITE, &((unsigned long){ 0 })) != 0) #else #error "This OS is not supported!" #endif

#if defined(x86_64) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64) #define __OBJ_X64 1 #elif defined(__i386) || defined(i386) || defined(_M_IX86) #define __OBJ_X86 1 #else #error This architecture is not supported! #endif VirtualProtect this function I can not find

bumagh avatar Dec 22 '22 16:12 bumagh

PAGE_SHIFT undefine

bumagh avatar Dec 22 '22 16:12 bumagh

no compilation errors , just show not support

cc: error "This OS is not supported!"
cc: error "This architecture is not supported!"

If you got errors of the header checks, you can disable these #error code.

https://github.com/small-c/obj.h/blob/c9f00cf063fb1f54a5e5c87a60af69564e719a8f/obj.h#L66-L76

In CPU arch checking, you should define one. For example, your web browser runtime is 64-bit (x86-64):

#define __OBJ_X64

Remember this library still does not support ARM64 (includes Apple M1/M2 Sillicon).

nomi-san avatar Dec 22 '22 16:12 nomi-san

VirtualProtect this function I can not find

This is a win32 API, in Emscripten you should use mprotect() instead.

PAGE_SHIFT undefine

This value usually is 12, you can find it in compiler's include files.

nomi-san avatar Dec 22 '22 16:12 nomi-san

./obj.h:101:71: error: use of undeclared identifier '__OBJ_CLOFNNUM' ...if (*(size_t *)((uintptr_t)prototype + offset) == (size_t)__OBJ_CLOFNNUM)

                                                           ^

./obj.h:142:30: error: use of undeclared identifier 'asmc'; did you mean 'asin'

ihsize = offset + sizeof(asmc);
                         ^~~~
                         asin

D:\cenv\emsdk\upstream\emscripten\cache\sysroot/include\math.h:143:13: note: 'asin' declared here double asin(double); ^ In file included from ./player.c:2: In file included from ./skillButton.h:4: In file included from ./textTexture.h:3: ./obj.h:145:10: error: use of undeclared identifier 'PAGE_SHIFT'; did you mean 'KMOD_SHIFT'? if (!__OBJ_ACTIV(code, ihsize)) { ^ ./obj.h:49:40: note: expanded from macro '__OBJ_ACTIV' (mprotect((void *)(((size_t)ptr >> PAGE_SHIFT) << PAGE_SHIFT), size... ^ D:\cenv\emsdk\upstream\emscripten\cache\sysroot\include\SDL2/SDL_keycode.h:344: : note: 'KMOD_SHIFT' declared here KMOD_SHIFT = KMOD_LSHIFT | KMOD_RSHIFT, ^ In file included from ./player.c:2: In file included from ./skillButton.h:4: In file included from ./textTexture.h:3: ./obj.h:145:10: error: use of undeclared identifier 'PAGE_SHIFT'; did you mean 'KMOD_SHIFT'? if (!__OBJ_ACTIV(code, ihsize)) { ^ ./obj.h:49:55: note: expanded from macro '__OBJ_ACTIV' (mprotect((void *)(((size_t)ptr >> PAGE_SHIFT) << PAGE_SHIFT), size... ^ D:\cenv\emsdk\upstream\emscripten\cache\sysroot\include\SDL2/SDL_keycode.h:344: : note: 'KMOD_SHIFT' declared here KMOD_SHIFT = KMOD_LSHIFT | KMOD_RSHIFT, ^ In file included from ./player.c:2: In file included from ./skillButton.h:4: In file included from ./textTexture.h:3: ./obj.h:157:5: error: use of undeclared identifier 'asmc' asmc.data = (uintptr_t)data; ^ ./obj.h:159:41: error: use of undeclared identifier 'asmc'; did you mean 'asin'

memcpy(code + offset, &asmc, sizeof(asmc));
                                    ^~~~
                                    asin

D:\cenv\emsdk\upstream\emscripten\cache\sysroot/include\math.h:143:13: note: 'asin' declared here double asin(double); ^ In file included from ./player.c:2: In file included from ./skillButton.h:4: In file included from ./textTexture.h:3: ./obj.h:159:28: error: use of undeclared identifier 'asmc'; did you mean 'asin'

memcpy(code + offset, &asmc, sizeof(asmc));
                       ^~~~

bumagh avatar Dec 22 '22 17:12 bumagh

index.js:2333 could't find closure declaration at prototype function (0x25)!could't find closure declaration at prototype function (0x26)!could't find closure declaration at prototype function (0x27)!could't find closure declaration at prototype function (0x28)!could't find closure declaration at prototype function (0x29)![obj.h] error: could't implement the method 'show'! put_char @ index.js:2333 index.js:2333 could't find closure declaration at prototype function (0x1b)!could't find closure declaration at prototype function (0x1c)!could't find closure declaration at prototype function (0x1d)!could't find closure declaration at prototype function (0x1e)!could't find closure declaration at prototype function (0x1f)![obj.h] error: could't implement the method 'show'!

when i run it

bumagh avatar Dec 22 '22 17:12 bumagh

These errors related to CPU arch and function ABI, also wasm code when Emscripten compiles. I'll test them soon.

Leave your system information here, about CPU, OS, web browser name and Emscripten version.

nomi-san avatar Dec 22 '22 17:12 nomi-san

These errors related to CPU arch and function ABI, also wasm code when Emscripten compiles. I'll test them soon.

Leave your system information here, about CPU, OS, web browser name and Emscripten version.

windows7 x64 Intel core i5 2467m [email protected] chrome 108.0.5359.125 (64 位) emscripten version 3.1.7 Thank you

bumagh avatar Dec 22 '22 18:12 bumagh

Anything new? @nomi-san

bumagh avatar Jan 03 '23 19:01 bumagh