Refactor js exports
This commit is contained in:
parent
5f0480a974
commit
8b3da34d8c
|
|
@ -226,9 +226,9 @@ For C/C++ files:
|
|||
|
||||
```c
|
||||
// Example: src/games/life/life.c
|
||||
EXPORT(init) void init();
|
||||
EXPORT(step) void step();
|
||||
EXPORT(getCell) int getCell(int x, int y);
|
||||
void EXPORT(init, void);
|
||||
void EXPORT(frme, void);
|
||||
int EXPORT(getCell, int x, int y);
|
||||
|
||||
// Must export memory
|
||||
```
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <js.h>
|
||||
|
||||
#define GRAPHICS_INIT image_data_t* EXPORT(canvas_init, void)
|
||||
#define GRAPHICS_FRAME void EXPORT(canvas_frame, void)
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
||||
#define EXPORT(name) __attribute__((export_name(#name)))
|
||||
#define EXPORT_NAME(name) __attribute__((export_name(#name)))
|
||||
#define EXPORT(name, ...) EXPORT_NAME(name) name(__VA_ARGS__)
|
||||
|
|
|
|||
|
|
@ -30,21 +30,21 @@ export function createCanvas(width: number, height: number) {
|
|||
|
||||
interface WasmModule {
|
||||
data: DataView;
|
||||
init?: (width: number, height: number) => number;
|
||||
frame?: () => void;
|
||||
canvas_init?: (width: number, height: number) => number;
|
||||
canvas_frame?: () => void;
|
||||
}
|
||||
|
||||
export function createWasmCanvas(module: WasmModule, width: number = 0, height: number = 0) {
|
||||
const pointer = module.init?.(width, height);
|
||||
const pointer = module.canvas_init?.(width, height);
|
||||
|
||||
if (pointer) {
|
||||
const imageData = loadImageData(module.data, pointer);
|
||||
const canvas = createCanvas(imageData.width, imageData.height);
|
||||
const context = canvas.getContext('2d');
|
||||
|
||||
if (context) {
|
||||
if (context && typeof module.canvas_frame === 'function') {
|
||||
const frame = () => {
|
||||
module.frame?.();
|
||||
module.canvas_frame?.();
|
||||
context.putImageData(imageData, 0, 0);
|
||||
requestAnimationFrame(frame);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ static rigid_body_collision_callback_t rigid_body_collision_callback = NULL;
|
|||
|
||||
////////// Functions
|
||||
|
||||
EXPORT(rigid_body_get) rigid_body* _rigid_body_get(rigid_body_index idx) {
|
||||
EXPORT_NAME(rigid_body_get) rigid_body* _rigid_body_get(rigid_body_index idx) {
|
||||
return rigid_body_get(idx);
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ size_t rigid_body_new(float x, float y, float vx, float vy, float mass) {
|
|||
return idx;
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_new_circle) rigid_body_index rigid_body_new_circle(float x, float y, float vx, float vy, float mass, float radius) {
|
||||
rigid_body_index EXPORT(rigid_body_new_circle, float x, float y, float vx, float vy, float mass, float radius) {
|
||||
rigid_body_index idx = rigid_body_new(x, y, vx, vy, mass);
|
||||
rigid_body* rb = rigid_body_get(idx);
|
||||
|
||||
|
|
@ -84,7 +84,7 @@ EXPORT(rigid_body_new_circle) rigid_body_index rigid_body_new_circle(float x, fl
|
|||
return idx;
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_new_plane) rigid_body_index rigid_body_new_plane(float x, float y, float nx, float ny) {
|
||||
rigid_body_index EXPORT(rigid_body_new_plane, float x, float y, float nx, float ny) {
|
||||
rigid_body_index idx = rigid_body_new(x, y, 0, 0, INFINITY);
|
||||
rigid_body* rb = rigid_body_get(idx);
|
||||
|
||||
|
|
@ -94,11 +94,11 @@ EXPORT(rigid_body_new_plane) rigid_body_index rigid_body_new_plane(float x, floa
|
|||
return idx;
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_free) void rigid_body_free(rigid_body_index idx) {
|
||||
void EXPORT(rigid_body_free, rigid_body_index idx) {
|
||||
memset(rigid_body_get(idx), 0, sizeof(rigid_body));
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_update) void rigid_body_update(rigid_body_index idx, float dt) {
|
||||
void EXPORT(rigid_body_update, rigid_body_index idx, float dt) {
|
||||
rigid_body_resolve_collision(idx);
|
||||
rigid_body* rb = rigid_body_get(idx);
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ EXPORT(rigid_body_update) void rigid_body_update(rigid_body_index idx, float dt)
|
|||
rb->force.y = 0;
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_update_all) void rigid_body_update_all(float dt) {
|
||||
void EXPORT(rigid_body_update_all, float dt) {
|
||||
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
||||
rigid_body* current = rigid_body_get(idx);
|
||||
if (current->type == TYPE_EMPTY) {
|
||||
|
|
@ -124,13 +124,13 @@ EXPORT(rigid_body_update_all) void rigid_body_update_all(float dt) {
|
|||
}
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_add_force) void rigid_body_add_force(rigid_body_index idx, float fx, float fy) {
|
||||
void EXPORT(rigid_body_add_force, rigid_body_index idx, float fx, float fy) {
|
||||
rigid_body* rb = rigid_body_get(idx);
|
||||
rb->force.x += fx;
|
||||
rb->force.y += fy;
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_add_global_force) void rigid_body_add_global_force(float fx, float fy) {
|
||||
void EXPORT(rigid_body_add_global_force, float fx, float fy) {
|
||||
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
||||
rigid_body* current = rigid_body_get(idx);
|
||||
if (current->type == TYPE_EMPTY) {
|
||||
|
|
@ -140,7 +140,7 @@ EXPORT(rigid_body_add_global_force) void rigid_body_add_global_force(float fx, f
|
|||
}
|
||||
}
|
||||
|
||||
EXPORT(rigid_body_set_collision_callback) void rigid_body_set_collision_callback(rigid_body_collision_callback_t callback) {
|
||||
void EXPORT(rigid_body_set_collision_callback, rigid_body_collision_callback_t callback) {
|
||||
rigid_body_collision_callback = callback;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include <js.h>
|
||||
#include <graphics.h>
|
||||
#include <js.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
|
@ -12,7 +12,7 @@ static image_data_t image_data;
|
|||
|
||||
static int count_neighbours(int x, int y);
|
||||
|
||||
EXPORT(frame) void frame(void) {
|
||||
GRAPHICS_FRAME {
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
int count = count_neighbours(x, y);
|
||||
|
|
@ -59,7 +59,7 @@ static int count_neighbours(int x, int y) {
|
|||
return count;
|
||||
}
|
||||
|
||||
EXPORT(init) image_data_t* init(void) {
|
||||
GRAPHICS_INIT {
|
||||
field = malloc(WIDTH * HEIGHT);
|
||||
next_field = malloc(WIDTH * HEIGHT);
|
||||
image_data = image_create(WIDTH, HEIGHT);
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ constexpr bit bit::nor(const bit& a, const bit& b) {
|
|||
return result;
|
||||
}
|
||||
|
||||
uint64_t awoo() {
|
||||
uint64_t EXPORT(awoo) {
|
||||
bit a{1, "a"};
|
||||
bit b{1, "b"};
|
||||
|
||||
|
|
@ -88,7 +88,7 @@ uint64_t awoo() {
|
|||
return sum;
|
||||
}
|
||||
|
||||
EXPORT(play) void play() {
|
||||
void EXPORT(play) {
|
||||
auto awoo = std::format("{2} {1}{0}!\n", 23, "C++", "Hello");
|
||||
std::puts(awoo.c_str());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue