Bug 799640 - Part 1: Refactor JSObjectBuilder. r=ehsan
☠☠ backed out by fd22236ffb15 ☠ ☠
authorBenoit Girard <b56girard@gmail.com>
Fri, 30 Nov 2012 12:46:59 -0500
changeset 124470 e47b059493cfb024e03b446701ede2e677d0f8bf
parent 124469 b88467155adc05de38258af53d51e2ba381a1284
child 124471 b130bb991d84db729555ad5b837dc3c5eb1a74a7
push id297
push userlsblakk@mozilla.com
push dateTue, 26 Mar 2013 17:28:00 +0000
treeherdermozilla-release@64d7b45c34e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs799640
milestone20.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 799640 - Part 1: Refactor JSObjectBuilder. r=ehsan
tools/profiler/JSObjectBuilder.cpp
tools/profiler/JSObjectBuilder.h
tools/profiler/Makefile.in
new file mode 100644
--- /dev/null
+++ b/tools/profiler/JSObjectBuilder.cpp
@@ -0,0 +1,149 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "jsapi.h"
+#include "nsStringGlue.h"
+#include "JSObjectBuilder.h"
+
+JSObjectBuilder::JSObjectBuilder(JSContext *aCx) : mCx(aCx), mOk(JS_TRUE)
+{}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, JSObject *aValue)
+{
+  if (!mOk)
+    return;
+
+  mOk = JS_DefineProperty(mCx, aObject, name, OBJECT_TO_JSVAL(aValue), NULL, NULL, JSPROP_ENUMERATE);
+}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, int value)
+{
+  if (!mOk)
+    return;
+
+  mOk = JS_DefineProperty(mCx, aObject, name, INT_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
+}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, double value)
+{
+  if (!mOk)
+    return;
+
+  mOk = JS_DefineProperty(mCx, aObject, name, DOUBLE_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
+}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, nsAString &value)
+{
+  if (!mOk)
+    return;
+
+  const nsString &flat = PromiseFlatString(value);
+  JSString *string = JS_NewUCStringCopyN(mCx, static_cast<const jschar*>(flat.get()), flat.Length());
+  if (!string)
+    mOk = JS_FALSE;
+
+  if (!mOk)
+    return;
+
+  mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
+}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, const char *value, size_t valueLength)
+{
+  if (!mOk)
+    return;
+
+  JSString *string = JS_InternStringN(mCx, value, valueLength);
+  if (!string) {
+    mOk = JS_FALSE;
+    return;
+  }
+
+  mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
+}
+
+void
+JSObjectBuilder::DefineProperty(JSObject *aObject, const char *name, const char *value)
+{
+  DefineProperty(aObject, name, value, strlen(value));
+}
+
+void
+JSObjectBuilder::ArrayPush(JSObject *aArray, int value)
+{
+  if (!mOk)
+    return;
+
+  jsval objval = INT_TO_JSVAL(value);
+  uint32_t length;
+  mOk = JS_GetArrayLength(mCx, aArray, &length);
+
+  if (!mOk)
+    return;
+
+  mOk = JS_SetElement(mCx, aArray, length, &objval);
+}
+
+void
+JSObjectBuilder::ArrayPush(JSObject *aArray, const char *value)
+{
+  if (!mOk)
+    return;
+
+  JSString *string = JS_NewStringCopyN(mCx, value, strlen(value));
+  if (!string) {
+    mOk = JS_FALSE;
+    return;
+  }
+
+  jsval objval = STRING_TO_JSVAL(string);
+  uint32_t length;
+  mOk = JS_GetArrayLength(mCx, aArray, &length);
+
+  if (!mOk)
+    return;
+
+  mOk = JS_SetElement(mCx, aArray, length, &objval);
+}
+
+void
+JSObjectBuilder::ArrayPush(JSObject *aArray, JSObject *aObject)
+{
+  if (!mOk)
+    return;
+
+  jsval objval = OBJECT_TO_JSVAL(aObject);
+  uint32_t length;
+  mOk = JS_GetArrayLength(mCx, aArray, &length);
+
+  if (!mOk)
+    return;
+
+  mOk = JS_SetElement(mCx, aArray, length, &objval);
+}
+
+JSObject*
+JSObjectBuilder::CreateArray() {
+  JSObject *array = JS_NewArrayObject(mCx, 0, NULL);
+  if (!array)
+    mOk = JS_FALSE;
+
+  return array;
+}
+
+JSObject*
+JSObjectBuilder::CreateObject() {
+  JSObject *obj = JS_NewObject(mCx, NULL, NULL, NULL);
+  if (!obj)
+    mOk = JS_FALSE;
+
+  return obj;
+}
+
--- a/tools/profiler/JSObjectBuilder.h
+++ b/tools/profiler/JSObjectBuilder.h
@@ -1,153 +1,39 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 "jsapi.h"
+class JSObject;
+class JSObjectBuilder;
+class nsAString;
 
 /* this is handy wrapper around JSAPI to make it more pleasant to use.
  * We collect the JSAPI errors and so that callers don't need to */
 class JSObjectBuilder
 {
-  public:
-
-  void DefineProperty(JSObject *aObject, const char *name, JSObject *aValue)
-  {
-    if (!mOk)
-      return;
-
-    mOk = JS_DefineProperty(mCx, aObject, name, OBJECT_TO_JSVAL(aValue), NULL, NULL, JSPROP_ENUMERATE);
-  }
-
-  void DefineProperty(JSObject *aObject, const char *name, int value)
-  {
-    if (!mOk)
-      return;
-
-    mOk = JS_DefineProperty(mCx, aObject, name, INT_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
-  }
-
-  void DefineProperty(JSObject *aObject, const char *name, double value)
-  {
-    if (!mOk)
-      return;
-
-    mOk = JS_DefineProperty(mCx, aObject, name, DOUBLE_TO_JSVAL(value), NULL, NULL, JSPROP_ENUMERATE);
-  }
-
-  void DefineProperty(JSObject *aObject, const char *name, nsAString &value)
-  {
-    if (!mOk)
-      return;
-
-    const nsString &flat = PromiseFlatString(value);
-    JSString *string = JS_NewUCStringCopyN(mCx, static_cast<const jschar*>(flat.get()), flat.Length());
-    if (!string)
-      mOk = JS_FALSE;
-
-    if (!mOk)
-      return;
-
-    mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
-  }
-
-  void DefineProperty(JSObject *aObject, const char *name, const char *value, size_t valueLength)
-  {
-    if (!mOk)
-      return;
-
-    JSString *string = JS_InternStringN(mCx, value, valueLength);
-    if (!string) {
-      mOk = JS_FALSE;
-      return;
-    }
-
-    mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
-  }
-
-  void DefineProperty(JSObject *aObject, const char *name, const char *value)
-  {
-    DefineProperty(aObject, name, value, strlen(value));
-  }
-
-  void ArrayPush(JSObject *aArray, int value)
-  {
-    if (!mOk)
-      return;
-
-    jsval objval = INT_TO_JSVAL(value);
-    uint32_t length;
-    mOk = JS_GetArrayLength(mCx, aArray, &length);
-
-    if (!mOk)
-      return;
-
-    mOk = JS_SetElement(mCx, aArray, length, &objval);
-  }
-
-  void ArrayPush(JSObject *aArray, const char *value)
-  {
-    if (!mOk)
-      return;
-
-    JSString *string = JS_NewStringCopyN(mCx, value, strlen(value));
-    if (!string) {
-      mOk = JS_FALSE;
-      return;
-    }
-
-    jsval objval = STRING_TO_JSVAL(string);
-    uint32_t length;
-    mOk = JS_GetArrayLength(mCx, aArray, &length);
-
-    if (!mOk)
-      return;
-
-    mOk = JS_SetElement(mCx, aArray, length, &objval);
-  }
-
-  void ArrayPush(JSObject *aArray, JSObject *aObject)
-  {
-    if (!mOk)
-      return;
-
-    jsval objval = OBJECT_TO_JSVAL(aObject);
-    uint32_t length;
-    mOk = JS_GetArrayLength(mCx, aArray, &length);
-
-    if (!mOk)
-      return;
-
-    mOk = JS_SetElement(mCx, aArray, length, &objval);
-  }
-
-  JSObject *CreateArray() {
-    JSObject *array = JS_NewArrayObject(mCx, 0, NULL);
-    if (!array)
-      mOk = JS_FALSE;
-
-    return array;
-  }
-
-  JSObject *CreateObject() {
-    JSObject *obj = JS_NewObject(mCx, NULL, NULL, NULL);
-    if (!obj)
-      mOk = JS_FALSE;
-
-    return obj;
-  }
-
+public:
 
   // We need to ensure that this object lives on the stack so that GC sees it properly
-  JSObjectBuilder(JSContext *aCx) : mCx(aCx), mOk(JS_TRUE)
-  {
-  }
-  private:
+  JSObjectBuilder(JSContext *aCx);
+
+  void DefineProperty(JSObject *aObject, const char *name, JSObject *aValue);
+  void DefineProperty(JSObject *aObject, const char *name, int value);
+  void DefineProperty(JSObject *aObject, const char *name, double value);
+  void DefineProperty(JSObject *aObject, const char *name, nsAString &value);
+  void DefineProperty(JSObject *aObject, const char *name, const char *value, size_t valueLength);
+  void DefineProperty(JSObject *aObject, const char *name, const char *value);
+  void ArrayPush(JSObject *aArray, int value);
+  void ArrayPush(JSObject *aArray, const char *value);
+  void ArrayPush(JSObject *aArray, JSObject *aObject);
+  JSObject *CreateArray();
+  JSObject *CreateObject();
+
+private:
   JSObjectBuilder(JSObjectBuilder&);
 
   JSContext *mCx;
   JSObject *mObj;
-  JSBool mOk;
+  int mOk;
 };
 
 
--- a/tools/profiler/Makefile.in
+++ b/tools/profiler/Makefile.in
@@ -58,16 +58,17 @@ LIBRARY_NAME    = profiler
 EXPORT_LIBRARY  = 1
 LIBXUL_LIBRARY  = 1
 IS_COMPONENT    = 1
 
 CPPSRCS		= \
   nsProfilerFactory.cpp \
   nsProfiler.cpp \
   TableTicker.cpp \
+  JSObjectBuilder.cpp \
   $(NULL)
 
 XPIDLSRCS = \
   nsIProfiler.idl \
   $(NULL)
 
 EXTRA_JS_MODULES = \
   Profiler.jsm \