Skip to content

Add compileError when a function is used where it is not supported #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ pub const Lua = opaque {
///
/// See https://www.lua.org/manual/5.4/manual.html#lua_atpanic
pub fn atPanic(lua: *Lua, panic_fn: CFn) ?CFn {
if (lang == .luau) @compileError(@src().fn_name ++ " is not implemented in Luau.");

return c.lua_atpanic(@ptrCast(lua), panic_fn);
}

Expand Down Expand Up @@ -964,6 +966,7 @@ pub const Lua = opaque {
pub const dump = switch (lang) {
.lua53, .lua54 => dump53,
else => dump51,
.luau => @compileError("Not implemented in Luau."),
};

/// Returns `true` if the two values in acceptable indices `index1` and `index2` are equal, following the semantics of the Lua `==`
Expand Down Expand Up @@ -1296,11 +1299,13 @@ pub const Lua = opaque {

/// Only available in Luau
pub fn setReadonly(lua: *Lua, idx: i32, enabled: bool) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
c.lua_setreadonly(@ptrCast(lua), idx, @intFromBool(enabled));
}

/// Only available in Luau
pub fn getReadonly(lua: *Lua, idx: i32) bool {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
return c.lua_getreadonly(@ptrCast(lua), idx) != 0;
}

Expand Down Expand Up @@ -1470,6 +1475,7 @@ pub const Lua = opaque {
/// * Errors: `never`
///
pub fn isVector(lua: *Lua, index: i32) bool {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
return c.lua_isvector(@as(*LuaState, @ptrCast(lua)), index);
}

Expand Down Expand Up @@ -1891,6 +1897,7 @@ pub const Lua = opaque {
///
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcclosure
pub fn pushClosureNamed(lua: *Lua, c_fn: CFn, debugname: [:0]const u8, n: i32) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
c.lua_pushcclosurek(@ptrCast(lua), c_fn, debugname, n, null);
}

Expand All @@ -1914,6 +1921,7 @@ pub const Lua = opaque {
///
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushcfunction
pub fn pushFunctionNamed(lua: *Lua, c_fn: CFn, debugname: [:0]const u8) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
c.lua_pushcclosurek(@ptrCast(lua), c_fn, debugname, 0, null);
}

Expand Down Expand Up @@ -2103,7 +2111,10 @@ pub const Lua = opaque {
/// Pushes a floating point 3-vector (or 4-vector if configured) `v` onto the stack
///
/// Only available in Luau
pub const pushVector = if (luau_vector_size == 3) pushVector3 else pushVector4;
pub const pushVector = switch (lang) {
.luau => if (luau_vector_size == 3) pushVector3 else pushVector4,
else => @compileError("pushVector is only available in Luau."),
};

/// Returns true if the two values in indices `index1` and `index2` are primitively equal
/// (that is, equal without calling the `__eq` metamethod). Otherwise returns false.
Expand Down Expand Up @@ -2483,6 +2494,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn setUserdataTag(lua: *Lua, index: i32, tag: i32) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
std.debug.assert((tag >= 0 and tag < c.LUA_UTAG_LIMIT)); // Luau will do the same assert, this is easier to debug
c.lua_setuserdatatag(@ptrCast(lua), index, tag);
}
Expand Down Expand Up @@ -2734,6 +2746,7 @@ pub const Lua = opaque {

/// Only available in Luau
pub fn toUserdataTagged(lua: *Lua, comptime T: type, index: i32, tag: i32) !*T {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
if (c.lua_touserdatatagged(@ptrCast(lua), index, tag)) |ptr| return @ptrCast(@alignCast(ptr));
return error.LuaError;
}
Expand All @@ -2743,6 +2756,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn toVector(lua: *Lua, index: i32) ![luau_vector_size]f32 {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
const res = c.lua_tovector(@ptrCast(lua), index);
if (res) |r| {
switch (luau_vector_size) {
Expand All @@ -2759,6 +2773,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn toStringAtom(lua: *Lua, index: i32) !struct { i32, [:0]const u8 } {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
var atom: c_int = undefined;
if (c.lua_tostringatom(@ptrCast(lua), index, &atom)) |ptr| {
return .{ atom, std.mem.span(ptr) };
Expand All @@ -2771,6 +2786,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn namecallAtom(lua: *Lua) !struct { i32, [:0]const u8 } {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
var atom: c_int = undefined;
if (c.lua_namecallatom(@ptrCast(lua), &atom)) |ptr| {
return .{ atom, std.mem.span(ptr) };
Expand Down Expand Up @@ -3200,13 +3216,15 @@ pub const Lua = opaque {

/// Only available in Luau
pub fn setInterruptCallbackFn(lua: *Lua, cb: ?CInterruptCallbackFn) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
if (c.lua_callbacks(@ptrCast(lua))) |cb_struct| {
cb_struct.*.interrupt = cb;
}
}

/// Only available in Luau
pub fn setUserAtomCallbackFn(lua: *Lua, cb: CUserAtomCallbackFn) void {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
if (c.lua_callbacks(@ptrCast(lua))) |cb_struct| {
cb_struct.*.useratom = cb;
}
Expand Down Expand Up @@ -3471,6 +3489,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn checkVector(lua: *Lua, arg: i32) [luau_vector_size]f32 {
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
const vec = lua.toVector(arg) catch {
lua.typeError(arg, lua.typeName(LuaType.vector));
};
Expand Down Expand Up @@ -3550,7 +3569,7 @@ pub const Lua = opaque {
///
/// Only available in Luau
pub fn raiseInterruptErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
if (lang != .luau) return;
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
c.lua_rawcheckstack(@ptrCast(lua), 1);
lua.raiseErrorStr(fmt, args);
unreachable;
Expand Down Expand Up @@ -3644,6 +3663,7 @@ pub const Lua = opaque {
///
/// See https://www.lua.org/manual/5.4/manual.html#luaL_gsub
pub fn globalSub(lua: *Lua, str: [:0]const u8, pat: [:0]const u8, rep: [:0]const u8) []const u8 {
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
const s = c.luaL_gsub(@ptrCast(lua), str.ptr, pat.ptr, rep.ptr);
const l = lua.rawLen(-1);
return s[0..l];
Expand Down Expand Up @@ -4205,6 +4225,7 @@ pub const Lua = opaque {
/// * Pushes: `0`
/// * Errors: `other`
pub fn openPackage(lua: *Lua) void {
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
lua.requireF(c.LUA_LOADLIBNAME, c.luaopen_package, true);
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
}
Expand Down Expand Up @@ -4259,6 +4280,7 @@ pub const Lua = opaque {
/// * Pushes: `0`
/// * Errors: `other`
pub fn openIO(lua: *Lua) void {
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
lua.requireF(c.LUA_IOLIBNAME, c.luaopen_io, true);
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
}
Expand Down Expand Up @@ -4301,6 +4323,7 @@ pub const Lua = opaque {
/// * Pushes: `0`
/// * Errors: `other`
pub fn openVector(lua: *Lua) void {
if (lang == .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
lua.requireF(c.LUA_VECLIBNAME, c.luaopen_vector, true);
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
}
Expand Down
Loading