dlib/wasm/runtime/wasm.d

107 lines
1.7 KiB
D

module wasm;
import ldc.attributes;
import dlib.util;
import std.format;
enum SprintfType : size_t
{
None,
U8,
I8,
U16,
I16,
U32,
I32,
U64,
I64,
SizeT,
F32,
F64,
Bool,
String,
U8Array,
I8Array,
U16Array,
I16Array,
U32Array,
I32Array,
U64Array,
I64Array,
F32Array,
F64Array,
Char,
CharArray,
Pointer,
}
extern extern(C) @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) @nogc;
@llvmAttr("wasm-import-name", "SprintfLoadValue") void
SprintfLoadValue(const(void)* ptr, SprintfType type) @nogc;
@llvmAttr("wasm-import-name", "SprintfLoadArray") void
SprintfLoadArray(size_t length, const(void)* ptr, SprintfType type) @nogc;
@llvmAttr("wasm-import-name", "SprintfEnd") size_t
SprintfEnd(char[] buffer, string format) @nogc;
@llvmAttr("wasm-import-name", "pow") double
pow(double base, double exponent) @nogc;
@llvmAttr("wasm-import-name", "cos") double
cos(double x) @nogc;
@llvmAttr("wasm-import-name", "acos") double
acos(double x) @nogc;
void
Abortf(Args...)(string fmt, Args args) @nogc
{
char[1024] buffer;
string abort_message = Str(sformat(buffer, fmt, args));
Abort(abort_message);
}
export void
_start()
{
import dlib.alloc;
import dlib.util;
import std.format;
MallocInit(256, 16, (void*).sizeof*2);
ResetScratch(MB(2));
char[100] buffer;
uint[2] arr = [1, 2];
sformat(buffer, "%s", arr);
}
version(DLIB_TEST) export void
RunTests()
{
import dlib;
DLibTestMath();
DLibTestUtil();
DLibTestAlloc();
Console("Tests succeeded", true);
}