Skip to content

Commit eb35edd

Browse files
committed
Add compileError when a function is used where it is not supported
1 parent 73b596b commit eb35edd

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/lib.zig

+25-2
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,8 @@ pub const Lua = opaque {
727727
///
728728
/// See https://www.lua.org/manual/5.4/manual.html#lua_atpanic
729729
pub fn atPanic(lua: *Lua, panic_fn: CFn) ?CFn {
730+
if (lang == .luau) @compileError(@src().fn_name ++ " is not implemented in Luau.");
731+
730732
return c.lua_atpanic(@ptrCast(lua), panic_fn);
731733
}
732734

@@ -964,6 +966,7 @@ pub const Lua = opaque {
964966
pub const dump = switch (lang) {
965967
.lua53, .lua54 => dump53,
966968
else => dump51,
969+
.luau => @compileError("Not implemented in Luau."),
967970
};
968971

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

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

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

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

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

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

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

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

27352747
/// Only available in Luau
27362748
pub fn toUserdataTagged(lua: *Lua, comptime T: type, index: i32, tag: i32) !*T {
2749+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
27372750
if (c.lua_touserdatatagged(@ptrCast(lua), index, tag)) |ptr| return @ptrCast(@alignCast(ptr));
27382751
return error.LuaError;
27392752
}
@@ -2743,6 +2756,7 @@ pub const Lua = opaque {
27432756
///
27442757
/// Only available in Luau
27452758
pub fn toVector(lua: *Lua, index: i32) ![luau_vector_size]f32 {
2759+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
27462760
const res = c.lua_tovector(@ptrCast(lua), index);
27472761
if (res) |r| {
27482762
switch (luau_vector_size) {
@@ -2759,6 +2773,7 @@ pub const Lua = opaque {
27592773
///
27602774
/// Only available in Luau
27612775
pub fn toStringAtom(lua: *Lua, index: i32) !struct { i32, [:0]const u8 } {
2776+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
27622777
var atom: c_int = undefined;
27632778
if (c.lua_tostringatom(@ptrCast(lua), index, &atom)) |ptr| {
27642779
return .{ atom, std.mem.span(ptr) };
@@ -2771,6 +2786,7 @@ pub const Lua = opaque {
27712786
///
27722787
/// Only available in Luau
27732788
pub fn namecallAtom(lua: *Lua) !struct { i32, [:0]const u8 } {
2789+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
27742790
var atom: c_int = undefined;
27752791
if (c.lua_namecallatom(@ptrCast(lua), &atom)) |ptr| {
27762792
return .{ atom, std.mem.span(ptr) };
@@ -3200,13 +3216,15 @@ pub const Lua = opaque {
32003216

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

32083225
/// Only available in Luau
32093226
pub fn setUserAtomCallbackFn(lua: *Lua, cb: CUserAtomCallbackFn) void {
3227+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
32103228
if (c.lua_callbacks(@ptrCast(lua))) |cb_struct| {
32113229
cb_struct.*.useratom = cb;
32123230
}
@@ -3471,6 +3489,7 @@ pub const Lua = opaque {
34713489
///
34723490
/// Only available in Luau
34733491
pub fn checkVector(lua: *Lua, arg: i32) [luau_vector_size]f32 {
3492+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
34743493
const vec = lua.toVector(arg) catch {
34753494
lua.typeError(arg, lua.typeName(LuaType.vector));
34763495
};
@@ -3550,7 +3569,7 @@ pub const Lua = opaque {
35503569
///
35513570
/// Only available in Luau
35523571
pub fn raiseInterruptErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
3553-
if (lang != .luau) return;
3572+
if (lang != .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
35543573
c.lua_rawcheckstack(@ptrCast(lua), 1);
35553574
lua.raiseErrorStr(fmt, args);
35563575
unreachable;
@@ -3644,6 +3663,7 @@ pub const Lua = opaque {
36443663
///
36453664
/// See https://www.lua.org/manual/5.4/manual.html#luaL_gsub
36463665
pub fn globalSub(lua: *Lua, str: [:0]const u8, pat: [:0]const u8, rep: [:0]const u8) []const u8 {
3666+
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
36473667
const s = c.luaL_gsub(@ptrCast(lua), str.ptr, pat.ptr, rep.ptr);
36483668
const l = lua.rawLen(-1);
36493669
return s[0..l];
@@ -4205,6 +4225,7 @@ pub const Lua = opaque {
42054225
/// * Pushes: `0`
42064226
/// * Errors: `other`
42074227
pub fn openPackage(lua: *Lua) void {
4228+
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
42084229
lua.requireF(c.LUA_LOADLIBNAME, c.luaopen_package, true);
42094230
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
42104231
}
@@ -4259,6 +4280,7 @@ pub const Lua = opaque {
42594280
/// * Pushes: `0`
42604281
/// * Errors: `other`
42614282
pub fn openIO(lua: *Lua) void {
4283+
if (lang == .luau) @compileError(@src().fn_name ++ " is not available in Luau.");
42624284
lua.requireF(c.LUA_IOLIBNAME, c.luaopen_io, true);
42634285
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
42644286
}
@@ -4301,6 +4323,7 @@ pub const Lua = opaque {
43014323
/// * Pushes: `0`
43024324
/// * Errors: `other`
43034325
pub fn openVector(lua: *Lua) void {
4326+
if (lang == .luau) @compileError(@src().fn_name ++ " is only available in Luau.");
43044327
lua.requireF(c.LUA_VECLIBNAME, c.luaopen_vector, true);
43054328
if (lang == .lua52 or lang == .lua53 or lang == .lua54) lua.pop(1);
43064329
}

0 commit comments

Comments
 (0)