From dbaba5be581ff525d7691c9a33e25a696fe7c808 Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Sat, 14 Dec 2024 11:31:37 +0100 Subject: [PATCH] Use cstr literals where possible Signed-off-by: Jonathan Schwender --- mozjs/examples/wasm.rs | 14 +++++++------- mozjs/src/error.rs | 22 ++++++++-------------- mozjs/src/gc/custom.rs | 5 ++--- mozjs/src/gc/macros.rs | 8 -------- mozjs/src/gc/trace.rs | 25 ++++++++++++------------- mozjs/src/rust.rs | 2 +- mozjs/tests/callback.rs | 7 ++----- mozjs/tests/capture_stack.rs | 2 +- mozjs/tests/enumerate.rs | 2 +- mozjs/tests/panic.rs | 2 +- mozjs/tests/property_descriptor.rs | 12 ++++++------ mozjs/tests/rooting.rs | 8 ++++---- mozjs/tests/runtime.rs | 2 +- 13 files changed, 46 insertions(+), 65 deletions(-) diff --git a/mozjs/examples/wasm.rs b/mozjs/examples/wasm.rs index 0976f518202..13e43161e6d 100644 --- a/mozjs/examples/wasm.rs +++ b/mozjs/examples/wasm.rs @@ -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() )); @@ -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, @@ -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() )); @@ -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() )); @@ -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() )); diff --git a/mozjs/src/error.rs b/mozjs/src/error.rs index bd018ddf885..d3dedb8fd7e 100644 --- a/mozjs/src/error.rs +++ b/mozjs/src/error.rs @@ -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, }; @@ -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 diff --git a/mozjs/src/gc/custom.rs b/mozjs/src/gc/custom.rs index 7dd365c2351..55b49b45f05 100644 --- a/mozjs/src/gc/custom.rs +++ b/mozjs/src/gc/custom.rs @@ -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}; @@ -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()); } } } @@ -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()); } } } diff --git a/mozjs/src/gc/macros.rs b/mozjs/src/gc/macros.rs index 68da81d1203..74e82c6c69d 100644 --- a/mozjs/src/gc/macros.rs +++ b/mozjs/src/gc/macros.rs @@ -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) => { diff --git a/mozjs/src/gc/trace.rs b/mozjs/src/gc/trace.rs index 42269b7f6d4..4c875215a4e 100644 --- a/mozjs/src/gc/trace.rs +++ b/mozjs/src/gc/trace.rs @@ -1,4 +1,3 @@ -use crate::c_str; use crate::glue::{ CallBigIntTracer, CallFunctionTracer, CallIdTracer, CallObjectTracer, CallScriptTracer, CallStringTracer, CallSymbolTracer, CallValueRootTracer, CallValueTracer, @@ -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()); } } @@ -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()); } } @@ -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()); } } @@ -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()); } } @@ -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()); } } @@ -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 { #[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 { #[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()); } } @@ -131,20 +130,20 @@ unsafe impl Traceable for Heap { CallValueTracer( trc, &desc.value_ as *const _ as *mut Heap, - 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(), ); } } diff --git a/mozjs/src/rust.rs b/mozjs/src/rust.rs index deeb9648554..66c7f7f3dec 100644 --- a/mozjs/src/rust.rs +++ b/mozjs/src/rust.rs @@ -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), diff --git a/mozjs/tests/callback.rs b/mozjs/tests/callback.rs index f0dbd829e21..71df42496bb 100644 --- a/mozjs/tests/callback.rs +++ b/mozjs/tests/callback.rs @@ -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, @@ -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; } diff --git a/mozjs/tests/capture_stack.rs b/mozjs/tests/capture_stack.rs index 68573cb0ab4..acc5225b437 100644 --- a/mozjs/tests/capture_stack.rs +++ b/mozjs/tests/capture_stack.rs @@ -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, diff --git a/mozjs/tests/enumerate.rs b/mozjs/tests/enumerate.rs index 74f2908202d..d892aa39988 100644 --- a/mozjs/tests/enumerate.rs +++ b/mozjs/tests/enumerate.rs @@ -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); diff --git a/mozjs/tests/panic.rs b/mozjs/tests/panic.rs index 674086c05da..d74cce7d5c5 100644 --- a/mozjs/tests/panic.rs +++ b/mozjs/tests/panic.rs @@ -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, diff --git a/mozjs/tests/property_descriptor.rs b/mozjs/tests/property_descriptor.rs index 7540a5a5bd2..963fa343334 100644 --- a/mozjs/tests/property_descriptor.rs +++ b/mozjs/tests/property_descriptor.rs @@ -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 )); @@ -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 @@ -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()); diff --git a/mozjs/tests/rooting.rs b/mozjs/tests/rooting.rs index c79461d1e7c..156d04f2ad9 100644 --- a/mozjs/tests/rooting.rs +++ b/mozjs/tests/rooting.rs @@ -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), @@ -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), @@ -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), @@ -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(), diff --git a/mozjs/tests/runtime.rs b/mozjs/tests/runtime.rs index 02d975b416c..c60d89f67bc 100644 --- a/mozjs/tests/runtime.rs +++ b/mozjs/tests/runtime.rs @@ -65,7 +65,7 @@ static CLASS_OPS: JSClassOps = JSClassOps { }; static CLASS: JSClass = JSClass { - name: b"EventTargetPrototype\0" as *const u8 as *const libc::c_char, + name: c"EventTargetPrototype".as_ptr(), flags: JSCLASS_FOREGROUND_FINALIZE, cOps: &CLASS_OPS as *const JSClassOps, spec: ptr::null(),