From 1ef594d7e9e83f7d313093014da3dcf166b50099 Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Wed, 21 Feb 2024 17:17:20 +0100 Subject: [PATCH 1/2] feat(value): add null Signed-off-by: Tony Gorez --- src/engine/include/includejs/engine_context.h | 1 + src/engine/include/includejs/engine_value.h | 1 + src/engine/javascript_core/engine_context.cc | 4 ++++ src/engine/javascript_core/engine_value.cc | 4 ++++ test/engine/CMakeLists.txt | 3 ++- test/engine/engine_value_null_test.cc | 16 ++++++++++++++++ 6 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/engine/engine_value_null_test.cc diff --git a/src/engine/include/includejs/engine_context.h b/src/engine/include/includejs/engine_context.h index f0e7cd4b..52f2e088 100644 --- a/src/engine/include/includejs/engine_context.h +++ b/src/engine/include/includejs/engine_context.h @@ -21,6 +21,7 @@ class INCLUDEJS_ENGINE_EXPORT Context { ~Context(); #endif auto make_undefined() const -> Value; + auto make_null() const -> Value; auto make_error(const std::string &message) const -> Value; auto make_object() const -> Value; auto make_promise() const -> Promise; diff --git a/src/engine/include/includejs/engine_value.h b/src/engine/include/includejs/engine_value.h index 966d9f87..c0b6696d 100644 --- a/src/engine/include/includejs/engine_value.h +++ b/src/engine/include/includejs/engine_value.h @@ -34,6 +34,7 @@ class INCLUDEJS_ENGINE_EXPORT Value { auto is_error() const -> bool; auto is_object() const -> bool; auto is_boolean() const -> bool; + auto is_null() const -> bool; auto to_number() const -> double; auto to_string() const -> std::string; auto to_boolean() const -> bool; diff --git a/src/engine/javascript_core/engine_context.cc b/src/engine/javascript_core/engine_context.cc index e8309d56..219d0bb1 100644 --- a/src/engine/javascript_core/engine_context.cc +++ b/src/engine/javascript_core/engine_context.cc @@ -29,6 +29,10 @@ auto Context::make_undefined() const -> Value { JSValueMakeUndefined(this->internal->context)}; } +auto Context::make_null() const -> Value { + return {this->internal->context, JSValueMakeNull(this->internal->context)}; +} + auto Context::make_error(const std::string &message) const -> Value { JSStringRef message_value = JSStringCreateWithUTF8CString(message.c_str()); JSValueRef error_arguments[]{ diff --git a/src/engine/javascript_core/engine_value.cc b/src/engine/javascript_core/engine_value.cc index 660aa40b..2ccd2e73 100644 --- a/src/engine/javascript_core/engine_value.cc +++ b/src/engine/javascript_core/engine_value.cc @@ -66,6 +66,10 @@ auto Value::is_boolean() const -> bool { return JSValueIsBoolean(this->internal->context, this->internal->value); } +auto Value::is_null() const -> bool { + return JSValueIsNull(this->internal->context, this->internal->value); +} + auto Value::to_number() const -> double { assert(is_number()); JSValueRef exception = nullptr; diff --git a/test/engine/CMakeLists.txt b/test/engine/CMakeLists.txt index 7d768c30..53492745 100644 --- a/test/engine/CMakeLists.txt +++ b/test/engine/CMakeLists.txt @@ -2,7 +2,8 @@ add_executable(includejs_engine_unit engine_binding_test.cc engine_evaluate_test.cc engine_stacktraces_test.cc - engine_value_object_test.cc) + engine_value_object_test.cc + engine_value_null_test.cc) sourcemeta_includejs_add_compile_options(includejs_engine_unit) diff --git a/test/engine/engine_value_null_test.cc b/test/engine/engine_value_null_test.cc new file mode 100644 index 00000000..27bc7dcf --- /dev/null +++ b/test/engine/engine_value_null_test.cc @@ -0,0 +1,16 @@ +#include + +#include + +TEST(IncludeJS_Engine, create_null) { + sourcemeta::includejs::Engine engine; + + auto null = engine.context().make_null(); + EXPECT_TRUE(null.is_null()); +} + +TEST(IncludeJS_Engine, evaluate_null) { + sourcemeta::includejs::Engine engine; + sourcemeta::includejs::Value result{engine.evaluate("null", "index.js")}; + EXPECT_TRUE(result.is_null()); +} From d8503df583c7ffe6e9c6cd141986e76f6bba422d Mon Sep 17 00:00:00 2001 From: Tony Gorez Date: Wed, 21 Feb 2024 18:20:44 +0100 Subject: [PATCH 2/2] fix: use .from instead Signed-off-by: Tony Gorez --- src/engine/include/includejs/engine_context.h | 1 - src/engine/javascript_core/engine_context.cc | 8 ++++---- test/engine/engine_value_null_test.cc | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/engine/include/includejs/engine_context.h b/src/engine/include/includejs/engine_context.h index 52f2e088..f0e7cd4b 100644 --- a/src/engine/include/includejs/engine_context.h +++ b/src/engine/include/includejs/engine_context.h @@ -21,7 +21,6 @@ class INCLUDEJS_ENGINE_EXPORT Context { ~Context(); #endif auto make_undefined() const -> Value; - auto make_null() const -> Value; auto make_error(const std::string &message) const -> Value; auto make_object() const -> Value; auto make_promise() const -> Promise; diff --git a/src/engine/javascript_core/engine_context.cc b/src/engine/javascript_core/engine_context.cc index 219d0bb1..74ba79c9 100644 --- a/src/engine/javascript_core/engine_context.cc +++ b/src/engine/javascript_core/engine_context.cc @@ -29,10 +29,6 @@ auto Context::make_undefined() const -> Value { JSValueMakeUndefined(this->internal->context)}; } -auto Context::make_null() const -> Value { - return {this->internal->context, JSValueMakeNull(this->internal->context)}; -} - auto Context::make_error(const std::string &message) const -> Value { JSStringRef message_value = JSStringCreateWithUTF8CString(message.c_str()); JSValueRef error_arguments[]{ @@ -84,6 +80,10 @@ auto Context::from(const char *value) const -> Value { return {this->internal->context, result}; } +auto Context::from(std::nullptr_t) const -> Value { + return {this->internal->context, JSValueMakeNull(this->internal->context)}; +} + auto Context::global() const -> Value { return {this->internal->context, this->internal->global}; } diff --git a/test/engine/engine_value_null_test.cc b/test/engine/engine_value_null_test.cc index 27bc7dcf..532af606 100644 --- a/test/engine/engine_value_null_test.cc +++ b/test/engine/engine_value_null_test.cc @@ -5,7 +5,7 @@ TEST(IncludeJS_Engine, create_null) { sourcemeta::includejs::Engine engine; - auto null = engine.context().make_null(); + auto null = engine.context().from(nullptr); EXPECT_TRUE(null.is_null()); }