#anyopaque alignment change

1 messages · Page 1 of 1 (latest)

iron flicker
#

Is it possible to do something similar to what I'm attempting here?

fn JS_VALUE_GET_OBJ(v: JSValue) *JSObject {
    return @ptrCast(*JSObject, JS_VALUE_GET_PTR(v));
}

fn JS_VALUE_GET_PTR(v: JSValue) *anyopaque {
    return v.u.ptr;
}

I get this error:

src/main.zig:95:12: error: cast increases pointer alignment
    return @ptrCast(*JSObject, JS_VALUE_GET_PTR(v));
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.zig:95:48: note: '*anyopaque' has alignment '1'
    return @ptrCast(*JSObject, JS_VALUE_GET_PTR(v));
                               ~~~~~~~~~~~~~~~~^~~
src/main.zig:95:21: note: '*main.JSObject' has alignment '8'
    return @ptrCast(*JSObject, JS_VALUE_GET_PTR(v));
                    ^~~~~~~~~

I guess generally I'm curious how you cast *anyopaque to a pointer of another type

#

awkward timing... boarding a plane shortly, but will try to look at this from the flight if possible

fresh iris
#

you need to give an alignment guarantee to the compiler

#

@ptrCast(*JSObject, @alignCast(@alignOf(JSObject), JS_VALUE_GET_PTR(v)))

willow acorn
#

if you know the pointer is always aligned, you can use *align(@alignOf(JSObject)) anyopaque