Bug 825545 - Make the ScriptSource API work even if it is being compressed. r=jorendorff

This commit is contained in:
Benjamin Peterson 2013-01-07 18:38:38 -06:00
parent 15e048913a
commit ea9ab6a32e
3 changed files with 40 additions and 20 deletions

View File

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "tests.h"
#include "jsscript.h"
BEGIN_TEST(testBug795104)
{
@ -21,3 +22,27 @@ BEGIN_TEST(testBug795104)
return true;
}
END_TEST(testBug795104)
const char *simpleSource = "var x = 4;";
static void
newScriptHook(JSContext *cx, const char *fn, unsigned lineno,
JSScript *script, JSFunction *fun, void *data)
{
// Just need this not to fail.
if (!JS_StringEqualsAscii(cx, script->sourceData(cx), simpleSource, (JSBool *)data))
*((JSBool *)data) = JS_FALSE;
}
BEGIN_TEST(testScriptSourceReentrant)
{
JS::CompileOptions opts(cx);
JSBool match = false;
JS_SetNewScriptHook(rt, newScriptHook, &match);
CHECK(JS::Evaluate(cx, global, opts, simpleSource, strlen(simpleSource), NULL));
CHECK(match);
JS_SetNewScriptHook(rt, NULL, NULL);
return true;
}
END_TEST(testScriptSourceReentrant)

View File

@ -945,6 +945,13 @@ SourceCompressorThread::finish()
PR_DestroyLock(lock);
}
const jschar *
SourceCompressorThread::currentChars() const
{
JS_ASSERT(tok);
return tok->chars;
}
bool
SourceCompressorThread::internalCompress()
{
@ -1051,6 +1058,7 @@ SourceCompressorThread::compress(SourceCompressionToken *sct)
JS_ASSERT(!tok);
stop = false;
PR_Lock(lock);
sct->ss->ready_ = false;
tok = sct;
state = COMPRESSING;
PR_NotifyCondVar(wakeup);
@ -1070,9 +1078,7 @@ SourceCompressorThread::waitOnCompression(SourceCompressionToken *userTok)
PR_Unlock(lock);
JS_ASSERT(!saveTok->ss->ready());
#ifdef DEBUG
saveTok->ss->ready_ = true;
#endif
// Update memory accounting.
if (!saveTok->oom)
@ -1183,11 +1189,12 @@ SourceDataCache::purge()
JSFlatString *
ScriptSource::substring(JSContext *cx, uint32_t start, uint32_t stop)
{
JS_ASSERT(ready());
const jschar *chars;
#if USE_ZLIB
Rooted<JSStableString *> cached(cx, NULL);
if (compressed()) {
if (!ready()) {
chars = cx->runtime->sourceCompressorThread.currentChars();
} else if (compressed()) {
cached = cx->runtime->sourceDataCache.lookup(this);
if (!cached) {
const size_t nbytes = sizeof(jschar) * (length_ + 1);
@ -1229,9 +1236,6 @@ ScriptSource::setSourceCopy(JSContext *cx, StableCharPtr src, uint32_t length,
#ifdef JS_THREADSAFE
if (tok && cx->runtime->useHelperThreads()) {
#ifdef DEBUG
ready_ = false;
#endif
tok->ss = this;
tok->chars = src.get();
cx->runtime->sourceCompressorThread.compress(tok);
@ -1287,9 +1291,7 @@ ScriptSource::destroy(JSRuntime *rt)
JS_ASSERT(ready());
adjustDataSize(0);
js_free(sourceMap_);
#ifdef DEBUG
ready_ = false;
#endif
js_free(this);
}
@ -1373,10 +1375,8 @@ ScriptSource::performXDR(XDRState<mode> *xdr)
sourceMap_[sourceMapLen] = '\0';
}
#ifdef DEBUG
if (mode == XDR_DECODE)
ready_ = true;
#endif
return true;
}

View File

@ -1036,9 +1036,7 @@ struct ScriptSource
// possible to get source at all.
bool sourceRetrievable_:1;
bool argumentsNotIncluded_:1;
#ifdef DEBUG
bool ready_:1;
#endif
public:
ScriptSource()
@ -1047,10 +1045,8 @@ struct ScriptSource
compressedLength_(0),
sourceMap_(NULL),
sourceRetrievable_(false),
argumentsNotIncluded_(false)
#ifdef DEBUG
,ready_(true)
#endif
argumentsNotIncluded_(false),
ready_(true)
{
data.source = NULL;
}
@ -1066,12 +1062,10 @@ struct ScriptSource
bool argumentsNotIncluded,
SourceCompressionToken *tok);
void setSource(const jschar *src, uint32_t length);
#ifdef DEBUG
bool ready() const { return ready_; }
#endif
void setSourceRetrievable() { sourceRetrievable_ = true; }
bool sourceRetrievable() const { return sourceRetrievable_; }
bool hasSourceData() const { return !!data.source; }
bool hasSourceData() const { return !!data.source || !ready(); }
uint32_t length() const {
JS_ASSERT(hasSourceData());
return length_;
@ -1173,6 +1167,7 @@ class SourceCompressorThread
void compress(SourceCompressionToken *tok);
void waitOnCompression(SourceCompressionToken *userTok);
void abort(SourceCompressionToken *userTok);
const jschar *currentChars() const;
};
#endif