git.delta.rocks / jrsonnet / refs/commits / 097494ede5f1

difftreelog

refactor trivial code review suggestions

Yaroslav Bolyukin2022-09-18parent: #f2fefaf.patch.diff
in: master

8 files changed

modifiedbindings/jsonnet/src/lib.rsdiffbeforeafterboth
before · bindings/jsonnet/src/lib.rs
1#[cfg(feature = "interop")]2pub mod interop;34pub mod import;5pub mod native;6pub mod val_extract;7pub mod val_make;8pub mod val_modify;9pub mod vars_tlas;1011use std::{12	alloc::Layout,13	borrow::Cow,14	ffi::{CStr, CString, OsStr},15	os::raw::{c_char, c_double, c_int, c_uint},16	path::Path,17};1819use jrsonnet_evaluator::{20	trace::PathResolver, FileImportResolver, IStr, ManifestFormat, State, Val,21};2223/// WASM stub24#[cfg(target_arch = "wasm32")]25#[no_mangle]26pub extern "C" fn _start() {}2728/// Return the version string of the Jsonnet interpreter.  Conforms to semantic versioning29/// http://semver.org/ If this does not match LIB_JSONNET_VERSION then there is a mismatch between30/// header and compiled library.31#[no_mangle]32pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {33	b"v0.16.0\0"34}3536unsafe fn parse_path(input: &CStr) -> Cow<Path> {37	#[cfg(target_family = "unix")]38	{39		use std::os::unix::ffi::OsStrExt;40		let str = OsStr::from_bytes(input.to_bytes());41		Cow::Borrowed(Path::new(str))42	}43	#[cfg(target_family = "windows")]44	{45		use std::os::windows::ffi::OsStringExt;46		let str = input.to_str().expect("input is not utf8");47		let wide = str.encode_utf16().collect::<Vec<_>>();48		let wide = OsString::from_wide(&wide);49		Cow::Owned(PathBuf::new(wide))50	}51	#[cfg(not(any(target_family = "unix", target_family = "windows")))]52	{53		compile_error!("unsupported os")54	}55}5657unsafe fn unparse_path(input: &Path) -> Cow<CStr> {58	#[cfg(target_family = "unix")]59	{60		use std::os::unix::ffi::OsStrExt;61		let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");62		Cow::Owned(str)63	}64	#[cfg(not(any(target_family = "unix", target_family = "windows")))]65	{66		compile_error!("unsupported os")67	}68}6970/// Create a new Jsonnet virtual machine.71#[no_mangle]72pub extern "C" fn jsonnet_make() -> *mut State {73	let state = State::default();74	state.settings_mut().import_resolver = Box::new(FileImportResolver::default());75	state.settings_mut().context_initializer = Box::new(jrsonnet_stdlib::ContextInitializer::new(76		state.clone(),77		PathResolver::new_cwd_fallback(),78	));79	Box::into_raw(Box::new(state))80}8182/// Complement of `jsonnet_vm_make`83#[no_mangle]84#[allow(clippy::boxed_local)]85pub extern "C" fn jsonnet_destroy(vm: Box<State>) {86	drop(vm);87}8889/// Set the maximum stack depth.90#[no_mangle]91pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {92	vm.settings_mut().max_stack = v as usize;93}9495/// Set the number of objects required before a garbage collection cycle is allowed.96///97/// No-op for now98#[no_mangle]99pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}100101/// Run the garbage collector after this amount of growth in the number of objects102///103/// No-op for now104#[no_mangle]105pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}106107/// Expect a string as output and don't JSON encode it.108#[no_mangle]109pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {110	match v {111		1 => vm.set_manifest_format(ManifestFormat::String),112		0 => vm.set_manifest_format(ManifestFormat::Json {113			padding: 4,114			#[cfg(feature = "exp-preserve-order")]115			preserve_order: false,116		}),117		_ => panic!("incorrect output format"),118	}119}120121/// Allocate, resize, or free a buffer.  This will abort if the memory cannot be allocated.  It will122/// only return NULL if sz was zero.123///124/// # Safety125///126/// `buf` should be either previosly allocated by this library, or NULL127///128/// This function is most definitely broken, but it works somehow, see TODO inside129#[no_mangle]130pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {131	if buf.is_null() {132		if sz == 0 {133			return std::ptr::null_mut();134		}135		return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());136	}137	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D138	// OR (Alternative way of fixing this TODO)139	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,140	// TODO: so it should work in normal cases. Maybe force allocator for this library?141	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();142	if sz == 0 {143		std::alloc::dealloc(buf, old_layout);144		return std::ptr::null_mut();145	}146	std::alloc::realloc(buf, old_layout, sz)147}148149/// Clean up a JSON subtree.150///151/// This is useful if you want to abort with an error mid-way through building a complex value.152#[no_mangle]153#[allow(clippy::boxed_local)]154pub extern "C" fn jsonnet_json_destroy(_vm: &State, v: Box<Val>) {155	drop(v);156}157158/// Set the number of lines of stack trace to display (0 for all of them).159#[no_mangle]160pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {161	vm.set_max_trace(v as usize)162}163164/// Evaluate a file containing Jsonnet code, return a JSON string.165///166/// The returned string should be cleaned up with jsonnet_realloc.167///168/// # Safety169///170/// `filename` should be a \0-terminated string171#[no_mangle]172pub unsafe extern "C" fn jsonnet_evaluate_file(173	vm: &State,174	filename: *const c_char,175	error: &mut c_int,176) -> *const c_char {177	let filename = parse_path(CStr::from_ptr(filename));178	match vm179		.import(&filename)180		.and_then(|v| vm.with_tla(v))181		.and_then(|v| vm.manifest(v))182	{183		Ok(v) => {184			*error = 0;185			CString::new(&*v as &str).unwrap().into_raw()186		}187		Err(e) => {188			*error = 1;189			let out = vm.stringify_err(&e);190			CString::new(&out as &str).unwrap().into_raw()191		}192	}193}194195/// Evaluate a string containing Jsonnet code, return a JSON string.196///197/// The returned string should be cleaned up with jsonnet_realloc.198///199/// # Safety200///201/// `filename`, `snippet` should be a \0-terminated strings202#[no_mangle]203pub unsafe extern "C" fn jsonnet_evaluate_snippet(204	vm: &State,205	filename: *const c_char,206	snippet: *const c_char,207	error: &mut c_int,208) -> *const c_char {209	let filename = CStr::from_ptr(filename);210	let snippet = CStr::from_ptr(snippet);211	match vm212		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())213		.and_then(|v| vm.with_tla(v))214		.and_then(|v| vm.manifest(v))215	{216		Ok(v) => {217			*error = 0;218			CString::new(&*v as &str).unwrap().into_raw()219		}220		Err(e) => {221			*error = 1;222			let out = vm.stringify_err(&e);223			CString::new(&out as &str).unwrap().into_raw()224		}225	}226}227228fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {229	let mut out = Vec::new();230	for (i, (k, v)) in multi.iter().enumerate() {231		if i != 0 {232			out.push(0);233		}234		out.extend_from_slice(k.as_bytes());235		out.push(0);236		out.extend_from_slice(v.as_bytes());237	}238	out.push(0);239	out.push(0);240	let v = out.as_ptr();241	std::mem::forget(out);242	v as *const c_char243}244245/// # Safety246#[no_mangle]247pub unsafe extern "C" fn jsonnet_evaluate_file_multi(248	vm: &State,249	filename: *const c_char,250	error: &mut c_int,251) -> *const c_char {252	let filename = parse_path(CStr::from_ptr(filename));253	match vm254		.import(&filename)255		.and_then(|v| vm.with_tla(v))256		.and_then(|v| vm.manifest_multi(v))257	{258		Ok(v) => {259			*error = 0;260			multi_to_raw(v)261		}262		Err(e) => {263			*error = 1;264			let out = vm.stringify_err(&e);265			CString::new(&out as &str).unwrap().into_raw()266		}267	}268}269270/// # Safety271#[no_mangle]272pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(273	vm: &State,274	filename: *const c_char,275	snippet: *const c_char,276	error: &mut c_int,277) -> *const c_char {278	let filename = CStr::from_ptr(filename);279	let snippet = CStr::from_ptr(snippet);280	match vm281		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())282		.and_then(|v| vm.with_tla(v))283		.and_then(|v| vm.manifest_multi(v))284	{285		Ok(v) => {286			*error = 0;287			multi_to_raw(v)288		}289		Err(e) => {290			*error = 1;291			let out = vm.stringify_err(&e);292			CString::new(&out as &str).unwrap().into_raw()293		}294	}295}296297fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {298	let mut out = Vec::new();299	for (i, v) in multi.iter().enumerate() {300		if i != 0 {301			out.push(0);302		}303		out.extend_from_slice(v.as_bytes());304	}305	out.push(0);306	out.push(0);307	let v = out.as_ptr();308	std::mem::forget(out);309	v as *const c_char310}311312/// # Safety313#[no_mangle]314pub unsafe extern "C" fn jsonnet_evaluate_file_stream(315	vm: &State,316	filename: *const c_char,317	error: &mut c_int,318) -> *const c_char {319	let filename = parse_path(CStr::from_ptr(filename));320	match vm321		.import(&filename)322		.and_then(|v| vm.with_tla(v))323		.and_then(|v| vm.manifest_stream(v))324	{325		Ok(v) => {326			*error = 0;327			stream_to_raw(v)328		}329		Err(e) => {330			*error = 1;331			let out = vm.stringify_err(&e);332			CString::new(&out as &str)333				.expect("there should be no \\0 in the error string")334				.into_raw()335		}336	}337}338339/// # Safety340#[no_mangle]341pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(342	vm: &State,343	filename: *const c_char,344	snippet: *const c_char,345	error: &mut c_int,346) -> *const c_char {347	let filename = CStr::from_ptr(filename);348	let snippet = CStr::from_ptr(snippet);349	match vm350		.evaluate_snippet(351			filename.to_str().expect("filename is not utf-8"),352			snippet.to_str().expect("snippet is not utf-8"),353		)354		.and_then(|v| vm.with_tla(v))355		.and_then(|v| vm.manifest_stream(v))356	{357		Ok(v) => {358			*error = 0;359			stream_to_raw(v)360		}361		Err(e) => {362			*error = 1;363			let out = vm.stringify_err(&e);364			CString::new(&out as &str)365				.expect("there should be no \\0 in the error string")366				.into_raw()367		}368	}369}
after · bindings/jsonnet/src/lib.rs
1#[cfg(feature = "interop")]2pub mod interop;34pub mod import;5pub mod native;6pub mod val_extract;7pub mod val_make;8pub mod val_modify;9pub mod vars_tlas;1011use std::{12	alloc::Layout,13	borrow::Cow,14	ffi::{CStr, CString, OsStr},15	os::raw::{c_char, c_double, c_int, c_uint},16	path::Path,17};1819use jrsonnet_evaluator::{20	trace::PathResolver, FileImportResolver, IStr, ManifestFormat, State, Val,21};2223/// WASM stub24#[cfg(target_arch = "wasm32")]25#[no_mangle]26pub extern "C" fn _start() {}2728/// Return the version string of the Jsonnet interpreter.29/// Conforms to [semantic versioning](http://semver.org/).30/// If this does not match `LIB_JSONNET_VERSION`31/// then there is a mismatch between header and compiled library.32#[no_mangle]33pub extern "C" fn jsonnet_version() -> &'static [u8; 8] {34	b"v0.16.0\0"35}3637unsafe fn parse_path(input: &CStr) -> Cow<Path> {38	#[cfg(target_family = "unix")]39	{40		use std::os::unix::ffi::OsStrExt;41		let str = OsStr::from_bytes(input.to_bytes());42		Cow::Borrowed(Path::new(str))43	}44	#[cfg(target_family = "windows")]45	{46		use std::os::windows::ffi::OsStringExt;47		let str = input.to_str().expect("input is not utf8");48		let wide = str.encode_utf16().collect::<Vec<_>>();49		let wide = OsString::from_wide(&wide);50		Cow::Owned(PathBuf::new(wide))51	}52	#[cfg(not(any(target_family = "unix", target_family = "windows")))]53	{54		compile_error!("unsupported os")55	}56}5758unsafe fn unparse_path(input: &Path) -> Cow<CStr> {59	#[cfg(target_family = "unix")]60	{61		use std::os::unix::ffi::OsStrExt;62		let str = CString::new(input.as_os_str().as_bytes()).expect("input has zero byte in it");63		Cow::Owned(str)64	}65	#[cfg(not(any(target_family = "unix", target_family = "windows")))]66	{67		compile_error!("unsupported os")68	}69}7071/// Creates a new Jsonnet virtual machine.72#[no_mangle]73pub extern "C" fn jsonnet_make() -> *mut State {74	let state = State::default();75	state.settings_mut().import_resolver = Box::new(FileImportResolver::default());76	state.settings_mut().context_initializer = Box::new(jrsonnet_stdlib::ContextInitializer::new(77		state.clone(),78		PathResolver::new_cwd_fallback(),79	));80	Box::into_raw(Box::new(state))81}8283/// Complement of [`jsonnet_vm_make`].84#[no_mangle]85#[allow(clippy::boxed_local)]86pub extern "C" fn jsonnet_destroy(vm: Box<State>) {87	drop(vm);88}8990/// Set the maximum stack depth.91#[no_mangle]92pub extern "C" fn jsonnet_max_stack(vm: &State, v: c_uint) {93	vm.settings_mut().max_stack = v as usize;94}9596/// Set the number of objects required before a garbage collection cycle is allowed.97///98/// No-op for now99#[no_mangle]100pub extern "C" fn jsonnet_gc_min_objects(_vm: &State, _v: c_uint) {}101102/// Run the garbage collector after this amount of growth in the number of objects103///104/// No-op for now105#[no_mangle]106pub extern "C" fn jsonnet_gc_growth_trigger(_vm: &State, _v: c_double) {}107108/// Expect a string as output and don't JSON encode it.109#[no_mangle]110pub extern "C" fn jsonnet_string_output(vm: &State, v: c_int) {111	match v {112		1 => vm.set_manifest_format(ManifestFormat::String),113		0 => vm.set_manifest_format(ManifestFormat::Json {114			padding: 4,115			#[cfg(feature = "exp-preserve-order")]116			preserve_order: false,117		}),118		_ => panic!("incorrect output format"),119	}120}121122/// Allocate, resize, or free a buffer.  This will abort if the memory cannot be allocated. It will123/// only return NULL if sz was zero.124///125/// # Safety126///127/// `buf` should be either previosly allocated by this library, or NULL128///129/// This function is most definitely broken, but it works somehow, see TODO inside130#[no_mangle]131pub unsafe extern "C" fn jsonnet_realloc(_vm: &State, buf: *mut u8, sz: usize) -> *mut u8 {132	if buf.is_null() {133		if sz == 0 {134			return std::ptr::null_mut();135		}136		return std::alloc::alloc(Layout::from_size_align(sz, std::mem::align_of::<u8>()).unwrap());137	}138	// TODO: Somehow store size of allocation, because its real size is probally not 16 :D139	// OR (Alternative way of fixing this TODO)140	// TODO: Standard allocator uses malloc, and it doesn't uses allocation size,141	// TODO: so it should work in normal cases. Maybe force allocator for this library?142	let old_layout = Layout::from_size_align(16, std::mem::align_of::<u8>()).unwrap();143	if sz == 0 {144		std::alloc::dealloc(buf, old_layout);145		return std::ptr::null_mut();146	}147	std::alloc::realloc(buf, old_layout, sz)148}149150/// Clean up a JSON subtree.151///152/// This is useful if you want to abort with an error mid-way through building a complex value.153#[no_mangle]154#[allow(clippy::boxed_local)]155pub extern "C" fn jsonnet_json_destroy(_vm: &State, v: Box<Val>) {156	drop(v);157}158159/// Set the number of lines of stack trace to display (0 for all of them).160#[no_mangle]161pub extern "C" fn jsonnet_max_trace(vm: &State, v: c_uint) {162	vm.set_max_trace(v as usize)163}164165/// Evaluate a file containing Jsonnet code, return a JSON string.166///167/// The returned string should be cleaned up with jsonnet_realloc.168///169/// # Safety170///171/// `filename` should be a \0-terminated string172#[no_mangle]173pub unsafe extern "C" fn jsonnet_evaluate_file(174	vm: &State,175	filename: *const c_char,176	error: &mut c_int,177) -> *const c_char {178	let filename = parse_path(CStr::from_ptr(filename));179	match vm180		.import(&filename)181		.and_then(|v| vm.with_tla(v))182		.and_then(|v| vm.manifest(v))183	{184		Ok(v) => {185			*error = 0;186			CString::new(&*v as &str).unwrap().into_raw()187		}188		Err(e) => {189			*error = 1;190			let out = vm.stringify_err(&e);191			CString::new(&out as &str).unwrap().into_raw()192		}193	}194}195196/// Evaluate a string containing Jsonnet code, return a JSON string.197///198/// The returned string should be cleaned up with jsonnet_realloc.199///200/// # Safety201///202/// `filename`, `snippet` should be a \0-terminated strings203#[no_mangle]204pub unsafe extern "C" fn jsonnet_evaluate_snippet(205	vm: &State,206	filename: *const c_char,207	snippet: *const c_char,208	error: &mut c_int,209) -> *const c_char {210	let filename = CStr::from_ptr(filename);211	let snippet = CStr::from_ptr(snippet);212	match vm213		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())214		.and_then(|v| vm.with_tla(v))215		.and_then(|v| vm.manifest(v))216	{217		Ok(v) => {218			*error = 0;219			CString::new(&*v as &str).unwrap().into_raw()220		}221		Err(e) => {222			*error = 1;223			let out = vm.stringify_err(&e);224			CString::new(&out as &str).unwrap().into_raw()225		}226	}227}228229fn multi_to_raw(multi: Vec<(IStr, IStr)>) -> *const c_char {230	let mut out = Vec::new();231	for (i, (k, v)) in multi.iter().enumerate() {232		if i != 0 {233			out.push(0);234		}235		out.extend_from_slice(k.as_bytes());236		out.push(0);237		out.extend_from_slice(v.as_bytes());238	}239	out.push(0);240	out.push(0);241	let v = out.as_ptr();242	std::mem::forget(out);243	v as *const c_char244}245246/// # Safety247#[no_mangle]248pub unsafe extern "C" fn jsonnet_evaluate_file_multi(249	vm: &State,250	filename: *const c_char,251	error: &mut c_int,252) -> *const c_char {253	let filename = parse_path(CStr::from_ptr(filename));254	match vm255		.import(&filename)256		.and_then(|v| vm.with_tla(v))257		.and_then(|v| vm.manifest_multi(v))258	{259		Ok(v) => {260			*error = 0;261			multi_to_raw(v)262		}263		Err(e) => {264			*error = 1;265			let out = vm.stringify_err(&e);266			CString::new(&out as &str).unwrap().into_raw()267		}268	}269}270271/// # Safety272#[no_mangle]273pub unsafe extern "C" fn jsonnet_evaluate_snippet_multi(274	vm: &State,275	filename: *const c_char,276	snippet: *const c_char,277	error: &mut c_int,278) -> *const c_char {279	let filename = CStr::from_ptr(filename);280	let snippet = CStr::from_ptr(snippet);281	match vm282		.evaluate_snippet(filename.to_str().unwrap(), snippet.to_str().unwrap())283		.and_then(|v| vm.with_tla(v))284		.and_then(|v| vm.manifest_multi(v))285	{286		Ok(v) => {287			*error = 0;288			multi_to_raw(v)289		}290		Err(e) => {291			*error = 1;292			let out = vm.stringify_err(&e);293			CString::new(&out as &str).unwrap().into_raw()294		}295	}296}297298fn stream_to_raw(multi: Vec<IStr>) -> *const c_char {299	let mut out = Vec::new();300	for (i, v) in multi.iter().enumerate() {301		if i != 0 {302			out.push(0);303		}304		out.extend_from_slice(v.as_bytes());305	}306	out.push(0);307	out.push(0);308	let v = out.as_ptr();309	std::mem::forget(out);310	v as *const c_char311}312313/// # Safety314#[no_mangle]315pub unsafe extern "C" fn jsonnet_evaluate_file_stream(316	vm: &State,317	filename: *const c_char,318	error: &mut c_int,319) -> *const c_char {320	let filename = parse_path(CStr::from_ptr(filename));321	match vm322		.import(&filename)323		.and_then(|v| vm.with_tla(v))324		.and_then(|v| vm.manifest_stream(v))325	{326		Ok(v) => {327			*error = 0;328			stream_to_raw(v)329		}330		Err(e) => {331			*error = 1;332			let out = vm.stringify_err(&e);333			CString::new(&out as &str)334				.expect("there should be no \\0 in the error string")335				.into_raw()336		}337	}338}339340/// # Safety341#[no_mangle]342pub unsafe extern "C" fn jsonnet_evaluate_snippet_stream(343	vm: &State,344	filename: *const c_char,345	snippet: *const c_char,346	error: &mut c_int,347) -> *const c_char {348	let filename = CStr::from_ptr(filename);349	let snippet = CStr::from_ptr(snippet);350	match vm351		.evaluate_snippet(352			filename.to_str().expect("filename is not utf-8"),353			snippet.to_str().expect("snippet is not utf-8"),354		)355		.and_then(|v| vm.with_tla(v))356		.and_then(|v| vm.manifest_stream(v))357	{358		Ok(v) => {359			*error = 0;360			stream_to_raw(v)361		}362		Err(e) => {363			*error = 1;364			let out = vm.stringify_err(&e);365			CString::new(&out as &str)366				.expect("there should be no \\0 in the error string")367				.into_raw()368		}369	}370}
modifiedbindings/jsonnet/src/native.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/native.rs
+++ b/bindings/jsonnet/src/native.rs
@@ -12,9 +12,9 @@
 };
 use jrsonnet_gcmodule::Cc;
 
-/// The returned JsonnetJsonValue* should be allocated with jsonnet_realloc.  It will be cleaned up
-/// along with the objects rooted at argv by libjsonnet when no-longer needed.  Return a string upon
-/// failure, which will appear in Jsonnet as an error.  The argv pointer is an array whose size
+/// The returned `JsonnetJsonValue*` should be allocated with `jsonnet_realloc`. It will be cleaned up
+/// along with the objects rooted at `argv` by `libjsonnet` when no-longer needed. Return a string upon
+/// failure, which will appear in Jsonnet as an error. The `argv` pointer is an array whose size
 /// matches the array of parameters supplied when the native callback was originally registered.
 ///
 /// - `ctx` User pointer, given in jsonnet_native_callback.
modifiedbindings/jsonnet/src/val_extract.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_extract.rs
+++ b/bindings/jsonnet/src/val_extract.rs
@@ -7,7 +7,7 @@
 
 use jrsonnet_evaluator::{State, Val};
 
-/// If the value is a string, return it as UTF8 otherwise return NULL.
+/// If the value is a string, return it as UTF-8, otherwise return `NULL`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_string(_vm: &State, v: &Val) -> *mut c_char {
 	match v {
@@ -16,7 +16,7 @@
 	}
 }
 
-/// If the value is a number, return 1 and store the number in out, otherwise return 0.
+/// If the value is a number, return `1` and store the number in out, otherwise return `0`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_number(_vm: &State, v: &Val, out: &mut c_double) -> c_int {
 	match v {
@@ -28,7 +28,7 @@
 	}
 }
 
-/// Return 0 if the value is false, 1 if it is true, and 2 if it is not a bool.
+/// Return `0` if the value is `false`, `1` if it is `true`, and `2` if it is not a `bool`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_bool(_vm: &State, v: &Val) -> c_int {
 	match v {
@@ -38,7 +38,7 @@
 	}
 }
 
-/// Return 1 if the value is null, else 0.
+/// Return `1` if the value is `null`, otherwise return `0`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_extract_null(_vm: &State, v: &Val) -> c_int {
 	match v {
modifiedbindings/jsonnet/src/val_make.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_make.rs
+++ b/bindings/jsonnet/src/val_make.rs
@@ -8,7 +8,7 @@
 use jrsonnet_evaluator::{val::ArrValue, ObjValue, State, Val};
 use jrsonnet_gcmodule::Cc;
 
-/// Convert the given UTF8 string to a JsonnetJsonValue.
+/// Convert the given `UTF-8` string to a `JsonnetJsonValue`.
 ///
 /// # Safety
 ///
@@ -20,34 +20,34 @@
 	Box::into_raw(Box::new(Val::Str(val.into())))
 }
 
-/// Convert the given double to a JsonnetJsonValue.
+/// Convert the given double to a `JsonnetJsonValue`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_number(_vm: &State, v: c_double) -> *mut Val {
 	Box::into_raw(Box::new(Val::Num(v)))
 }
 
-/// Convert the given bool (1 or 0) to a JsonnetJsonValue.
+/// Convert the given `bool` (`1` or `0`) to a `JsonnetJsonValue`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_bool(_vm: &State, v: c_int) -> *mut Val {
 	assert!(v == 0 || v == 1, "bad boolean value");
 	Box::into_raw(Box::new(Val::Bool(v == 1)))
 }
 
-/// Make a JsonnetJsonValue representing null.
+/// Make a `JsonnetJsonValue` representing `null`.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_null(_vm: &State) -> *mut Val {
 	Box::into_raw(Box::new(Val::Null))
 }
 
-/// Make a JsonnetJsonValue representing an array.
+/// Make a `JsonnetJsonValue` representing an array.
 ///
-/// Assign elements with jsonnet_json_array_append.
+/// Assign elements with [`jsonnet_json_array_append`].
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_array(_vm: &State) -> *mut Val {
 	Box::into_raw(Box::new(Val::Arr(ArrValue::Eager(Cc::new(Vec::new())))))
 }
 
-/// Make a JsonnetJsonValue representing an object.
+/// Make a `JsonnetJsonValue` representing an object.
 #[no_mangle]
 pub extern "C" fn jsonnet_json_make_object(_vm: &State) -> *mut Val {
 	Box::into_raw(Box::new(Val::Obj(ObjValue::new_empty())))
modifiedbindings/jsonnet/src/val_modify.rsdiffbeforeafterboth
--- a/bindings/jsonnet/src/val_modify.rs
+++ b/bindings/jsonnet/src/val_modify.rs
@@ -7,7 +7,7 @@
 use jrsonnet_evaluator::{val::ArrValue, State, Thunk, Val};
 use jrsonnet_gcmodule::Cc;
 
-/// Add value to the end of the array arr
+/// Adds value to the end of the array `arr`.
 ///
 /// # Safety
 ///
@@ -29,13 +29,13 @@
 	}
 }
 
-/// Add the field to the object, bound to value.
+/// Adds the field to the object, bound to value.
 ///
 /// This shadows any previous binding of the field.
 ///
 /// # Safety
 ///
-/// `obj` should be pointer to object value allocated by make_object, or returned by other library call
+/// `obj` should be a valid pointer to object value allocated by `make_object`, or returned by other library call
 /// `name` should be \0-terminated string
 #[no_mangle]
 pub unsafe extern "C" fn jsonnet_json_object_append(
modifiedcrates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -105,7 +105,7 @@
 	fn as_any(&self) -> &dyn Any;
 }
 
-/// Context initializer, which adds noth
+/// Context initializer which adds nothing.
 pub struct DummyContextInitializer;
 impl ContextInitializer for DummyContextInitializer {
 	fn initialize(&self, _state: State, _for_file: Source) -> Context {
modifiedcrates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -66,7 +66,7 @@
 }
 
 fn frexp(s: f64) -> (f64, i16) {
-	if 0.0 == s {
+	if s == 0.0 {
 		(s, 0)
 	} else {
 		let lg = s.abs().log2();
modifiedcrates/jrsonnet-stdlib/src/sort.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/sort.rs
+++ b/crates/jrsonnet-stdlib/src/sort.rs
@@ -41,9 +41,9 @@
 			(Val::Num(_), SortKeyType::Unknown) => sort_type = SortKeyType::Number,
 			(Val::Str(_), SortKeyType::String) | (Val::Num(_), SortKeyType::Number) => {}
 			(Val::Str(_) | Val::Num(_), _) => {
-				throw_runtime!("sort elements should have same types")
+				throw_runtime!("sort elements should have the same types")
 			}
-			_ => throw_runtime!("sort key should be string or number"),
+			_ => throw_runtime!("sort key should either be a string or a number"),
 		}
 	}
 	Ok(sort_type)