From 85d54f5d0bfa7b3fe4a29a3bceded909c3afbaf8 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Mon, 23 Oct 2023 12:40:06 +0200 Subject: [PATCH] Some fixes --- wasmrt.nim | 67 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/wasmrt.nim b/wasmrt.nim index 42b90a9..eb046b4 100644 --- a/wasmrt.nim +++ b/wasmrt.nim @@ -419,6 +419,24 @@ N_LIB_PRIVATE void* memchr(register const void* src_void, int c, size_t length) return NULL; } +N_LIB_PRIVATE int memcmp(const void* a, const void* b, size_t s) { + char* aa = (char*)a; + char* bb = (char*)b; + if (aa == bb) return 0; + + while(s) { + --s; + int ia = *aa; + int ib = *bb; + int r = ia - ib; // TODO: The result might be inverted. Verify against C standard. + if (r) return r; + *aa = *bb; + ++aa; + ++bb; + } + return 0; +} + N_LIB_PRIVATE void* memmem(const void *l, size_t l_len, const void *s, size_t s_len) { register char *cur, *last; const char *cl = (const char *)l; @@ -446,24 +464,6 @@ N_LIB_PRIVATE void* memmem(const void *l, size_t l_len, const void *s, size_t s_ return NULL; } -N_LIB_PRIVATE int memcmp(const void* a, const void* b, size_t s) { - char* aa = (char*)a; - char* bb = (char*)b; - if (aa == bb) return 0; - - while(s) { - --s; - int ia = *aa; - int ib = *bb; - int r = ia - ib; // TODO: The result might be inverted. Verify against C standard. - if (r) return r; - *aa = *bb; - ++aa; - ++bb; - } - return 0; -} - N_LIB_PRIVATE void* memset(void* a, int b, size_t s) { char* aa = (char*)a; while(s) { @@ -508,6 +508,14 @@ N_LIB_PRIVATE char* strstr(char *haystack, const char *needle) { return NULL; } +N_LIB_PRIVATE double trunc(double x) { + if (x >= 0.0) { + return (double)((int)x); + } else { + return -((double)((int)-x)); + } +} + N_LIB_PRIVATE double fmod(double x, double y) { return x - trunc(x / y) * y; } @@ -518,6 +526,24 @@ N_LIB_PRIVATE float fmodf(float x, float y) { """].} +proc consoleWarn(a: cstring) {.importwasmf: "console.warn".} + +proc logException(e: ref Exception) = + consoleWarn("Exception in dynCall") + consoleWarn(e.msg) + when compileOption("stackTrace"): + consoleWarn($e.getStackTrace) + +template dyncallWrap(a: untyped) = + when defined(release): + a + else: + try: + a + except Exception as e: + logException(e) + raise + macro defDyncall(sig: static[string]): untyped = let callbackIdent = ident"callback" let callbackParams = newTree(nnkFormalParams) @@ -544,7 +570,8 @@ macro defDyncall(sig: static[string]): untyped = callbackTy.addPragma(ident"cdecl") params.insert(newIdentDefs(callbackIdent, callbackTy), 1) - result = newProc(ident("dyncall"), params, callbackCall) + let wrappedCall = newCall(bindSym"dyncallWrap", callbackCall) + result = newProc(ident("dyncall"), params, wrappedCall) result.addPragma(newColonExpr(ident"exportc", newLit("_d" & sig))) result.addPragma(ident"dynlib") # echo repr result @@ -563,8 +590,6 @@ proc isNodejsAux(): bool {.importwasmp: "typeof process!='undefined'".} proc nodejsWriteToStream(s: int, b: pointer, l: int) {.importwasmraw:"process[$0?'stderr':'stdout'].write(_nims($1,$2))".} -proc consoleWarn(a: cstring) {.importwasmf: "console.warn".} - proc consoleAppend(b: pointer, l: int) {.importwasmraw: "_nimc += _nims($0,$1)".} proc consoleFlush(s: int) {.importwasmraw: "console[$0?'error':'log'](_nimc); _nimc = ''".}