From a09d392711e6e4a416e9d051c8555c190efe182d Mon Sep 17 00:00:00 2001 From: Romain Marcadier Date: Tue, 1 Feb 2022 01:15:49 +0100 Subject: [PATCH] Make v8::script_compiler::Source.get_cached_data return Option (#885) The `cached_data` property of `Source` is optional, so reading the value should return `Option<&CachedData>`. --- src/script_compiler.rs | 11 +++++++++-- tests/test_api.rs | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/script_compiler.rs b/src/script_compiler.rs index 2273187a..23c0268b 100644 --- a/src/script_compiler.rs +++ b/src/script_compiler.rs @@ -143,8 +143,15 @@ impl Source { } } - pub fn get_cached_data(&self) -> &CachedData { - unsafe { &*v8__ScriptCompiler__Source__GetCachedData(self) } + pub fn get_cached_data(&self) -> Option<&CachedData> { + unsafe { + let cached_data = v8__ScriptCompiler__Source__GetCachedData(self); + if cached_data.is_null() { + None + } else { + Some(&*cached_data) + } + } } } diff --git a/tests/test_api.rs b/tests/test_api.rs index f73c1bb7..76c0d0a8 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -2348,6 +2348,8 @@ fn script_compiler_source() { Some(&script_origin), ); + assert!(source.get_cached_data().is_none()); + let result = v8::script_compiler::compile_module(scope, source); assert!(result.is_some()); } @@ -5320,6 +5322,7 @@ fn create_module<'s>( false, true, ); + let has_cache = code_cache.is_some(); let source = match code_cache { Some(x) => v8::script_compiler::Source::new_with_cached_data( source, @@ -5328,6 +5331,7 @@ fn create_module<'s>( ), None => v8::script_compiler::Source::new(source, Some(&script_origin)), }; + assert_eq!(source.get_cached_data().is_some(), has_cache); let module = v8::script_compiler::compile_module2( scope, source,