[Common] Implementation of long long and unsigned long long conversions.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 2 Oct 2015 14:01:15 +0000 (16:01 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 5 Oct 2015 08:12:26 +0000 (17:12 +0900)
Currently, only MediaController uses such conversions.

[Verification] TCT pass rate (r47): 100% (162/162/0/0/0).

Change-Id: I589c1f66f204f90c5eb2f50948f4e04d9f6c256b
Signed-off-by: Pawel Andruszkiewicz <p.andruszkie@samsung.com>
src/utils/utils_api.js
src/utils/utils_instance.cc
src/utils/utils_instance.h

index dc70f76..9263c32 100755 (executable)
@@ -384,8 +384,12 @@ Converter.prototype.toLong = function(val, nullable) {
 };
 
 function _toLongLong(val) {
-  // TODO: how to implement this?
-  return _toLong(val);
+  // According to WebIDL specification this will not be a precise representation
+  // of requested val. We're converting the val to signed long and then pass it
+  // to C++ to get the value in required range.
+  return native_.getResultObject(native_.callSync('Utils_toLongLong', {
+    n : _toLong(val)
+  }));
 }
 
 Converter.prototype.toLongLong = function(val, nullable) {
@@ -401,8 +405,12 @@ Converter.prototype.toUnsignedLong = function(val, nullable) {
 };
 
 function _toUnsignedLongLong(val) {
-  // TODO: how to implement this?
-  return _toUnsignedLong(val);
+  // According to WebIDL specification this will not be a precise representation
+  // of requested val. We're converting the val to signed long and then pass it
+  // to C++ to get the value in required range.
+  return native_.getResultObject(native_.callSync('Utils_toUnsignedLongLong', {
+    n : _toLong(val)
+  }));
 }
 
 Converter.prototype.toUnsignedLongLong = function(val, nullable) {
index 45b0fb3..71d7aca 100644 (file)
@@ -31,6 +31,8 @@ UtilsInstance::UtilsInstance() {
   REGISTER_SYNC("Utils_getPkgApiVersion", GetPkgApiVersion);
   REGISTER_SYNC("Utils_checkPrivilegeAccess", CheckPrivilegeAccess);
   REGISTER_SYNC("Utils_checkBackwardCompabilityPrivilegeAccess", CheckBackwardCompabilityPrivilegeAccess);
+  REGISTER_SYNC("Utils_toLongLong", ToLongLong);
+  REGISTER_SYNC("Utils_toUnsignedLongLong", ToUnsignedLongLong);
 
 #undef REGISTER_SYNC
 #undef REGISTER_ASYNC
@@ -116,5 +118,56 @@ void UtilsInstance::CheckBackwardCompabilityPrivilegeAccess(const picojson::valu
   ReportSuccess(out);
 }
 
+namespace {
+
+template <typename T> int sgn(T val) {
+    return (T(0) < val) - (val < T(0));
+}
+
+const double kTwoPow63 = 9223372036854775808.0;
+const double kTwoPow64 = 18446744073709551616.0;
+
+}  // namespace
+
+void UtilsInstance::ToLongLong(const picojson::value& args,
+                               picojson::object& out) {
+  LoggerD("Entered");
+
+  const auto& n = args.get("n");
+  long long output = 0;
+
+  if (n.is<double>()) {
+    auto d = n.get<double>();
+    d = sgn<double>(d) * std::floor(std::fabs(d));
+    d = std::fmod(d, kTwoPow64);
+    if (d > kTwoPow63) {
+      d -= kTwoPow64;
+    }
+    output = static_cast<long long>(d);
+  }
+
+  ReportSuccess(picojson::value(static_cast<double>(output)), out);
+}
+
+void UtilsInstance::ToUnsignedLongLong(const picojson::value& args,
+                                       picojson::object& out) {
+  LoggerD("Entered");
+
+  const auto& n = args.get("n");
+  unsigned long long output = 0;
+
+  if (n.is<double>()) {
+    auto d = n.get<double>();
+    d = sgn<double>(d) * std::floor(std::fabs(d));
+    d = std::fmod(d, kTwoPow64);
+    if (d < 0.0) {
+      d += kTwoPow64;
+    }
+    output = static_cast<unsigned long long>(d);
+  }
+
+  ReportSuccess(picojson::value(static_cast<double>(output)), out);
+}
+
 }  // namespace utils
 }  // namespace extension
index 723f8c1..5c5e1d3 100644 (file)
@@ -20,6 +20,9 @@ class UtilsInstance : public common::ParsedInstance {
   void GetPkgApiVersion(const picojson::value& args, picojson::object& out);
   void CheckPrivilegeAccess(const picojson::value& args, picojson::object& out);
   void CheckBackwardCompabilityPrivilegeAccess(const picojson::value& args, picojson::object& out);
+
+  void ToLongLong(const picojson::value& args, picojson::object& out);
+  void ToUnsignedLongLong(const picojson::value& args, picojson::object& out);
 };
 }  // namespace utils
 }  // namespace extension