120 lines
2.9 KiB
C
120 lines
2.9 KiB
C
#include "jsffi.h"
|
|
#include "stb_ds.h"
|
|
#include "string.h"
|
|
#include <assert.h>
|
|
#include <time.h>
|
|
#include <sys/time.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <limits.h>
|
|
#include "HandmadeMath.h"
|
|
#include <stdint.h>
|
|
#include "cell.h"
|
|
|
|
#if defined(_WIN32)
|
|
#include <windows.h>
|
|
#else
|
|
#include <dlfcn.h>
|
|
#endif
|
|
|
|
#define JS_GETNUM(JS,VAL,I,TO,TYPE) { \
|
|
JSValue val = JS_GetPropertyUint32(JS,VAL,I); \
|
|
TO = js2##TYPE(JS, val); \
|
|
JS_FreeValue(JS, val); } \
|
|
|
|
int js2bool(JSContext *js, JSValue v) { return JS_ToBool(js,v); }
|
|
JSValue bool2js(JSContext *js, int b) { return JS_NewBool(js,b); }
|
|
|
|
JSValue number2js(JSContext *js, double g) { return JS_NewFloat64(js,g); }
|
|
double js2number(JSContext *js, JSValue v) {
|
|
double g;
|
|
JS_ToFloat64(js, &g, v);
|
|
if (isnan(g)) g = 0;
|
|
return g;
|
|
}
|
|
|
|
JSValue js_getproperty(JSContext *js, JSValue v, const char *prop)
|
|
{
|
|
JSValue ret = JS_GetPropertyStr(js, v, prop);
|
|
JS_FreeValue(js,ret);
|
|
return ret;
|
|
}
|
|
|
|
JSValue make_quad_indices_buffer(JSContext *js, int quads)
|
|
{
|
|
cell_rt *rt = JS_GetContextOpaque(js);
|
|
int count = quads*6;
|
|
if (!JS_IsNull(rt->idx_buffer) && rt->idx_count >= count)
|
|
return JS_DupValue(js,rt->idx_buffer);
|
|
|
|
int verts = quads*4;
|
|
uint16_t *indices = malloc(sizeof(*indices)*count);
|
|
for (int i = 0, v = 0; v < verts; i +=6, v += 4) {
|
|
indices[i] = v;
|
|
indices[i+1] = v+2;
|
|
indices[i+2] = v+1;
|
|
indices[i+3] = v+2;
|
|
indices[i+4] = v+3;
|
|
indices[i+5] = v+1;
|
|
}
|
|
|
|
if (!JS_IsNull(rt->idx_buffer))
|
|
JS_FreeValue(js,rt->idx_buffer);
|
|
|
|
// rt->idx_buffer = make_gpu_buffer(js,indices, sizeof(*indices)*count, JS_TYPED_ARRAY_UINT16, 1,0,1);
|
|
rt->idx_count = count;
|
|
return JS_DupValue(js,rt->idx_buffer);
|
|
}
|
|
|
|
char *js2strdup(JSContext *js, JSValue v) {
|
|
const char *str = JS_ToCString(js, v);
|
|
char *ret = strdup(str);
|
|
JS_FreeCString(js, str);
|
|
return ret;
|
|
}
|
|
|
|
JSValue angle2js(JSContext *js,double g) {
|
|
return number2js(js,g*HMM_RadToTurn);
|
|
}
|
|
|
|
double js2angle(JSContext *js,JSValue v) {
|
|
double n = js2number(js,v);
|
|
return n * HMM_TurnToRad;
|
|
}
|
|
|
|
JSValue js_os_use(JSContext *js);
|
|
|
|
void ffi_load(JSContext *js)
|
|
{
|
|
cell_rt *rt = JS_GetContextOpaque(js);
|
|
JS_FreeValue(js, js_blob_use(js));
|
|
|
|
JSValue globalThis = JS_GetGlobalObject(js);
|
|
|
|
JSValue cell = JS_NewObject(js);
|
|
JS_SetPropertyStr(js,globalThis,"cell", cell);
|
|
|
|
JSValue hidden_fn = JS_NewObject(js);
|
|
|
|
JS_SetPropertyStr(js, cell, "hidden", hidden_fn);
|
|
JS_SetPropertyStr(js, hidden_fn, "os", js_os_use(js));
|
|
|
|
const char actorsym_script[] = "var sym = Symbol(`actordata`); sym;";
|
|
|
|
JSValue actorsym = JS_Eval(js, actorsym_script, sizeof(actorsym_script)-1, "internal", 0);
|
|
|
|
JS_SetPropertyStr(js, hidden_fn, "actorsym", actorsym);
|
|
|
|
rt->actor_sym = JS_ValueToAtom(js, actorsym);
|
|
|
|
if (rt->init_wota) {
|
|
JS_SetPropertyStr(js, hidden_fn, "init", wota2value(js, rt->init_wota));
|
|
// init wota can now be freed
|
|
free(rt->init_wota);
|
|
rt->init_wota = NULL;
|
|
}
|
|
|
|
JS_FreeValue(js,globalThis);
|
|
}
|