Skip to content

Commit

Permalink
Merge pull request #599 from JerwuQu/native-tracef
Browse files Browse the repository at this point in the history
Add tracef to native runtime
  • Loading branch information
aduros authored Apr 20, 2024
2 parents cb6250e + 82e0bc1 commit 304127c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
49 changes: 44 additions & 5 deletions runtimes/native/src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,50 @@ void w4_runtimeTraceUtf16 (const uint16_t* str, int byteLength) {
}

void w4_runtimeTracef (const uint8_t* str, const void* stack) {
puts(str);

// This seems to crash on Linux release builds
// vprintf(str, (void*)&stack);
// putchar('\n');
const uint8_t* argPtr = stack;
uint32_t strPtr;
for (; *str != 0; ++str) {
if (*str == '%') {
const uint8_t sym = *(++str);
switch (sym) {
case 0:
return; // Interrupted
case '%':
putc('%', stdout);
break;
case 'c':
putc(*(char*)argPtr, stdout);
argPtr += 4;
break;
case 'd':
printf("%d", *(int32_t*)argPtr);
argPtr += 4;
break;
case 'x':
printf("%x", *(uint32_t*)argPtr);
argPtr += 4;
break;
case 's':
strPtr = *(uint32_t*)argPtr;
argPtr += 4;
if (strPtr > 0 && strPtr < sizeof(*memory)) {
printf("%.*s", (int)(sizeof(*memory) - strPtr), (char*)memory + strPtr);
} else {
printf("<invalid memory>");
}
break;
case 'f':
printf("%lg", *(double*)argPtr);
argPtr += 8;
break;
default:
printf("%%%c", sym);
}
} else {
putc(*str, stdout);
}
}
putc('\n', stdout);
}

void w4_runtimeUpdate () {
Expand Down
3 changes: 3 additions & 0 deletions runtimes/web/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ export class Runtime {
output += this.data.getFloat64(argPtr, true);
argPtr += 8;
break;
default: // unknown
output += "%" + String.fromCharCode(ch);
break;
}
} else {
output += String.fromCharCode(ch);
Expand Down

0 comments on commit 304127c

Please sign in to comment.