Skip to content

Commit f689c23

Browse files
committed
feat: javascript helpers extension
fix #881
1 parent 06abddc commit f689c23

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/lib/Support/JavaScript.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ Value::~Value()
119119
}
120120

121121
Value::Value(Value const& other)
122-
: impl_(other.impl_), ctx_(other.ctx_), val_(to_handle(jerry_value_copy(to_js(other.val_))))
122+
: impl_(other.impl_), ctx_(other.ctx_), val_(0)
123123
{
124+
if (other.val_)
125+
val_ = to_handle(jerry_value_copy(to_js(other.val_)));
124126
}
125127

126128
Value::Value(Value&& other) noexcept
@@ -138,7 +140,7 @@ Value& Value::operator=(Value const& other)
138140
jerry_value_free(to_js(val_));
139141
impl_ = other.impl_;
140142
ctx_ = other.ctx_;
141-
val_ = to_handle(jerry_value_copy(to_js(other.val_)));
143+
val_ = other.val_ ? to_handle(jerry_value_copy(to_js(other.val_))) : 0;
142144
return *this;
143145
}
144146

@@ -720,15 +722,24 @@ static dom::Value toDomValue(jerry_value_t v, std::shared_ptr<Context::Impl> con
720722
if (jerry_value_is_function(v))
721723
{
722724
// Wrap the JS function so it can be invoked from DOM helpers.
723-
jerry_value_t fn = jerry_value_copy(v);
724-
return dom::makeVariadicInvocable([fn, impl](dom::Array const& args) -> Expected<dom::Value, Error>
725+
auto fnHandle = std::shared_ptr<jerry_value_t>(
726+
new jerry_value_t(jerry_value_copy(v)),
727+
[](jerry_value_t* h)
728+
{
729+
if (!h)
730+
return;
731+
jerry_value_free(*h);
732+
delete h;
733+
});
734+
735+
return dom::makeVariadicInvocable([fnHandle, impl](dom::Array const& args) -> Expected<dom::Value, Error>
725736
{
726737
std::vector<jerry_value_t> jsArgs;
727738
jsArgs.reserve(args.size());
728739
for (auto const& a : args)
729740
jsArgs.push_back(toJsValue(a, impl));
730741

731-
jerry_value_t ret = jerry_call(fn, jerry_undefined(), jsArgs.data(), jsArgs.size());
742+
jerry_value_t ret = jerry_call(*fnHandle, jerry_undefined(), jsArgs.data(), jsArgs.size());
732743
for (auto& a : jsArgs) jerry_value_free(a);
733744
if (jerry_value_is_exception(ret))
734745
{

0 commit comments

Comments
 (0)