luau-lang/luau

Vector XYZW fast path not supported for vectors within immutable globals

Sleitnick opened this issue · 1 comments

The XYZW properties for Luau vectors are supported through the GETTABLEKS opcode, but not through the GETIMPORT opcode. Thus, access to the XYZW properties directly through immutable globals does not work.

Consider the global table foo with a single vector field vec:

// Create global "foo" with "vec" vector:
lua_newtable(L);
lua_pushvector(L, 0, 0, 0);
lua_setfield(L, -2, "vec");
lua_setglobal(L, "foo");
-- Error accessing "x" (or "X", or other properties)
print(foo.vec.x) -- error (.x goes through GETIMPORT opcode, which does not support the XYZW property fast path)

-- Works if the access to the vector is wrapped in parentheses:
print((foo.vec).x) -- ok

-- Works if the vector is first stored in a variable:
local v = foo.vec
print(v.x) -- ok

Workarounds:

  • Add the global to the mutableGlobals compilation option (not ideal).
  • Add XYZW fields as needed to a custom vector index metamethod.

I was running this in v0.609.

Second workaround is correct, you are supposed to setup the vector type metatable.

For an example, see this discussion #1298 (comment)