Bug 830855 - Remove JSScript::principals (sort of) (r=luke)
authorBill McCloskey <wmccloskey@mozilla.com>
Wed, 30 Jan 2013 18:10:46 -0800
changeset 120424 25c7af76b27408269a0acdd8c0c5305030215c90
parent 120423 66068b93679090f8accca08b5844ec6384dd592e
child 120425 83305a2fa2241888247477976ac50ea6c5d1c65c
push id24251
push userryanvm@gmail.com
push dateThu, 31 Jan 2013 20:56:22 +0000
treeherdermozilla-central@683b08dc1afd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersluke
bugs830855
milestone21.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 830855 - Remove JSScript::principals (sort of) (r=luke)
js/src/frontend/BytecodeEmitter.cpp
js/src/jsapi-tests/testXDR.cpp
js/src/jsdbgapi.cpp
js/src/jsscript.cpp
js/src/jsscript.h
js/src/jsscriptinlines.h
js/src/vm/Xdr.h
--- a/js/src/frontend/BytecodeEmitter.cpp
+++ b/js/src/frontend/BytecodeEmitter.cpp
@@ -4831,17 +4831,17 @@ EmitFunc(JSContext *cx, BytecodeEmitter 
         if (outersc->isFunctionBox() && outersc->asFunctionBox()->mightAliasLocals())
             funbox->setMightAliasLocals();      // inherit mightAliasLocals from parent
         JS_ASSERT_IF(outersc->strict, funbox->strict);
 
         // Inherit most things (principals, version, etc) from the parent.
         Rooted<JSScript*> parent(cx, bce->script);
         Rooted<JSObject*> enclosingScope(cx, EnclosingStaticScope(bce));
         CompileOptions options(cx);
-        options.setPrincipals(parent->principals)
+        options.setPrincipals(parent->principals())
                .setOriginPrincipals(parent->originPrincipals)
                .setCompileAndGo(parent->compileAndGo)
                .setNoScriptRval(false)
                .setVersion(parent->getVersion())
                .setUserBit(parent->userBit);
         Rooted<JSScript*> script(cx, JSScript::Create(cx, enclosingScope, false, options,
                                                       parent->staticLevel + 1,
                                                       bce->script->scriptSource(),
--- a/js/src/jsapi-tests/testXDR.cpp
+++ b/js/src/jsapi-tests/testXDR.cpp
@@ -6,16 +6,18 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 
 #include "tests.h"
 #include "jsscript.h"
 #include "jsstr.h"
 #include "jsfriendapi.h"
 
+#include "jsscriptinlines.h"
+
 static JSScript *
 CompileScriptForPrincipalsVersionOrigin(JSContext *cx, JS::HandleObject obj,
                                         JSPrincipals *principals, JSPrincipals *originPrincipals,
                                         const char *bytes, size_t nbytes,
                                         const char *filename, unsigned lineno,
                                         JSVersion version)
 {
     size_t nchars;
@@ -40,17 +42,17 @@ FreezeThaw(JSContext *cx, JSScript *scri
 {
     // freeze
     uint32_t nbytes;
     void *memory = JS_EncodeScript(cx, script, &nbytes);
     if (!memory)
         return NULL;
 
     // thaw
-    script = JS_DecodeScript(cx, memory, nbytes, script->principals, script->originPrincipals);
+    script = JS_DecodeScript(cx, memory, nbytes, script->principals(), script->originPrincipals);
     js_free(memory);
     return script;
 }
 
 static JSScript *
 GetScript(JSContext *cx, JS::HandleObject funobj)
 {
     return JS_GetFunctionScript(cx, JS_GetObjectFunction(funobj));
@@ -63,65 +65,49 @@ FreezeThaw(JSContext *cx, JS::HandleObje
     uint32_t nbytes;
     void *memory = JS_EncodeInterpretedFunction(cx, funobj, &nbytes);
     if (!memory)
         return NULL;
 
     // thaw
     JSScript *script = GetScript(cx, funobj);
     JSObject *funobj2 = JS_DecodeInterpretedFunction(cx, memory, nbytes,
-                                          script->principals, script->originPrincipals);
+                                                     script->principals(),
+                                                     script->originPrincipals);
     js_free(memory);
     return funobj2;
 }
 
 static JSPrincipals testPrincipals[] = {
     { 1 },
     { 1 },
 };
 
 BEGIN_TEST(testXDR_principals)
 {
     JSScript *script;
     JSCompartment *compartment = js::GetContextCompartment(cx);
     for (int i = TEST_FIRST; i != TEST_END; ++i) {
-        script = createScriptViaXDR(NULL, NULL, i);
-        CHECK(script);
-        CHECK(!JS_GetScriptPrincipals(script));
-        CHECK(!JS_GetScriptOriginPrincipals(script));
-
-        script = createScriptViaXDR(NULL, NULL, i);
-        CHECK(script);
-        CHECK(!JS_GetScriptPrincipals(script));
-        CHECK(!JS_GetScriptOriginPrincipals(script));
-
         // Appease the new JSAPI assertions. The stuff being tested here is
         // going away anyway.
-        JSPrincipals *old = JS_GetCompartmentPrincipals(compartment);
         JS_SetCompartmentPrincipals(compartment, &testPrincipals[0]);
         script = createScriptViaXDR(&testPrincipals[0], NULL, i);
         CHECK(script);
         CHECK(JS_GetScriptPrincipals(script) == &testPrincipals[0]);
         CHECK(JS_GetScriptOriginPrincipals(script) == &testPrincipals[0]);
 
         script = createScriptViaXDR(&testPrincipals[0], &testPrincipals[0], i);
         CHECK(script);
         CHECK(JS_GetScriptPrincipals(script) == &testPrincipals[0]);
         CHECK(JS_GetScriptOriginPrincipals(script) == &testPrincipals[0]);
 
         script = createScriptViaXDR(&testPrincipals[0], &testPrincipals[1], i);
         CHECK(script);
         CHECK(JS_GetScriptPrincipals(script) == &testPrincipals[0]);
         CHECK(JS_GetScriptOriginPrincipals(script) == &testPrincipals[1]);
-
-        script = createScriptViaXDR(NULL, &testPrincipals[1], i);
-        CHECK(script);
-        CHECK(!JS_GetScriptPrincipals(script));
-        CHECK(JS_GetScriptOriginPrincipals(script) == &testPrincipals[1]);
-        JS_SetCompartmentPrincipals(compartment, old);
     }
 
     return true;
 }
 
 enum TestCase {
     TEST_FIRST,
     TEST_SCRIPT = TEST_FIRST,
--- a/js/src/jsdbgapi.cpp
+++ b/js/src/jsdbgapi.cpp
@@ -529,17 +529,17 @@ JS_PUBLIC_API(JSNative)
 JS_GetFunctionNative(JSContext *cx, JSFunction *fun)
 {
     return fun->maybeNative();
 }
 
 JS_PUBLIC_API(JSPrincipals *)
 JS_GetScriptPrincipals(JSScript *script)
 {
-    return script->principals;
+    return script->principals();
 }
 
 JS_PUBLIC_API(JSPrincipals *)
 JS_GetScriptOriginPrincipals(JSScript *script)
 {
     return script->originPrincipals;
 }
 
@@ -897,17 +897,17 @@ JS_GetScriptTotalSize(JSContext *cx, JSS
         do {
             nbytes += JS_GetObjectTotalSize(cx, objarray->vector[--i]);
         } while (i != 0);
     }
 
     if (script->hasTrynotes())
         nbytes += sizeof(TryNoteArray) + script->trynotes()->length * sizeof(JSTryNote);
 
-    principals = script->principals;
+    principals = script->principals();
     if (principals) {
         JS_ASSERT(principals->refcount);
         pbytes = sizeof *principals;
         if (principals->refcount > 1)
             pbytes = JS_HOWMANY(pbytes, principals->refcount);
         nbytes += pbytes;
     }
 
--- a/js/src/jsscript.cpp
+++ b/js/src/jsscript.cpp
@@ -1592,20 +1592,19 @@ JSScript::Create(JSContext *cx, HandleOb
     PodZero(script.get());
     new (&script->bindings) Bindings;
 
     script->enclosingScope_ = enclosingScope;
     script->savedCallerFun = savedCallerFun;
 
     /* Establish invariant: principals implies originPrincipals. */
     if (options.principals) {
-        script->principals = options.principals;
+        JS_ASSERT(options.principals == cx->compartment->principals);
         script->originPrincipals
             = options.originPrincipals ? options.originPrincipals : options.principals;
-        JS_HoldPrincipals(script->principals);
         JS_HoldPrincipals(script->originPrincipals);
     } else if (options.originPrincipals) {
         script->originPrincipals = options.originPrincipals;
         JS_HoldPrincipals(script->originPrincipals);
     }
 
     script->compileAndGo = options.compileAndGo;
     script->noScriptRval = options.noScriptRval;
@@ -1921,19 +1920,16 @@ JSScript::finalize(FreeOp *fop)
     // NOTE: this JSScript may be partially initialized at this point.  E.g. we
     // may have created it and partially initialized it with
     // JSScript::Create(), but not yet finished initializing it with
     // fullyInitFromEmitter() or fullyInitTrivial().
 
     CallDestroyScriptHook(fop, this);
     fop->runtime()->spsProfiler.onScriptFinalized(this);
 
-    JS_ASSERT_IF(principals, originPrincipals);
-    if (principals)
-        JS_DropPrincipals(fop->runtime(), principals);
     if (originPrincipals)
         JS_DropPrincipals(fop->runtime(), originPrincipals);
 
     if (types)
         types->destroy();
 
 #ifdef JS_METHODJIT
     mjit::ReleaseScriptCode(fop, this);
--- a/js/src/jsscript.h
+++ b/js/src/jsscript.h
@@ -363,17 +363,17 @@ class JSScript : public js::gc::Cell
   public:
     jsbytecode      *code;      /* bytecodes and their immediate operands */
     uint8_t         *data;      /* pointer to variable-length data array (see
                                    comment above Create() for details) */
 
     const char      *filename;  /* source filename or null */
     js::HeapPtrAtom *atoms;     /* maps immediate index to literal struct */
 
-    JSPrincipals    *principals;/* principals for this script */
+    void            *principalsPad;
     JSPrincipals    *originPrincipals; /* see jsapi.h 'originPrincipals' comment */
 
     /* Persistent type information retained across GCs. */
     js::types::TypeScript *types;
 
   private:
     js::ScriptSource *scriptSource_; /* source code */
 #ifdef JS_METHODJIT
@@ -524,16 +524,18 @@ class JSScript : public js::gc::Cell
     static bool partiallyInit(JSContext *cx, JS::Handle<JSScript*> script,
                               uint32_t length, uint32_t nsrcnotes, uint32_t natoms,
                               uint32_t nobjects, uint32_t nregexps, uint32_t ntrynotes, uint32_t nconsts,
                               uint32_t nTypeSets);
     static bool fullyInitTrivial(JSContext *cx, JS::Handle<JSScript*> script);  // inits a JSOP_STOP-only script
     static bool fullyInitFromEmitter(JSContext *cx, JS::Handle<JSScript*> script,
                                      js::frontend::BytecodeEmitter *bce);
 
+    inline JSPrincipals *principals();
+
     void setVersion(JSVersion v) { version = v; }
 
     /* See ContextFlags::funArgumentsHasLocalBinding comment. */
     bool argumentsHasVarBinding() const { return argsHasVarBinding_; }
     jsbytecode *argumentsBytecode() const { JS_ASSERT(code[0] == JSOP_ARGUMENTS); return code; }
     void setArgumentsHasVarBinding();
 
     /*
--- a/js/src/jsscriptinlines.h
+++ b/js/src/jsscriptinlines.h
@@ -167,9 +167,15 @@ JSScript::writeBarrierPre(js::UnrootedSc
 #endif
 }
 
 inline void
 JSScript::writeBarrierPost(js::UnrootedScript script, void *addr)
 {
 }
 
+inline JSPrincipals *
+JSScript::principals()
+{
+    return compartment()->principals;
+}
+
 #endif /* jsscriptinlines_h___ */
--- a/js/src/vm/Xdr.h
+++ b/js/src/vm/Xdr.h
@@ -259,22 +259,20 @@ class XDRState {
     bool codeFunction(JSMutableHandleObject objp);
     bool codeScript(JSMutableHandleScript scriptp);
 
     void initScriptPrincipals(JSScript *script) {
         JS_ASSERT(mode == XDR_DECODE);
 
         /* The origin principals must be normalized at this point. */
         JS_ASSERT_IF(principals, originPrincipals);
-        JS_ASSERT(!script->principals);
         JS_ASSERT(!script->originPrincipals);
-        if (principals) {
-            script->principals = principals;
-            JS_HoldPrincipals(principals);
-        }
+        if (principals)
+            JS_ASSERT(script->principals() == principals);
+
         if (originPrincipals) {
             script->originPrincipals = originPrincipals;
             JS_HoldPrincipals(originPrincipals);
         }
     }
 };
 
 class XDREncoder : public XDRState<XDR_ENCODE> {