From 015783bf5cfbc6601e0b6a63cdb40d5f3d7cb411 Mon Sep 17 00:00:00 2001 From: Mergul Date: Sat, 2 Nov 2019 18:51:03 +0100 Subject: [PATCH] -remove '-defaultlib' from dub.json -start working with WebAssembly -modified .gitignore -added meson build file (WIP) --- .gitignore | 4 +- dub.json | 3 -- meson.build | 44 ++++++++++++++++++++++ meson_options.txt | 1 + source/ecs/manager.d | 4 +- source/ecs/simple_vector.d | 2 +- source/ecs/std.d | 76 +++++++++++++++++++++++++++++++++++++- source/ecs/vector.d | 5 ++- tests/tests.d | 23 +++++++++--- 9 files changed, 145 insertions(+), 17 deletions(-) create mode 100644 meson.build create mode 100644 meson_options.txt diff --git a/.gitignore b/.gitignore index 280be93..91092a6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ !tests/** !README.md !./dub.json -!.gitignore \ No newline at end of file +!.gitignore +!meson.build +!meson_options.txt \ No newline at end of file diff --git a/dub.json b/dub.json index 89db254..e4d9704 100755 --- a/dub.json +++ b/dub.json @@ -7,9 +7,6 @@ "copyright": "Copyright © 2018-2019, Michał Masiukiewicz, Dawid Masiukiewicz", "license": "BSD", "sourcePaths" : ["source\/"], - "dflags-posix-ldc": [ - "-defaultlib=phobos2-ldc,druntime-ldc" - ], "dflagss": [ "-betterC" ], diff --git a/meson.build b/meson.build new file mode 100644 index 0000000..27c4a79 --- /dev/null +++ b/meson.build @@ -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) + + diff --git a/meson_options.txt b/meson_options.txt new file mode 100644 index 0000000..fdf13a4 --- /dev/null +++ b/meson_options.txt @@ -0,0 +1 @@ +option('betterC', type: 'boolean', value: false) \ No newline at end of file diff --git a/source/ecs/manager.d b/source/ecs/manager.d index 3708969..8d461fd 100644 --- a/source/ecs/manager.d +++ b/source/ecs/manager.d @@ -8,8 +8,8 @@ import std.conv : to; import std.traits; import core.atomic; -import core.stdc.stdlib : qsort; -import core.stdc.string; +//import core.stdc.stdlib : qsort; +//import core.stdc.string; import ecs.system;//not ordered as forward reference bug workaround import ecs.block_allocator; diff --git a/source/ecs/simple_vector.d b/source/ecs/simple_vector.d index 6310d69..175c015 100644 --- a/source/ecs/simple_vector.d +++ b/source/ecs/simple_vector.d @@ -2,7 +2,7 @@ module ecs.simple_vector; import ecs.std; -import core.stdc.string; +//import core.stdc.string; struct SimpleVector { diff --git a/source/ecs/std.d b/source/ecs/std.d index 249ec41..0e80660 100644 --- a/source/ecs/std.d +++ b/source/ecs/std.d @@ -1,10 +1,30 @@ module ecs.std; -import core.stdc.stdlib : malloc, free, realloc; -import core.stdc.string : memcpy; +//import core.stdc.stdlib : malloc, free, realloc; +//import core.stdc.string : memcpy; 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) { import core.sys.windows.windows; @@ -43,6 +63,19 @@ version(D_betterC) 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; static struct Mallocator @@ -109,6 +142,7 @@ static struct Mallocator void* ret; version(Posix)posix_memalign(&ret, alignment, length);//ret = aligned_alloc(alignment, length); else version(Windows)ret = _aligned_malloc(length, alignment); + else version(WebAssembly)ret = malloc(length); else static assert(0, "Unimplemented platform!"); return ret; } @@ -124,6 +158,7 @@ static struct Mallocator static if(__traits(hasMember, T, "__dtor"))object.__dtor(); version(Posix)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!"); } @@ -197,4 +232,41 @@ struct Mutex 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!"); } \ No newline at end of file diff --git a/source/ecs/vector.d b/source/ecs/vector.d index 7c029d0..81474f0 100644 --- a/source/ecs/vector.d +++ b/source/ecs/vector.d @@ -1,8 +1,9 @@ module ecs.vector; import core.bitop; -import core.stdc.stdlib : free, malloc; -import core.stdc.string : memcpy, memset; +//import core.stdc.stdlib : free, malloc; +import ecs.std; +//import core.stdc.string : memcpy, memset; //import std.algorithm : swap; import std.conv : emplace; import std.traits : hasMember, isCopyable, TemplateOf, Unqual; diff --git a/tests/tests.d b/tests/tests.d index f966005..57cb991 100644 --- a/tests/tests.d +++ b/tests/tests.d @@ -10,11 +10,24 @@ import ecs.system; import ecs.attributes; 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; struct Time { @@ -29,6 +42,7 @@ version(Windows) } else version(Posix) { + import core.stdc.stdio : printf; import core.sys.posix.time; struct Time { @@ -287,7 +301,6 @@ struct TestSystem { assert(cast(size_t)&test % TestComp.alignof == 0); assert(cast(size_t)&test2 % TestComp2.alignof == 0); - import std.stdio; test.a += 1000; test.b += 2000; @@ -440,7 +453,6 @@ struct TestSystem2 void onEnable() { - import std.stdio; //writeln("TestSystem2 enabled"); printf("TestSystem2 enabled\n"); @@ -448,7 +460,6 @@ struct TestSystem2 void onDisable() { - import std.stdio; //writeln("TestSystem2 disabled"); printf("TestSystem2 disabled\n");