upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / TimeUtil / JSTimeDuration.cpp
index cfb1971..bc62448 100755 (executable)
@@ -18,7 +18,7 @@
 #include <string>
 #include <memory>
 #include <dpl/log/log.h>
-
+#include <cmath>
 #include <Commons/Exception.h>
 #include <CommonsJavaScript/Utils.h>
 #include <CommonsJavaScript/JSCallbackManager.h>
@@ -36,6 +36,7 @@ namespace Tizen1_0 {
 
 using namespace DPL;
 using namespace TizenApis::Commons;
+using namespace TizenApis::Api::TimeUtil;
 using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
 
@@ -100,10 +101,9 @@ void JSTimeDuration::initialize(JSContextRef context, JSObjectRef object)
        if (!JSObjectGetPrivate(object)) {
                LogDebug("Private object not set... setting it.");
                DurationProperties durations;
-               std::auto_ptr<DurationProperties> durationProps(new DurationProperties(
-                                                                  durations));
-               TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps.get());
-               durationProps.release();
+               DurationPropertiesPtr durationProps(new DurationProperties(durations));
+               TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
+
                if (!JSObjectSetPrivate(object, priv)) {
                        delete priv;
                }
@@ -157,35 +157,25 @@ bool JSTimeDuration::setProperty(JSContextRef context, JSObjectRef object,
                        ThrowMsg(NullPointerException, "Private object not initialized");
                }
 
-               DurationProperties *duration = privateObject->getObject();
+               DurationPropertiesPtr duration = privateObject->getObject();
                TimeUtilConverter convert(context);
 
-               if (JSValueIsNull(context, value) || JSValueIsUndefined(context, value)) {
-                       LogError("value is invald.");
-                       ThrowMsg(ConversionException, "value is invald.");
-               }
                if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_TIMEDURATION_LENGTH)) {
-                       if (!JSValueIsNumber(context, value)) {
-                               LogError("value is invald.");
-                               ThrowMsg(ConversionException, "value is invald.");
-                       }
                        duration->length = static_cast<long long>(convert.toDouble(value));
+                       if (std::isnan(duration->length)  || std::isinf(duration->length))
+                               duration->length = 0;
                } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN10_TIMEDURATION_UNIT)) {
-                       if (!JSValueIsString(context, value)) {
-                               LogError("value is invald.");
-                               ThrowMsg(ConversionException, "value is invald.");
-                       }
                        short unit = convert.toDurationUnit(convert.toString(value));
                        if (unit == 0xff)
-                               ThrowMsg(InvalidArgumentException, "property doesn't exist.");
+                               ThrowMsg(ConversionException, "property doesn't exist.");
                        duration->unit = unit;
                } else
-                       ThrowMsg(InvalidArgumentException, "property doesn't exist.");
+                       return false;
 
                return true;
        } Catch (NullPointerException) {
                LogError("NullPointerException: " << _rethrown_exception.GetMessage());
-               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
        } Catch (ConversionException) {
                LogError("ConversionException: " << _rethrown_exception.GetMessage());
                *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
@@ -202,10 +192,9 @@ bool JSTimeDuration::setProperty(JSContextRef context, JSObjectRef object,
 JSObjectRef JSTimeDuration::createJSObject(JSContextRef context,
         const DurationProperties &durations)
 {
-    std::auto_ptr<DurationProperties> durationProps(new DurationProperties(
-                                                       durations));
-    TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps.get());
-    durationProps.release();
+    DurationPropertiesPtr durationProps(new DurationProperties(durations));
+    TimeDurationPrivObject *priv = new TimeDurationPrivObject(context, durationProps);
+
     if (!priv) {
         ThrowMsg(NullPointerException, "Can not new an object");
     }
@@ -216,33 +205,30 @@ JSObjectRef JSTimeDuration::constructor(JSContextRef ctx, JSObjectRef constructo
        LogDebug("entered");
        TimeUtilConverter convert(ctx);
        Try {
-               if ((argumentCount < 1) || (argumentCount > 2)) {
-                       LogError("Wrong argument count");
-                       ThrowMsg(InvalidArgumentException, "Wrong TimeDuration argumentCount");
-               }
-
                TimeDurationPrivObject* mainPriv = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(constructor));
                JSContextRef global_context = mainPriv ? mainPriv->getContext() : ctx;
 
                DurationProperties duration;
-               if (JSValueIsNull(ctx, arguments[0]) || JSValueIsUndefined(ctx, arguments[0]) || !JSValueIsNumber(ctx, arguments[0])) {
-                       ThrowMsg(ConversionException, "Argument(length) is invalid");
-               }
-               duration.length = convert.toDouble(arguments[0]);
-               if (argumentCount == 2) {
-                       if (JSValueIsUndefined(ctx, arguments[1]) || !JSValueIsString(ctx, arguments[1])) {
-                               ThrowMsg(ConversionException, "Argument(unit) is invalid");
-                       } else if (!JSValueIsNull(ctx, arguments[1])) {
+               if (argumentCount == 0)
+                       duration.length = convert.toDouble(JSValueMakeUndefined(ctx));
+               else
+                       duration.length = convert.toDouble(arguments[0]);
+
+               if (std::isnan(duration.length) || std::isinf(duration.length))
+                       duration.length = 0;
+
+               if (argumentCount > 1) {
+                       if (!JSValueIsUndefined(ctx, arguments[1]) && !JSValueIsNull(ctx, arguments[1])) {
                                std::string unit = convert.toString(arguments[1]);
                                duration.unit = convert.toDurationUnit(unit);
                                if (duration.unit == 0xff)
-                                       ThrowMsg(InvalidArgumentException, "Argument(unit) is invalid(wrong type unit)");
+                                       ThrowMsg(ConversionException, "Argument(unit) is invalid(wrong type unit)");
                        }
                }
-               return createJSObject(global_context, duration);
+               return createJSObject(ctx, duration);
        } Catch(NullPointerException) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
-               *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::UNKNOWN_ERROR, "Unknown Error");
+               *exception = JSTizenExceptionFactory::makeErrorObject(ctx, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
                return NULL;
        } Catch(UnknownException) {
                LogError("Exception: " << _rethrown_exception.GetMessage());
@@ -274,33 +260,35 @@ bool JSTimeDuration::hasInstance(JSContextRef context,
 JSValueRef JSTimeDuration::diffTimeDuration(JSContextRef context, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception, CompareType type) {
        LogDebug("entered");
 
-               TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
+       AceSecurityStatus status = TIMEUTIL_CHECK_ACCESS(
+               TIMEUTIL_FUNCTION_API_READ_FUNCS);
+       TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
 
-               if (argumentCount != 1) {
-                       LogError("Wrong parameters");
-                       ThrowMsg(InvalidArgumentException, "Wrong parameters");
-               }
+       TimeDurationPrivObject* privateObject = static_cast<TimeDurationPrivObject*>(JSObjectGetPrivate(thisObject));
+       if (!privateObject) {
+               LogError("Private object is not set.");
+               return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type Mismatch");
+       }
 
-               AceSecurityStatus status = TIMEUTIL_CHECK_ACCESS(
-                       privateObject->getContext(),
-                       TIMEUTIL_FUNCTION_API_READ_FUNCS);
-               TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
-               
-               TimeUtilConverter converter(privateObject->getContext());
-
-               DurationProperties first = converter.getDurationPropertis(thisObject);
-               DurationProperties second = converter.getDurationPropertis(arguments[0]);
-
-               DurationProperties diff;
-               if (first.unit > second.unit) {
-                       long long firstLength = converter.convertDurationLength(first, second.unit);
-                       diff.unit = second.unit;
-                       diff.length = firstLength - second.length;
-               } else {
-                       long long secondLength = converter.convertDurationLength(second, first.unit);
-                       diff.unit = first.unit;
-                       diff.length = first.length - secondLength;
-               }
+       TimeUtilConverter converter(context);
+
+       DurationProperties first = converter.getDurationPropertis(thisObject);
+       DurationProperties second;
+       if (argumentCount == 0)
+               second= converter.getDurationPropertis(JSValueMakeUndefined(context));
+       else
+               second= converter.getDurationPropertis(arguments[0]);
+
+       DurationProperties diff;
+       if (first.unit > second.unit) {
+               long long firstLength = converter.convertDurationLength(first, second.unit);
+               diff.unit = second.unit;
+               diff.length = firstLength - second.length;
+       } else {
+               long long secondLength = converter.convertDurationLength(second, first.unit);
+               diff.unit = first.unit;
+               diff.length = first.length - secondLength;
+       }
 
        switch (type) {
                case EQUALSTO: