Skip to content

Commit

Permalink
Bug 1529758 - Add a pref for fields. r=tcampbell
Browse files Browse the repository at this point in the history
This creates a shell command-line option, `--enable-experimental-fields`, and a
Gecko pref, `javascript.options.experimental.fields`.

Both are off by default everywhere, for now.

Differential Revision: https://phabricator.services.mozilla.com/D22045
  • Loading branch information
Jason Orendorff committed Mar 12, 2019
1 parent 72b34f3 commit f251ba3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 16 deletions.
1 change: 1 addition & 0 deletions js/public/CompileOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class JS_PUBLIC_API TransitiveCompileOptions {
bool isProbablySystemCode = false;
bool hideScriptFromDebugger = false;
bool bigIntEnabledOption = false;
bool fieldsEnabledOption = false;

/**
* |introductionType| is a statically allocated C string: one of "eval",
Expand Down
9 changes: 4 additions & 5 deletions js/src/frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6779,11 +6779,10 @@ bool GeneralParser<ParseHandler, Unit>::classMember(
}

if (propType == PropertyType::Field) {
// TODO(khyperia): Delete the two lines below once fields are fully
// supported in the backend. We can't fail in BytecodeCompiler because of
// lazy parsing.
errorAt(propNameOffset, JSMSG_FIELDS_NOT_SUPPORTED);
return false;
if (!options().fieldsEnabledOption) {
errorAt(propNameOffset, JSMSG_FIELDS_NOT_SUPPORTED);
return false;
}

if (isStatic) {
errorAt(propNameOffset, JSMSG_BAD_METHOD_DEF);
Expand Down
7 changes: 4 additions & 3 deletions js/src/frontend/TokenStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1994,9 +1994,10 @@ MOZ_MUST_USE bool TokenStreamSpecific<Unit, AnyCharsAccess>::identifierName(
"Private identifier starts with #");
newPrivateNameToken(atom->asPropertyName(), start, modifier, out);

// TODO(khypera): Delete the below once private names are supported.
errorAt(start.offset(), JSMSG_FIELDS_NOT_SUPPORTED);
return false;
if (!anyCharsAccess().options().fieldsEnabledOption) {
errorAt(start.offset(), JSMSG_FIELDS_NOT_SUPPORTED);
return false;
}
} else {
newNameToken(atom->asPropertyName(), start, modifier, out);
}
Expand Down
6 changes: 4 additions & 2 deletions js/src/fuzz-tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ static JSObject* jsfuzz_createGlobal(JSContext* cx, JSPrincipals* principals) {
/* Create the global object. */
JS::RootedObject newGlobal(cx);
JS::RealmOptions options;
options.creationOptions().setStreamsEnabled(true);
options.creationOptions().setBigIntEnabled(true);
options.creationOptions()
.setStreamsEnabled(true)
.setBigIntEnabled(true)
.setFieldsEnabled(false);
newGlobal = JS_NewGlobalObject(cx, getGlobalClass(), principals,
JS::FireOnNewGlobalHook, options);
if (!newGlobal) {
Expand Down
6 changes: 4 additions & 2 deletions js/src/jsapi-tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ JSObject* JSAPITest::createGlobal(JSPrincipals* principals) {
/* Create the global object. */
JS::RootedObject newGlobal(cx);
JS::RealmOptions options;
options.creationOptions().setStreamsEnabled(true);
options.creationOptions().setBigIntEnabled(true);
options.creationOptions()
.setStreamsEnabled(true)
.setBigIntEnabled(true)
.setFieldsEnabled(true);
newGlobal = JS_NewGlobalObject(cx, getGlobalClass(), principals,
JS::FireOnNewGlobalHook, options);
if (!newGlobal) {
Expand Down
2 changes: 2 additions & 0 deletions js/src/jsapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,7 @@ void JS::TransitiveCompileOptions::copyPODTransitiveOptions(
isProbablySystemCode = rhs.isProbablySystemCode;
hideScriptFromDebugger = rhs.hideScriptFromDebugger;
bigIntEnabledOption = rhs.bigIntEnabledOption;
fieldsEnabledOption = rhs.fieldsEnabledOption;
};

void JS::ReadOnlyCompileOptions::copyPODOptions(
Expand Down Expand Up @@ -3552,6 +3553,7 @@ JS::CompileOptions::CompileOptions(JSContext* cx)
throwOnAsmJSValidationFailureOption =
cx->options().throwOnAsmJSValidationFailure();
bigIntEnabledOption = cx->realm()->creationOptions().getBigIntEnabled();
fieldsEnabledOption = cx->realm()->creationOptions().getFieldsEnabled();
}

CompileOptions& CompileOptions::setIntroductionInfoToCaller(
Expand Down
8 changes: 8 additions & 0 deletions js/src/jsapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ class JS_PUBLIC_API RealmCreationOptions {
sharedMemoryAndAtomics_(false),
streams_(false),
bigint_(false),
fields_(false),
secureContext_(false),
clampAndJitterTime_(true) {}

Expand Down Expand Up @@ -1045,6 +1046,12 @@ class JS_PUBLIC_API RealmCreationOptions {
return *this;
}

bool getFieldsEnabled() const { return fields_; }
RealmCreationOptions& setFieldsEnabled(bool flag) {
fields_ = flag;
return *this;
}

// This flag doesn't affect JS engine behavior. It is used by Gecko to
// mark whether content windows and workers are "Secure Context"s. See
// https://w3c.github.io/webappsec-secure-contexts/
Expand Down Expand Up @@ -1075,6 +1082,7 @@ class JS_PUBLIC_API RealmCreationOptions {
bool sharedMemoryAndAtomics_;
bool streams_;
bool bigint_;
bool fields_;
bool secureContext_;
bool clampAndJitterTime_;
};
Expand Down
9 changes: 6 additions & 3 deletions js/src/shell/js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ static bool enableTestWasmAwaitTier2 = false;
static bool enableAsyncStacks = false;
static bool enableStreams = false;
static bool enableBigInt = false;
static bool enableFields = false;
#ifdef JS_GC_ZEAL
static uint32_t gZealBits = 0;
static uint32_t gZealFrequency = 0;
Expand Down Expand Up @@ -3763,7 +3764,8 @@ static void SetStandardRealmOptions(JS::RealmOptions& options) {
options.creationOptions()
.setSharedMemoryAndAtomicsEnabled(enableSharedMemory)
.setBigIntEnabled(enableBigInt)
.setStreamsEnabled(enableStreams);
.setStreamsEnabled(enableStreams)
.setFieldsEnabled(enableFields);
}

static MOZ_MUST_USE bool CheckRealmOptions(JSContext* cx,
Expand Down Expand Up @@ -10186,6 +10188,7 @@ static bool SetContextOptions(JSContext* cx, const OptionParser& op) {
enableAsyncStacks = !op.getBoolOption("no-async-stacks");
enableStreams = !op.getBoolOption("no-streams");
enableBigInt = !op.getBoolOption("no-bigint");
enableFields = op.getBoolOption("enable-experimental-fields");

JS::ContextOptionsRef(cx)
.setBaseline(enableBaseline)
Expand Down Expand Up @@ -10897,8 +10900,8 @@ int main(int argc, char** argv, char** envp) {
!op.addBoolOption('\0', "enable-streams",
"Enable WHATWG Streams (default)") ||
!op.addBoolOption('\0', "no-streams", "Disable WHATWG Streams") ||
!op.addBoolOption('\0', "no-bigint",
"Disable experimental BigInt support") ||
!op.addBoolOption('\0', "no-bigint", "Disable BigInt support") ||
!op.addBoolOption('\0', "enable-experimental-fields", "Enable fields in classes") ||
!op.addStringOption('\0', "shared-memory", "on/off",
"SharedArrayBuffer and Atomics "
#if SHARED_MEMORY_DEFAULT
Expand Down
5 changes: 4 additions & 1 deletion js/xpconnect/src/XPCJSContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,14 @@ bool xpc::ExtraWarningsForSystemJS() { return false; }
static mozilla::Atomic<bool> sSharedMemoryEnabled(false);
static mozilla::Atomic<bool> sStreamsEnabled(false);
static mozilla::Atomic<bool> sBigIntEnabled(false);
static mozilla::Atomic<bool> sFieldsEnabled(false);

void xpc::SetPrefableRealmOptions(JS::RealmOptions& options) {
options.creationOptions()
.setSharedMemoryAndAtomicsEnabled(sSharedMemoryEnabled)
.setBigIntEnabled(sBigIntEnabled)
.setStreamsEnabled(sStreamsEnabled);
.setStreamsEnabled(sStreamsEnabled)
.setFieldsEnabled(sFieldsEnabled);
}

static void ReloadPrefsCallback(const char* pref, XPCJSContext* xpccx) {
Expand Down Expand Up @@ -847,6 +849,7 @@ static void ReloadPrefsCallback(const char* pref, XPCJSContext* xpccx) {
sSharedMemoryEnabled =
Preferences::GetBool(JS_OPTIONS_DOT_STR "shared_memory");
sStreamsEnabled = Preferences::GetBool(JS_OPTIONS_DOT_STR "streams");
sFieldsEnabled = Preferences::GetBool(JS_OPTIONS_DOT_STR "experimental.fields");

#ifdef DEBUG
sExtraWarningsForSystemJS =
Expand Down
6 changes: 6 additions & 0 deletions modules/libpref/init/StaticPrefList.h
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,12 @@ VARCACHE_PREF(
RelaxedAtomicBool, false
)

VARCACHE_PREF(
"javascript.options.experimental.fields",
javascript_options_experimental_fields,
RelaxedAtomicBool, false
)


//---------------------------------------------------------------------------
// Media prefs
Expand Down
2 changes: 2 additions & 0 deletions modules/libpref/init/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,8 @@ pref("javascript.options.streams", true);
// BigInt API
pref("javascript.options.bigint", false);

pref("javascript.options.experimental.fields", false);

// Dynamic module import.
pref("javascript.options.dynamicImport", true);

Expand Down

0 comments on commit f251ba3

Please sign in to comment.