Bug 990353 - Add a pref for system source discarding. r=bent
--- a/b2g/app/b2g.js
+++ b/b2g/app/b2g.js
@@ -596,16 +596,23 @@ pref("dom.forms.number", true);
// Don't enable <input type=color> yet as we don't have a color picker
// implemented for b2g (bug 875751)
pref("dom.forms.color", false);
// Turns on gralloc-based direct texturing for Gonk
pref("gfx.gralloc.enabled", false);
+// This preference instructs the JS engine to discard the
+// source of any privileged JS after compilation. This saves
+// memory, but makes things like Function.prototype.toSource()
+// fail.
+// XXXbholley - we flip this to true in the last patch.
+pref("javascript.options.discardSystemSource", false);
+
// XXXX REMOVE FOR PRODUCTION. Turns on GC and CC logging
pref("javascript.options.mem.log", false);
// Increase mark slice time from 10ms to 30ms
pref("javascript.options.mem.gc_incremental_slice_ms", 30);
// Increase time to get more high frequency GC on benchmarks from 1000ms to 1500ms
pref("javascript.options.mem.gc_high_frequency_time_limit_ms", 1500);
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -35,16 +35,17 @@
#include "jsfriendapi.h"
#include "jsprf.h"
#include "js/MemoryMetrics.h"
#include "js/OldDebugAPI.h"
#include "mozilla/dom/GeneratedAtomList.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/WindowBinding.h"
+#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "AccessCheck.h"
#include "nsGlobalWindow.h"
#include "nsAboutProtocolUtils.h"
#include "GeckoProfiler.h"
#include "nsIXULRuntime.h"
#include "nsJSPrincipals.h"
@@ -79,21 +80,20 @@ const char* const XPCJSRuntime::mStrings
"__iterator__", // IDX_ITERATOR
"__exposedProps__", // IDX_EXPOSEDPROPS
"eval", // IDX_EVAL
"controllers", // IDX_CONTROLLERS
};
/***************************************************************************/
-struct CX_AND_XPCRT_Data
-{
- JSContext* cx;
- XPCJSRuntime* rt;
-};
+static mozilla::Atomic<bool> sDiscardSystemSource(false);
+
+bool
+xpc::ShouldDiscardSystemSource() { return sDiscardSystemSource; }
static void * const UNMARK_ONLY = nullptr;
static void * const UNMARK_AND_SWEEP = (void *)1;
static PLDHashOperator
NativeInterfaceSweeper(PLDHashTable *table, PLDHashEntryHdr *hdr,
uint32_t number, void *arg)
{
@@ -1555,16 +1555,18 @@ ReloadPrefsCallback(const char *pref, vo
bool parallelParsing = Preferences::GetBool(JS_OPTIONS_DOT_STR "parallel_parsing");
bool parallelIonCompilation = Preferences::GetBool(JS_OPTIONS_DOT_STR
"ion.parallel_compilation");
bool useBaselineEager = Preferences::GetBool(JS_OPTIONS_DOT_STR
"baselinejit.unsafe_eager_compilation");
bool useIonEager = Preferences::GetBool(JS_OPTIONS_DOT_STR "ion.unsafe_eager_compilation");
+ sDiscardSystemSource = Preferences::GetBool(JS_OPTIONS_DOT_STR "discardSystemSource");
+
JS::RuntimeOptionsRef(rt).setBaseline(useBaseline)
.setIon(useIon)
. setAsmJS(useAsmJS);
JS_SetParallelParsingEnabled(rt, parallelParsing);
JS_SetParallelIonCompilationEnabled(rt, parallelIonCompilation);
JS_SetGlobalJitCompilerOption(rt, JSJITCOMPILER_BASELINE_USECOUNT_TRIGGER,
useBaselineEager ? 0 : -1);
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -473,16 +473,21 @@ void
SimulateActivityCallback(bool aActive);
void
RecordAdoptedNode(JSCompartment *c);
void
RecordDonatedNode(JSCompartment *c);
+// This function may be used off-main-thread, in which case it is benignly
+// racey.
+bool
+ShouldDiscardSystemSource();
+
} // namespace xpc
namespace mozilla {
namespace dom {
typedef JSObject*
(*DefineInterface)(JSContext *cx, JS::Handle<JSObject*> global,
JS::Handle<jsid> id, bool defineOnGlobal);
--- a/modules/libpref/src/init/all.js
+++ b/modules/libpref/src/init/all.js
@@ -780,16 +780,21 @@ pref("javascript.options.strict",
#ifdef DEBUG
pref("javascript.options.strict.debug", true);
#endif
pref("javascript.options.baselinejit", true);
pref("javascript.options.ion", true);
pref("javascript.options.asmjs", true);
pref("javascript.options.parallel_parsing", true);
pref("javascript.options.ion.parallel_compilation", true);
+// This preference instructs the JS engine to discard the
+// source of any privileged JS after compilation. This saves
+// memory, but makes things like Function.prototype.toSource()
+// fail.
+pref("javascript.options.discardSystemSource", false);
// This preference limits the memory usage of javascript.
// If you want to change these values for your device,
// please find Bug 417052 comment 17 and Bug 456721
// Comment 32 and Bug 613551.
pref("javascript.options.mem.high_water_mark", 128);
pref("javascript.options.mem.max", -1);
pref("javascript.options.mem.gc_per_compartment", true);
pref("javascript.options.mem.gc_incremental", true);