Skip to content

Commit 06abddc

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

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/lib/Support/JavaScript.cpp

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
// JerryScript-backed JavaScript bridge for MrDocs
33
//
44

5-
#include <mrdocs/Support/JavaScript.hpp>
5+
#include <mrdocs/Dom.hpp>
66
#include <mrdocs/Support/Handlebars.hpp>
7-
#include <mrdocs/Support/Report.hpp>
7+
#include <mrdocs/Support/JavaScript.hpp>
88
#include <mrdocs/Support/Path.hpp>
9-
#include <mrdocs/Dom.hpp>
10-
11-
#include <jerryscript.h>
12-
13-
#include <cctype>
9+
#include <mrdocs/Support/Report.hpp>
1410
#include <algorithm>
11+
#include <cctype>
1512
#include <format>
13+
#include <jerryscript.h>
1614
#include <limits>
1715
#include <memory>
1816
#include <mutex>
@@ -35,7 +33,10 @@ static std::string toString(jerry_value_t v)
3533
{
3634
jerry_value_t str = jerry_value_to_string(v);
3735
if (jerry_value_is_exception(str))
36+
{
37+
jerry_value_free(str);
3838
return "<error>";
39+
}
3940
jerry_size_t sz = jerry_string_size(str, JERRY_ENCODING_UTF8);
4041
std::string out(sz, '\0');
4142
jerry_string_to_buffer(str, JERRY_ENCODING_UTF8, (jerry_char_t*)out.data(), sz);
@@ -51,7 +52,10 @@ static Error makeError(jerry_value_t exc)
5152
}
5253
jerry_value_t msg_prop = jerry_object_get(exc, jerry_string_sz("message"));
5354
if (jerry_value_is_exception(msg_prop))
55+
{
56+
jerry_value_free(msg_prop);
5457
return Error("JavaScript error");
58+
}
5559
std::string msg = toString(msg_prop);
5660
jerry_value_free(msg_prop);
5761
return Error(msg.empty() ? "JavaScript error" : msg);
@@ -369,7 +373,10 @@ Value Value::get(std::string_view name) const
369373
jerry_value_t v = jerry_object_get(obj, k);
370374
jerry_value_free(k);
371375
if (jerry_value_is_exception(v))
376+
{
377+
jerry_value_free(v);
372378
return {};
379+
}
373380
return Value::fromJs(nullptr, to_handle(v), impl_);
374381
}
375382

@@ -393,7 +400,11 @@ Expected<Value, Error> Value::callImpl(std::span<const dom::Value> args) const
393400
jerry_value_t ret = jerry_call(fn, jerry_undefined(), jsArgs.data(), jsArgs.size());
394401
for (auto& a : jsArgs) jerry_value_free(a);
395402
if (jerry_value_is_exception(ret))
396-
return Unexpected(makeError(ret));
403+
{
404+
auto err = makeError(ret);
405+
jerry_value_free(ret);
406+
return Unexpected(err);
407+
}
397408
return Value::fromJs(nullptr, to_handle(ret), impl_);
398409
}
399410

@@ -459,7 +470,11 @@ Expected<Value, Error> Scope::eval(std::string_view script)
459470
std::scoped_lock<std::recursive_mutex> lk(impl_->mtx);
460471
jerry_value_t res = jerry_eval((const jerry_char_t*)script.data(), script.size(), JERRY_PARSE_NO_OPTS);
461472
if (jerry_value_is_exception(res))
462-
return Unexpected(makeError(res));
473+
{
474+
auto err = makeError(res);
475+
jerry_value_free(res);
476+
return Unexpected(err);
477+
}
463478
return Value::fromJs(nullptr, res, impl_);
464479
}
465480

@@ -587,7 +602,11 @@ Expected<Value, Error> Scope::getGlobal(std::string_view name)
587602
jerry_value_free(realm);
588603
jerry_value_free(k);
589604
if (jerry_value_is_exception(v))
590-
return Unexpected(makeError(v));
605+
{
606+
auto err = makeError(v);
607+
jerry_value_free(v);
608+
return Unexpected(err);
609+
}
591610
return Value::fromJs(nullptr, v, impl_);
592611
}
593612

0 commit comments

Comments
 (0)