Bug 792668 - Simplify code shared between implementations of HANDLE. r=froydnj
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Sat, 22 Sep 2012 17:11:44 -0400
changeset 107825 3f95fd3795b615b9686d3f34ca0b47d4c271b47c
parent 107824 9d285bedbc1f2ff03db780382ceaf56a7dea3180
child 107826 0067a5de7114d5f8367240af804342fd03551340
push id23511
push userryanvm@gmail.com
push dateSun, 23 Sep 2012 11:59:11 +0000
treeherdermozilla-central@ec54f1bf1c83 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs792668
milestone18.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 792668 - Simplify code shared between implementations of HANDLE. r=froydnj
toolkit/components/osfile/osfile_shared_allthreads.jsm
toolkit/components/osfile/osfile_win_back.jsm
--- a/toolkit/components/osfile/osfile_shared_allthreads.jsm
+++ b/toolkit/components/osfile/osfile_shared_allthreads.jsm
@@ -27,29 +27,30 @@
      // that everybody uses the same definition of |OS|.
      if (typeof Components != "undefined") {
        Components.utils.import("resource://gre/modules/ctypes.jsm");
        Components.classes["@mozilla.org/net/osfileconstantsservice;1"].
          getService(Components.interfaces.nsIOSFileConstantsService).init();
      }
 
      // Define a lazy getter for a property
-     let defineLazyGetter = function(object, name, getter) {
+     let defineLazyGetter = function defineLazyGetter(object, name, getter) {
        Object.defineProperty(object, name, {
          configurable: true,
          get: function lazy() {
            delete this[name];
            let value = getter.call(this);
            Object.defineProperty(object, name, {
              value: value
            });
            return value;
          }
        });
      };
+     exports.OS.Shared.defineLazyGetter = defineLazyGetter;
 
      /**
       * A variable controlling whether we should printout logs.
       */
      exports.OS.Shared.DEBUG = false;
      let LOG;
      if (typeof console != "undefined" && console.log) {
        LOG = console.log.bind(console, "OS");
--- a/toolkit/components/osfile/osfile_win_back.jsm
+++ b/toolkit/components/osfile/osfile_win_back.jsm
@@ -67,54 +67,49 @@
        if (!exports.OS.Types) {
          exports.OS.Types = {};
        }
        let Type = exports.OS.Shared.Type;
        let Types = Type;
 
        // Initialize types
 
-       Types.HANDLE =
-         Types.voidptr_t.withName("HANDLE");
-
        /**
         * A C integer holding INVALID_HANDLE_VALUE in case of error or
         * a file descriptor in case of success.
         */
-       Types.maybe_HANDLE =
-         Types.HANDLE.withName("maybe_HANDLE");
-       Types.maybe_HANDLE.importFromC =
-         function maybe_HANDLE_importFromC(maybe) {
-           if (Types.int.cast(maybe).value == INVALID_HANDLE) {
-             // Ensure that API clients can effectively compare against
-             // Const.INVALID_HANDLE_VALUE. Without this cast,
-             // == would always return |false|.
-             return INVALID_HANDLE;
-           }
-         return ctypes.CDataFinalizer(maybe, _CloseHandle);
+       Types.HANDLE =
+         Types.voidptr_t.withName("HANDLE");
+       Types.HANDLE.importFromC = function importFromC(maybe) {
+         if (Types.int.cast(maybe).value == INVALID_HANDLE) {
+           // Ensure that API clients can effectively compare against
+           // Const.INVALID_HANDLE_VALUE. Without this cast,
+           // == would always return |false|.
+           return INVALID_HANDLE;
+         }
+         return ctypes.CDataFinalizer(maybe, this.finalizeHANDLE);
        };
+       Types.HANDLE.finalizeHANDLE = function placeholder() {
+         throw new Error("finalizeHANDLE should be implemented");
+       };
+       let INVALID_HANDLE = exports.OS.Constants.Win.INVALID_HANDLE_VALUE;
 
-       /**
-        * A C integer holding INVALID_HANDLE_VALUE in case of error or
-        * a file descriptor in case of success.
-        */
-       Types.maybe_find_HANDLE =
-         Types.HANDLE.withName("maybe_find_HANDLE");
-       Types.maybe_find_HANDLE.importFromC =
-         function maybe_find_HANDLE_importFromC(maybe) {
-           if (Types.int.cast(maybe).value == INVALID_HANDLE) {
-             // Ensure that API clients can effectively compare against
-             // Const.INVALID_HANDLE_VALUE. Without this cast,
-             // == would always return |false|.
-             return INVALID_HANDLE;
-           }
-         return ctypes.CDataFinalizer(maybe, _FindClose);
-       };
+       Types.file_HANDLE = Types.HANDLE.withName("file HANDLE");
+       exports.OS.Shared.defineLazyGetter(Types.file_HANDLE,
+         "finalizeHANDLE",
+         function() {
+           return _CloseHandle;
+         });
 
-       let INVALID_HANDLE = exports.OS.Constants.Win.INVALID_HANDLE_VALUE;
+       Types.find_HANDLE = Types.HANDLE.withName("find HANDLE");
+       exports.OS.Shared.defineLazyGetter(Types.find_HANDLE,
+         "finalizeHANDLE",
+         function() {
+           return _FindClose;
+         });
 
        Types.DWORD = Types.int32_t.withName("DWORD");
 
        /**
         * A C integer holding -1 in case of error or a positive integer
         * in case of success.
         */
        Types.negative_or_DWORD =
@@ -217,17 +212,17 @@
        WinFile.CreateDirectory =
          declareFFI("CreateDirectoryW", ctypes.winapi_abi,
                     /*return*/ Types.zero_or_nothing,
                     /*name*/   Types.jschar.in_ptr,
                     /*security*/Types.SECURITY_ATTRIBUTES.in_ptr);
 
        WinFile.CreateFile =
          declareFFI("CreateFileW", ctypes.winapi_abi,
-                    /*return*/  Types.maybe_HANDLE,
+                    /*return*/  Types.file_HANDLE,
                     /*name*/    Types.path,
                     /*access*/  Types.DWORD,
                     /*share*/   Types.DWORD,
                     /*security*/Types.SECURITY_ATTRIBUTES.in_ptr,
                     /*creation*/Types.DWORD,
                     /*flags*/   Types.DWORD,
                     /*template*/Types.HANDLE);
 
@@ -239,24 +234,24 @@
        WinFile.FileTimeToSystemTime =
          declareFFI("FileTimeToSystemTime", ctypes.winapi_abi,
                     /*return*/ Types.zero_or_nothing,
                     /*filetime*/Types.FILETIME.in_ptr,
                     /*systime*/ Types.SystemTime.out_ptr);
 
        WinFile.FindFirstFile =
          declareFFI("FindFirstFileW", ctypes.winapi_abi,
-                    /*return*/ Types.maybe_find_HANDLE,
+                    /*return*/ Types.find_HANDLE,
                     /*pattern*/Types.path,
                     /*data*/   Types.FindData.out_ptr);
 
        WinFile.FindNextFile =
          declareFFI("FindNextFileW", ctypes.winapi_abi,
                     /*return*/ Types.zero_or_nothing,
-                    /*prev*/   Types.HANDLE,
+                    /*prev*/   Types.find_HANDLE,
                     /*data*/   Types.FindData.out_ptr);
 
        WinFile.FormatMessage =
          declareFFI("FormatMessageW", ctypes.winapi_abi,
                     /*return*/ Types.DWORD,
                     /*flags*/  Types.DWORD,
                     /*source*/ Types.void_t.in_ptr,
                     /*msgid*/  Types.DWORD,