Bug 742206 part 1. Introduce a simple mozilla::dom::Date for use in WebIDL bindings. r=smaug
authorBoris Zbarsky <bzbarsky@mit.edu>
Thu, 02 May 2013 14:38:19 -0400
changeset 141588 b89e5dc046a6ed8fe3fe4e715b4d36f0b84cc968
parent 141587 4c1074ad5172ebe7dd4a9948206366c9c23a9b94
child 141589 c4013f62540efc1c95fd7351006ecbb76b0a06a8
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs742206
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 742206 part 1. Introduce a simple mozilla::dom::Date for use in WebIDL bindings. r=smaug
dom/bindings/BindingDeclarations.h
dom/bindings/BindingUtils.cpp
--- a/dom/bindings/BindingDeclarations.h
+++ b/dom/bindings/BindingDeclarations.h
@@ -439,12 +439,40 @@ struct ParentObject {
     mObject(aObject),
     mWrapperCache(aCache)
   {}
 
   nsISupports* const mObject;
   nsWrapperCache* const mWrapperCache;
 };
 
+// Representation for dates
+class Date {
+public:
+  // Not inlining much here to avoid the extra includes we'd need
+  Date();
+  Date(double aMilliseconds) :
+    mMsecSinceEpoch(aMilliseconds)
+  {}
+
+  bool IsUndefined() const;
+  double TimeStamp() const
+  {
+    return mMsecSinceEpoch;
+  }
+  void SetTimeStamp(double aMilliseconds)
+  {
+    mMsecSinceEpoch = aMilliseconds;
+  }
+  // Can return false if CheckedUnwrap fails.  This will NOT throw;
+  // callers should do it as needed.
+  bool SetTimeStamp(JSContext* cx, JSObject* obj);
+
+  bool ToDateObject(JSContext* cx, JS::Value* vp) const;
+
+private:
+  double mMsecSinceEpoch;
+};
+
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_BindingDeclarations_h__
--- a/dom/bindings/BindingUtils.cpp
+++ b/dom/bindings/BindingUtils.cpp
@@ -3,26 +3,28 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <algorithm>
 #include <stdarg.h>
 
 #include "mozilla/DebugOnly.h"
+#include "mozilla/FloatingPoint.h"
 
 #include "BindingUtils.h"
 
 #include "AccessCheck.h"
 #include "nsContentUtils.h"
 #include "nsIXPConnect.h"
 #include "WrapperFactory.h"
 #include "xpcprivate.h"
 #include "XPCQuickStubs.h"
 #include "XrayWrapper.h"
+#include "jsfriendapi.h"
 
 #include "mozilla/dom/HTMLObjectElement.h"
 #include "mozilla/dom/HTMLObjectElementBinding.h"
 #include "mozilla/dom/HTMLSharedObjectElement.h"
 #include "mozilla/dom/HTMLEmbedElementBinding.h"
 #include "mozilla/dom/HTMLAppletElementBinding.h"
 
 namespace mozilla {
@@ -1707,10 +1709,48 @@ ReportLenientThisUnwrappingFailure(JSCon
 {
   GlobalObject global(cx, obj);
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(global.Get());
   if (window && window->GetDoc()) {
     window->GetDoc()->WarnOnceAbout(nsIDocument::eLenientThis);
   }
 }
 
+// Date implementation methods
+Date::Date() :
+  mMsecSinceEpoch(MOZ_DOUBLE_NaN())
+{
+}
+
+bool
+Date::IsUndefined() const
+{
+  return MOZ_DOUBLE_IS_NaN(mMsecSinceEpoch);
+}
+
+bool
+Date::SetTimeStamp(JSContext* cx, JSObject* objArg)
+{
+  JS::Rooted<JSObject*> obj(cx, objArg);
+  MOZ_ASSERT(JS_ObjectIsDate(cx, obj));
+
+  obj = js::CheckedUnwrap(obj);
+  // This really sucks: even if JS_ObjectIsDate, CheckedUnwrap can _still_ fail
+  if (!obj) {
+    return false;
+  }
+  mMsecSinceEpoch = js_DateGetMsecSinceEpoch(obj);
+  return true;
+}
+
+bool
+Date::ToDateObject(JSContext* cx, JS::Value* vp) const
+{
+  JSObject* obj = JS_NewDateObjectMsec(cx, mMsecSinceEpoch);
+  if (!obj) {
+    return false;
+  }
+  *vp = JS::ObjectValue(*obj);
+  return true;
+}
+
 } // namespace dom
 } // namespace mozilla