Skip to content

Commit 2acb1e2

Browse files
committed
feat: javascript helpers extension
fix #881
1 parent 2d0d6ae commit 2acb1e2

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/lib/Support/JavaScript.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,19 @@ dom::Value Value::getDom() const
301301

302302
std::string Value::getString() const { return std::string(getDom().getString()); }
303303
bool Value::getBool() const { return getDom().getBool(); }
304-
std::int64_t Value::getInteger() const { return getDom().getInteger(); }
304+
std::int64_t Value::getInteger() const
305+
{
306+
auto lock = lockContext(impl_);
307+
double d = jerry_value_as_number(to_js(val_));
308+
309+
// Mirror legacy behavior: convert any JS number to an integer by
310+
// truncating toward zero instead of requiring an integral DOM value.
311+
if (d >= (double)std::numeric_limits<std::int64_t>::max())
312+
return std::numeric_limits<std::int64_t>::max();
313+
if (d <= (double)std::numeric_limits<std::int64_t>::min())
314+
return std::numeric_limits<std::int64_t>::min();
315+
return static_cast<std::int64_t>(d);
316+
}
305317
double Value::getDouble() const
306318
{
307319
auto lock = lockContext(impl_);

0 commit comments

Comments
 (0)