Skip to content

Commit 06a83db

Browse files
authored
Use cstr literals where possible (#537)
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
1 parent d6c3bd9 commit 06a83db

File tree

13 files changed

+46
-65
lines changed

13 files changed

+46
-65
lines changed

mozjs/examples/wasm.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,20 @@ fn run(rt: Runtime) {
6363
assert!(JS_GetProperty(
6464
rt.cx(),
6565
global.handle(),
66-
b"WebAssembly\0".as_ptr() as *const c_char,
66+
c"WebAssembly".as_ptr(),
6767
&mut wasm.handle_mut()
6868
));
6969
rooted!(in(rt.cx()) let mut wasm_obj = wasm.to_object());
7070
assert!(JS_GetProperty(
7171
rt.cx(),
7272
wasm_obj.handle(),
73-
b"Module\0".as_ptr() as *const c_char,
73+
c"Module".as_ptr(),
7474
&mut wasm_module.handle_mut()
7575
));
7676
assert!(JS_GetProperty(
7777
rt.cx(),
7878
wasm_obj.handle(),
79-
b"Instance\0".as_ptr() as *const c_char,
79+
c"Instance".as_ptr(),
8080
&mut wasm_instance.handle_mut()
8181
));
8282

@@ -113,7 +113,7 @@ fn run(rt: Runtime) {
113113
let function = JS_DefineFunction(
114114
rt.cx(),
115115
env_import_obj.handle().into(),
116-
b"bar\0".as_ptr() as *const c_char,
116+
c"bar".as_ptr(),
117117
Some(bar),
118118
1,
119119
0,
@@ -126,7 +126,7 @@ fn run(rt: Runtime) {
126126
assert!(JS_SetProperty(
127127
rt.cx(),
128128
imports.handle(),
129-
b"env\0".as_ptr() as *const c_char,
129+
c"env".as_ptr(),
130130
env_import.handle()
131131
));
132132

@@ -146,7 +146,7 @@ fn run(rt: Runtime) {
146146
assert!(JS_GetProperty(
147147
rt.cx(),
148148
instance.handle(),
149-
b"exports\0".as_ptr() as *const c_char,
149+
c"exports".as_ptr(),
150150
&mut exports.handle_mut()
151151
));
152152

@@ -155,7 +155,7 @@ fn run(rt: Runtime) {
155155
assert!(JS_GetProperty(
156156
rt.cx(),
157157
exports_obj.handle(),
158-
b"foo\0".as_ptr() as *const c_char,
158+
c"foo".as_ptr(),
159159
&mut foo.handle_mut()
160160
));
161161

mozjs/src/error.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,24 @@
88

99
use crate::jsapi::{JSContext, JSErrorFormatString, JSExnType, JS_ReportErrorNumberUTF8};
1010
use libc;
11-
use std::ffi::CString;
12-
use std::ptr::addr_of;
11+
use std::ffi::{CStr, CString};
1312
use std::{mem, os, ptr};
1413

1514
/// Format string used to throw javascript errors.
16-
static ERROR_FORMAT_STRING_STRING: [libc::c_char; 4] = [
17-
'{' as libc::c_char,
18-
'0' as libc::c_char,
19-
'}' as libc::c_char,
20-
0 as libc::c_char,
21-
];
15+
static ERROR_FORMAT_STRING_STRING: &CStr = c"{0}";
2216

2317
/// Format string struct used to throw `TypeError`s.
2418
static mut TYPE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
25-
name: b"RUSTMSG_TYPE_ERROR\0" as *const _ as *const libc::c_char,
26-
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
19+
name: c"RUSTMSG_TYPE_ERROR".as_ptr(),
20+
format: ERROR_FORMAT_STRING_STRING.as_ptr(),
2721
argCount: 1,
2822
exnType: JSExnType::JSEXN_TYPEERR as i16,
2923
};
3024

3125
/// Format string struct used to throw `RangeError`s.
3226
static mut RANGE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
33-
name: b"RUSTMSG_RANGE_ERROR\0" as *const _ as *const libc::c_char,
34-
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
27+
name: c"RUSTMSG_RANGE_ERROR".as_ptr(),
28+
format: ERROR_FORMAT_STRING_STRING.as_ptr(),
3529
argCount: 1,
3630
exnType: JSExnType::JSEXN_RANGEERR as i16,
3731
};
@@ -44,8 +38,8 @@ unsafe extern "C" fn get_error_message(
4438
) -> *const JSErrorFormatString {
4539
let num: JSExnType = mem::transmute(error_number);
4640
match num {
47-
JSExnType::JSEXN_TYPEERR => addr_of!(TYPE_ERROR_FORMAT_STRING),
48-
JSExnType::JSEXN_RANGEERR => addr_of!(RANGE_ERROR_FORMAT_STRING),
41+
JSExnType::JSEXN_TYPEERR => &raw const TYPE_ERROR_FORMAT_STRING,
42+
JSExnType::JSEXN_RANGEERR => &raw const RANGE_ERROR_FORMAT_STRING,
4943
_ => panic!(
5044
"Bad js error number given to get_error_message: {}",
5145
error_number

mozjs/src/gc/custom.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::ffi::c_void;
22
use std::ops::{Deref, DerefMut};
33

4-
use crate::c_str;
54
use crate::glue::{CallObjectRootTracer, CallValueRootTracer};
65
use crate::jsapi;
76
use crate::jsapi::{AutoGCRooter, AutoGCRooterKind, JSContext, JSObject, JSTracer, Value};
@@ -18,7 +17,7 @@ unsafe impl CustomTrace for *mut JSObject {
1817
fn trace(&self, trc: *mut JSTracer) {
1918
let this = self as *const *mut _ as *mut *mut _;
2019
unsafe {
21-
CallObjectRootTracer(trc, this, c_str!("object"));
20+
CallObjectRootTracer(trc, this, c"object".as_ptr());
2221
}
2322
}
2423
}
@@ -27,7 +26,7 @@ unsafe impl CustomTrace for Value {
2726
fn trace(&self, trc: *mut JSTracer) {
2827
let this = self as *const _ as *mut _;
2928
unsafe {
30-
CallValueRootTracer(trc, this, c_str!("any"));
29+
CallValueRootTracer(trc, this, c"any".as_ptr());
3130
}
3231
}
3332
}

mozjs/src/gc/macros.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
// Creates a C string literal `$str`.
2-
#[macro_export]
3-
macro_rules! c_str {
4-
($str:expr) => {
5-
concat!($str, "\0").as_ptr() as *const ::std::os::raw::c_char
6-
};
7-
}
8-
91
#[macro_export]
102
macro_rules! rooted {
113
(in($cx:expr) let $($var:ident)+ = $init:expr) => {

mozjs/src/gc/trace.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::c_str;
21
use crate::glue::{
32
CallBigIntTracer, CallFunctionTracer, CallIdTracer, CallObjectTracer, CallScriptTracer,
43
CallStringTracer, CallSymbolTracer, CallValueRootTracer, CallValueTracer,
@@ -51,7 +50,7 @@ unsafe impl Traceable for Heap<*mut JSFunction> {
5150
if self.get().is_null() {
5251
return;
5352
}
54-
CallFunctionTracer(trc, self as *const _ as *mut Self, c_str!("function"));
53+
CallFunctionTracer(trc, self as *const _ as *mut Self, c"function".as_ptr());
5554
}
5655
}
5756

@@ -61,7 +60,7 @@ unsafe impl Traceable for Heap<*mut JSObject> {
6160
if self.get().is_null() {
6261
return;
6362
}
64-
CallObjectTracer(trc, self as *const _ as *mut Self, c_str!("object"));
63+
CallObjectTracer(trc, self as *const _ as *mut Self, c"object".as_ptr());
6564
}
6665
}
6766

@@ -70,7 +69,7 @@ unsafe impl Traceable for Heap<*mut Symbol> {
7069
if self.get().is_null() {
7170
return;
7271
}
73-
CallSymbolTracer(trc, self as *const _ as *mut Self, c_str!("symbol"));
72+
CallSymbolTracer(trc, self as *const _ as *mut Self, c"symbol".as_ptr());
7473
}
7574
}
7675

@@ -79,7 +78,7 @@ unsafe impl Traceable for Heap<*mut BigInt> {
7978
if self.get().is_null() {
8079
return;
8180
}
82-
CallBigIntTracer(trc, self as *const _ as *mut Self, c_str!("bigint"));
81+
CallBigIntTracer(trc, self as *const _ as *mut Self, c"bigint".as_ptr());
8382
}
8483
}
8584

@@ -89,7 +88,7 @@ unsafe impl Traceable for Heap<*mut JSScript> {
8988
if self.get().is_null() {
9089
return;
9190
}
92-
CallScriptTracer(trc, self as *const _ as *mut Self, c_str!("script"));
91+
CallScriptTracer(trc, self as *const _ as *mut Self, c"script".as_ptr());
9392
}
9493
}
9594

@@ -99,28 +98,28 @@ unsafe impl Traceable for Heap<*mut JSString> {
9998
if self.get().is_null() {
10099
return;
101100
}
102-
CallStringTracer(trc, self as *const _ as *mut Self, c_str!("string"));
101+
CallStringTracer(trc, self as *const _ as *mut Self, c"string".as_ptr());
103102
}
104103
}
105104

106105
unsafe impl Traceable for Heap<Value> {
107106
#[inline]
108107
unsafe fn trace(&self, trc: *mut JSTracer) {
109-
CallValueTracer(trc, self as *const _ as *mut Self, c_str!("value"));
108+
CallValueTracer(trc, self as *const _ as *mut Self, c"value".as_ptr());
110109
}
111110
}
112111

113112
unsafe impl Traceable for Value {
114113
#[inline]
115114
unsafe fn trace(&self, trc: *mut JSTracer) {
116-
CallValueRootTracer(trc, self as *const _ as *mut Self, c_str!("value"));
115+
CallValueRootTracer(trc, self as *const _ as *mut Self, c"value".as_ptr());
117116
}
118117
}
119118

120119
unsafe impl Traceable for Heap<jsid> {
121120
#[inline]
122121
unsafe fn trace(&self, trc: *mut JSTracer) {
123-
CallIdTracer(trc, self as *const _ as *mut Self, c_str!("id"));
122+
CallIdTracer(trc, self as *const _ as *mut Self, c"id".as_ptr());
124123
}
125124
}
126125

@@ -131,20 +130,20 @@ unsafe impl Traceable for Heap<PropertyDescriptor> {
131130
CallValueTracer(
132131
trc,
133132
&desc.value_ as *const _ as *mut Heap<Value>,
134-
c_str!("PropertyDescriptor::value"),
133+
c"PropertyDescriptor::value".as_ptr(),
135134
);
136135
if !desc.getter_.is_null() {
137136
CallObjectTracer(
138137
trc,
139138
&desc.getter_ as *const _ as *mut Heap<*mut JSObject>,
140-
c_str!("PropertyDescriptor::getter"),
139+
c"PropertyDescriptor::getter".as_ptr(),
141140
);
142141
}
143142
if !desc.setter_.is_null() {
144143
CallObjectTracer(
145144
trc,
146145
&desc.setter_ as *const _ as *mut Heap<*mut JSObject>,
147-
c_str!("PropertyDescriptor::setter"),
146+
c"PropertyDescriptor::setter".as_ptr(),
148147
);
149148
}
150149
}

mozjs/src/rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ static SIMPLE_GLOBAL_CLASS_OPS: JSClassOps = JSClassOps {
827827

828828
/// This is a simple `JSClass` for global objects, primarily intended for tests.
829829
pub static SIMPLE_GLOBAL_CLASS: JSClass = JSClass {
830-
name: b"Global\0" as *const u8 as *const _,
830+
name: c"Global".as_ptr(),
831831
flags: JSCLASS_IS_GLOBAL
832832
| ((JSCLASS_GLOBAL_SLOT_COUNT & JSCLASS_RESERVED_SLOTS_MASK)
833833
<< JSCLASS_RESERVED_SLOTS_SHIFT),

mozjs/tests/callback.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn callback() {
3838
let function = JS_DefineFunction(
3939
context,
4040
global.handle().into(),
41-
b"puts\0".as_ptr() as *const libc::c_char,
41+
c"puts".as_ptr(),
4242
Some(puts),
4343
1,
4444
0,
@@ -57,10 +57,7 @@ unsafe extern "C" fn puts(context: *mut JSContext, argc: u32, vp: *mut Value) ->
5757
let args = CallArgs::from_vp(vp, argc);
5858

5959
if args.argc_ != 1 {
60-
JS_ReportErrorASCII(
61-
context,
62-
b"puts() requires exactly 1 argument\0".as_ptr() as *const libc::c_char,
63-
);
60+
JS_ReportErrorASCII(context, c"puts() requires exactly 1 argument".as_ptr());
6461
return false;
6562
}
6663

mozjs/tests/capture_stack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn capture_stack() {
3636
let function = JS_DefineFunction(
3737
context,
3838
global.handle().into(),
39-
b"print_stack\0".as_ptr() as *const libc::c_char,
39+
c"print_stack".as_ptr(),
4040
Some(print_stack),
4141
0,
4242
0,

mozjs/tests/enumerate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn enumerate() {
6464
assert!(JS_StringEqualsAscii(
6565
context,
6666
id.get(),
67-
b"a\0" as *const _ as *const _,
67+
c"a".as_ptr(),
6868
&mut matches
6969
));
7070
assert!(matches);

mozjs/tests/panic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_panic() {
3737
let function = JS_DefineFunction(
3838
context,
3939
global.handle().into(),
40-
b"test\0".as_ptr() as *const _,
40+
c"test".as_ptr(),
4141
Some(test),
4242
0,
4343
0,

0 commit comments

Comments
 (0)