From 0da0a96ffa012af448117125ed761d51da57c369 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Sat, 7 Feb 2009 23:23:01 -0800 Subject: [PATCH] Bug 467747 - JS version/option unsyncing results in JS modules not being loaded with the very latest JS version, resulting in syntax errors when loading modules that use new JS syntax. r=brendan --- js/src/jsapi.cpp | 16 ++-------------- js/src/jscntxt.cpp | 36 +++++++++++++++++++++++++++++++++++- js/src/jscntxt.h | 8 ++++++++ 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index dd650ed92d4d..7ddcb3c8c615 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -1191,24 +1191,12 @@ JS_GetOptions(JSContext *cx) return cx->options; } -#define SYNC_OPTIONS_TO_VERSION(cx) \ - JS_BEGIN_MACRO \ - if ((cx)->options & JSOPTION_XML) \ - (cx)->version |= JSVERSION_HAS_XML; \ - else \ - (cx)->version &= ~JSVERSION_HAS_XML; \ - if ((cx)->options & JSOPTION_ANONFUNFIX) \ - (cx)->version |= JSVERSION_ANONFUNFIX; \ - else \ - (cx)->version &= ~JSVERSION_ANONFUNFIX; \ - JS_END_MACRO - JS_PUBLIC_API(uint32) JS_SetOptions(JSContext *cx, uint32 options) { uint32 oldopts = cx->options; cx->options = options; - SYNC_OPTIONS_TO_VERSION(cx); + js_SyncOptionsToVersion(cx); return oldopts; } @@ -1217,7 +1205,7 @@ JS_ToggleOptions(JSContext *cx, uint32 options) { uint32 oldopts = cx->options; cx->options ^= options; - SYNC_OPTIONS_TO_VERSION(cx); + js_SyncOptionsToVersion(cx); return oldopts; } diff --git a/js/src/jscntxt.cpp b/js/src/jscntxt.cpp index 0491411995db..bae57053e9a9 100644 --- a/js/src/jscntxt.cpp +++ b/js/src/jscntxt.cpp @@ -1,4 +1,4 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- +/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * vim: set ts=8 sw=4 et tw=80: * * ***** BEGIN LICENSE BLOCK ***** @@ -61,6 +61,7 @@ #include "jsnum.h" #include "jsobj.h" #include "jsopcode.h" +#include "jspubtd.h" #include "jsscan.h" #include "jsscope.h" #include "jsscript.h" @@ -207,6 +208,38 @@ js_InitContextThread(JSContext *cx, JSThread *thread) #endif /* JS_THREADSAFE */ +/* + * JSOPTION_XML and JSOPTION_ANONFUNFIX must be part of the JS version + * associated with scripts, so in addition to storing them in cx->options we + * duplicate them in cx->version (script->version, etc.) and ensure each bit + * remains synchronized between the two through these two functions. + */ +void +js_SyncOptionsToVersion(JSContext* cx) +{ + if (cx->options & JSOPTION_XML) + cx->version |= JSVERSION_HAS_XML; + else + cx->version &= ~JSVERSION_HAS_XML; + if (cx->options & JSOPTION_ANONFUNFIX) + cx->version |= JSVERSION_ANONFUNFIX; + else + cx->version &= ~JSVERSION_ANONFUNFIX; +} + +inline void +js_SyncVersionToOptions(JSContext* cx) +{ + if (cx->version & JSVERSION_HAS_XML) + cx->options |= JSOPTION_XML; + else + cx->options &= ~JSOPTION_XML; + if (cx->version & JSVERSION_ANONFUNFIX) + cx->options |= JSOPTION_ANONFUNFIX; + else + cx->options &= ~JSOPTION_ANONFUNFIX; +} + void js_OnVersionChange(JSContext *cx) { @@ -221,6 +254,7 @@ void js_SetVersion(JSContext *cx, JSVersion version) { cx->version = version; + js_SyncVersionToOptions(cx); js_OnVersionChange(cx); } diff --git a/js/src/jscntxt.h b/js/src/jscntxt.h index 471f4ce5ada4..2d1980758598 100644 --- a/js/src/jscntxt.h +++ b/js/src/jscntxt.h @@ -1157,6 +1157,14 @@ js_InitThreadPrivateIndex(void (*ptr)(void *)); extern JSBool js_CleanupThreadPrivateData(); +/* + * Ensures the JSOPTION_XML and JSOPTION_ANONFUNFIX bits of cx->options are + * reflected in cx->version, since each bit must travel with a script that has + * it set. + */ +extern void +js_SyncOptionsToVersion(JSContext *cx); + /* * Common subroutine of JS_SetVersion and js_SetVersion, to update per-context * data that depends on version.