author Igor Bukanov <>
Mon, 19 Mar 2012 15:27:58 +0100
changeset 89777 ca0782c1f3d5deb2986793580662a11e07935dd7
parent 87967 9dde6af30ab201c2fdb0084e298182533eac48ff
child 90996 d5057ff02ffb9786f62ca69ebc22cf11aa86f612
child 106001 cea47dfc3fb7a47fd12e48fd7305a703f637788c
permissions -rw-r--r--
bug 736978 - remove JS_FinalizeStub. r=:billm Currently the GC finalizes on the background thread only objects with null JSClass::finalize. However, this implies that any object that uses JS_FinalizeStub for the finalizer would be prevented from the background finalization. To fix this the patch removes JS_FinalizeStub replacing it with NULL in all cases when the class has no custom finalizer. For style consistency the patch also removed the usage of JSCLASS_NO_OPTIONAL_MEMBERS in the static declarations as the compiler fills the missing fields with null in any cases.

/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * The Original Code is Web Workers.
 * The Initial Developer of the Original Code is
 *   The Mozilla Foundation.
 * Portions created by the Initial Developer are Copyright (C) 2011
 * the Initial Developer. All Rights Reserved.
 * Contributor(s):
 *   Ben Turner <> (Original Author)
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * ***** END LICENSE BLOCK ***** */

#include "Navigator.h"

#include "jsapi.h"
#include "jsfriendapi.h"

#include "nsTraceRefcnt.h"

#include "RuntimeService.h"



namespace {

class Navigator
  static JSClass sClass;
  static JSPropertySpec sProperties[];

  enum SLOT {
    SLOT_appName = 0,


  static JSObject*
  InitClass(JSContext* aCx, JSObject* aObj)
    return JS_InitClass(aCx, aObj, NULL, &sClass, Construct, 0, sProperties,
                        NULL, NULL, NULL);

  static JSObject*
  Create(JSContext* aCx)
    RuntimeService* rts = RuntimeService::GetService();

    const RuntimeService::NavigatorStrings& strings =

    JSString* appName, *version, *platform, *userAgent;

#define COPY_STRING(_jsstr, _str)                                              \
  if (strings. _str .IsEmpty()) {                                              \
    _jsstr = NULL;                                                             \
  }                                                                            \
  else if (!(_jsstr = JS_NewUCStringCopyN(aCx, strings. _str .get(),           \
                                          strings. _str .Length()))) {         \
    return NULL;                                                               \

    COPY_STRING(appName, mAppName);
    COPY_STRING(version, mAppVersion);
    COPY_STRING(platform, mPlatform);
    COPY_STRING(userAgent, mUserAgent);


    JSObject* obj = JS_NewObject(aCx, &sClass, NULL, NULL);
    if (!obj) {
      return NULL;

    jsval empty = JS_GetEmptyStringValue(aCx);

    JS_SetReservedSlot(obj, SLOT_appName,
                       appName ? STRING_TO_JSVAL(appName) : empty);
    JS_SetReservedSlot(obj, SLOT_appVersion,
                       version ? STRING_TO_JSVAL(version) : empty);
    JS_SetReservedSlot(obj, SLOT_platform,
                       platform ? STRING_TO_JSVAL(platform) : empty);
    JS_SetReservedSlot(obj, SLOT_userAgent,
                       userAgent ? STRING_TO_JSVAL(userAgent) : empty);

    Navigator* priv = new Navigator();
    JS_SetPrivate(obj, priv);

    return obj;



  static JSBool
  Construct(JSContext* aCx, unsigned aArgc, jsval* aVp)
    JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL, JSMSG_WRONG_CONSTRUCTOR,
    return false;

  static void
  Finalize(JSContext* aCx, JSObject* aObj)
    JS_ASSERT(JS_GetClass(aObj) == &sClass);
    delete static_cast<Navigator*>(JS_GetPrivate(aObj));

  static JSBool
  GetProperty(JSContext* aCx, JSObject* aObj, jsid aIdval, jsval* aVp)
    JSClass* classPtr = JS_GetClass(aObj);
    if (classPtr != &sClass) {
      JS_ReportErrorNumber(aCx, js_GetErrorMessage, NULL,
                           JSMSG_INCOMPATIBLE_PROTO,, "GetProperty",
      return false;

    JS_ASSERT(JSID_TO_INT(aIdval) >= 0 && JSID_TO_INT(aIdval) < SLOT_COUNT);

    *aVp = JS_GetReservedSlot(aObj, JSID_TO_INT(aIdval));
    return true;

JSClass Navigator::sClass = {
  JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
  JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, Finalize

JSPropertySpec Navigator::sProperties[] = {
  { "appName", SLOT_appName, PROPERTY_FLAGS, GetProperty, 
    js_GetterOnlyPropertyStub },
  { "appVersion", SLOT_appVersion, PROPERTY_FLAGS, GetProperty, 
    js_GetterOnlyPropertyStub },
  { "platform", SLOT_platform, PROPERTY_FLAGS, GetProperty, 
    js_GetterOnlyPropertyStub },
  { "userAgent", SLOT_userAgent, PROPERTY_FLAGS, GetProperty, 
    js_GetterOnlyPropertyStub },
  { 0, 0, 0, NULL, NULL }

} // anonymous namespace


namespace navigator {

InitClass(JSContext* aCx, JSObject* aGlobal)
  return !!Navigator::InitClass(aCx, aGlobal);

Create(JSContext* aCx)
  return Navigator::Create(aCx);

} // namespace navigator