layout uses x,y

This commit is contained in:
2025-07-06 20:35:15 -05:00
parent d4f0059419
commit 71c0056df4
5 changed files with 134 additions and 61 deletions

View File

@@ -218,8 +218,6 @@ JSValue __##PROP##__v = JS_GetPropertyStr(JS,VALUE,#ATOM); \
TARGET = js2##TYPE(JS, __##PROP##__v); \
JS_FreeValue(JS,__##PROP##__v); }\
#define JS_SETATOM(JS, TARGET, ATOM, VALUE, TYPE) JS_SetProperty(JS, TARGET, #ATOM, TYPE##2js(JS, VALUE));
int JS_GETBOOL(JSContext *js, JSValue v, const char *prop)
{
JSValue __v = JS_GetPropertyStr(js,v,prop);
@@ -429,8 +427,23 @@ JSValue color2js(JSContext *js, colorf color)
HMM_Vec2 js2vec2(JSContext *js,JSValue v)
{
HMM_Vec2 v2;
v2.X = js_getnum_uint32(js,v,0);
v2.Y = js_getnum_uint32(js,v,1);
// Check if it's an array
if (JS_IsArray(js, v)) {
v2.X = js_getnum_uint32(js,v,0);
v2.Y = js_getnum_uint32(js,v,1);
} else {
// Try to get x,y properties from object
JSValue x_val = JS_GetPropertyStr(js, v, "x");
JSValue y_val = JS_GetPropertyStr(js, v, "y");
v2.X = js2number(js, x_val);
v2.Y = js2number(js, y_val);
JS_FreeValue(js, x_val);
JS_FreeValue(js, y_val);
}
return v2;
}
@@ -609,10 +622,10 @@ HMM_Vec2 transform_point(SDL_Renderer *ren, HMM_Vec2 in, HMM_Mat3 *t)
JSValue rect2js(JSContext *js,rect rect) {
JSValue obj = JS_NewObject(js);
JS_SETATOM(js, obj, x, rect.x, number);
JS_SETATOM(js, obj, y, rect.y, number);
JS_SETATOM(js, obj, width, rect.w, number);
JS_SETATOM(js, obj, height, rect.h, number);
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;
}

View File

@@ -36,16 +36,35 @@ static JSValue js_layout_set_size(JSContext *js, JSValueConst self, int argc, JS
GETLAY
GETITEM(id, argv[0])
double size[2];
JSValue width_val = JS_GetPropertyUint32(js, argv[1], 0);
JSValue height_val = JS_GetPropertyUint32(js, argv[1], 1);
JS_ToFloat64(js, &size[0], width_val);
JS_ToFloat64(js, &size[1], height_val);
JS_FreeValue(js, width_val);
JS_FreeValue(js, height_val);
if (isnan(size[0])) size[0] = 0;
if (isnan(size[1])) size[1] = 0;
lay_set_size_xy(lay, id, size[0], size[1]);
double width = 0, height = 0;
// Check if it's an array (for backwards compatibility)
if (JS_IsArray(js, argv[1])) {
JSValue width_val = JS_GetPropertyUint32(js, argv[1], 0);
JSValue height_val = JS_GetPropertyUint32(js, argv[1], 1);
JS_ToFloat64(js, &width, width_val);
JS_ToFloat64(js, &height, height_val);
JS_FreeValue(js, width_val);
JS_FreeValue(js, height_val);
} else {
// Handle object with x,y or width,height properties
JSValue width_val = JS_GetPropertyStr(js, argv[1], "width");
if (JS_IsNull(width_val)) {
width_val = JS_GetPropertyStr(js, argv[1], "x");
}
JSValue height_val = JS_GetPropertyStr(js, argv[1], "height");
if (JS_IsNull(height_val)) {
height_val = JS_GetPropertyStr(js, argv[1], "y");
}
JS_ToFloat64(js, &width, width_val);
JS_ToFloat64(js, &height, height_val);
JS_FreeValue(js, width_val);
JS_FreeValue(js, height_val);
}
if (isnan(width)) width = 0;
if (isnan(height)) height = 0;
lay_set_size_xy(lay, id, width, height);
return JS_NULL;
}

View File

@@ -7129,11 +7129,14 @@ static JSValue JS_GetPropertyValue(JSContext *ctx, JSValueConst this_obj,
} else {
slow_path:
/* ToObject() must be done before ToPropertyKey() */
atom = JS_ValueToAtom(ctx, prop);
if (JS_IsNull(this_obj)) {
JS_FreeValue(ctx, prop);
return JS_ThrowTypeError(ctx, "cannot read property of null");
JSValue ret = JS_ThrowTypeErrorAtom(ctx, "cannot read property '%s' of null", atom);
JS_FreeAtom(ctx, atom);
return ret;
}
atom = JS_ValueToAtom(ctx, prop);
JS_FreeValue(ctx, prop);
if (unlikely(atom == JS_ATOM_NULL))
return JS_EXCEPTION;