Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(value): add null #35

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/engine/include/includejs/engine_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class INCLUDEJS_ENGINE_EXPORT Context {
~Context();
#endif
auto make_undefined() const -> Value;
auto make_null() const -> Value;
tony-go marked this conversation as resolved.
Show resolved Hide resolved
auto make_error(const std::string &message) const -> Value;
auto make_object() const -> Value;
auto make_promise() const -> Promise;
Expand Down
1 change: 1 addition & 0 deletions src/engine/include/includejs/engine_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
tony-go marked this conversation as resolved.
Show resolved Hide resolved
auto to_number() const -> double;
auto to_string() const -> std::string;
auto to_boolean() const -> bool;
Expand Down
4 changes: 4 additions & 0 deletions src/engine/javascript_core/engine_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)};
tony-go marked this conversation as resolved.
Show resolved Hide resolved
}

auto Context::make_error(const std::string &message) const -> Value {
JSStringRef message_value = JSStringCreateWithUTF8CString(message.c_str());
JSValueRef error_arguments[]{
Expand Down
4 changes: 4 additions & 0 deletions src/engine/javascript_core/engine_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions test/engine/engine_value_null_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <gtest/gtest.h>

#include <includejs/engine.h>

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());
}