Add time to wasm
This commit is contained in:
parent
a35c3197e9
commit
d5eaac4099
|
|
@ -145,13 +145,20 @@ async function instantiate(url: string) {
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
},
|
},
|
||||||
|
clock_time_get: (_id: number, _precision: bigint, timePtr: number) => {
|
||||||
|
const now = Date.now();
|
||||||
|
const nanosecondNow = BigInt(now) * 1_000_000n;
|
||||||
|
const data = new DataView(memory.buffer);
|
||||||
|
data.setBigInt64(timePtr, nanosecondNow, true);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
}, {
|
}, {
|
||||||
get(target, p) {
|
get(target, p) {
|
||||||
if (p in target) {
|
if (p in target) {
|
||||||
console.debug(`[${String(p)}] exists`);
|
console.debug(`[${String(p)}] exists`);
|
||||||
return target[p as keyof typeof target];
|
return target[p as keyof typeof target];
|
||||||
}
|
}
|
||||||
return (...args: any[]) => { console.debug(`[${String(p)}]`, args); return 0; }
|
return (...args: any[]) => { console.warn(`[${String(p)}]`, args); return 0; }
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
#include <js.h>
|
#include <js.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define WIDTH 512
|
#define WIDTH 512
|
||||||
#define HEIGHT 512
|
#define HEIGHT 512
|
||||||
|
|
@ -18,13 +19,15 @@ GRAPHICS_FRAME {
|
||||||
int count = count_neighbours(x, y);
|
int count = count_neighbours(x, y);
|
||||||
uint8_t current_cell = field[x + y * WIDTH];
|
uint8_t current_cell = field[x + y * WIDTH];
|
||||||
uint8_t next_cell = current_cell;
|
uint8_t next_cell = current_cell;
|
||||||
if (current_cell && count < 2) {
|
// if (current_cell && count < 2) {
|
||||||
next_cell = 0;
|
// next_cell = 0;
|
||||||
} else if (current_cell && count > 3) {
|
// } else if (current_cell && count > 3) {
|
||||||
next_cell = 0;
|
// next_cell = 0;
|
||||||
} else if (!current_cell && count == 3) {
|
// } else if (!current_cell && count == 3) {
|
||||||
next_cell = 1;
|
// next_cell = 1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
next_cell = (count + current_cell) >= 5;
|
||||||
|
|
||||||
next_field[x + y * WIDTH] = next_cell;
|
next_field[x + y * WIDTH] = next_cell;
|
||||||
}
|
}
|
||||||
|
|
@ -60,6 +63,7 @@ static int count_neighbours(int x, int y) {
|
||||||
}
|
}
|
||||||
|
|
||||||
GRAPHICS_INIT {
|
GRAPHICS_INIT {
|
||||||
|
srand((uint32_t)time(NULL));
|
||||||
field = malloc(WIDTH * HEIGHT);
|
field = malloc(WIDTH * HEIGHT);
|
||||||
next_field = malloc(WIDTH * HEIGHT);
|
next_field = malloc(WIDTH * HEIGHT);
|
||||||
image_data = image_create(WIDTH, HEIGHT);
|
image_data = image_create(WIDTH, HEIGHT);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <js.h>
|
#include <js.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
class bit {
|
class bit {
|
||||||
|
|
@ -77,7 +77,7 @@ constexpr bit bit::nor(const bit& a, const bit& b) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t EXPORT(awoo) {
|
JS_EXPORT uint64_t awoo() {
|
||||||
bit a{1, "a"};
|
bit a{1, "a"};
|
||||||
bit b{1, "b"};
|
bit b{1, "b"};
|
||||||
|
|
||||||
|
|
@ -88,7 +88,12 @@ uint64_t EXPORT(awoo) {
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(play) {
|
JS_EXPORT void play() {
|
||||||
auto awoo = std::format("{2} {1}{0}!\n", 23, "C++", "Hello");
|
auto awoo = std::format("{2} {1}{0}!\n", 23, "C++", "Hello");
|
||||||
std::puts(awoo.c_str());
|
std::puts(awoo.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
awoo();
|
||||||
|
play();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue