-make tests working on web

-WebAssembly now is compiled with emscripten use (stdc functions bindings only)
-added python script to build WASM version
This commit is contained in:
Mergul 2019-11-05 09:21:02 +01:00
parent 015783bf5c
commit a8c74d5045
5 changed files with 158 additions and 23 deletions

3
.gitignore vendored
View file

@ -6,4 +6,5 @@
!./dub.json !./dub.json
!.gitignore !.gitignore
!meson.build !meson.build
!meson_options.txt !meson_options.txt
!compile_wasm.py

91
compile_wasm.py Normal file
View file

@ -0,0 +1,91 @@
import os
import ntpath
import sys
shared_flags = ''
clean = 0
for arg in sys.argv:
if(arg == '-O3'):
shared_flags += '-O3 '
elif(arg == '-O2'):
shared_flags += '-O2 '
elif(arg == '-O1'):
shared_flags += '-O1 '
elif(arg == '-O0'):
shared_flags += '-O0 '
elif(arg == '-Os'):
shared_flags += '-Os '
elif(arg == '-Oz'):
shared_flags += '-Oz '
elif(arg == '-g'):
shared_flags += '-g '
elif(arg == '--clean'):
clean = 1
if clean == 1:
for path in ['bc']:
for r, d, f in os.walk(path):
for file in f:
filename, file_extension = os.path.splitext(file)
if file_extension == '.bc':
print('remove ' + os.path.join(r, file))
os.remove(os.path.join(r, file))
exit()
paths = ['tests', 'source']
files = []
# r=root, d=directories, f = files
for path in paths:
for r, d, f in os.walk(path):
for file in f:
if ntpath.basename(file) != 'win_dll.d':
filename, file_extension = os.path.splitext(file)
if file_extension == '.d':
files.append(os.path.join(r, file))
print('files:')
for f in files:
print(f)
print
ldc_cmd = 'ldc2 ' + shared_flags + '-mtriple=wasm32-unknown-unknown-wasm -betterC -L-allow-undefined --output-bc --od=bc --checkaction=C '
for path in paths:
ldc_cmd += '-I' + path + ' '
for f in files:
ldc_cmd += f + ' '
print ldc_cmd
os.system(ldc_cmd)
print
files = []
# r=root, d=directories, f = files
for path in ['bc']:
for r, d, f in os.walk(path):
for file in f:
filename, file_extension = os.path.splitext(file)
if file_extension == '.bc':
files.append(os.path.join(r, file))
print('BC files:')
for f in files:
print(f)
print
emcc_cmd = 'emcc -v ' + shared_flags + '-s ALLOW_MEMORY_GROWTH=1 -s MALLOC=dlmalloc -s WASM=1 -o index.html '
for f in files:
emcc_cmd += f + ' '
print emcc_cmd
os.system(emcc_cmd)

View file

@ -7,15 +7,16 @@ import std.traits;
version(WebAssembly) version(WebAssembly)
{ {
extern(C) int memcmp (const void *s1, const void *s2, size_t size);
extern(C) void exit (int status) nothrow @nogc;
extern(C) void __assert(const(char)* msg, const(char)* file, uint line) { exit(-20);}
extern(C) void free(void*) @nogc nothrow @system; extern(C) void free(void*) @nogc nothrow @system;
extern(C) void* malloc(size_t size) @nogc nothrow @system; extern(C) void* malloc(size_t size) @nogc nothrow @system;
extern(C) void* realloc(void*, size_t size) @nogc nothrow @system; extern(C) void* realloc(void*, size_t size) @nogc nothrow @system;
extern(C) void* memcpy(return void*, scope const void*, size_t size) @nogc nothrow @system; extern(C) void* memcpy(return void*, scope const void*, size_t size) @nogc nothrow @system;
extern(C) void* memset(void*, int val, size_t size) @nogc nothrow @system; extern(C) void* memset(void*, int val, size_t size) @nogc nothrow @system;
extern(C) int posix_memalign(void**, size_t, size_t) @nogc nothrow @system;
extern(C) void qsort(void* base, size_t num, size_t size, extern(C) void qsort(void* base, size_t num, size_t size, int function(const void*,const void*) compar) @nogc nothrow @system;
int function(const void*,const void*) compar) @nogc nothrow @system;
} }
else else
{ {
@ -142,7 +143,7 @@ static struct Mallocator
void* ret; void* ret;
version(Posix)posix_memalign(&ret, alignment, length);//ret = aligned_alloc(alignment, length); version(Posix)posix_memalign(&ret, alignment, length);//ret = aligned_alloc(alignment, length);
else version(Windows)ret = _aligned_malloc(length, alignment); else version(Windows)ret = _aligned_malloc(length, alignment);
else version(WebAssembly)ret = malloc(length); else version(WebAssembly)posix_memalign(&ret, alignment, length);//malloc(length);
else static assert(0, "Unimplemented platform!"); else static assert(0, "Unimplemented platform!");
return ret; return ret;
} }
@ -160,7 +161,6 @@ static struct Mallocator
else version(Windows)_aligned_free(cast(void*)object); else version(Windows)_aligned_free(cast(void*)object);
else version(WebAssembly)free(cast(void*)object); else version(WebAssembly)free(cast(void*)object);
else static assert(0, "Unimplemented platform!"); else static assert(0, "Unimplemented platform!");
} }
} }

View file

@ -198,7 +198,7 @@ public:
} }
export ref T opIndex(size_t elemNum) { export ref T opIndex(size_t elemNum) {
debug assert(elemNum < used, "Range violation [index]"); //debug assert(elemNum < used, "Range violation [index]");
return array.ptr[elemNum]; return array.ptr[elemNum];
} }
@ -221,12 +221,12 @@ public:
} }
export void opOpAssign(string op)(T obj) { export void opOpAssign(string op)(T obj) {
static assert(op == "~"); //static assert(op == "~");
add(obj); add(obj);
} }
export void opOpAssign(string op, X)(X[] obj) { export void opOpAssign(string op, X)(X[] obj) {
static assert(op == "~"); //static assert(op == "~");
add(obj); add(obj);
} }

View file

@ -14,15 +14,52 @@ import ecs.core;
version(WebAssembly) version(WebAssembly)
{ {
extern(C) void printf(const char *format, ...) @nogc nothrow @system; extern(C) int printf(scope const char* format, ...) @nogc nothrow @system;
struct Time /*{
return 0;
}*/
//import core.stdc.stdio : printf;
/*struct Time
{ {
static long getUSecTime() static long getUSecTime()
{ {
return 0; return 0;
} }
}*/
alias int time_t;
alias int clockid_t;
enum CLOCK_REALTIME = 0;
struct timespec
{
time_t tv_sec;
int tv_nsec;
} }
extern(C) int clock_gettime(clockid_t, timespec*) @nogc nothrow @system;
struct Time
{
static long getUSecTime()
{
time_t time;
timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
//time = spec.tv_sec;
return spec.tv_sec * 1000_000 + spec.tv_nsec / 1000;//time / 1000_000;
/*LARGE_INTEGER time, freq;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&time);
return time.QuadPart / (freq.QuadPart / 1000_000);*/
}
}
extern(C) void _start() {} extern(C) void _start() {}
} }
else version(Windows) else version(Windows)
@ -195,20 +232,20 @@ struct ChangeTestSystem
{ {
//printf("Entity added! ID: "); //printf("Entity added! ID: ");
foreach (i; 0 .. data.length) foreach (i; 0 .. data.length)
printf("Entity added! ID: %u\n",data.entites[i].id); printf("Entity added! ID: %u\n",cast(uint)data.entites[i].id.id);
////writeln("Entity added! ID: ", data.entites[i].id); ////writeln("Entity added! ID: ", data.entites[i].id);
} }
void onRemoveEntity(EntitiesData data) void onRemoveEntity(EntitiesData data)
{ {
////writeln("Entity removed! ID: ", data.entites[0].id); ////writeln("Entity removed! ID: ", data.entites[0].id);
printf("Entity removed! ID: %u\n",data.entites[0].id); printf("Entity removed! ID: %u\n",cast(uint)data.entites[0].id.id);
} }
void onChangeEntity(EntitiesData data) void onChangeEntity(EntitiesData data)
{ {
////writeln("Entity changed! ID: ", data.entites[0].id); ////writeln("Entity changed! ID: ", data.entites[0].id);
printf("Entity changed! ID: %u\n",data.entites[0].id); printf("Entity changed! ID: %u\n",cast(uint)data.entites[0].id.id);
} }
bool onBegin() bool onBegin()
@ -299,8 +336,8 @@ struct TestSystem
void onUpdate(ref Entity entity, ref TestComp test, ref TestComp2 test2) //, TestComp3* test3) //ref TestComp comp) void onUpdate(ref Entity entity, ref TestComp test, ref TestComp2 test2) //, TestComp3* test3) //ref TestComp comp)
{ {
assert(cast(size_t)&test % TestComp.alignof == 0); //assert(cast(size_t)&test % TestComp.alignof == 0);
assert(cast(size_t)&test2 % TestComp2.alignof == 0); //assert(cast(size_t)&test2 % TestComp2.alignof == 0);
test.a += 1000; test.a += 1000;
test.b += 2000; test.b += 2000;
@ -504,6 +541,8 @@ struct TestSystem2
extern(C) int main() extern(C) int main()
{ {
void dispatch(EntityManager.JobGroup jobs) nothrow @nogc void dispatch(EntityManager.JobGroup jobs) nothrow @nogc
{ {
foreach (job; jobs.jobs) foreach (job; jobs.jobs)
@ -516,28 +555,32 @@ extern(C) int main()
void writeEntityComponents(Entity* entity) void writeEntityComponents(Entity* entity)
{ {
printf("EntityID(%u, %u)",entity.id.id,entity.id.counter); printf("EntityID(%u, %u)",cast(uint)entity.id.id,cast(uint)entity.id.counter);
//write(entity.id); //write(entity.id);
TestComp* test_comp = entity.getComponent!TestComp; TestComp* test_comp = entity.getComponent!TestComp;
if (test_comp) if (test_comp)
printf("TestComp(%u, %u)",test_comp.a,test_comp.b);//write(*test_comp); printf("TestComp(%u, %u)",cast(uint)test_comp.a,cast(uint)test_comp.b);//write(*test_comp);
TestComp2* test_comp2 = entity.getComponent!TestComp2; TestComp2* test_comp2 = entity.getComponent!TestComp2;
if (test_comp2) if (test_comp2)
printf("TestComp2(%u, %u)",test_comp2.b,test_comp2.a);//write(*test_comp2); printf("TestComp2(%u, %u)",cast(uint)test_comp2.b,cast(uint)test_comp2.a);//write(*test_comp2);
TestComp3* test_comp3 = entity.getComponent!TestComp3; TestComp3* test_comp3 = entity.getComponent!TestComp3;
if (test_comp3) if (test_comp3)
printf("TestComp3(%u, %u)",test_comp3.gg,test_comp3.bg);//write(*test_comp3); printf("TestComp3(%u, %u)",cast(uint)test_comp3.gg,cast(uint)test_comp3.bg);//write(*test_comp3);
TestComp4* test_comp4 = entity.getComponent!TestComp4; TestComp4* test_comp4 = entity.getComponent!TestComp4;
if (test_comp4) if (test_comp4)
printf("TestComp4(%u, %u, %u, %u, %u, %u)",test_comp4.gg,test_comp4.bg,test_comp4.a,test_comp4.b,test_comp4.c,test_comp4.g);//write(*test_comp4); printf("TestComp4(%u, %u, %u, %u, %u, %u)",test_comp4.gg,test_comp4.bg,cast(uint)test_comp4.a,cast(uint)test_comp4.b,cast(uint)test_comp4.c,cast(uint)test_comp4.g);//write(*test_comp4);
printf("\n"); printf("\n");
//writeln(); //writeln();
////writeln((cast(uint*) pp)[0 .. 14], " ", pp); ////writeln((cast(uint*) pp)[0 .. 14], " ", pp);
} }
/*int a = 0;
if(!a)assert(0);*/
EntityManager.initialize(1); EntityManager.initialize(1);
gEM.setJobDispachFunc(&dispatch); gEM.setJobDispachFunc(&dispatch);
assert(gEM !is null); //assert(gEM !is null);
gEM.beginRegister(); gEM.beginRegister();
gEM.registerPass("fixed"); gEM.registerPass("fixed");