diff --git a/dlib/alloc.d b/dlib/alloc.d index 07bd62a..729f0ab 100644 --- a/dlib/alloc.d +++ b/dlib/alloc.d @@ -56,6 +56,8 @@ extern(C) } */ +@nogc: + version(WebAssembly) { diff --git a/dlib/dlibincludes.c b/dlib/dlibincludes.c index 55c2a15..8e6429e 100644 --- a/dlib/dlibincludes.c +++ b/dlib/dlibincludes.c @@ -18,22 +18,22 @@ #endif #if !defined(NO_STBI) && !defined(BUILD_WASM) -# include "external/stb/stb_image.h" -# include "external/stb/stb_image_write.h" +# include "../external/stb/stb_image.h" +# include "../external/stb/stb_image_write.h" #endif #ifdef BUILD_WASM -# include "external/tinyalloc/tinyalloc.c" +# include "../external/tinyalloc/tinyalloc.c" #endif #ifndef NO_STBI -# include "external/stb/stb_truetype.h" +# include "../external/stb/stb_truetype.h" #endif #ifdef DLIB_INCLUDE_VULKAN -# include "../VulkanRenderer/vulkan_includes.c" +# include "../../VulkanRenderer/vulkan_includes.c" #endif #ifndef BUILD_WASM -# include "external/cgltf/cgltf.h" +# include "../external/cgltf/cgltf.h" #endif diff --git a/dlib/externdecl.d b/dlib/externdecl.d index 94b494d..d804d3e 100644 --- a/dlib/externdecl.d +++ b/dlib/externdecl.d @@ -47,7 +47,7 @@ struct stbtt_fontinfo stbtt__buf fdselect; } -extern(C): +@nogc extern(C): // stb_truetype diff --git a/dlib/math.d b/dlib/math.d index 98bbec3..0b7d854 100644 --- a/dlib/math.d +++ b/dlib/math.d @@ -461,7 +461,7 @@ struct Vector(T, int N) return this; } - ref Vector opAssign(U)(U u) if(isStaticArray!(U) && U.length == N && isAssignable!(T, typeof(*U.init.ptr))) + ref Vector opAssign(U)(U u) if(isArray!(U) && U.length == N) { static foreach(i; 0 .. N) { @@ -470,9 +470,12 @@ struct Vector(T, int N) return this; } - ref Vector opAssign(U)(U u) if(is(U : Vector)) + ref Vector opAssign(U)(U u) if(isInstanceOf!(Vector, U)) { - v[] = u.v[]; + static foreach(i; 0 .. N) + { + v[i] = cast(T)u.v[i]; + } return this; } diff --git a/dlib/package.d b/dlib/package.d index 85fc176..5f27603 100644 --- a/dlib/package.d +++ b/dlib/package.d @@ -10,30 +10,7 @@ public import dlib.assets; public import dlib.externdecl; version(WebAssembly) -{ - version(unittest) - { - extern(C) void _start() - { - import wasm; - import std.format; - - char[512] buffer; - u32[5] arr = [3, 55, 123, 528, 3943]; - string str = Str(sformat(buffer, "Nigger %s pppp %s", "faggot", arr)); - - Console(str, true); - - static foreach(test_fn; __traits(getUnitTests, __traits(parent, _start))) - { - test_fn(); - } - - - Console("success", true); - } - } -} +{} else { public import dlibincludes; diff --git a/dlib/platform.d b/dlib/platform.d index 63297de..7aa2eb3 100644 --- a/dlib/platform.d +++ b/dlib/platform.d @@ -1972,10 +1972,10 @@ version(DLIB_TEST) unittest u8[] data_valid, data_test; - File f = File("platform.d", "rb"); + File f = File("dlib/platform.d", "rb"); data_valid = f.rawRead(new u8[f.size()]); - data_test = LoadFile(&arena, "platform.d"); + data_test = LoadFile(&arena, "dlib/platform.d"); assert(data_valid == data_test); } diff --git a/dlib/util.d b/dlib/util.d index d30fd40..e82f35c 100644 --- a/dlib/util.d +++ b/dlib/util.d @@ -7,12 +7,13 @@ import std.traits : isIntegral, hasMember, isArray; import core.stdc.string; +import std.format : sformat; +import std.stdio : writeln, writef, writefln; + //import std.traits; static if(NativeTarget) { - import std.format : sformat; - import std.stdio : writeln, writef, writefln; import dlib.platform; } @@ -77,6 +78,8 @@ CastArr(T, U)(U[] input_array) return output_array; } +// TODO: remove exceptions here + void Logf(string prefix = "INFO ", Args...)(string fmt, Args args) { @@ -92,6 +95,11 @@ Logf(string prefix = "INFO ", Args...)(string fmt, Args args) assert(false, "Incompatible format type"); } } + else + { + writef("[%s]: ", prefix); + writefln(fmt, args); + } } void @@ -109,6 +117,11 @@ Logif(Args...)(string fmt, Args args, string func = __FUNCTION__) assert(false, "Incompatible format type"); } } + else + { + writef("FN: [%s] ", func); + Logf(fmt, args); + } } void @@ -132,44 +145,46 @@ Debugf(Args...)(string fmt, Args args) string Scratchf(Args...)(string fmt, Args args) { - static if(NativeTarget) - { - assert(g_scratch.init); - char[] buf = ScratchAlloc!(char)(fmt.length < 16 ? 32 : 128); - return Str(sformat(buf, fmt, args)); - } - else static assert(false, NO_IMPL); + assert(g_scratch.init); + char[] buf = ScratchAlloc!(char)(fmt.length < 16 ? 32 : 128); + return Str(sformat(buf, fmt, args)); } void Log(string str) { - static if(NativeTarget) writeln(str); + writeln(str); } void Log(char* str) { - static if(NativeTarget) writeln(str); + writeln(str[0 .. cast(usize)(strlen(str))]); } -@nogc usize +@nogc pure @safe +{ + +usize KB(usize v) { return v * 1024; -}; +} -@nogc usize +usize MB(usize v) { return KB(v) * 1024; -}; +} -@nogc usize +usize GB(usize v) { return MB(v) * 1024; -}; +} + +} + u32 StrCharCount(T)(T str, u8 ch) if(is(T: string) || is(T: u8[]) || is(T: char[])) @@ -1319,10 +1334,10 @@ version(DLIB_TEST) unittest } { // Hash - u8[10] arr_1 = 5; + u8[10] arr_1 = 5; u64[10] arr_2 = 555; - u64 val_1 = 555555; - u32 val_2 = 33333; + u64 val_1 = 555555; + u32 val_2 = 33333; u64 v1 = Hash(arr_1); u64 v2 = Hash(arr_2); @@ -1339,11 +1354,11 @@ version(DLIB_TEST) unittest arr = CastArr!(u8)(char_arr); } - static if(NativeTarget) { ResetScratch(MB(4)); string str = Scratchf("%s testing %s", 55, "testing"); assert(str == "55 testing testing"); + Logf(str); } { diff --git a/test.sh b/test.sh index 460b512..a3e9303 100755 --- a/test.sh +++ b/test.sh @@ -5,18 +5,18 @@ name="Test_Runner" shared_src="dlib/package.d dlib/platform.d dlib/fonts.d dlib/aliases.d dlib/math.d dlib/util.d dlib/alloc.d dlib/assets.d dlib/externdecl.d" if [ "$1" == "wasm" ]; then - flags="-c -vgc -mtriple=wasm32-unknown-unknown-wasm -fvisibility=public -dllimport=all --unittest --Xcc=-DBUILD_WASM -Iwasm/runtime -L--no-entry -i=core -i=std -i=. -g --real-precision=double --of=build/dlibmain.wasm --d-version=inline_concat -verrors=90" + flags="-c -vgc -mtriple=wasm32-unknown-unknown-wasm -d-version=DLIB_TEST -fvisibility=public -dllimport=all --unittest --Xcc=-DBUILD_WASM -Iwasm/runtime -L--no-entry -i=core -i=std -i=. --real-precision=double --of=build/dlibmain.wasm --d-version=inline_concat -verrors=90" wasm_src="wasm/runtime/object.d" /bin/bash ./build.sh build wasm ldc2 $flags $shared_src $wasm_src - wasm-ld build/dlibmain.wasm build/dlibincludes.wasm --error-limit=0 --export-memory -obuild/dlib.wasm + wasm-ld build/dlibmain.wasm build/dlibincludes.wasm --export=RunTests --error-limit=0 --export-memory -obuild/dlib.wasm cp build/dlib.wasm wasm/dlib.wasm else - flags="-P-I/usr/include/freetype2 -L-lfreetype --main --unittest -g --of=$name" + flags="-P-I/usr/include/freetype2 -Idlib -L-lfreetype --main --unittest -g --of=$name" /bin/bash ./build.sh build diff --git a/wasm/dlib.wasm b/wasm/dlib.wasm index d89ea37..f85bcfd 100755 Binary files a/wasm/dlib.wasm and b/wasm/dlib.wasm differ diff --git a/wasm/dlibmain.wasm b/wasm/dlibmain.wasm deleted file mode 100644 index 31ee0b7..0000000 Binary files a/wasm/dlibmain.wasm and /dev/null differ diff --git a/wasm/runtime/object.d b/wasm/runtime/object.d index 60a456e..a6e613b 100644 --- a/wasm/runtime/object.d +++ b/wasm/runtime/object.d @@ -212,7 +212,7 @@ extern(C) void _d_arraybounds_index(string file, uint line, size_t index, size_t // } -extern(C) void _d_assert(string file, uint line) @trusted @nogc pure +extern(C) void _d_assert(string file, uint line) @trusted @nogc { Abort("Assertion failure"); } @@ -222,12 +222,12 @@ void _d_assertp(immutable(char)* file, uint line) Abort("Assertion failure"); } -extern(C) void _d_assert_msg(string msg, string file, uint line) @trusted @nogc pure +extern(C) void _d_assert_msg(string msg, string file, uint line) @trusted @nogc { Abort("Assertion failure with msg"); } -void __switch_error(string file, size_t line) @trusted @nogc pure +void __switch_error(string file, size_t line) @trusted @nogc { _d_assert_msg("final switch error", file, line); } diff --git a/wasm/runtime/std/stdio.d b/wasm/runtime/std/stdio.d index 36b7b43..a10b5ad 100644 --- a/wasm/runtime/std/stdio.d +++ b/wasm/runtime/std/stdio.d @@ -2,21 +2,49 @@ module std.stdio; import dlib.util; import std.traits; +import wasm; +import std.format; void writefln(alias fmt, A...)(A args) if(isSomeString!(typeof(fmt))) { - static foreach(i; 0 .. A.length) - { - { - static if() - } - } + .writefln(Str(fmt), args); } void writefln(Char, A...)(in Char[] fmt, A args) { - writefln(Str(fmt), args); + char[1024] buffer; + + string result = Str(sformat(buffer, fmt, args)); + + Console(result, true); } +void +writef(alias fmt, A...)(A args) if(isSomeString!(typeof(fmt))) +{ + .writef(Str(fmt), args); +} + +void +writef(Char, A...)(in Char[] fmt, A args) +{ + char[1024] buffer; + + string result = Str(sformat(buffer, fmt, args)); + + Console(result, false); +} + +void +write(T...)(T args) if(T.length == 1 && isSomeString!(T[0])) +{ + Console(Str(args[0]), false); +} + +void +writeln(T...)(T args) if(T.length == 1 && isSomeString!(T[0])) +{ + Console(Str(args[0]), true); +} diff --git a/wasm/runtime/wasm.d b/wasm/runtime/wasm.d index c53e15e..3620e1a 100644 --- a/wasm/runtime/wasm.d +++ b/wasm/runtime/wasm.d @@ -32,11 +32,14 @@ enum SprintfType : size_t CharArray, } -extern extern(C) @nogc pure @llvmAttr("wasm-import-module", "env"): +extern extern(C) @nogc @llvmAttr("wasm-import-module", "env"): @llvmAttr("wasm-import-name", "Console") void Console(string str, bool write_line); +@llvmAttr("wasm-import-name", "Console2") void +Console2(size_t length, const(void)* ptr, bool write_line); + @llvmAttr("wasm-import-name", "Abort") void Abort(string message); @@ -49,3 +52,28 @@ SprintfLoadArray(size_t length, const(void)* ptr, SprintfType type); @llvmAttr("wasm-import-name", "SprintfEnd") size_t SprintfEnd(char[] buffer, string format); +export void +_start() +{ + import dlib.alloc; + import dlib.util; + + ResetScratch(MB(2)); +} + +version(DLIB_TEST) +{ + +export void +RunTests() +{ + Console("azZ", true); + static foreach(test_fn; __traits(getUnitTests, __traits(parent, _start))) + { + Console("Running test", true); + test_fn(); + } + Console("Tests succeeded", true); +} + +} diff --git a/wasm/wasm.js b/wasm/wasm.js index 5f4f923..0bbb0db 100644 --- a/wasm/wasm.js +++ b/wasm/wasm.js @@ -253,6 +253,19 @@ class WasmManager Console(string_length, string_ptr, write_line) { manager.stdout_string += manager.LoadString(string_length, string_ptr); + const arr = manager.LoadArray(Uint8Array, string_length, string_ptr); + + if(write_line || manager.stdout_string.includes("\n")) + { + console.log(manager.stdout_string); + manager.stdout_string = ""; + } + }, + Console2(string_length, string_ptr, write_line) + { + manager.stdout_string += manager.LoadString(string_length, string_ptr); + const arr = manager.LoadArray(Uint8Array, string_length, string_ptr); + if(write_line || manager.stdout_string.includes("\n")) { console.log(manager.stdout_string); @@ -281,5 +294,7 @@ async function StartWasm(path) exports?._start(); + exports?.RunTests(); + return; }