From 2aaa62ae41b0ebb83fa8d1ba3e501b81c7242352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 5 Jan 2020 00:08:27 +0100 Subject: [PATCH] Add v8::Boolean::new() (#184) --- src/binding.cc | 8 ++------ src/primitives.rs | 20 +++++++++----------- tests/test_api.rs | 30 ++++++++++++++++-------------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/binding.cc b/src/binding.cc index 7661af15..a680153b 100644 --- a/src/binding.cc +++ b/src/binding.cc @@ -528,12 +528,8 @@ v8::Primitive* v8__Undefined(v8::Isolate* isolate) { return local_to_ptr(v8::Undefined(isolate)); } -v8::Boolean* v8__True(v8::Isolate* isolate) { - return local_to_ptr(v8::True(isolate)); -} - -v8::Boolean* v8__False(v8::Isolate* isolate) { - return local_to_ptr(v8::False(isolate)); +v8::Boolean* v8__Boolean__New(v8::Isolate* isolate, bool value) { + return local_to_ptr(v8::Boolean::New(isolate, value)); } v8::PrimitiveArray* v8__PrimitiveArray__New(v8::Isolate* isolate, int length) { diff --git a/src/primitives.rs b/src/primitives.rs index bb05390d..d557df3d 100644 --- a/src/primitives.rs +++ b/src/primitives.rs @@ -9,9 +9,7 @@ extern "C" { fn v8__Undefined(isolate: *mut Isolate) -> *mut Primitive; - fn v8__True(isolate: *mut Isolate) -> *mut Boolean; - - fn v8__False(isolate: *mut Isolate) -> *mut Boolean; + fn v8__Boolean__New(isolate: *mut Isolate, value: bool) -> *mut Boolean; } pub fn new_null<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Primitive> { @@ -26,12 +24,12 @@ pub fn new_undefined<'sc>( unsafe { scope.to_local(ptr) }.unwrap() } -pub fn new_true<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Boolean> { - let ptr = unsafe { v8__True(scope.isolate()) }; - unsafe { scope.to_local(ptr) }.unwrap() -} - -pub fn new_false<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Boolean> { - let ptr = unsafe { v8__False(scope.isolate()) }; - unsafe { scope.to_local(ptr) }.unwrap() +impl Boolean { + pub fn new<'sc>( + scope: &mut impl ToLocal<'sc>, + value: bool, + ) -> Local<'sc, Boolean> { + let ptr = unsafe { v8__Boolean__New(scope.isolate(), value) }; + unsafe { scope.to_local(ptr) }.unwrap() + } } diff --git a/tests/test_api.rs b/tests/test_api.rs index 75f2c951..9905e102 100644 --- a/tests/test_api.rs +++ b/tests/test_api.rs @@ -432,7 +432,7 @@ fn terminate_execution() { let mut context = v8::Context::new(scope); context.enter(); let result = eval(scope, context, "true").unwrap(); - let true_val = v8::new_true(scope).into(); + let true_val = v8::Boolean::new(scope, true).into(); assert!(result.same_value(true_val)); context.exit(); } @@ -460,7 +460,7 @@ fn terminate_execution() { let mut context = v8::Context::new(scope); context.enter(); let result = eval(scope, context, "true").unwrap(); - let true_val = v8::new_true(scope).into(); + let true_val = v8::Boolean::new(scope, true).into(); assert!(result.same_value(true_val)); context.exit(); } @@ -633,12 +633,12 @@ fn script_origin() { let resource_name = v8::String::new(s, "foo.js").unwrap(); let resource_line_offset = v8::Integer::new(s, 4); let resource_column_offset = v8::Integer::new(s, 5); - let resource_is_shared_cross_origin = v8::new_true(s); + let resource_is_shared_cross_origin = v8::Boolean::new(s, true); let script_id = v8::Integer::new(s, 123); let source_map_url = v8::String::new(s, "source_map_url").unwrap(); - let resource_is_opaque = v8::new_true(s); - let is_wasm = v8::new_false(s); - let is_module = v8::new_false(s); + let resource_is_opaque = v8::Boolean::new(s, true); + let is_wasm = v8::Boolean::new(s, false); + let is_module = v8::Boolean::new(s, false); let script_origin = v8::ScriptOrigin::new( resource_name.into(), @@ -728,12 +728,14 @@ fn test_primitives() { assert!(!undefined.is_null()); assert!(undefined.is_null_or_undefined()); - let true_ = v8::new_true(scope); + let true_ = v8::Boolean::new(scope, true); + assert!(true_.is_true()); assert!(!true_.is_undefined()); assert!(!true_.is_null()); assert!(!true_.is_null_or_undefined()); - let false_ = v8::new_false(scope); + let false_ = v8::Boolean::new(scope, false); + assert!(false_.is_false()); assert!(!false_.is_undefined()); assert!(!false_.is_null()); assert!(!false_.is_null_or_undefined()); @@ -1177,12 +1179,12 @@ fn mock_script_origin<'sc>( let resource_name = v8_str(scope, resource_name_); let resource_line_offset = v8::Integer::new(scope, 0); let resource_column_offset = v8::Integer::new(scope, 0); - let resource_is_shared_cross_origin = v8::new_true(scope); + let resource_is_shared_cross_origin = v8::Boolean::new(scope, true); let script_id = v8::Integer::new(scope, 123); let source_map_url = v8_str(scope, "source_map_url"); - let resource_is_opaque = v8::new_true(scope); - let is_wasm = v8::new_false(scope); - let is_module = v8::new_true(scope); + let resource_is_opaque = v8::Boolean::new(scope, true); + let is_wasm = v8::Boolean::new(scope, false); + let is_module = v8::Boolean::new(scope, true); v8::ScriptOrigin::new( resource_name.into(), resource_line_offset, @@ -1536,7 +1538,7 @@ fn snapshot_creator() { let mut script = v8::Script::compile(scope, context, source, None).unwrap(); let result = script.run(scope, context).unwrap(); - let true_val = v8::new_true(scope).into(); + let true_val = v8::Boolean::new(scope, true).into(); assert!(result.same_value(true_val)); context.exit(); } @@ -1985,7 +1987,7 @@ fn try_from_local() { } { - let value: v8::Local = v8::new_true(scope).into(); + let value: v8::Local = v8::Boolean::new(scope, true).into(); let primitive = v8::Local::::try_from(value).unwrap(); let _boolean = v8::Local::::try_from(value).unwrap(); let _boolean = v8::Local::::try_from(primitive).unwrap();