Skip to content

Commit 357ee76

Browse files
committed
Add documentation and fix clippy issues.
1 parent 56009d0 commit 357ee76

File tree

11 files changed

+351
-127
lines changed

11 files changed

+351
-127
lines changed

src/lib.rs

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
//! V8-rs is a crate containing bindings to the V8 C++ API.
77
8-
#![warn(missing_docs)]
8+
#![deny(missing_docs)]
99

1010
/// The module contains the rust-idiomatic data structures and functions.
1111
pub mod v8;
@@ -57,17 +57,13 @@ impl From<UserIndex> for RawIndex {
5757
mod json_path_tests {
5858
use crate as v8_rs;
5959
use crate::v8::types::any::LocalValueAny;
60-
use crate::v8::types::native_function::LocalNativeFunction;
6160
use crate::v8::types::native_function_template::LocalNativeFunctionTemplate;
6261
use crate::v8::types::object_template::LocalObjectTemplate;
6362
use crate::v8::types::promise::LocalPromise;
6463
use crate::v8::types::try_catch::TryCatch;
6564
use crate::v8::types::utf8::LocalUtf8;
6665
use crate::v8::types::Value;
67-
use crate::v8::{
68-
context_scope, isolate, isolate_scope, types, types::array, types::array_buffer,
69-
types::native_function_template, types::object, types::set, types::utf8, v8_init,
70-
};
66+
use crate::v8::{context_scope, isolate, isolate_scope, types, v8_init};
7167

7268
use v8_derive::new_native_function;
7369

@@ -199,7 +195,7 @@ mod json_path_tests {
199195
.create_native_function_template(|args, isolate_scope, ctx_scope| {
200196
let foo: LocalValueAny = isolate_scope.create_string("foo").try_into().unwrap();
201197
let v: LocalValueAny = args.get(0).try_into().unwrap();
202-
let _res = v.call(ctx_scope, Some(&[&foo.into()]));
198+
let _res = v.call(ctx_scope, Some(&[&foo]));
203199
None
204200
})
205201
.try_into()
@@ -256,7 +252,7 @@ mod json_path_tests {
256252
let trycatch: TryCatch = isolate_scope.create_try_catch().try_into().unwrap();
257253
assert!(script.run(&ctx_scope).is_none());
258254
let exception = trycatch.get_exception();
259-
let exception_msg = exception.into_utf8().unwrap();
255+
let exception_msg = LocalUtf8::try_from(exception).unwrap();
260256
assert_eq!(exception_msg.as_str(), "this is an error");
261257
}
262258

@@ -275,10 +271,10 @@ mod json_path_tests {
275271
let trycatch: TryCatch = isolate_scope.create_try_catch().try_into().unwrap();
276272
assert!(script.run(&ctx_scope).is_none());
277273
let exception = trycatch.get_exception();
278-
let exception_msg = exception.into_utf8().unwrap();
274+
let exception_msg = LocalUtf8::try_from(exception).unwrap();
279275
assert_eq!(exception_msg.as_str(), "Error: this is an error!");
280276
let trace = trycatch.get_trace(&ctx_scope);
281-
let trace_str = trace.unwrap().into_utf8().unwrap();
277+
let trace_str = LocalUtf8::try_from(trace.unwrap()).unwrap();
282278
assert!(trace_str.as_str().contains("at foo"));
283279
}
284280

@@ -368,7 +364,7 @@ mod json_path_tests {
368364
crate::v8::types::promise::PromiseState::Fulfilled
369365
);
370366
let promise_res = promise.get_result();
371-
let res_utf8 = promise_res.into_utf8().unwrap();
367+
let res_utf8 = LocalUtf8::try_from(promise_res).unwrap();
372368
assert_eq!(res_utf8.as_str(), "1");
373369
}
374370

@@ -382,7 +378,7 @@ mod json_path_tests {
382378
globals.add_native_function("foo", |_args, isolate_scope, ctx_scope| {
383379
let resolver = ctx_scope.create_resolver();
384380
resolver.resolve(
385-
&ctx_scope,
381+
ctx_scope,
386382
&isolate_scope.create_string("foo").try_into().unwrap(),
387383
);
388384
let promise = resolver.get_promise();
@@ -404,7 +400,7 @@ mod json_path_tests {
404400
crate::v8::types::promise::PromiseState::Fulfilled
405401
);
406402
let promise_res = promise.get_result();
407-
let res_utf8 = promise_res.into_utf8().unwrap();
403+
let res_utf8 = LocalUtf8::try_from(promise_res).unwrap();
408404
assert_eq!(res_utf8.as_str(), "foo");
409405
}
410406

@@ -420,7 +416,9 @@ mod json_path_tests {
420416
let script = ctx_scope.compile(&code_str);
421417
assert!(script.is_none());
422418
assert_eq!(
423-
trycatch.get_exception().into_utf8().unwrap().as_str(),
419+
LocalUtf8::try_from(trycatch.get_exception())
420+
.unwrap()
421+
.as_str(),
424422
"SyntaxError: Unexpected end of input"
425423
);
426424
}
@@ -438,7 +436,9 @@ mod json_path_tests {
438436
let res = script.run(&ctx_scope);
439437
assert!(res.is_none());
440438
assert_eq!(
441-
trycatch.get_exception().into_utf8().unwrap().as_str(),
439+
LocalUtf8::try_from(trycatch.get_exception())
440+
.unwrap()
441+
.as_str(),
442442
"ReferenceError: foo is not defined"
443443
);
444444
}
@@ -472,9 +472,7 @@ mod json_path_tests {
472472
let trycatch: TryCatch = isolate_scope.create_try_catch().try_into().unwrap();
473473
let res = match script.run(&ctx_scope) {
474474
Some(_res) => Ok(()),
475-
None => Err(trycatch
476-
.get_exception()
477-
.into_utf8()
475+
None => Err(LocalUtf8::try_from(trycatch.get_exception())
478476
.unwrap()
479477
.as_str()
480478
.to_string()),
@@ -485,11 +483,7 @@ mod json_path_tests {
485483
#[test]
486484
fn test_value_is_object() {
487485
define_function_and_call("foo({})", "foo", |args, _isolate, _ctx_scope| {
488-
if let Value::Object(_) = args.get(0) {
489-
assert!(true);
490-
} else {
491-
assert!(false, "The value should have been an object!");
492-
}
486+
assert!(args.get(0).is_object());
493487
None
494488
})
495489
.expect("Got error on function run");
@@ -501,7 +495,7 @@ mod json_path_tests {
501495
if let Value::Other(any) = args.get(0) {
502496
assert!(any.is_function());
503497
} else {
504-
assert!(false, "The value should have been an object!");
498+
unreachable!("The value should have been a function!");
505499
}
506500
None
507501
})
@@ -517,7 +511,7 @@ mod json_path_tests {
517511
if let Value::Other(any) = args.get(0) {
518512
assert!(any.is_async_function());
519513
} else {
520-
assert!(false, "The value should have been an object!");
514+
unreachable!("The value should have been an async function!");
521515
}
522516
None
523517
},
@@ -528,11 +522,7 @@ mod json_path_tests {
528522
#[test]
529523
fn test_value_is_string() {
530524
define_function_and_call("foo(\"foo\")", "foo", |args, _isolate, _ctx_scope| {
531-
if let Value::String(_) = args.get(0) {
532-
assert!(true);
533-
} else {
534-
assert!(false, "The value should have been a string!");
535-
}
525+
assert!(args.get(0).is_string());
536526
None
537527
})
538528
.expect("Got error on function run");
@@ -541,11 +531,7 @@ mod json_path_tests {
541531
#[test]
542532
fn test_value_is_number() {
543533
define_function_and_call("foo(1)", "foo", |args, _isolate, _ctx_scope| {
544-
if let Value::Double(_) = args.get(0) {
545-
assert!(true);
546-
} else {
547-
assert!(false, "The value should have been a number!");
548-
}
534+
assert!(args.get(0).is_number());
549535
None
550536
})
551537
.expect("Got error on function run");
@@ -557,11 +543,7 @@ mod json_path_tests {
557543
"foo(async function(){}())",
558544
"foo",
559545
|args, _isolate, _ctx_scope| {
560-
if let Value::Other(any) = args.get(0) {
561-
assert!(any.is_promise());
562-
} else {
563-
assert!(false, "The value should have been a number!");
564-
}
546+
assert!(args.get(0).is_promise());
565547
None
566548
},
567549
)
@@ -619,7 +601,7 @@ mod json_path_tests {
619601
new_native_function!(|_isolate, _ctx_scope, arg1: i64, arg2: f64, arg3: bool| {
620602
assert_eq!(arg1, 1);
621603
assert_eq!(arg2, 2.2);
622-
assert_eq!(arg3, true);
604+
assert!(arg3);
623605
Result::<Option<LocalValueAny>, String>::Ok(None)
624606
}),
625607
)
@@ -839,9 +821,8 @@ mod json_path_tests {
839821
assert_eq!(arg2, 2);
840822
if let Some(array) = arg3 {
841823
assert_eq!(array.len(), 2);
842-
assert!(true);
843824
} else {
844-
assert!(false, "Should have been an array.");
825+
unreachable!("Should have been an array.");
845826
}
846827
Result::<Option<LocalValueAny>, String>::Ok(None)
847828
}
@@ -864,11 +845,7 @@ mod json_path_tests {
864845
|_isolate, _ctx_scope, arg1: i64, arg2: i64, arg3: Option<types::Value>| {
865846
assert_eq!(arg1, 1);
866847
assert_eq!(arg2, 2);
867-
if let Some(Value::Array(_)) = arg3 {
868-
assert!(true);
869-
} else {
870-
assert!(false, "Should have been an array.");
871-
}
848+
assert!(arg3.unwrap().is_array());
872849
Result::<Option<LocalValueAny>, String>::Ok(None)
873850
}
874851
),

src/v8/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
44
* the Server Side Public License v1 (SSPLv1).
55
*/
6+
//! See [Context].
67
78
use crate::v8_c_raw::bindings::{
89
v8_ContextEnter, v8_FreeContext, v8_GetPrivateData, v8_NewContext, v8_ResetPrivateData,
@@ -46,6 +47,8 @@ impl<'context, 'data, T: 'data> Drop for V8ContextDataGuard<'context, 'data, T>
4647
}
4748
}
4849

50+
/// A sandboxed execution context with its own set of built-in objects
51+
/// and functions.
4952
pub struct Context {
5053
pub(crate) inner_ctx: *mut v8_context,
5154
}

src/v8/context_scope.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
44
* the Server Side Public License v1 (SSPLv1).
55
*/
6+
//! See [ContextScope].
67
78
use crate::v8_c_raw::bindings::v8_SetPrivateDataOnCtxRef;
89
use crate::v8_c_raw::bindings::{
@@ -65,6 +66,8 @@ impl<'context_scope, 'data, 'isolate_scope, 'isolate, T: 'data> Drop
6566
}
6667
}
6768

69+
/// A lifetime guard for [super::context::Context], which sets the
70+
/// execution context for all operations executed within a local scope.
6871
pub struct ContextScope<'isolate_scope, 'isolate> {
6972
pub(crate) inner_ctx_ref: *mut v8_context_ref,
7073
pub(crate) exit_on_drop: bool,
@@ -82,6 +85,7 @@ impl<'isolate_scope, 'isolate> ContextScope<'isolate_scope, 'isolate> {
8285
})
8386
}
8487

88+
/// Returns all the global variables attached to an object.
8589
pub fn get_globals(&self) -> LocalObject<'isolate_scope, 'isolate> {
8690
let inner_val = unsafe { v8_ContextRefGetGlobals(self.inner_ctx_ref) };
8791
LocalObject(ScopedValue {
@@ -175,6 +179,7 @@ impl<'isolate_scope, 'isolate> ContextScope<'isolate_scope, 'isolate> {
175179
ContextScopeDataGuard::new(index, self)
176180
}
177181

182+
/// Resets the private data at the provided slot.
178183
pub fn reset_private_data<I: Into<UserIndex>>(&self, index: I) {
179184
let index = index.into();
180185
self.reset_private_data_raw(index)
@@ -189,6 +194,9 @@ impl<'isolate_scope, 'isolate> ContextScope<'isolate_scope, 'isolate> {
189194
})
190195
}
191196

197+
/// Creates a new JavaScript object from the passed JavaScript
198+
/// string object. The string should contain the object structure in
199+
/// the JavaScript Object Notation (JSON).
192200
pub fn create_object_from_json(
193201
&self,
194202
val: &LocalString,
@@ -206,6 +214,9 @@ impl<'isolate_scope, 'isolate> ContextScope<'isolate_scope, 'isolate> {
206214
)
207215
}
208216

217+
/// Creates a JavaScript string for the provided JavaScript object
218+
/// containing its representation in the JavaScript Object Notation
219+
/// (JSON).
209220
pub fn json_stringify(
210221
&self,
211222
val: &LocalValueAny,
@@ -220,6 +231,7 @@ impl<'isolate_scope, 'isolate> ContextScope<'isolate_scope, 'isolate> {
220231
}))
221232
}
222233

234+
/// Creates a function. See [LocalNativeFunction].
223235
pub fn create_native_function<
224236
T: for<'d, 'c> Fn(
225237
&LocalNativeFunctionArgs<'d, 'c>,

0 commit comments

Comments
 (0)