Skip to content

Commit

Permalink
Use cstr literals where possible (#537)
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Schwender <[email protected]>
  • Loading branch information
jschwe authored Dec 14, 2024
1 parent d6c3bd9 commit 06a83db
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 65 deletions.
14 changes: 7 additions & 7 deletions mozjs/examples/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ fn run(rt: Runtime) {
assert!(JS_GetProperty(
rt.cx(),
global.handle(),
b"WebAssembly\0".as_ptr() as *const c_char,
c"WebAssembly".as_ptr(),
&mut wasm.handle_mut()
));
rooted!(in(rt.cx()) let mut wasm_obj = wasm.to_object());
assert!(JS_GetProperty(
rt.cx(),
wasm_obj.handle(),
b"Module\0".as_ptr() as *const c_char,
c"Module".as_ptr(),
&mut wasm_module.handle_mut()
));
assert!(JS_GetProperty(
rt.cx(),
wasm_obj.handle(),
b"Instance\0".as_ptr() as *const c_char,
c"Instance".as_ptr(),
&mut wasm_instance.handle_mut()
));

Expand Down Expand Up @@ -113,7 +113,7 @@ fn run(rt: Runtime) {
let function = JS_DefineFunction(
rt.cx(),
env_import_obj.handle().into(),
b"bar\0".as_ptr() as *const c_char,
c"bar".as_ptr(),
Some(bar),
1,
0,
Expand All @@ -126,7 +126,7 @@ fn run(rt: Runtime) {
assert!(JS_SetProperty(
rt.cx(),
imports.handle(),
b"env\0".as_ptr() as *const c_char,
c"env".as_ptr(),
env_import.handle()
));

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

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

Expand Down
22 changes: 8 additions & 14 deletions mozjs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@

use crate::jsapi::{JSContext, JSErrorFormatString, JSExnType, JS_ReportErrorNumberUTF8};
use libc;
use std::ffi::CString;
use std::ptr::addr_of;
use std::ffi::{CStr, CString};
use std::{mem, os, ptr};

/// Format string used to throw javascript errors.
static ERROR_FORMAT_STRING_STRING: [libc::c_char; 4] = [
'{' as libc::c_char,
'0' as libc::c_char,
'}' as libc::c_char,
0 as libc::c_char,
];
static ERROR_FORMAT_STRING_STRING: &CStr = c"{0}";

/// Format string struct used to throw `TypeError`s.
static mut TYPE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
name: b"RUSTMSG_TYPE_ERROR\0" as *const _ as *const libc::c_char,
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
name: c"RUSTMSG_TYPE_ERROR".as_ptr(),
format: ERROR_FORMAT_STRING_STRING.as_ptr(),
argCount: 1,
exnType: JSExnType::JSEXN_TYPEERR as i16,
};

/// Format string struct used to throw `RangeError`s.
static mut RANGE_ERROR_FORMAT_STRING: JSErrorFormatString = JSErrorFormatString {
name: b"RUSTMSG_RANGE_ERROR\0" as *const _ as *const libc::c_char,
format: &ERROR_FORMAT_STRING_STRING as *const libc::c_char,
name: c"RUSTMSG_RANGE_ERROR".as_ptr(),
format: ERROR_FORMAT_STRING_STRING.as_ptr(),
argCount: 1,
exnType: JSExnType::JSEXN_RANGEERR as i16,
};
Expand All @@ -44,8 +38,8 @@ unsafe extern "C" fn get_error_message(
) -> *const JSErrorFormatString {
let num: JSExnType = mem::transmute(error_number);
match num {
JSExnType::JSEXN_TYPEERR => addr_of!(TYPE_ERROR_FORMAT_STRING),
JSExnType::JSEXN_RANGEERR => addr_of!(RANGE_ERROR_FORMAT_STRING),
JSExnType::JSEXN_TYPEERR => &raw const TYPE_ERROR_FORMAT_STRING,
JSExnType::JSEXN_RANGEERR => &raw const RANGE_ERROR_FORMAT_STRING,
_ => panic!(
"Bad js error number given to get_error_message: {}",
error_number
Expand Down
5 changes: 2 additions & 3 deletions mozjs/src/gc/custom.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ffi::c_void;
use std::ops::{Deref, DerefMut};

use crate::c_str;
use crate::glue::{CallObjectRootTracer, CallValueRootTracer};
use crate::jsapi;
use crate::jsapi::{AutoGCRooter, AutoGCRooterKind, JSContext, JSObject, JSTracer, Value};
Expand All @@ -18,7 +17,7 @@ unsafe impl CustomTrace for *mut JSObject {
fn trace(&self, trc: *mut JSTracer) {
let this = self as *const *mut _ as *mut *mut _;
unsafe {
CallObjectRootTracer(trc, this, c_str!("object"));
CallObjectRootTracer(trc, this, c"object".as_ptr());
}
}
}
Expand All @@ -27,7 +26,7 @@ unsafe impl CustomTrace for Value {
fn trace(&self, trc: *mut JSTracer) {
let this = self as *const _ as *mut _;
unsafe {
CallValueRootTracer(trc, this, c_str!("any"));
CallValueRootTracer(trc, this, c"any".as_ptr());
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions mozjs/src/gc/macros.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
// Creates a C string literal `$str`.
#[macro_export]
macro_rules! c_str {
($str:expr) => {
concat!($str, "\0").as_ptr() as *const ::std::os::raw::c_char
};
}

#[macro_export]
macro_rules! rooted {
(in($cx:expr) let $($var:ident)+ = $init:expr) => {
Expand Down
25 changes: 12 additions & 13 deletions mozjs/src/gc/trace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::c_str;
use crate::glue::{
CallBigIntTracer, CallFunctionTracer, CallIdTracer, CallObjectTracer, CallScriptTracer,
CallStringTracer, CallSymbolTracer, CallValueRootTracer, CallValueTracer,
Expand Down Expand Up @@ -51,7 +50,7 @@ unsafe impl Traceable for Heap<*mut JSFunction> {
if self.get().is_null() {
return;
}
CallFunctionTracer(trc, self as *const _ as *mut Self, c_str!("function"));
CallFunctionTracer(trc, self as *const _ as *mut Self, c"function".as_ptr());
}
}

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

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

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

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

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

unsafe impl Traceable for Heap<Value> {
#[inline]
unsafe fn trace(&self, trc: *mut JSTracer) {
CallValueTracer(trc, self as *const _ as *mut Self, c_str!("value"));
CallValueTracer(trc, self as *const _ as *mut Self, c"value".as_ptr());
}
}

unsafe impl Traceable for Value {
#[inline]
unsafe fn trace(&self, trc: *mut JSTracer) {
CallValueRootTracer(trc, self as *const _ as *mut Self, c_str!("value"));
CallValueRootTracer(trc, self as *const _ as *mut Self, c"value".as_ptr());
}
}

unsafe impl Traceable for Heap<jsid> {
#[inline]
unsafe fn trace(&self, trc: *mut JSTracer) {
CallIdTracer(trc, self as *const _ as *mut Self, c_str!("id"));
CallIdTracer(trc, self as *const _ as *mut Self, c"id".as_ptr());
}
}

Expand All @@ -131,20 +130,20 @@ unsafe impl Traceable for Heap<PropertyDescriptor> {
CallValueTracer(
trc,
&desc.value_ as *const _ as *mut Heap<Value>,
c_str!("PropertyDescriptor::value"),
c"PropertyDescriptor::value".as_ptr(),
);
if !desc.getter_.is_null() {
CallObjectTracer(
trc,
&desc.getter_ as *const _ as *mut Heap<*mut JSObject>,
c_str!("PropertyDescriptor::getter"),
c"PropertyDescriptor::getter".as_ptr(),
);
}
if !desc.setter_.is_null() {
CallObjectTracer(
trc,
&desc.setter_ as *const _ as *mut Heap<*mut JSObject>,
c_str!("PropertyDescriptor::setter"),
c"PropertyDescriptor::setter".as_ptr(),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion mozjs/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ static SIMPLE_GLOBAL_CLASS_OPS: JSClassOps = JSClassOps {

/// This is a simple `JSClass` for global objects, primarily intended for tests.
pub static SIMPLE_GLOBAL_CLASS: JSClass = JSClass {
name: b"Global\0" as *const u8 as *const _,
name: c"Global".as_ptr(),
flags: JSCLASS_IS_GLOBAL
| ((JSCLASS_GLOBAL_SLOT_COUNT & JSCLASS_RESERVED_SLOTS_MASK)
<< JSCLASS_RESERVED_SLOTS_SHIFT),
Expand Down
7 changes: 2 additions & 5 deletions mozjs/tests/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn callback() {
let function = JS_DefineFunction(
context,
global.handle().into(),
b"puts\0".as_ptr() as *const libc::c_char,
c"puts".as_ptr(),
Some(puts),
1,
0,
Expand All @@ -57,10 +57,7 @@ unsafe extern "C" fn puts(context: *mut JSContext, argc: u32, vp: *mut Value) ->
let args = CallArgs::from_vp(vp, argc);

if args.argc_ != 1 {
JS_ReportErrorASCII(
context,
b"puts() requires exactly 1 argument\0".as_ptr() as *const libc::c_char,
);
JS_ReportErrorASCII(context, c"puts() requires exactly 1 argument".as_ptr());
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion mozjs/tests/capture_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn capture_stack() {
let function = JS_DefineFunction(
context,
global.handle().into(),
b"print_stack\0".as_ptr() as *const libc::c_char,
c"print_stack".as_ptr(),
Some(print_stack),
0,
0,
Expand Down
2 changes: 1 addition & 1 deletion mozjs/tests/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ fn enumerate() {
assert!(JS_StringEqualsAscii(
context,
id.get(),
b"a\0" as *const _ as *const _,
c"a".as_ptr(),
&mut matches
));
assert!(matches);
Expand Down
2 changes: 1 addition & 1 deletion mozjs/tests/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn test_panic() {
let function = JS_DefineFunction(
context,
global.handle().into(),
b"test\0".as_ptr() as *const _,
c"test".as_ptr(),
Some(test),
0,
0,
Expand Down
12 changes: 6 additions & 6 deletions mozjs/tests/property_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn property_descriptor() {
assert!(JS_DefineProperty(
context,
object.handle().into(),
b"property\0" as *const u8 as *const libc::c_char,
c"property".as_ptr(),
property.handle().into(),
attrs
));
Expand All @@ -59,7 +59,7 @@ fn property_descriptor() {
assert!(JS_GetPropertyDescriptor(
context,
object.handle().into(),
b"property\0" as *const u8 as *const libc::c_char,
c"property".as_ptr(),
descriptor.handle_mut().into(),
holder.handle_mut().into(),
&mut is_none
Expand All @@ -81,28 +81,28 @@ fn property_descriptor() {
assert!(JS_GetProperty(
context,
desc_object.handle().into(),
b"value\0" as *const u8 as *const libc::c_char,
c"value".as_ptr(),
rval.handle_mut().into()
));
assert_eq!(rval.get().to_int32(), 32);
assert!(JS_GetProperty(
context,
desc_object.handle().into(),
b"configurable\0" as *const u8 as *const libc::c_char,
c"configurable".as_ptr(),
rval.handle_mut().into()
));
assert!(!rval.get().to_boolean());
assert!(JS_GetProperty(
context,
desc_object.handle().into(),
b"enumerable\0" as *const u8 as *const libc::c_char,
c"enumerable".as_ptr(),
rval.handle_mut().into()
));
assert!(rval.get().to_boolean());
assert!(JS_GetProperty(
context,
desc_object.handle().into(),
b"writable\0" as *const u8 as *const libc::c_char,
c"writable".as_ptr(),
rval.handle_mut().into()
));
assert!(!rval.get().to_boolean());
Expand Down
8 changes: 4 additions & 4 deletions mozjs/tests/rooting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ unsafe extern "C" fn generic_method(_: *mut JSContext, _: u32, _: *mut Value) ->
const METHODS: &'static [JSFunctionSpec] = &[
JSFunctionSpec {
name: JSPropertySpec_Name {
string_: b"addEventListener\0" as *const u8 as *const libc::c_char,
string_: c"addEventListener".as_ptr(),
},
call: JSNativeWrapper {
op: Some(generic_method),
Expand All @@ -74,7 +74,7 @@ const METHODS: &'static [JSFunctionSpec] = &[
},
JSFunctionSpec {
name: JSPropertySpec_Name {
string_: b"removeEventListener\0" as *const u8 as *const libc::c_char,
string_: c"removeEventListener".as_ptr(),
},
call: JSNativeWrapper {
op: Some(generic_method),
Expand All @@ -86,7 +86,7 @@ const METHODS: &'static [JSFunctionSpec] = &[
},
JSFunctionSpec {
name: JSPropertySpec_Name {
string_: b"dispatchEvent\0" as *const u8 as *const libc::c_char,
string_: c"dispatchEvent".as_ptr(),
},
call: JSNativeWrapper {
op: Some(generic_method),
Expand All @@ -100,7 +100,7 @@ const METHODS: &'static [JSFunctionSpec] = &[
];

static CLASS: JSClass = JSClass {
name: b"EventTargetPrototype\0" as *const u8 as *const libc::c_char,
name: c"EventTargetPrototype".as_ptr(),
flags: 0,
cOps: 0 as *const _,
spec: ptr::null(),
Expand Down
Loading

0 comments on commit 06a83db

Please sign in to comment.