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..74ba79c9 100644 --- a/src/engine/javascript_core/engine_context.cc +++ b/src/engine/javascript_core/engine_context.cc @@ -80,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/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..532af606 --- /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().from(nullptr); + 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()); +}