Bug 1865406 - Use JS::Prefs to add browser preference r=jandem

Even though it supports adding preferences to JS Shell, I kept the custom
`--enable-json-parse-with-source` because it makes the shell-option flags very
slightly easier in jstests.

Differential Revision: https://phabricator.services.mozilla.com/D202615
This commit is contained in:
Bryan Thrall 2024-03-20 16:30:58 +00:00
parent 9d2a3bd102
commit 77e3e18357
6 changed files with 23 additions and 35 deletions

View File

@ -178,14 +178,6 @@ class JS_PUBLIC_API RealmCreationOptions {
return *this;
}
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
bool getJSONParseWithSource() const { return jsonParseWithSource; }
RealmCreationOptions& setJSONParseWithSource(bool flag) {
jsonParseWithSource = flag;
return *this;
}
#endif
// 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/
@ -246,9 +238,6 @@ class JS_PUBLIC_API RealmCreationOptions {
bool defineSharedArrayBufferConstructor_ = true;
bool coopAndCoep_ = false;
bool toSource_ = false;
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
bool jsonParseWithSource = false;
#endif
bool secureContext_ = false;
bool freezeBuiltins_ = false;

View File

@ -24,6 +24,7 @@
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit
#include "js/Object.h" // JS::GetBuiltinClass
#include "js/Prefs.h" // JS::Prefs
#include "js/PropertySpec.h"
#include "js/StableStringChars.h"
#include "js/TypeDecls.h"
@ -1768,7 +1769,7 @@ static bool InternalizeJSONProperty(
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
RootedObject context(cx);
Rooted<UniquePtr<ParseRecordObject::EntryMap>> entries(cx);
if (cx->realm()->creationOptions().getJSONParseWithSource()) {
if (JS::Prefs::experimental_json_parse_with_source()) {
// https://tc39.es/proposal-json-parse-with-source/#sec-internalizejsonproperty
bool sameVal = false;
Rooted<Value> parsedValue(cx, parseRecord.get().value);
@ -1919,7 +1920,7 @@ static bool InternalizeJSONProperty(
RootedValue keyVal(cx, StringValue(key));
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
if (cx->realm()->creationOptions().getJSONParseWithSource()) {
if (JS::Prefs::experimental_json_parse_with_source()) {
RootedValue contextVal(cx, ObjectValue(*context));
return js::Call(cx, reviver, holder, keyVal, val, contextVal, vp);
}
@ -1940,7 +1941,7 @@ static bool Revive(JSContext* cx, HandleValue reviver,
}
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
MOZ_ASSERT_IF(cx->realm()->creationOptions().getJSONParseWithSource(),
MOZ_ASSERT_IF(JS::Prefs::experimental_json_parse_with_source(),
pro.get().value == vp.get());
#endif
Rooted<jsid> id(cx, NameToId(cx->names().empty_));
@ -1962,8 +1963,7 @@ bool js::ParseJSONWithReviver(JSContext* cx,
/* https://262.ecma-international.org/14.0/#sec-json.parse steps 2-10. */
Rooted<ParseRecordObject> pro(cx);
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
if (cx->realm()->creationOptions().getJSONParseWithSource() &&
IsCallable(reviver)) {
if (JS::Prefs::experimental_json_parse_with_source() && IsCallable(reviver)) {
Rooted<JSONReviveParser<CharT>> parser(cx, cx, chars);
if (!parser.get().parse(vp, &pro)) {
return false;

View File

@ -729,9 +729,6 @@ bool shell::enableSourcePragmas = true;
bool shell::enableAsyncStacks = false;
bool shell::enableAsyncStackCaptureDebuggeeOnly = false;
bool shell::enableToSource = false;
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
bool shell::enableJSONParseWithSource = false;
#endif
bool shell::enableImportAttributes = false;
bool shell::enableImportAttributesAssertSyntax = false;
#ifdef JS_GC_ZEAL
@ -4123,11 +4120,7 @@ static void SetStandardRealmOptions(JS::RealmOptions& options) {
options.creationOptions()
.setSharedMemoryAndAtomicsEnabled(enableSharedMemory)
.setCoopAndCoepEnabled(false)
.setToSourceEnabled(enableToSource)
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
.setJSONParseWithSource(enableJSONParseWithSource)
#endif
;
.setToSourceEnabled(enableToSource);
}
[[nodiscard]] static bool CheckRealmOptions(JSContext* cx,
@ -12430,6 +12423,14 @@ bool SetGlobalOptionsPreJSInit(const OptionParser& op) {
JS::Prefs::setAtStartup_experimental_uint8array_base64(true);
}
#endif
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
JS::Prefs::setAtStartup_experimental_json_parse_with_source(
op.getBoolOption("enable-json-parse-with-source"));
#else
if (op.getBoolOption("enable-json-parse-with-source")) {
fprintf(stderr, "JSON.parse with source is not enabled on this build.\n");
}
#endif
if (op.getBoolOption("disable-weak-refs")) {
JS::Prefs::setAtStartup_weakrefs(false);
@ -12649,13 +12650,6 @@ bool SetContextOptions(JSContext* cx, const OptionParser& op) {
enableAsyncStackCaptureDebuggeeOnly =
op.getBoolOption("async-stacks-capture-debuggee-only");
enableToSource = !op.getBoolOption("disable-tosource");
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
enableJSONParseWithSource = op.getBoolOption("enable-json-parse-with-source");
#else
if (op.getBoolOption("enable-json-parse-with-source")) {
fprintf(stderr, "JSON.parse with source is not enabled on this build.\n");
}
#endif
enableImportAttributesAssertSyntax =
op.getBoolOption("enable-import-assertions");
enableImportAttributes = op.getBoolOption("enable-import-attributes") ||

View File

@ -127,9 +127,6 @@ extern bool enableWellFormedUnicodeStrings;
extern bool enableArrayBufferTransfer;
extern bool enableArrayBufferResizable;
extern bool enableSymbolsAsWeakMapKeys;
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
extern bool enableJSONParseWithSource;
#endif
extern bool enableNewSetMethods;
extern bool enableImportAttributes;
extern bool enableImportAttributesAssertSyntax;

View File

@ -2260,7 +2260,7 @@ JS_PUBLIC_API bool js::ShouldIgnorePropertyDefinition(JSContext* cx,
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
if (key == JSProto_JSON &&
!cx->realm()->creationOptions().getJSONParseWithSource() &&
!JS::Prefs::experimental_json_parse_with_source() &&
(id == NameToId(cx->names().isRawJSON) ||
id == NameToId(cx->names().rawJSON))) {
return true;

View File

@ -7663,6 +7663,14 @@
mirror: always
#endif // NIGHTLY_BUILD
#ifdef ENABLE_JSON_PARSE_WITH_SOURCE
- name: javascript.options.experimental.json_parse_with_source
type: bool
value: false
mirror: always
set_spidermonkey_pref: startup
#endif // ENABLE_JSON_PARSE_WITH_SOURCE
- name: javascript.options.wasm_caching
type: bool
value: true