Skip to content

Commit

Permalink
Bug 987556 Part 8 Use JS::SourceBufferHolder in XULDocument. r=bz
Browse files Browse the repository at this point in the history
  • Loading branch information
wanderview committed Apr 25, 2014
1 parent 0ff1f6a commit 80a0d6e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
24 changes: 18 additions & 6 deletions content/xul/content/src/nsXULElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2619,8 +2619,7 @@ OffThreadScriptReceiverCallback(void *aToken, void *aCallbackData)
}

nsresult
nsXULPrototypeScript::Compile(const char16_t* aText,
int32_t aTextLength,
nsXULPrototypeScript::Compile(JS::SourceBufferHolder& aSrcBuf,
nsIURI* aURI,
uint32_t aLineNo,
nsIDocument* aDocument,
Expand Down Expand Up @@ -2651,25 +2650,38 @@ nsXULPrototypeScript::Compile(const char16_t* aText,
JS::ExposeObjectToActiveJS(scope);
}

if (aOffThreadReceiver && JS::CanCompileOffThread(cx, options, aTextLength)) {
if (aOffThreadReceiver && JS::CanCompileOffThread(cx, options, aSrcBuf.length())) {
if (!JS::CompileOffThread(cx, options,
static_cast<const jschar*>(aText), aTextLength,
aSrcBuf.get(), aSrcBuf.length(),
OffThreadScriptReceiverCallback,
static_cast<void*>(aOffThreadReceiver))) {
return NS_ERROR_OUT_OF_MEMORY;
}
// This reference will be consumed by the NotifyOffThreadScriptCompletedRunnable.
NS_ADDREF(aOffThreadReceiver);
} else {
JSScript* script = JS::Compile(cx, scope, options,
static_cast<const jschar*>(aText), aTextLength);
JSScript* script = JS::Compile(cx, scope, options, aSrcBuf);
if (!script)
return NS_ERROR_OUT_OF_MEMORY;
Set(script);
}
return NS_OK;
}

nsresult
nsXULPrototypeScript::Compile(const char16_t* aText,
int32_t aTextLength,
nsIURI* aURI,
uint32_t aLineNo,
nsIDocument* aDocument,
nsXULPrototypeDocument* aProtoDoc,
nsIOffThreadScriptReceiver *aOffThreadReceiver /* = nullptr */)
{
JS::SourceBufferHolder srcBuf(aText, aTextLength,
JS::SourceBufferHolder::NoOwnership);
return Compile(srcBuf, aURI, aLineNo, aDocument, aProtoDoc, aOffThreadReceiver);
}

void
nsXULPrototypeScript::UnlinkJSObjects()
{
Expand Down
10 changes: 10 additions & 0 deletions content/xul/content/src/nsXULElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class StyleRule;
}
}

namespace JS {
class SourceBufferHolder;
}

////////////////////////////////////////////////////////////////////////

#ifdef XUL_PROTOTYPE_ATTRIBUTE_METERING
Expand Down Expand Up @@ -231,6 +235,12 @@ class nsXULPrototypeScript : public nsXULPrototypeNode
nsresult DeserializeOutOfLine(nsIObjectInputStream* aInput,
nsXULPrototypeDocument* aProtoDoc);

nsresult Compile(JS::SourceBufferHolder& aSrcBuf,
nsIURI* aURI, uint32_t aLineNo,
nsIDocument* aDocument,
nsXULPrototypeDocument* aProtoDoc,
nsIOffThreadScriptReceiver *aOffThreadReceiver = nullptr);

nsresult Compile(const char16_t* aText, int32_t aTextLength,
nsIURI* aURI, uint32_t aLineNo,
nsIDocument* aDocument,
Expand Down
37 changes: 31 additions & 6 deletions content/xul/document/src/XULDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ XULDocument::~XULDocument()
NS_IF_RELEASE(kNC_attribute);
NS_IF_RELEASE(kNC_value);
}

if (mOffThreadCompileStringBuf) {
js_free(mOffThreadCompileStringBuf);
}
}

} // namespace dom
Expand Down Expand Up @@ -3526,14 +3530,26 @@ XULDocument::OnStreamComplete(nsIStreamLoader* aLoader,

// XXX should also check nsIHttpChannel::requestSucceeded

MOZ_ASSERT(!mOffThreadCompiling && mOffThreadCompileString.Length() == 0,
MOZ_ASSERT(!mOffThreadCompiling && (mOffThreadCompileStringLength == 0 &&
!mOffThreadCompileStringBuf),
"XULDocument can't load multiple scripts at once");

rv = nsScriptLoader::ConvertToUTF16(channel, string, stringLen,
EmptyString(), this, mOffThreadCompileString);
EmptyString(), this,
mOffThreadCompileStringBuf,
mOffThreadCompileStringLength);
if (NS_SUCCEEDED(rv)) {
rv = mCurrentScriptProto->Compile(mOffThreadCompileString.get(),
mOffThreadCompileString.Length(),
// Attempt to give ownership of the buffer to the JS engine. If
// we hit offthread compilation, however, we will have to take it
// back below in order to keep the memory alive until compilation
// completes.
JS::SourceBufferHolder srcBuf(mOffThreadCompileStringBuf,
mOffThreadCompileStringLength,
JS::SourceBufferHolder::GiveOwnership);
mOffThreadCompileStringBuf = nullptr;
mOffThreadCompileStringLength = 0;

rv = mCurrentScriptProto->Compile(srcBuf,
uri, 1, this,
mCurrentPrototype,
this);
Expand All @@ -3542,10 +3558,15 @@ XULDocument::OnStreamComplete(nsIStreamLoader* aLoader,
// compile finishes. Keep the contents of the compiled script
// alive until the compilation finishes.
mOffThreadCompiling = true;
// If the JS engine did not take the source buffer, then take
// it back here to ensure it remains alive.
mOffThreadCompileStringBuf = srcBuf.take();
if (mOffThreadCompileStringBuf) {
mOffThreadCompileStringLength = srcBuf.length();
}
BlockOnload();
return NS_OK;
}
mOffThreadCompileString.Truncate();
}
}

Expand All @@ -3567,7 +3588,11 @@ XULDocument::OnScriptCompileComplete(JSScript* aScript, nsresult aStatus)
}

// After compilation finishes the script's characters are no longer needed.
mOffThreadCompileString.Truncate();
if (mOffThreadCompileStringBuf) {
js_free(mOffThreadCompileStringBuf);
mOffThreadCompileStringBuf = nullptr;
mOffThreadCompileStringLength = 0;
}

// Clear mCurrentScriptProto now, but save it first for use below in
// the execute code, and in the while loop that resumes walks of other
Expand Down
3 changes: 2 additions & 1 deletion content/xul/document/src/XULDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,8 @@ class XULDocument MOZ_FINAL : public XMLDocument,
* If the current transcluded script is being compiled off thread, the
* source for that script.
*/
nsString mOffThreadCompileString;
jschar* mOffThreadCompileStringBuf;
size_t mOffThreadCompileStringLength;

/**
* Check if a XUL template builder has already been hooked up.
Expand Down

0 comments on commit 80a0d6e

Please sign in to comment.