Web assembly #6

Merged
Mergul merged 38 commits from WebAssembly into master 2020-04-14 17:44:27 +02:00
9 changed files with 145 additions and 17 deletions
Showing only changes of commit 015783bf5c - Show all commits

4
.gitignore vendored
View file

@ -4,4 +4,6 @@
!tests/** !tests/**
!README.md !README.md
!./dub.json !./dub.json
!.gitignore !.gitignore
!meson.build
!meson_options.txt

View file

@ -7,9 +7,6 @@
"copyright": "Copyright © 2018-2019, Michał Masiukiewicz, Dawid Masiukiewicz", "copyright": "Copyright © 2018-2019, Michał Masiukiewicz, Dawid Masiukiewicz",
"license": "BSD", "license": "BSD",
"sourcePaths" : ["source\/"], "sourcePaths" : ["source\/"],
"dflags-posix-ldc": [
"-defaultlib=phobos2-ldc,druntime-ldc"
],
"dflagss": [ "dflagss": [
"-betterC" "-betterC"
], ],

44
meson.build Normal file
View file

@ -0,0 +1,44 @@
project('DECS', 'd')
src = [
'source/ecs/attributes.d',
'source/ecs/block_allocator.d',
'source/ecs/core.d',
'source/ecs/entity.d',
'source/ecs/events.d',
'source/ecs/hash_map.d',
'source/ecs/id_manager.d',
'source/ecs/manager.d',
'source/ecs/simple_vector.d',
'source/ecs/simple_vector.d',
'source/ecs/std.d',
'source/ecs/system.d',
'source/ecs/traits.d',
'source/ecs/vector.d',
'source/ecs/package.d'
]
tests_src = [
'tests/tests.d'
]
betterC_opt = get_option('betterC')
comp = meson.get_compiler('d')
comp_id = comp.get_id()
args = []
link_args = []
if betterC_opt
args += '-betterC'
link_args += '-betterC'
endif
inc = include_directories('source/')
tests_inc = include_directories('source/')
executable('ecs', [tests_src, src], include_directories : [tests_inc, inc], d_args: args, link_args: link_args)

1
meson_options.txt Normal file
View file

@ -0,0 +1 @@
option('betterC', type: 'boolean', value: false)

View file

@ -8,8 +8,8 @@ import std.conv : to;
import std.traits; import std.traits;
import core.atomic; import core.atomic;
import core.stdc.stdlib : qsort; //import core.stdc.stdlib : qsort;
import core.stdc.string; //import core.stdc.string;
import ecs.system;//not ordered as forward reference bug workaround import ecs.system;//not ordered as forward reference bug workaround
import ecs.block_allocator; import ecs.block_allocator;

View file

@ -2,7 +2,7 @@ module ecs.simple_vector;
import ecs.std; import ecs.std;
import core.stdc.string; //import core.stdc.string;
struct SimpleVector struct SimpleVector
{ {

View file

@ -1,10 +1,30 @@
module ecs.std; module ecs.std;
import core.stdc.stdlib : malloc, free, realloc; //import core.stdc.stdlib : malloc, free, realloc;
import core.stdc.string : memcpy; //import core.stdc.string : memcpy;
import std.traits; import std.traits;
version(WebAssembly)
{
extern(C) void free(void*) @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* 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 qsort(void* base, size_t num, size_t size,
int function(const void*,const void*) compar) @nogc nothrow @system;
}
else
{
public import core.stdc.stdlib : malloc, free, realloc;
public import core.stdc.string : memcpy, memset;
public import core.stdc.stdlib : qsort;
}
version (Windows) version (Windows)
{ {
import core.sys.windows.windows; import core.sys.windows.windows;
@ -43,6 +63,19 @@ version(D_betterC)
return ret; return ret;
} }
} }
else version(WebAssembly)
{
private const uint max_alloca = 10000;
private char[max_alloca] alloca_array;
private uint alloca_pos = 0;
void* alloca(size_t length) @nogc nothrow
{
if(alloca_pos + length > max_alloca)alloca_pos = 0;
void* ret = &alloca_array[alloca_pos];
alloca_pos += length;
return ret;
}
}
else public import core.stdc.stdlib : alloca; else public import core.stdc.stdlib : alloca;
static struct Mallocator static struct Mallocator
@ -109,6 +142,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 static assert(0, "Unimplemented platform!"); else static assert(0, "Unimplemented platform!");
return ret; return ret;
} }
@ -124,6 +158,7 @@ static struct Mallocator
static if(__traits(hasMember, T, "__dtor"))object.__dtor(); static if(__traits(hasMember, T, "__dtor"))object.__dtor();
version(Posix)free(cast(void*)object); version(Posix)free(cast(void*)object);
else version(Windows)_aligned_free(cast(void*)object); else version(Windows)_aligned_free(cast(void*)object);
else version(WebAssembly)free(cast(void*)object);
else static assert(0, "Unimplemented platform!"); else static assert(0, "Unimplemented platform!");
} }
@ -197,4 +232,41 @@ struct Mutex
private pthread_mutex_t m_handle; private pthread_mutex_t m_handle;
} }
else version(WebAssembly)
{
void initialize() nothrow @nogc
{
/*pthread_mutexattr_t attr = void;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(cast(pthread_mutex_t*) &m_handle, &attr);
pthread_mutexattr_destroy(&attr);*/
}
void destroy() nothrow @nogc
{
//pthread_mutex_destroy(&m_handle);
}
void lock() nothrow @nogc
{
//pthread_mutex_lock(&m_handle);
}
void unlock() nothrow @nogc
{
//pthread_mutex_unlock(&m_handle);
}
int tryLock() nothrow @nogc
{
return 0;//return pthread_mutex_trylock(&m_handle) == 0;
}
private int m_handle;
}
else static assert(0, "unsupported platform!");
} }

View file

@ -1,8 +1,9 @@
module ecs.vector; module ecs.vector;
import core.bitop; import core.bitop;
import core.stdc.stdlib : free, malloc; //import core.stdc.stdlib : free, malloc;
import core.stdc.string : memcpy, memset; import ecs.std;
//import core.stdc.string : memcpy, memset;
//import std.algorithm : swap; //import std.algorithm : swap;
import std.conv : emplace; import std.conv : emplace;
import std.traits : hasMember, isCopyable, TemplateOf, Unqual; import std.traits : hasMember, isCopyable, TemplateOf, Unqual;

View file

@ -10,11 +10,24 @@ import ecs.system;
import ecs.attributes; import ecs.attributes;
import ecs.core; import ecs.core;
import core.time;
import std.stdio;
version(Windows)
version(WebAssembly)
{ {
extern(C) void printf(const char *format, ...) @nogc nothrow @system;
struct Time
{
static long getUSecTime()
{
return 0;
}
}
extern(C) void _start() {}
}
else version(Windows)
{
import core.stdc.stdio : printf;
import core.sys.windows.windows; import core.sys.windows.windows;
struct Time struct Time
{ {
@ -29,6 +42,7 @@ version(Windows)
} }
else version(Posix) else version(Posix)
{ {
import core.stdc.stdio : printf;
import core.sys.posix.time; import core.sys.posix.time;
struct Time struct Time
{ {
@ -287,7 +301,6 @@ struct TestSystem
{ {
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);
import std.stdio;
test.a += 1000; test.a += 1000;
test.b += 2000; test.b += 2000;
@ -440,7 +453,6 @@ struct TestSystem2
void onEnable() void onEnable()
{ {
import std.stdio;
//writeln("TestSystem2 enabled"); //writeln("TestSystem2 enabled");
printf("TestSystem2 enabled\n"); printf("TestSystem2 enabled\n");
@ -448,7 +460,6 @@ struct TestSystem2
void onDisable() void onDisable()
{ {
import std.stdio;
//writeln("TestSystem2 disabled"); //writeln("TestSystem2 disabled");
printf("TestSystem2 disabled\n"); printf("TestSystem2 disabled\n");