fix syntax

This commit is contained in:
2026-02-17 16:03:43 -06:00
parent 4e1b63fd0e
commit e78faccab8
8 changed files with 234 additions and 220 deletions

View File

@@ -80,18 +80,20 @@ static void render_frame(plm_t *mpeg, plm_frame_t *frame, void *d) {
plm_frame_to_rgba(frame, rgb, frame->width*4);
// Create surface data object instead of SDL_Surface
JSValue surfData = JS_NewObject(ds->js);
JS_SetPropertyStr(ds->js, surfData, "width", JS_NewInt32(ds->js, frame->width));
JS_SetPropertyStr(ds->js, surfData, "height", JS_NewInt32(ds->js, frame->height));
JS_SetPropertyStr(ds->js, surfData, "format", JS_NewString(ds->js, "rgba32"));
JS_SetPropertyStr(ds->js, surfData, "pitch", JS_NewInt32(ds->js, frame->width*4));
JS_SetPropertyStr(ds->js, surfData, "pixels", js_new_blob_stoned_copy(ds->js, rgb, frame->height*frame->width*4));
JS_FRAME(ds->js);
JS_ROOT(surfData, JS_NewObject(ds->js));
JS_SetPropertyStr(ds->js, surfData.val, "width", JS_NewInt32(ds->js, frame->width));
JS_SetPropertyStr(ds->js, surfData.val, "height", JS_NewInt32(ds->js, frame->height));
JS_SetPropertyStr(ds->js, surfData.val, "format", JS_NewString(ds->js, "rgba32"));
JS_SetPropertyStr(ds->js, surfData.val, "pitch", JS_NewInt32(ds->js, frame->width*4));
JS_SetPropertyStr(ds->js, surfData.val, "pixels", js_new_blob_stoned_copy(ds->js, rgb, frame->height*frame->width*4));
JSValue s[1];
s[0] = surfData;
s[0] = surfData.val;
JSValue cb = JS_DupValue(ds->js,ds->callback);
JSValue ret = JS_Call(ds->js, cb, JS_NULL, 1, s);
JSValue cret = JS_Call(ds->js, cb, JS_NULL, 1, s);
JS_FreeValue(ds->js,cb);
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
free(rgb);
// TODO: raise exception
}

View File

@@ -241,13 +241,11 @@ JSValue quads_to_mesh(JSContext *js, text_vert *buffer)
{
size_t verts = arrlen(buffer);
JSValue ret = JS_NewObject(js);
// Allocate flat arrays for xy, uv, and color data
size_t xy_size = verts * 2 * sizeof(float);
size_t uv_size = verts * 2 * sizeof(float);
size_t color_size = verts * sizeof(SDL_FColor);
float *xy_data = malloc(xy_size);
float *uv_data = malloc(uv_size);
SDL_FColor *color_data = malloc(color_size);
@@ -256,10 +254,10 @@ JSValue quads_to_mesh(JSContext *js, text_vert *buffer)
for (int i = 0; i < verts; i++) {
xy_data[i*2] = buffer[i].pos.x;
xy_data[i*2+1] = buffer[i].pos.y;
uv_data[i*2] = buffer[i].uv.x;
uv_data[i*2+1] = buffer[i].uv.y;
color_data[i].r = buffer[i].color.x;
color_data[i].g = buffer[i].color.y;
color_data[i].b = buffer[i].color.z;
@@ -268,7 +266,7 @@ JSValue quads_to_mesh(JSContext *js, text_vert *buffer)
size_t quads = verts/4;
size_t count = quads*6;
// Create indices
uint16_t *indices = malloc(sizeof(uint16_t)*count);
for (int i = 0, v = 0; i < count; i += 6, v += 4) {
@@ -280,24 +278,25 @@ JSValue quads_to_mesh(JSContext *js, text_vert *buffer)
indices[i+5] = v+1;
}
// Create blobs for geometry data
JS_SetPropertyStr(js, ret, "xy", js_new_blob_stoned_copy(js, xy_data, xy_size));
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "uv", js_new_blob_stoned_copy(js, uv_data, uv_size));
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "color", js_new_blob_stoned_copy(js, color_data, color_size));
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret, "indices", js_new_blob_stoned_copy(js, indices, sizeof(uint16_t)*count));
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, verts));
JS_SetPropertyStr(js, ret, "num_indices", JS_NewInt32(js, count));
JS_SetPropertyStr(js, ret, "size_indices", JS_NewInt32(js, 2)); // uint16_t size
JS_FRAME(js);
JS_ROOT(ret, JS_NewObject(js));
JS_SetPropertyStr(js, ret.val, "xy", js_new_blob_stoned_copy(js, xy_data, xy_size));
JS_SetPropertyStr(js, ret.val, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret.val, "uv", js_new_blob_stoned_copy(js, uv_data, uv_size));
JS_SetPropertyStr(js, ret.val, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret.val, "color", js_new_blob_stoned_copy(js, color_data, color_size));
JS_SetPropertyStr(js, ret.val, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret.val, "indices", js_new_blob_stoned_copy(js, indices, sizeof(uint16_t)*count));
JS_SetPropertyStr(js, ret.val, "num_vertices", JS_NewInt32(js, verts));
JS_SetPropertyStr(js, ret.val, "num_indices", JS_NewInt32(js, count));
JS_SetPropertyStr(js, ret.val, "size_indices", JS_NewInt32(js, 2)); // uint16_t size
free(xy_data);
free(uv_data);
free(color_data);
free(indices);
return ret;
JS_RETURN(ret.val);
}
JSC_CCALL(gpu_slice9,
@@ -724,31 +723,28 @@ JSC_CCALL(geometry_tilemap_to_data,
}
JS_FreeValue(js, tiles_array);
// Create result object with blob data
ret = JS_NewObject(js);
// Create blobs for each data type
JSValue xy_blob = js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float));
JSValue uv_blob = js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float));
JSValue color_blob = js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor));
JSValue index_blob = js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t));
JS_SetPropertyStr(js, ret, "xy", xy_blob);
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "uv", uv_blob);
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "color", color_blob);
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret, "indices", index_blob);
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, vertex_count));
JS_SetPropertyStr(js, ret, "num_indices", JS_NewInt32(js, index_count));
JS_SetPropertyStr(js, ret, "size_indices", JS_NewInt32(js, 2)); // using uint16_t
JS_FRAME(js);
JS_ROOT(result, JS_NewObject(js));
JS_SetPropertyStr(js, result.val, "xy", js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv", js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "color", js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "indices", js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t)));
JS_SetPropertyStr(js, result.val, "num_vertices", JS_NewInt32(js, vertex_count));
JS_SetPropertyStr(js, result.val, "num_indices", JS_NewInt32(js, index_count));
JS_SetPropertyStr(js, result.val, "size_indices", JS_NewInt32(js, 2)); // using uint16_t
free(xy_data);
free(uv_data);
free(color_data);
free(index_data);
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = result.val;
)
static void print_buffers(float *xy_data, float *uv_data, SDL_FColor *color_data, uint16_t *index_data, int vertex_count, int index_count)
@@ -954,29 +950,26 @@ JSC_CCALL(geometry_sprites_to_data,
}
// Create result object with blob data
ret = JS_NewObject(js);
// Create blobs for each data type
JSValue xy_blob = js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float));
JSValue uv_blob = js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float));
JSValue color_blob = js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor));
JSValue index_blob = js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t));
JS_SetPropertyStr(js, ret, "xy", xy_blob);
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "uv", uv_blob);
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "color", color_blob);
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret, "indices", index_blob);
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, vertex_count));
JS_SetPropertyStr(js, ret, "num_indices", JS_NewInt32(js, index_count));
JS_SetPropertyStr(js, ret, "size_indices", JS_NewInt32(js, 2)); // using uint16_t
JS_FRAME(js);
JS_ROOT(result, JS_NewObject(js));
JS_SetPropertyStr(js, result.val, "xy", js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv", js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "color", js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "indices", js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t)));
JS_SetPropertyStr(js, result.val, "num_vertices", JS_NewInt32(js, vertex_count));
JS_SetPropertyStr(js, result.val, "num_indices", JS_NewInt32(js, index_count));
JS_SetPropertyStr(js, result.val, "size_indices", JS_NewInt32(js, 2)); // using uint16_t
free(xy_data);
free(uv_data);
free(color_data);
free(index_data);
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = result.val;
)
JSC_CCALL(geometry_transform_xy_blob,
@@ -1139,19 +1132,17 @@ JSC_CCALL(geometry_make_rect_quad,
color_data[i] = color;
// Create result object
ret = JS_NewObject(js);
JSValue xy_blob = js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float));
JSValue uv_blob = js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float));
JSValue color_blob = js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor));
JS_SetPropertyStr(js, ret, "xy", xy_blob);
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "uv", uv_blob);
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "color", color_blob);
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, vertex_count));
JS_FRAME(js);
JS_ROOT(result, JS_NewObject(js));
JS_SetPropertyStr(js, result.val, "xy", js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv", js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, result.val, "color", js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, result.val, "num_vertices", JS_NewInt32(js, vertex_count));
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = result.val;
)
JSC_CCALL(geometry_weave,

View File

@@ -144,13 +144,13 @@ static JSValue js_layout_get_rect(JSContext *js, JSValueConst self, int argc, JS
GETLAY
GETITEM(id, argv[0])
lay_vec4 rect = lay_get_rect(lay, id);
JSValue ret = JS_NewObject(js);
JS_SetPropertyStr(js, ret, "x", JS_NewFloat64(js, rect[0]));
JS_SetPropertyStr(js, ret, "y", JS_NewFloat64(js, rect[1]));
JS_SetPropertyStr(js, ret, "width", JS_NewFloat64(js, rect[2]));
JS_SetPropertyStr(js, ret, "height", JS_NewFloat64(js, rect[3]));
return ret;
JS_FRAME(js);
JS_ROOT(ret, JS_NewObject(js));
JS_SetPropertyStr(js, ret.val, "x", JS_NewFloat64(js, rect[0]));
JS_SetPropertyStr(js, ret.val, "y", JS_NewFloat64(js, rect[1]));
JS_SetPropertyStr(js, ret.val, "width", JS_NewFloat64(js, rect[2]));
JS_SetPropertyStr(js, ret.val, "height", JS_NewFloat64(js, rect[3]));
JS_RETURN(ret.val);
}
static JSValue js_layout_reset(JSContext *js, JSValueConst self, int argc, JSValueConst *argv) {
@@ -192,40 +192,41 @@ CELL_USE_INIT(
JS_NewClassID(&js_layout_class_id);
JS_NewClass(js, js_layout_class_id, &js_layout_class);
JSValue proto = JS_NewObject(js);
JS_SetPropertyFunctionList(js, proto, js_layout_proto_funcs, sizeof(js_layout_proto_funcs) / sizeof(JSCFunctionListEntry));
JS_SetClassProto(js, js_layout_class_id, proto);
JS_FRAME(js);
JS_ROOT(proto, JS_NewObject(js));
JS_SetPropertyFunctionList(js, proto.val, js_layout_proto_funcs, sizeof(js_layout_proto_funcs) / sizeof(JSCFunctionListEntry));
JS_SetClassProto(js, js_layout_class_id, proto.val);
JSValue contain = JS_NewObject(js);
JSValue behave = JS_NewObject(js);
JS_SetPropertyStr(js, contain, "row", JS_NewUint32(js, LAY_ROW));
JS_SetPropertyStr(js, contain, "column", JS_NewUint32(js, LAY_COLUMN));
JS_SetPropertyStr(js, contain, "layout", JS_NewUint32(js, LAY_LAYOUT));
JS_SetPropertyStr(js, contain, "flex", JS_NewUint32(js, LAY_FLEX));
JS_SetPropertyStr(js, contain, "wrap", JS_NewUint32(js, LAY_WRAP));
JS_SetPropertyStr(js, contain, "nowrap", JS_NewUint32(js, LAY_NOWRAP));
JS_SetPropertyStr(js, contain, "start", JS_NewUint32(js, LAY_START));
JS_SetPropertyStr(js, contain, "middle", JS_NewUint32(js, LAY_MIDDLE));
JS_SetPropertyStr(js, contain, "end", JS_NewUint32(js, LAY_END));
JS_SetPropertyStr(js, contain, "justify", JS_NewUint32(js, LAY_JUSTIFY));
JS_SetPropertyStr(js, behave, "left", JS_NewUint32(js, LAY_LEFT));
JS_SetPropertyStr(js, behave, "top", JS_NewUint32(js, LAY_TOP));
JS_SetPropertyStr(js, behave, "right", JS_NewUint32(js, LAY_RIGHT));
JS_SetPropertyStr(js, behave, "bottom", JS_NewUint32(js, LAY_BOTTOM));
JS_SetPropertyStr(js, behave, "hfill", JS_NewUint32(js, LAY_HFILL));
JS_SetPropertyStr(js, behave, "vfill", JS_NewUint32(js, LAY_VFILL));
JS_SetPropertyStr(js, behave, "hcenter", JS_NewUint32(js, LAY_HCENTER));
JS_SetPropertyStr(js, behave, "vcenter", JS_NewUint32(js, LAY_VCENTER));
JS_SetPropertyStr(js, behave, "center", JS_NewUint32(js, LAY_CENTER));
JS_SetPropertyStr(js, behave, "fill", JS_NewUint32(js, LAY_FILL));
JS_SetPropertyStr(js, behave, "break", JS_NewUint32(js, LAY_BREAK));
JS_ROOT(contain, JS_NewObject(js));
JS_ROOT(behave, JS_NewObject(js));
JS_SetPropertyStr(js, contain.val, "row", JS_NewUint32(js, LAY_ROW));
JS_SetPropertyStr(js, contain.val, "column", JS_NewUint32(js, LAY_COLUMN));
JS_SetPropertyStr(js, contain.val, "layout", JS_NewUint32(js, LAY_LAYOUT));
JS_SetPropertyStr(js, contain.val, "flex", JS_NewUint32(js, LAY_FLEX));
JS_SetPropertyStr(js, contain.val, "wrap", JS_NewUint32(js, LAY_WRAP));
JS_SetPropertyStr(js, contain.val, "nowrap", JS_NewUint32(js, LAY_NOWRAP));
JSValue export = JS_NewObject(js);
JS_SetPropertyFunctionList(js, export, js_layout_funcs, sizeof(js_layout_funcs)/sizeof(JSCFunctionListEntry));
JS_SetPropertyStr(js, export, "behave", behave);
JS_SetPropertyStr(js, export, "contain", contain);
JS_SetPropertyStr(js, contain.val, "start", JS_NewUint32(js, LAY_START));
JS_SetPropertyStr(js, contain.val, "middle", JS_NewUint32(js, LAY_MIDDLE));
JS_SetPropertyStr(js, contain.val, "end", JS_NewUint32(js, LAY_END));
JS_SetPropertyStr(js, contain.val, "justify", JS_NewUint32(js, LAY_JUSTIFY));
return export;
JS_SetPropertyStr(js, behave.val, "left", JS_NewUint32(js, LAY_LEFT));
JS_SetPropertyStr(js, behave.val, "top", JS_NewUint32(js, LAY_TOP));
JS_SetPropertyStr(js, behave.val, "right", JS_NewUint32(js, LAY_RIGHT));
JS_SetPropertyStr(js, behave.val, "bottom", JS_NewUint32(js, LAY_BOTTOM));
JS_SetPropertyStr(js, behave.val, "hfill", JS_NewUint32(js, LAY_HFILL));
JS_SetPropertyStr(js, behave.val, "vfill", JS_NewUint32(js, LAY_VFILL));
JS_SetPropertyStr(js, behave.val, "hcenter", JS_NewUint32(js, LAY_HCENTER));
JS_SetPropertyStr(js, behave.val, "vcenter", JS_NewUint32(js, LAY_VCENTER));
JS_SetPropertyStr(js, behave.val, "center", JS_NewUint32(js, LAY_CENTER));
JS_SetPropertyStr(js, behave.val, "fill", JS_NewUint32(js, LAY_FILL));
JS_SetPropertyStr(js, behave.val, "break", JS_NewUint32(js, LAY_BREAK));
JS_ROOT(export_obj, JS_NewObject(js));
JS_SetPropertyFunctionList(js, export_obj.val, js_layout_funcs, sizeof(js_layout_funcs)/sizeof(JSCFunctionListEntry));
JS_SetPropertyStr(js, export_obj.val, "behave", behave.val);
JS_SetPropertyStr(js, export_obj.val, "contain", contain.val);
JS_RETURN(export_obj.val);
)

24
math.c
View File

@@ -28,11 +28,12 @@ static float *js2floats(JSContext *js, JSValue v, size_t *len)
// Convert float array to JS array
static JSValue floats2array(JSContext *js, float *vals, size_t len) {
JSValue arr = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
for (size_t i = 0; i < len; i++) {
JS_SetPropertyNumber(js, arr, i, number2js(js, vals[i]));
JS_SetPropertyNumber(js, arr.val, i, number2js(js, vals[i]));
}
return arr;
JS_RETURN(arr.val);
}
// Calculate vector length
@@ -91,12 +92,14 @@ JSC_CCALL(math_norm,
}
double length = arr_vec_length(js,argv[0]);
JSValue newarr = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(newarr, JS_NewArray(js));
for (int i = 0; i < len; i++)
JS_SetPropertyNumber(js, newarr, i, number2js(js,js_getnum_uint32(js, argv[0],i)/length));
JS_SetPropertyNumber(js, newarr.val, i, number2js(js,js_getnum_uint32(js, argv[0],i)/length));
ret = newarr;
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = newarr.val;
)
// Compute the angle between two vectors (2D/3D/4D).
@@ -234,14 +237,15 @@ JSC_CCALL(math_from_to,
int inclusive = JS_ToBool(js,argv[3]);
int arr = JS_ToBool(js,argv[4]);
JSValue jsarr = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(jsarr, JS_NewArray(js));
int i = 0;
for (double val = start; val <= end; val += step) {
if (val == end && !inclusive) break;
JS_SetPropertyNumber(js, jsarr, i++, number2js(js, val));
JS_SetPropertyNumber(js, jsarr.val, i++, number2js(js, val));
}
return jsarr;
JS_RETURN(jsarr.val);
)
// Compute the dot product between two numeric arrays, returning a scalar. Extra elements are ignored.

View File

@@ -115,28 +115,30 @@ static JSValue js_mersenne_use_call(JSContext *js, JSValueConst new_target, int
m_seedRand(mrand, seed);
JSValue obj = JS_NewObjectClass(js, js_mersenne_class_id);
if (JS_IsException(obj)) {
JS_FRAME(js);
JS_ROOT(obj, JS_NewObjectClass(js, js_mersenne_class_id));
if (JS_IsException(obj.val)) {
js_free(js, mrand);
return obj;
JS_RETURN_EX();
}
JS_SetOpaque(obj, mrand);
// Store seed as a read-only property
JS_SetPropertyStr(js, obj, "seed", JS_NewFloat64(js, seed));
return obj;
JS_SetOpaque(obj.val, mrand);
// Store seed as a read-only property
JS_SetPropertyStr(js, obj.val, "seed", JS_NewFloat64(js, seed));
JS_RETURN(obj.val);
}
CELL_USE_INIT(
JS_NewClassID(&js_mersenne_class_id);
JS_NewClass(js, js_mersenne_class_id, &js_mersenne_class);
JSValue proto = JS_NewObject(js);
JS_SetPropertyFunctionList(js, proto, js_mersenne_funcs, sizeof(js_mersenne_funcs)/sizeof(JSCFunctionListEntry));
JS_SetClassProto(js, js_mersenne_class_id, proto);
JS_FRAME(js);
JS_ROOT(proto, JS_NewObject(js));
JS_SetPropertyFunctionList(js, proto.val, js_mersenne_funcs, sizeof(js_mersenne_funcs)/sizeof(JSCFunctionListEntry));
JS_SetClassProto(js, js_mersenne_class_id, proto.val);
// Return the factory function
return JS_NewCFunction2(js, js_mersenne_use_call, "mersenne", 1, JS_CFUNC_generic, 0);
JS_RETURN(JS_NewCFunction2(js, js_mersenne_use_call, "mersenne", 1, JS_CFUNC_generic, 0));
)

View File

@@ -30,12 +30,13 @@ colorf js2color(JSContext *js,JSValue v) {
JSValue color2js(JSContext *js, colorf color)
{
JSValue arr = JS_NewArray(js);
JS_SetPropertyNumber(js, arr,0,number2js(js,(double)color.r));
JS_SetPropertyNumber(js, arr,1,number2js(js,(double)color.g));
JS_SetPropertyNumber(js, arr,2,number2js(js,(double)color.b));
JS_SetPropertyNumber(js, arr,3,number2js(js,(double)color.a));
return arr;
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
JS_SetPropertyNumber(js, arr.val,0,number2js(js,(double)color.r));
JS_SetPropertyNumber(js, arr.val,1,number2js(js,(double)color.g));
JS_SetPropertyNumber(js, arr.val,2,number2js(js,(double)color.b));
JS_SetPropertyNumber(js, arr.val,3,number2js(js,(double)color.a));
JS_RETURN(arr.val);
}
HMM_Vec2 js2vec2(JSContext *js,JSValue v)
@@ -100,11 +101,12 @@ HMM_Vec3 js2vec3f(JSContext *js, JSValue v)
JSValue vec32js(JSContext *js, HMM_Vec3 v)
{
JSValue array = JS_NewArray(js);
JS_SetPropertyNumber(js, array,0,number2js(js,v.x));
JS_SetPropertyNumber(js, array,1,number2js(js,v.y));
JS_SetPropertyNumber(js, array,2,number2js(js,v.z));
return array;
JS_FRAME(js);
JS_ROOT(array, JS_NewArray(js));
JS_SetPropertyNumber(js, array.val,0,number2js(js,v.x));
JS_SetPropertyNumber(js, array.val,1,number2js(js,v.y));
JS_SetPropertyNumber(js, array.val,2,number2js(js,v.z));
JS_RETURN(array.val);
}
JSValue vec3f2js(JSContext *js, HMM_Vec3 v)
@@ -114,12 +116,13 @@ JSValue vec3f2js(JSContext *js, HMM_Vec3 v)
JSValue quat2js(JSContext *js, HMM_Quat q)
{
JSValue arr = JS_NewArray(js);
JS_SetPropertyNumber(js, arr, 0, number2js(js,q.x));
JS_SetPropertyNumber(js, arr,1,number2js(js,q.y));
JS_SetPropertyNumber(js, arr,2,number2js(js,q.z));
JS_SetPropertyNumber(js, arr,3,number2js(js,q.w));
return arr;
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
JS_SetPropertyNumber(js, arr.val, 0, number2js(js,q.x));
JS_SetPropertyNumber(js, arr.val,1,number2js(js,q.y));
JS_SetPropertyNumber(js, arr.val,2,number2js(js,q.z));
JS_SetPropertyNumber(js, arr.val,3,number2js(js,q.w));
JS_RETURN(arr.val);
}
HMM_Vec4 js2vec4(JSContext *js, JSValue v)
@@ -153,10 +156,11 @@ HMM_Quat js2quat(JSContext *js,JSValue v)
JSValue vec42js(JSContext *js, HMM_Vec4 v)
{
JSValue array = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(array, JS_NewArray(js));
for (int i = 0; i < 4; i++)
JS_SetPropertyNumber(js, array,i,number2js(js,v.e[i]));
return array;
JS_SetPropertyNumber(js, array.val,i,number2js(js,v.e[i]));
JS_RETURN(array.val);
}
HMM_Vec2 *js2cpvec2arr(JSContext *js,JSValue v) {
@@ -191,11 +195,12 @@ rect js2rect(JSContext *js,JSValue v) {
}
static JSValue floats2array(JSContext *js, float *vals, size_t len) {
JSValue arr = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
for (size_t i = 0; i < len; i++) {
JS_SetPropertyNumber(js, arr, i, number2js(js, vals[i]));
JS_SetPropertyNumber(js, arr.val, i, number2js(js, vals[i]));
}
return arr;
JS_RETURN(arr.val);
}
lrtb js2lrtb(JSContext *js, JSValue v)
@@ -210,27 +215,29 @@ lrtb js2lrtb(JSContext *js, JSValue v)
JSValue vec22js(JSContext *js,HMM_Vec2 v)
{
JSValue array = JS_NewArray(js);
JS_SetPropertyNumber(js, array,0,number2js(js,v.x));
JS_SetPropertyNumber(js, array,1,number2js(js,v.y));
return array;
JS_FRAME(js);
JS_ROOT(array, JS_NewArray(js));
JS_SetPropertyNumber(js, array.val,0,number2js(js,v.x));
JS_SetPropertyNumber(js, array.val,1,number2js(js,v.y));
JS_RETURN(array.val);
}
JSValue vecarr2js(JSContext *js,HMM_Vec2 *points, int n) {
JSValue array = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(array, JS_NewArray(js));
for (int i = 0; i < n; i++)
JS_SetPropertyNumber(js, array,i,vec22js(js,points[i]));
return array;
JS_SetPropertyNumber(js, array.val,i,vec22js(js,points[i]));
JS_RETURN(array.val);
}
JSValue rect2js(JSContext *js,rect rect) {
JSValue obj = JS_NewObject(js);
JS_SetPropertyStr(js, obj, "x", number2js(js, rect.x));
JS_SetPropertyStr(js, obj, "y", number2js(js, rect.y));
JS_SetPropertyStr(js, obj, "width", number2js(js, rect.w));
JS_SetPropertyStr(js, obj, "height", number2js(js, rect.h));
return obj;
JS_FRAME(js);
JS_ROOT(obj, JS_NewObject(js));
JS_SetPropertyStr(js, obj.val, "x", number2js(js, rect.x));
JS_SetPropertyStr(js, obj.val, "y", number2js(js, rect.y));
JS_SetPropertyStr(js, obj.val, "width", number2js(js, rect.w));
JS_SetPropertyStr(js, obj.val, "height", number2js(js, rect.h));
JS_RETURN(obj.val);
}
float *rgba2floats(float *r, struct rgba c)

69
staef.c
View File

@@ -678,28 +678,32 @@ struct text_vert *renderText(const char *text, HMM_Vec2 pos, font *f, colorf col
QJSCLASS(font,)
// Helper to attach texture and mode info to font JS object
static void attach_font_texture(JSContext *js, JSValue ret, font *f) {
static void attach_font_texture(JSContext *js, JSValue *pret, font *f) {
JS_FRAME(js);
JS_ROOT(obj, *pret);
if (f->pixels) {
JSValue texData = JS_NewObject(js);
JS_SetPropertyStr(js, texData, "width", JS_NewInt32(js, f->atlas_size));
JS_SetPropertyStr(js, texData, "height", JS_NewInt32(js, f->atlas_size));
JS_SetPropertyStr(js, texData, "format", JS_NewString(js, "rgba8"));
JS_ROOT(texData, JS_NewObject(js));
JS_SetPropertyStr(js, texData.val, "width", JS_NewInt32(js, f->atlas_size));
JS_SetPropertyStr(js, texData.val, "height", JS_NewInt32(js, f->atlas_size));
JS_SetPropertyStr(js, texData.val, "format", JS_NewString(js, "rgba8"));
size_t byte_size = f->atlas_size * f->atlas_size * 4;
JS_SetPropertyStr(js, texData, "pixels", js_new_blob_stoned_copy(js, f->pixels, byte_size));
JS_SetPropertyStr(js, ret, "texture", texData);
JS_SetPropertyStr(js, texData.val, "pixels", js_new_blob_stoned_copy(js, f->pixels, byte_size));
JS_SetPropertyStr(js, obj.val, "texture", texData.val);
}
// Add mode string
const char *mode_str = "bitmap";
if (f->mode == FONT_MODE_SDF) mode_str = "sdf";
else if (f->mode == FONT_MODE_MSDF) mode_str = "msdf";
JS_SetPropertyStr(js, ret, "mode", JS_NewString(js, mode_str));
JS_SetPropertyStr(js, obj.val, "mode", JS_NewString(js, mode_str));
// Add range_px and sharpness for SDF/MSDF fonts
JS_SetPropertyStr(js, ret, "range_px", JS_NewFloat64(js, f->range_px));
JS_SetPropertyStr(js, ret, "sharpness", JS_NewFloat64(js, f->sharpness));
JS_SetPropertyStr(js, obj.val, "range_px", JS_NewFloat64(js, f->range_px));
JS_SetPropertyStr(js, obj.val, "sharpness", JS_NewFloat64(js, f->sharpness));
*pret = obj.val;
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
}
// Font constructor (legacy: data, height, is_sdf)
@@ -717,7 +721,7 @@ JSC_CCALL(staef_font_new,
if (!f) return JS_ThrowReferenceError(js, "could not create font");
ret = font2js(js, f);
attach_font_texture(js, ret, f);
attach_font_texture(js, &ret, f);
)
// SDF font constructor: sdf_font(data, em_px, range_px, padding_px, sharpness)
@@ -736,7 +740,7 @@ JSC_CCALL(staef_sdf_font_new,
if (!f) return JS_ThrowReferenceError(js, "could not create SDF font");
ret = font2js(js, f);
attach_font_texture(js, ret, f);
attach_font_texture(js, &ret, f);
)
// MSDF font constructor: msdf_font(data, em_px, range_px, padding_px, sharpness)
@@ -755,7 +759,7 @@ JSC_CCALL(staef_msdf_font_new,
if (!f) return JS_ThrowReferenceError(js, "could not create MSDF font");
ret = font2js(js, f);
attach_font_texture(js, ret, f);
attach_font_texture(js, &ret, f);
)
// Calculate text size
@@ -841,27 +845,24 @@ static JSValue js_msdf_font_constructor(JSContext *ctx, JSValueConst new_target,
// Initialize the staef module
CELL_USE_INIT(
JSValue mod = JS_NewObject(js);
JSValue proto;
// Initialize font class
JS_NewClassID(&js_font_id);
JS_NewClass(JS_GetRuntime(js), js_font_id, &js_font_class);
proto = JS_NewObject(js);
JS_SetPropertyFunctionList(js, proto, js_font_funcs, countof(js_font_funcs));
JS_SetClassProto(js, js_font_id, proto);
JS_FRAME(js);
JS_ROOT(proto, JS_NewObject(js));
JS_SetPropertyFunctionList(js, proto.val, js_font_funcs, countof(js_font_funcs));
JS_SetClassProto(js, js_font_id, proto.val);
JS_ROOT(mod, JS_NewObject(js));
// Create font constructor (legacy)
JSValue font_ctor = JS_NewCFunction2(js, js_font_constructor, "font", 2, JS_CFUNC_generic, 0);
JS_SetPropertyStr(js, mod, "font", font_ctor);
JS_SetPropertyStr(js, mod.val, "font", JS_NewCFunction2(js, js_font_constructor, "font", 2, JS_CFUNC_generic, 0));
// Create SDF font constructor: sdf_font(data, em_px, range_px, padding_px, sharpness)
JSValue sdf_font_ctor = JS_NewCFunction2(js, js_sdf_font_constructor, "sdf_font", 5, JS_CFUNC_generic, 0);
JS_SetPropertyStr(js, mod, "sdf_font", sdf_font_ctor);
JS_SetPropertyStr(js, mod.val, "sdf_font", JS_NewCFunction2(js, js_sdf_font_constructor, "sdf_font", 5, JS_CFUNC_generic, 0));
// Create MSDF font constructor: msdf_font(data, em_px, range_px, padding_px, sharpness)
JSValue msdf_font_ctor = JS_NewCFunction2(js, js_msdf_font_constructor, "msdf_font", 5, JS_CFUNC_generic, 0);
JS_SetPropertyStr(js, mod, "msdf_font", msdf_font_ctor);
return mod;
JS_SetPropertyStr(js, mod.val, "msdf_font", JS_NewCFunction2(js, js_msdf_font_constructor, "msdf_font", 5, JS_CFUNC_generic, 0));
JS_RETURN(mod.val);
)

View File

@@ -329,9 +329,12 @@ JSC_CCALL(transform_rect,
JSC_CCALL(transform_array,
transform *t = js2transform(js,self);
HMM_Mat4 m= transform2mat(t);
ret = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
for (int i = 0; i < 16; i++)
JS_SetPropertyNumber(js,ret,i, number2js(js,m.em[i]));
JS_SetPropertyNumber(js,arr.val,i, number2js(js,m.em[i]));
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = arr.val;
)
JSC_CCALL(transform_torect,
@@ -341,9 +344,12 @@ JSC_CCALL(transform_torect,
JSC_CCALL(transform_children,
transform *t = js2transform(js,self);
ret = JS_NewArray(js);
JS_FRAME(js);
JS_ROOT(arr, JS_NewArray(js));
for (int i = 0; i < arrlen(t->jschildren); i++)
JS_SetPropertyNumber(js,ret,i,JS_DupValue(js,t->jschildren[i]));
JS_SetPropertyNumber(js,arr.val,i,JS_DupValue(js,t->jschildren[i]));
JS_RestoreFrame(_js_ctx, _js_gc_frame, _js_local_frame);
ret = arr.val;
)
static const JSCFunctionListEntry js_transform_funcs[] = {