Refactor wasm export
This commit is contained in:
parent
4b2a16c28f
commit
a35c3197e9
|
|
@ -3,8 +3,8 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <js.h>
|
#include <js.h>
|
||||||
|
|
||||||
#define GRAPHICS_INIT image_data_t* EXPORT(canvas_init, void)
|
#define GRAPHICS_INIT JS_EXPORT image_data_t* canvas_init(void)
|
||||||
#define GRAPHICS_FRAME void EXPORT(canvas_frame, void)
|
#define GRAPHICS_FRAME JS_EXPORT void canvas_frame(void)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
#define JS_IMPORT(name) __attribute__((import_module("env"), import_name(#name)))
|
||||||
#define EXPORT_NAME(name) __attribute__((export_name(#name)))
|
#define JS_EXPORT_AS(name) __attribute__((export_name(#name)))
|
||||||
#define EXPORT(name, ...) EXPORT_NAME(name) name(__VA_ARGS__)
|
#define JS_EXPORT __attribute__((visibility("default")))
|
||||||
|
|
|
||||||
|
|
@ -209,6 +209,7 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
|
||||||
'-Werror',
|
'-Werror',
|
||||||
'-Wshadow',
|
'-Wshadow',
|
||||||
'-Wconversion',
|
'-Wconversion',
|
||||||
|
'-fvisibility=hidden',
|
||||||
...features,
|
...features,
|
||||||
'-I', include,
|
'-I', include,
|
||||||
];
|
];
|
||||||
|
|
@ -220,10 +221,12 @@ const wasmPlugin = ({ production }: WasmLoaderConfig = {}): BunPlugin => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const linkFlags = [
|
const linkFlags = [
|
||||||
|
'--strip-all',
|
||||||
'--lto-O3',
|
'--lto-O3',
|
||||||
'--no-entry',
|
'--no-entry',
|
||||||
'--import-memory',
|
'--import-memory',
|
||||||
'--import-table',
|
'--import-table',
|
||||||
|
'--export-dynamic',
|
||||||
].map(f => `-Wl,${f}`);
|
].map(f => `-Wl,${f}`);
|
||||||
|
|
||||||
const linkResult = await $`${cc.cc} ${flags} -std=gnu23 ${linkFlags} -lstdc++ -nostartfiles -o ${wasmPath} ${objPath} ${stdlib}`;
|
const linkResult = await $`${cc.cc} ${flags} -std=gnu23 ${linkFlags} -lstdc++ -nostartfiles -o ${wasmPath} ${objPath} ${stdlib}`;
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,9 @@ namespace Input {
|
||||||
NUM_8 = 'Digit8',
|
NUM_8 = 'Digit8',
|
||||||
NUM_9 = 'Digit9',
|
NUM_9 = 'Digit9',
|
||||||
|
|
||||||
MOUSE_LEFT = `Mouse0`,
|
MOUSE_LEFT = 'Mouse0',
|
||||||
MOUSE_MIDDLE = `Mouse1`,
|
MOUSE_MIDDLE = 'Mouse1',
|
||||||
MOUSE_RIGHT = `Mouse2`,
|
MOUSE_RIGHT = 'Mouse2',
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum GamepadAxis {
|
export enum GamepadAxis {
|
||||||
|
|
@ -217,4 +217,4 @@ namespace Input {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Input;
|
export default Input;
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,6 @@
|
||||||
#define TYPE_CIRCLE 1
|
#define TYPE_CIRCLE 1
|
||||||
#define TYPE_PLANE 2
|
#define TYPE_PLANE 2
|
||||||
|
|
||||||
#define rigid_body_get(idx) (rigid_bodies + (idx))
|
|
||||||
|
|
||||||
////////// Types
|
////////// Types
|
||||||
|
|
||||||
typedef struct rigid_body_t {
|
typedef struct rigid_body_t {
|
||||||
|
|
@ -51,8 +49,8 @@ static rigid_body_collision_callback_t rigid_body_collision_callback = NULL;
|
||||||
|
|
||||||
////////// Functions
|
////////// Functions
|
||||||
|
|
||||||
EXPORT_NAME(rigid_body_get) rigid_body* _rigid_body_get(rigid_body_index idx) {
|
JS_EXPORT rigid_body* rigid_body_get(rigid_body_index idx) {
|
||||||
return rigid_body_get(idx);
|
return (rigid_bodies + (idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t rigid_body_new(float x, float y, float vx, float vy, float mass) {
|
size_t rigid_body_new(float x, float y, float vx, float vy, float mass) {
|
||||||
|
|
@ -74,7 +72,7 @@ size_t rigid_body_new(float x, float y, float vx, float vy, float mass) {
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
rigid_body_index EXPORT(rigid_body_new_circle, float x, float y, float vx, float vy, float mass, float radius) {
|
JS_EXPORT rigid_body_index 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_index idx = rigid_body_new(x, y, vx, vy, mass);
|
||||||
rigid_body* rb = rigid_body_get(idx);
|
rigid_body* rb = rigid_body_get(idx);
|
||||||
|
|
||||||
|
|
@ -84,7 +82,7 @@ rigid_body_index EXPORT(rigid_body_new_circle, float x, float y, float vx, float
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
rigid_body_index EXPORT(rigid_body_new_plane, float x, float y, float nx, float ny) {
|
JS_EXPORT rigid_body_index 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_index idx = rigid_body_new(x, y, 0, 0, INFINITY);
|
||||||
rigid_body* rb = rigid_body_get(idx);
|
rigid_body* rb = rigid_body_get(idx);
|
||||||
|
|
||||||
|
|
@ -94,11 +92,11 @@ rigid_body_index EXPORT(rigid_body_new_plane, float x, float y, float nx, float
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_free, rigid_body_index idx) {
|
JS_EXPORT void rigid_body_free(rigid_body_index idx) {
|
||||||
memset(rigid_body_get(idx), 0, sizeof(rigid_body));
|
memset(rigid_body_get(idx), 0, sizeof(rigid_body));
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_update, rigid_body_index idx, float dt) {
|
JS_EXPORT void rigid_body_update(rigid_body_index idx, float dt) {
|
||||||
rigid_body_resolve_collision(idx);
|
rigid_body_resolve_collision(idx);
|
||||||
rigid_body* rb = rigid_body_get(idx);
|
rigid_body* rb = rigid_body_get(idx);
|
||||||
|
|
||||||
|
|
@ -114,7 +112,7 @@ void EXPORT(rigid_body_update, rigid_body_index idx, float dt) {
|
||||||
rb->force.y = 0;
|
rb->force.y = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_update_all, float dt) {
|
JS_EXPORT void rigid_body_update_all(float dt) {
|
||||||
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
||||||
rigid_body* current = rigid_body_get(idx);
|
rigid_body* current = rigid_body_get(idx);
|
||||||
if (current->type == TYPE_EMPTY) {
|
if (current->type == TYPE_EMPTY) {
|
||||||
|
|
@ -124,13 +122,13 @@ void EXPORT(rigid_body_update_all, float dt) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_add_force, rigid_body_index idx, float fx, float fy) {
|
JS_EXPORT void rigid_body_add_force(rigid_body_index idx, float fx, float fy) {
|
||||||
rigid_body* rb = rigid_body_get(idx);
|
rigid_body* rb = rigid_body_get(idx);
|
||||||
rb->force.x += fx;
|
rb->force.x += fx;
|
||||||
rb->force.y += fy;
|
rb->force.y += fy;
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_add_global_force, float fx, float fy) {
|
JS_EXPORT void rigid_body_add_global_force(float fx, float fy) {
|
||||||
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
for (rigid_body_index idx = 0; idx < rigid_bodies_cap; idx++) {
|
||||||
rigid_body* current = rigid_body_get(idx);
|
rigid_body* current = rigid_body_get(idx);
|
||||||
if (current->type == TYPE_EMPTY) {
|
if (current->type == TYPE_EMPTY) {
|
||||||
|
|
@ -140,7 +138,7 @@ void EXPORT(rigid_body_add_global_force, float fx, float fy) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EXPORT(rigid_body_set_collision_callback, rigid_body_collision_callback_t callback) {
|
JS_EXPORT void rigid_body_set_collision_callback(rigid_body_collision_callback_t callback) {
|
||||||
rigid_body_collision_callback = callback;
|
rigid_body_collision_callback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue