Skip to content

Commit 3d2574a

Browse files
committed
Revert "Use c string literal where appropriate"
This reverts commit 46e949c.
1 parent 003935d commit 3d2574a

File tree

13 files changed

+55
-48
lines changed

13 files changed

+55
-48
lines changed

mlua-sys/src/lua51/compat.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) ->
9090
} else if compat53_findfield(L, objidx, level - 1) != 0 {
9191
// try recursively
9292
lua_remove(L, -2); // remove table (but keep name)
93-
lua_pushliteral(L, c".");
93+
lua_pushliteral(L, ".");
9494
lua_insert(L, -2); // place '.' between the two names
9595
lua_concat(L, 3);
9696
return 1;
@@ -121,13 +121,13 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, ar: *mut lua_Debug) {
121121
lua_pushfstring(L, cstr!("function '%s'"), (*ar).name);
122122
} else if *(*ar).what == b'm' as c_char {
123123
// main?
124-
lua_pushliteral(L, c"main chunk");
124+
lua_pushliteral(L, "main chunk");
125125
} else if *(*ar).what == b'C' as c_char {
126126
if compat53_pushglobalfuncname(L, ar) != 0 {
127127
lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1));
128128
lua_remove(L, -2); // remove name
129129
} else {
130-
lua_pushliteral(L, c"?");
130+
lua_pushliteral(L, "?");
131131
}
132132
} else {
133133
lua_pushfstring(
@@ -377,7 +377,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
377377
if !msg.is_null() {
378378
luaL_error(L, cstr!("stack overflow (%s)"), msg);
379379
} else {
380-
lua_pushliteral(L, c"stack overflow");
380+
lua_pushliteral(L, "stack overflow");
381381
lua_error(L);
382382
}
383383
}
@@ -467,20 +467,20 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const
467467
if !msg.is_null() {
468468
lua_pushfstring(L, cstr!("%s\n"), msg);
469469
}
470-
lua_pushliteral(L, c"stack traceback:");
470+
lua_pushliteral(L, "stack traceback:");
471471
while lua_getstack(L1, level, &mut ar) != 0 {
472472
level += 1;
473473
if level == mark {
474474
// too many levels?
475-
lua_pushliteral(L, c"\n\t..."); // add a '...'
475+
lua_pushliteral(L, "\n\t..."); // add a '...'
476476
level = numlevels - COMPAT53_LEVELS2; // and skip to last ones
477477
} else {
478478
lua_getinfo(L1, cstr!("Slnt"), &mut ar);
479479
lua_pushfstring(L, cstr!("\n\t%s:"), ar.short_src.as_ptr());
480480
if ar.currentline > 0 {
481481
lua_pushfstring(L, cstr!("%d:"), ar.currentline);
482482
}
483-
lua_pushliteral(L, c" in ");
483+
lua_pushliteral(L, " in ");
484484
compat53_pushfuncname(L, &mut ar);
485485
lua_concat(L, lua_gettop(L) - top);
486486
}
@@ -493,16 +493,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
493493
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
494494
match lua_type(L, idx) {
495495
LUA_TNIL => {
496-
lua_pushliteral(L, c"nil");
496+
lua_pushliteral(L, "nil");
497497
}
498498
LUA_TSTRING | LUA_TNUMBER => {
499499
lua_pushvalue(L, idx);
500500
}
501501
LUA_TBOOLEAN => {
502502
if lua_toboolean(L, idx) == 0 {
503-
lua_pushliteral(L, c"false");
503+
lua_pushliteral(L, "false");
504504
} else {
505-
lua_pushliteral(L, c"true");
505+
lua_pushliteral(L, "true");
506506
}
507507
}
508508
t => {

mlua-sys/src/lua51/lua.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains definitions from `lua.h`.
22
3-
use std::ffi::CStr;
43
use std::marker::{PhantomData, PhantomPinned};
54
use std::os::raw::{c_char, c_double, c_int, c_void};
65
use std::ptr;
@@ -313,8 +312,10 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
313312
}
314313

315314
#[inline(always)]
316-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
317-
lua_pushstring_(L, s.as_ptr());
315+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
316+
use std::ffi::CString;
317+
let c_str = CString::new(s).unwrap();
318+
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
318319
}
319320

320321
#[inline(always)]

mlua-sys/src/lua52/compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
199199
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
200200
match lua_type(L, idx) {
201201
LUA_TNIL => {
202-
lua_pushliteral(L, c"nil");
202+
lua_pushliteral(L, "nil");
203203
}
204204
LUA_TSTRING | LUA_TNUMBER => {
205205
lua_pushvalue(L, idx);
206206
}
207207
LUA_TBOOLEAN => {
208208
if lua_toboolean(L, idx) == 0 {
209-
lua_pushliteral(L, c"false");
209+
lua_pushliteral(L, "false");
210210
} else {
211-
lua_pushliteral(L, c"true");
211+
lua_pushliteral(L, "true");
212212
}
213213
}
214214
t => {

mlua-sys/src/lua52/lua.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains definitions from `lua.h`.
22
3-
use std::ffi::CStr;
43
use std::marker::{PhantomData, PhantomPinned};
54
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_uint, c_void};
65
use std::ptr;
@@ -396,8 +395,10 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
396395
}
397396

398397
#[inline(always)]
399-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
400-
lua_pushstring(L, s.as_ptr());
398+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
399+
use std::ffi::CString;
400+
let c_str = CString::new(s).unwrap();
401+
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
401402
}
402403

403404
#[inline(always)]

mlua-sys/src/lua53/lua.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains definitions from `lua.h`.
22
3-
use std::ffi::CStr;
43
use std::marker::{PhantomData, PhantomPinned};
54
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_void};
65
use std::{mem, ptr};
@@ -408,8 +407,10 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
408407
}
409408

410409
#[inline(always)]
411-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
412-
lua_pushstring(L, s.as_ptr());
410+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
411+
use std::ffi::CString;
412+
let c_str = CString::new(s).unwrap();
413+
lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len())
413414
}
414415

415416
#[inline(always)]

mlua-sys/src/lua54/lua.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains definitions from `lua.h`.
22
3-
use std::ffi::CStr;
43
use std::marker::{PhantomData, PhantomPinned};
54
use std::os::raw::{c_char, c_double, c_int, c_uchar, c_ushort, c_void};
65
use std::{mem, ptr};
@@ -435,8 +434,10 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
435434
}
436435

437436
#[inline(always)]
438-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
439-
lua_pushstring(L, s.as_ptr());
437+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) -> *const c_char {
438+
use std::ffi::CString;
439+
let c_str = CString::new(s).unwrap();
440+
lua_pushlstring(L, c_str.as_ptr(), c_str.as_bytes().len())
440441
}
441442

442443
#[inline(always)]

mlua-sys/src/luau/compat.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ unsafe fn compat53_findfield(L: *mut lua_State, objidx: c_int, level: c_int) ->
4343
} else if compat53_findfield(L, objidx, level - 1) != 0 {
4444
// try recursively
4545
lua_remove(L, -2); // remove table (but keep name)
46-
lua_pushliteral(L, c".");
46+
lua_pushliteral(L, ".");
4747
lua_insert(L, -2); // place '.' between the two names
4848
lua_concat(L, 3);
4949
return 1;
@@ -77,7 +77,7 @@ unsafe fn compat53_pushfuncname(L: *mut lua_State, level: c_int, ar: *mut lua_De
7777
lua_pushfstring(L, cstr!("function '%s'"), lua_tostring(L, -1));
7878
lua_remove(L, -2); // remove name
7979
} else {
80-
lua_pushliteral(L, c"?");
80+
lua_pushliteral(L, "?");
8181
}
8282
}
8383

@@ -192,7 +192,7 @@ pub unsafe fn lua_rawgetp(L: *mut lua_State, idx: c_int, p: *const c_void) -> c_
192192
pub unsafe fn lua_getuservalue(L: *mut lua_State, mut idx: c_int) -> c_int {
193193
luaL_checkstack(L, 2, cstr!("not enough stack slots available"));
194194
idx = lua_absindex(L, idx);
195-
lua_pushliteral(L, c"__mlua_uservalues");
195+
lua_pushliteral(L, "__mlua_uservalues");
196196
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
197197
return LUA_TNIL;
198198
}
@@ -229,13 +229,13 @@ pub unsafe fn lua_rawsetp(L: *mut lua_State, idx: c_int, p: *const c_void) {
229229
pub unsafe fn lua_setuservalue(L: *mut lua_State, mut idx: c_int) {
230230
luaL_checkstack(L, 4, cstr!("not enough stack slots available"));
231231
idx = lua_absindex(L, idx);
232-
lua_pushliteral(L, c"__mlua_uservalues");
232+
lua_pushliteral(L, "__mlua_uservalues");
233233
lua_pushvalue(L, -1);
234234
if lua_rawget(L, LUA_REGISTRYINDEX) != LUA_TTABLE {
235235
lua_pop(L, 1);
236236
lua_createtable(L, 0, 2); // main table
237237
lua_createtable(L, 0, 1); // metatable
238-
lua_pushliteral(L, c"k");
238+
lua_pushliteral(L, "k");
239239
lua_setfield(L, -2, cstr!("__mode"));
240240
lua_setmetatable(L, -2);
241241
lua_pushvalue(L, -2);
@@ -309,7 +309,7 @@ pub unsafe fn luaL_checkstack(L: *mut lua_State, sz: c_int, msg: *const c_char)
309309
if !msg.is_null() {
310310
luaL_error(L, cstr!("stack overflow (%s)"), msg);
311311
} else {
312-
lua_pushliteral(L, c"stack overflow");
312+
lua_pushliteral(L, "stack overflow");
313313
lua_error(L);
314314
}
315315
}
@@ -430,19 +430,19 @@ pub unsafe fn luaL_traceback(L: *mut lua_State, L1: *mut lua_State, msg: *const
430430
if !msg.is_null() {
431431
lua_pushfstring(L, cstr!("%s\n"), msg);
432432
}
433-
lua_pushliteral(L, c"stack traceback:");
433+
lua_pushliteral(L, "stack traceback:");
434434
while lua_getinfo(L1, level, cstr!(""), &mut ar) != 0 {
435435
if level + 1 == mark {
436436
// too many levels?
437-
lua_pushliteral(L, c"\n\t..."); // add a '...'
437+
lua_pushliteral(L, "\n\t..."); // add a '...'
438438
level = numlevels - COMPAT53_LEVELS2; // and skip to last ones
439439
} else {
440440
lua_getinfo(L1, level, cstr!("sln"), &mut ar);
441441
lua_pushfstring(L, cstr!("\n\t%s:"), ar.short_src);
442442
if ar.currentline > 0 {
443443
lua_pushfstring(L, cstr!("%d:"), ar.currentline);
444444
}
445-
lua_pushliteral(L, c" in ");
445+
lua_pushliteral(L, " in ");
446446
compat53_pushfuncname(L, level, &mut ar);
447447
lua_concat(L, lua_gettop(L) - top);
448448
}
@@ -456,16 +456,16 @@ pub unsafe fn luaL_tolstring(L: *mut lua_State, mut idx: c_int, len: *mut usize)
456456
if luaL_callmeta(L, idx, cstr!("__tostring")) == 0 {
457457
match lua_type(L, idx) {
458458
LUA_TNIL => {
459-
lua_pushliteral(L, c"nil");
459+
lua_pushliteral(L, "nil");
460460
}
461461
LUA_TSTRING | LUA_TNUMBER => {
462462
lua_pushvalue(L, idx);
463463
}
464464
LUA_TBOOLEAN => {
465465
if lua_toboolean(L, idx) == 0 {
466-
lua_pushliteral(L, c"false");
466+
lua_pushliteral(L, "false");
467467
} else {
468-
lua_pushliteral(L, c"true");
468+
lua_pushliteral(L, "true");
469469
}
470470
}
471471
t => {

mlua-sys/src/luau/lauxlib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub unsafe fn luaL_sandbox(L: *mut lua_State, enabled: c_int) {
148148
}
149149

150150
// set all builtin metatables to read-only
151-
lua_pushliteral(L, c"");
151+
lua_pushliteral(L, "");
152152
if lua_getmetatable(L, -1) != 0 {
153153
lua_setreadonly(L, -1, enabled);
154154
lua_pop(L, 2);

mlua-sys/src/luau/lua.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Contains definitions from `lua.h`.
22
3-
use std::ffi::CStr;
43
use std::marker::{PhantomData, PhantomPinned};
54
use std::os::raw::{c_char, c_double, c_float, c_int, c_uint, c_void};
65
use std::{mem, ptr};
@@ -403,8 +402,10 @@ pub unsafe fn lua_isnoneornil(L: *mut lua_State, n: c_int) -> c_int {
403402
}
404403

405404
#[inline(always)]
406-
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static CStr) {
407-
lua_pushstring_(L, s.as_ptr());
405+
pub unsafe fn lua_pushliteral(L: *mut lua_State, s: &'static str) {
406+
use std::ffi::CString;
407+
let c_str = CString::new(s).unwrap();
408+
lua_pushlstring_(L, c_str.as_ptr(), c_str.as_bytes().len())
408409
}
409410

410411
#[inline(always)]

src/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl Function {
280280
// Traverse upvalues until we find the _ENV one
281281
match ffi::lua_getupvalue(state, -1, i) {
282282
s if s.is_null() => break,
283-
s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => break,
283+
s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => break,
284284
_ => ffi::lua_pop(state, 1),
285285
}
286286
}
@@ -319,7 +319,7 @@ impl Function {
319319
for i in 1..=255 {
320320
match ffi::lua_getupvalue(state, -1, i) {
321321
s if s.is_null() => return Ok(false),
322-
s if std::ffi::CStr::from_ptr(s as _) == c"_ENV" => {
322+
s if std::ffi::CStr::from_ptr(s as _).to_bytes() == b"_ENV" => {
323323
ffi::lua_pop(state, 1);
324324
// Create an anonymous function with the new environment
325325
let f_with_env = lua

0 commit comments

Comments
 (0)