--- a/js/src/configure
+++ b/js/src/configure
@@ -99,6 +99,8 @@ ac_help="$ac_help
   --with-nspr-exec-prefix=PFX
                           Exec prefix where NSPR is installed"
 ac_help="$ac_help
+  --enable-system-ffi       Use system libffi (located with pkgconfig)"
+ac_help="$ac_help
   --with-arm-kuser         Use kuser helpers (Linux/ARM only -- requires kernel 2.6.13 or later)"
 ac_help="$ac_help
   --enable-ui-locale=ab-CD
@@ -13204,19 +13206,128 @@ rm -f conftest*
     CFLAGS=$_SAVE_CFLAGS
 fi
 
+# Check whether --enable-system-ffi or --disable-system-ffi was given.
+if test "${enable_system_ffi+set}" = set; then
+  enableval="$enable_system_ffi"
+  if test "$enableval" = "yes"; then
+    MOZ_NATIVE_FFI=1 
+  elif test "$enableval" = "no"; then
+    :
+  else
+    { echo "configure: error: Option, system-ffi, does not take an argument ($enableval)." 1>&2; exit 1; }
+  fi
+fi
+
+
+if test -n "$MOZ_NATIVE_FFI"; then
+    succeeded=no
+
+  if test -z "$PKG_CONFIG"; then
+    # Extract the first word of "pkg-config", so it can be a program name with args.
+set dummy pkg-config; ac_word=$2
+echo $ac_n "checking for $ac_word""... $ac_c" 1>&6
+echo "configure:13210: checking for $ac_word" >&5
+if eval "test \"`echo '$''{'ac_cv_path_PKG_CONFIG'+set}'`\" = set"; then
+  echo $ac_n "(cached) $ac_c" 1>&6
+else
+  case "$PKG_CONFIG" in
+  /*)
+  ac_cv_path_PKG_CONFIG="$PKG_CONFIG" # Let the user override the test with a path.
+  ;;
+  ?:/*)			 
+  ac_cv_path_PKG_CONFIG="$PKG_CONFIG" # Let the user override the test with a dos path.
+  ;;
+  *)
+  IFS="${IFS= 	}"; ac_save_ifs="$IFS"; IFS=":"
+  ac_dummy="$PATH"
+  for ac_dir in $ac_dummy; do 
+    test -z "$ac_dir" && ac_dir=.
+    if test -f $ac_dir/$ac_word; then
+      ac_cv_path_PKG_CONFIG="$ac_dir/$ac_word"
+      break
+    fi
+  done
+  IFS="$ac_save_ifs"
+  test -z "$ac_cv_path_PKG_CONFIG" && ac_cv_path_PKG_CONFIG="no"
+  ;;
+esac
+fi
+PKG_CONFIG="$ac_cv_path_PKG_CONFIG"
+if test -n "$PKG_CONFIG"; then
+  echo "$ac_t""$PKG_CONFIG" 1>&6
+else
+  echo "$ac_t""no" 1>&6
+fi
+
+  fi
+
+  if test "$PKG_CONFIG" = "no" ; then
+     echo "*** The pkg-config script could not be found. Make sure it is"
+     echo "*** in your path, or set the PKG_CONFIG environment variable"
+     echo "*** to the full path to pkg-config."
+     echo "*** Or see http://www.freedesktop.org/software/pkgconfig to get pkg-config."
+  else
+     PKG_CONFIG_MIN_VERSION=0.9.0
+     if $PKG_CONFIG --atleast-pkgconfig-version $PKG_CONFIG_MIN_VERSION; then
+        echo $ac_n "checking for libffi""... $ac_c" 1>&6
+echo "configure:13254: checking for libffi" >&5
+
+        if $PKG_CONFIG --exists "libffi" ; then
+            echo "$ac_t""yes" 1>&6
+            succeeded=yes
+
+            echo $ac_n "checking MOZ_FFI_CFLAGS""... $ac_c" 1>&6
+echo "configure:13261: checking MOZ_FFI_CFLAGS" >&5
+            MOZ_FFI_CFLAGS=`$PKG_CONFIG --cflags "libffi"`
+            echo "$ac_t""$MOZ_FFI_CFLAGS" 1>&6
+
+            echo $ac_n "checking MOZ_FFI_LIBS""... $ac_c" 1>&6
+echo "configure:13266: checking MOZ_FFI_LIBS" >&5
+            ## Remove evil flags like -Wl,--export-dynamic
+            MOZ_FFI_LIBS="`$PKG_CONFIG --libs \"libffi\" |sed s/-Wl,--export-dynamic//g`"
+            echo "$ac_t""$MOZ_FFI_LIBS" 1>&6
+        else
+            MOZ_FFI_CFLAGS=""
+            MOZ_FFI_LIBS=""
+            ## If we have a custom action on failure, don't print errors, but 
+            ## do set a variable so people can do so.
+            MOZ_FFI_PKG_ERRORS=`$PKG_CONFIG --errors-to-stdout --print-errors "libffi"`
+            echo $MOZ_FFI_PKG_ERRORS
+        fi
+
+        
+        
+     else
+        echo "*** Your version of pkg-config is too old. You need version $PKG_CONFIG_MIN_VERSION or newer."
+        echo "*** See http://www.freedesktop.org/software/pkgconfig"
+     fi
+  fi
+
+  if test $succeeded = yes; then
+     :
+  else
+     if test "$COMPILE_ENVIRONMENT"; then 
+       { echo "configure: error: Library requirements (libffi) not met; consider adjusting the PKG_CONFIG_PATH environment variable if your libraries are in a nonstandard prefix so pkg-config can find them." 1>&2; exit 1; }
+     fi
+  fi
+
+fi
+
+
+
 
 # Application
 
 BUILD_STATIC_LIBS=
 ENABLE_TESTS=1
 
 MOZ_THUMB2=
 USE_ARM_KUSER=
 
 case "${target}" in
     arm-android-eabi)
         USE_ARM_KUSER=1
         MOZ_THUMB2=1
         ;;
 esac
 
@@ -15741,9 +15851,13 @@ s%@HAVE_GCC3_ABI@%$HAVE_GCC3_ABI%g
 s%@NSPR_CFLAGS@%$NSPR_CFLAGS%g
 s%@NSPR_LIBS@%$NSPR_LIBS%g
 s%@NSPR_CONFIG@%$NSPR_CONFIG%g
+s%@PKG_CONFIG@%$PKG_CONFIG%g
+s%@MOZ_FFI_CFLAGS@%$MOZ_FFI_CFLAGS%g
+s%@MOZ_FFI_LIBS@%$MOZ_FFI_LIBS%g
+s%@MOZ_NATIVE_FFI@%$MOZ_NATIVE_FFI%g
 s%@MOZ_UI_LOCALE@%$MOZ_UI_LOCALE%g
 s%@MOZ_THUMB2@%$MOZ_THUMB2%g
 s%@MOZ_ARM_ARCH@%$MOZ_ARM_ARCH%g
 s%@MOZ_OPTIMIZE@%$MOZ_OPTIMIZE%g
 s%@MOZ_OPTIMIZE_FLAGS@%$MOZ_OPTIMIZE_FLAGS%g
 s%@MOZ_OPTIMIZE_LDFLAGS@%$MOZ_OPTIMIZE_LDFLAGS%g
@@ -16121,7 +16235,7 @@ echo "$ac_t""invoking make to create js-
 $GMAKE js-config
 
 # Build jsctypes if it's enabled.
-if test "$JS_HAS_CTYPES"; then
+if test "$JS_HAS_CTYPES" -a -z "$MOZ_NATIVE_FFI"; then
   # Run the libffi 'configure' script.
   ac_configure_args="--disable-shared --enable-static --disable-raw-api"
   if test "$MOZ_DEBUG"; then