Bug 538324 - Move ctypes into js/src. Part 5: Remove nsILocalFile option, and corresponding XPCOM dependency, for ctypes.open(). r=jorendorff
authorDan Witte <dwitte@mozilla.com>
Fri, 02 Apr 2010 13:07:02 -0700
changeset 40446 d052f11b495bfc278deed384c40dcc17f95f0398
parent 40445 7b252bd84c794a16ec75d8d361b1d6b3fcaadf04
child 40447 6cda3ed74baaace07cdc2341b1335e90de96f9ac
push id12610
push userrsayre@mozilla.com
push dateMon, 05 Apr 2010 17:26:41 +0000
treeherdermozilla-central@1942c0b4e101 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs538324
milestone1.9.3a4pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
Bug 538324 - Move ctypes into js/src. Part 5: Remove nsILocalFile option, and corresponding XPCOM dependency, for ctypes.open(). r=jorendorff
js/ctypes/tests/unit/test_jsctypes.js.in
js/src/ctypes/Library.cpp
--- a/js/ctypes/tests/unit/test_jsctypes.js.in
+++ b/js/ctypes/tests/unit/test_jsctypes.js.in
@@ -70,18 +70,18 @@ function do_check_class(obj, class, stac
   do_check_eq(Object.prototype.toString.call(obj), "[object " + class + "]", stack);
 }
 
 function run_test()
 {
   // Test ctypes.CType and ctypes.CData are set up correctly.
   run_abstract_class_tests();
 
-  // open the library with an nsILocalFile
-  let libfile = do_get_file(CTYPES_TEST_LIB);
+  // open the library
+  let libfile = do_get_file(CTYPES_TEST_LIB).path;
   let library = ctypes.open(libfile);
 
   // Make sure we can call a function in the library.
   run_void_tests(library);
 
   // Test Int64 and UInt64.
   run_Int64_tests();
   run_UInt64_tests();
@@ -188,31 +188,26 @@ function run_test()
   run_cast_tests();
 
   run_string_tests(library);
   run_struct_tests(library);
   run_function_tests(library);
   run_closure_tests(library);
   run_variadic_tests(library);
 
-  // test the string version of ctypes.open() as well
-  let libpath = libfile.path;
-  library = ctypes.open(libpath);
-  run_void_tests(library);
-
   // test library.close
   let test_void_t = library.declare("test_void_t_cdecl", ctypes.default_abi, ctypes.void_t);
   library.close();
   do_check_throws(function() { test_void_t(); }, Error);
   do_check_throws(function() {
     library.declare("test_void_t_cdecl", ctypes.default_abi, ctypes.void_t);
   }, Error);
 
   // test that library functions throw when bound to other objects
-  library = ctypes.open(libpath);
+  library = ctypes.open(libfile);
   let obj = {};
   obj.declare = library.declare;
   do_check_throws(function () { run_void_tests(obj); }, Error);
   obj.close = library.close;
   do_check_throws(function () { obj.close(); }, Error);
 
   // test that functions work as properties of other objects
   let getter = library.declare("get_int8_t_cdecl", ctypes.default_abi, ctypes.int8_t);
--- a/js/src/ctypes/Library.cpp
+++ b/js/src/ctypes/Library.cpp
@@ -36,20 +36,17 @@
  * 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 "jscntxt.h"
 #include "Library.h"
 #include "CTypes.h"
-#include "nsServiceManagerUtils.h"
-#include "nsIXPConnect.h"
-#include "nsILocalFile.h"
-#include "nsNativeCharsetUtils.h"
+#include "prlink.h"
 
 namespace js {
 namespace ctypes {
 
 /*******************************************************************************
 ** JSAPI function prototypes
 *******************************************************************************/
 
@@ -93,66 +90,47 @@ Library::Create(JSContext* cx, jsval aPa
   // initialize the library
   if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY, PRIVATE_TO_JSVAL(NULL)))
     return NULL;
 
   // attach API functions
   if (!JS_DefineFunctions(cx, libraryObj, sLibraryFunctions))
     return NULL;
 
-  nsresult rv;
-  PRLibrary* library;
-
-  // get the path argument. we accept either an nsILocalFile or a string path.
-  // determine which we have...
-  if (JSVAL_IS_STRING(aPath)) {
-    const PRUnichar* path = reinterpret_cast<const PRUnichar*>(
-      JS_GetStringCharsZ(cx, JSVAL_TO_STRING(aPath)));
-    if (!path)
-      return NULL;
+  if (!JSVAL_IS_STRING(aPath)) {
+    JS_ReportError(cx, "open takes a string argument");
+    return NULL;
+  }
 
-    // We don't use nsILocalFile, because it doesn't use the system search
-    // rules when resolving library path.
-    PRLibSpec libSpec;
+  PRLibSpec libSpec;
 #ifdef XP_WIN
-    // On Windows, converting to native charset may corrupt path string.
-    // So, we have to use Unicode path directly.
-    libSpec.value.pathname_u = path;
-    libSpec.type = PR_LibSpec_PathnameU;
+  // On Windows, converting to native charset may corrupt path string.
+  // So, we have to use Unicode path directly.
+  const PRUnichar* path = reinterpret_cast<const PRUnichar*>(
+    JS_GetStringCharsZ(cx, JSVAL_TO_STRING(aPath)));
+  if (!path)
+    return NULL;
+
+  libSpec.value.pathname_u = path;
+  libSpec.type = PR_LibSpec_PathnameU;
 #else
-    nsCAutoString nativePath;
-    NS_CopyUnicodeToNative(nsDependentString(path), nativePath);
-    libSpec.value.pathname = nativePath.get();
-    libSpec.type = PR_LibSpec_Pathname;
+  // Assume the JS string is not UTF-16, but is in the platform's native
+  // charset. (This basically means ASCII.) It would be nice to have a
+  // UTF-16 -> native charset implementation available. :(
+  const char* path = JS_GetStringBytesZ(cx, JSVAL_TO_STRING(aPath));
+  if (!path)
+    return NULL;
+
+  libSpec.value.pathname = path;
+  libSpec.type = PR_LibSpec_Pathname;
 #endif
-    library = PR_LoadLibraryWithFlags(libSpec, 0);
-    if (!library) {
-      JS_ReportError(cx, "couldn't open library");
-      return NULL;
-    }
 
-  } else if (!JSVAL_IS_PRIMITIVE(aPath)) {
-    nsCOMPtr<nsIXPConnect> xpc = do_GetService(nsIXPConnect::GetCID());
-
-    nsISupports* file = xpc->GetNativeOfWrapper(cx, JSVAL_TO_OBJECT(aPath));
-    nsCOMPtr<nsILocalFile> localFile = do_QueryInterface(file);
-    if (!localFile) {
-      JS_ReportError(cx, "open takes a string or nsILocalFile argument");
-      return NULL;
-    }
-
-    rv = localFile->Load(&library);
-    if (NS_FAILED(rv)) {
-      JS_ReportError(cx, "couldn't open library");
-      return NULL;
-    }
-
-  } else {
-    // don't convert the argument
-    JS_ReportError(cx, "open takes a string or nsIFile argument");
+  PRLibrary* library = PR_LoadLibraryWithFlags(libSpec, 0);
+  if (!library) {
+    JS_ReportError(cx, "couldn't open library");
     return NULL;
   }
 
   // stash the library
   if (!JS_SetReservedSlot(cx, libraryObj, SLOT_LIBRARY,
          PRIVATE_TO_JSVAL(library)))
     return NULL;