From ff539b503dc03347e05717190be40414f48d25e2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 23 May 2012 16:18:51 -0700 Subject: [PATCH] Bug 757690 - Merge TokenStream::TokenStream() and TokenStream::init(). r=jorendorff. --HG-- extra : rebase_source : a389e46e1c633a133a2f8b68b3cb5fa186c8c1fd --- js/src/frontend/BytecodeCompiler.cpp | 11 +++--- js/src/frontend/Parser.cpp | 18 +++------ js/src/frontend/Parser.h | 17 ++++----- js/src/frontend/TokenStream.cpp | 56 +++++++++++++++------------- js/src/frontend/TokenStream.h | 29 +++----------- js/src/jsapi.cpp | 6 ++- js/src/jsfun.cpp | 5 +-- js/src/jsreflect.cpp | 7 ++-- js/src/jsxml.cpp | 6 ++- js/src/shell/js.cpp | 10 +++-- 10 files changed, 77 insertions(+), 88 deletions(-) diff --git a/js/src/frontend/BytecodeCompiler.cpp b/js/src/frontend/BytecodeCompiler.cpp index 66c67626d4c..49362630cb2 100644 --- a/js/src/frontend/BytecodeCompiler.cpp +++ b/js/src/frontend/BytecodeCompiler.cpp @@ -94,9 +94,9 @@ frontend::CompileScript(JSContext *cx, JSObject *scopeChain, StackFrame *callerF JS_ASSERT_IF(callerFrame, compileAndGo); JS_ASSERT_IF(staticLevel != 0, callerFrame); - bool foldConstants = true; - Parser parser(cx, principals, originPrincipals, callerFrame, foldConstants, compileAndGo); - if (!parser.init(chars, length, filename, lineno, version)) + Parser parser(cx, principals, originPrincipals, chars, length, filename, lineno, version, + callerFrame, /* foldConstants = */ true, compileAndGo); + if (!parser.init()) return NULL; SharedContext sc(cx, /* inFunction = */ false); @@ -266,8 +266,9 @@ frontend::CompileFunctionBody(JSContext *cx, JSFunction *fun, Bindings *bindings, const jschar *chars, size_t length, const char *filename, unsigned lineno, JSVersion version) { - Parser parser(cx, principals, originPrincipals); - if (!parser.init(chars, length, filename, lineno, version)) + Parser parser(cx, principals, originPrincipals, chars, length, filename, lineno, version, + /* cfp = */ NULL, /* foldConstants = */ true, /* compileAndGo = */ false); + if (!parser.init()) return false; TokenStream &tokenStream = parser.tokenStream; diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index 483cd1f691f..10d7626e1fa 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -77,10 +77,12 @@ using namespace js::frontend; #define MUST_MATCH_TOKEN(tt, errno) MUST_MATCH_TOKEN_WITH_FLAGS(tt, errno, 0) Parser::Parser(JSContext *cx, JSPrincipals *prin, JSPrincipals *originPrin, + const jschar *chars, size_t length, const char *fn, unsigned ln, JSVersion v, StackFrame *cfp, bool foldConstants, bool compileAndGo) : AutoGCRooter(cx, PARSER), context(cx), - tokenStream(cx, prin, originPrin), + tokenStream(cx, prin, originPrin, chars, length, fn, ln, v), + tempPoolMark(NULL), principals(NULL), originPrincipals(NULL), callerFrame(cfp), @@ -97,19 +99,11 @@ Parser::Parser(JSContext *cx, JSPrincipals *prin, JSPrincipals *originPrin, } bool -Parser::init(const jschar *base, size_t length, const char *filename, unsigned lineno, - JSVersion version) +Parser::init() { - JSContext *cx = context; - if (!cx->ensureParseMapPool()) + if (!context->ensureParseMapPool()) return false; - tempPoolMark = cx->tempLifoAlloc().mark(); - if (!tokenStream.init(base, length, filename, lineno, version)) { - cx->tempLifoAlloc().release(tempPoolMark); - return false; - } - if (context->hasRunOption(JSOPTION_STRICT_MODE)) - tokenStream.setStrictMode(); + tempPoolMark = context->tempLifoAlloc().mark(); return true; } diff --git a/js/src/frontend/Parser.h b/js/src/frontend/Parser.h index b8470e164a7..faa699a3e8f 100644 --- a/js/src/frontend/Parser.h +++ b/js/src/frontend/Parser.h @@ -54,22 +54,21 @@ struct Parser : private AutoGCRooter /* Script can optimize name references based on scope chain. */ const bool compileAndGo:1; - Parser(JSContext *cx, JSPrincipals *prin = NULL, JSPrincipals *originPrin = NULL, - StackFrame *cfp = NULL, bool fold = true, bool compileAndGo = false); + Parser(JSContext *cx, JSPrincipals *prin, JSPrincipals *originPrin, + const jschar *chars, size_t length, const char *fn, unsigned ln, JSVersion version, + StackFrame *cfp, bool foldConstants, bool compileAndGo); ~Parser(); friend void AutoGCRooter::trace(JSTracer *trc); friend struct TreeContext; /* - * Initialize a parser. Parameters are passed on to init tokenStream. The - * compiler owns the arena pool "tops-of-stack" space above the current - * JSContext.tempLifoAlloc mark. This means you cannot allocate from - * tempLifoAlloc and save the pointer beyond the next Parser destructor - * invocation. + * Initialize a parser. The compiler owns the arena pool "tops-of-stack" + * space above the current JSContext.tempLifoAlloc mark. This means you + * cannot allocate from tempLifoAlloc and save the pointer beyond the next + * Parser destructor invocation. */ - bool init(const jschar *base, size_t length, const char *filename, unsigned lineno, - JSVersion version); + bool init(); void setPrincipals(JSPrincipals *prin, JSPrincipals *originPrin); diff --git a/js/src/frontend/TokenStream.cpp b/js/src/frontend/TokenStream.cpp index da36a1dc9ab..bf11f0ff153 100644 --- a/js/src/frontend/TokenStream.cpp +++ b/js/src/frontend/TokenStream.cpp @@ -119,32 +119,35 @@ js::IsIdentifier(JSLinearString *str) #endif /* Initialize members that aren't initialized in |init|. */ -TokenStream::TokenStream(JSContext *cx, JSPrincipals *prin, JSPrincipals *originPrin) - : tokens(), tokensRoot(cx, &tokens), - cursor(), lookahead(), flags(), - linebaseRoot(cx, &linebase), prevLinebaseRoot(cx, &prevLinebase), userbufRoot(cx, &userbuf), - sourceMap(), listenerTSData(), tokenbuf(cx), - cx(cx), originPrincipals(JSScript::normalizeOriginPrincipals(prin, originPrin)) +TokenStream::TokenStream(JSContext *cx, JSPrincipals *prin, JSPrincipals *originPrin, + const jschar *base, size_t length, const char *fn, unsigned ln, + JSVersion v) + : tokens(), + tokensRoot(cx, &tokens), + cursor(), + lookahead(), + lineno(ln), + flags(), + linebase(base), + prevLinebase(NULL), + linebaseRoot(cx, &linebase), + prevLinebaseRoot(cx, &prevLinebase), + userbuf(base, length), + userbufRoot(cx, &userbuf), + filename(fn), + sourceMap(NULL), + listenerTSData(), + tokenbuf(cx), + version(v), + xml(VersionHasXML(v)), + cx(cx), + originPrincipals(JSScript::normalizeOriginPrincipals(prin, originPrin)) { if (originPrincipals) JS_HoldPrincipals(originPrincipals); -} -#ifdef _MSC_VER -#pragma warning(pop) -#endif - -bool -TokenStream::init(const jschar *base, size_t length, const char *fn, unsigned ln, JSVersion v) -{ - filename = fn; - lineno = ln; - version = v; - xml = VersionHasXML(v); - - userbuf.init(base, length); - linebase = base; - prevLinebase = NULL; + if (cx->hasRunOption(JSOPTION_STRICT_MODE)) + setStrictMode(); JSSourceHandler listener = cx->runtime->debugHooks.sourceHandler; void *listenerData = cx->runtime->debugHooks.sourceHandlerData; @@ -206,9 +209,12 @@ TokenStream::init(const jschar *base, size_t length, const char *fn, unsigned ln * way to address the dependency from statements on the current token. */ tokens[0].pos.begin.lineno = tokens[0].pos.end.lineno = ln; - return true; } +#ifdef _MSC_VER +#pragma warning(pop) +#endif + TokenStream::~TokenStream() { if (flags & TSF_OWNFILENAME) @@ -263,8 +269,8 @@ TokenStream::getChar() * are by the far the most common) this gives false positives for '(' * (0x0028) and ')' (0x0029). We could avoid those by incorporating * the 13th bit of d into the lookup, but that requires extra shifting - * and masking and isn't worthwhile. See TokenStream::init() for the - * initialization of the relevant entries in the table. + * and masking and isn't worthwhile. See TokenStream::TokenStream() + * for the initialization of the relevant entries in the table. */ if (JS_UNLIKELY(maybeEOL[c & 0xff])) { if (c == '\n') diff --git a/js/src/frontend/TokenStream.h b/js/src/frontend/TokenStream.h index 0f6fe972710..84985e6d27c 100644 --- a/js/src/frontend/TokenStream.h +++ b/js/src/frontend/TokenStream.h @@ -427,24 +427,9 @@ class TokenStream public: typedef Vector CharBuffer; - /* - * To construct a TokenStream, first call the constructor, which is - * infallible, then call |init|, which can fail. To destroy a TokenStream, - * first call |close| then call the destructor. If |init| fails, do not call - * |close|. - * - * This class uses JSContext.tempLifoAlloc to allocate internal buffers. The - * caller should JS_ARENA_MARK before calling |init| and JS_ARENA_RELEASE - * after calling |close|. - */ - TokenStream(JSContext *, JSPrincipals *principals, JSPrincipals *originPrincipals); - - /* - * Create a new token stream from an input buffer. - * Return false on memory-allocation failure. - */ - bool init(const jschar *base, size_t length, const char *filename, unsigned lineno, - JSVersion version); + TokenStream(JSContext *, JSPrincipals *principals, JSPrincipals *originPrincipals, + const jschar *base, size_t length, const char *filename, unsigned lineno, + JSVersion version); ~TokenStream(); /* Accessors. */ @@ -652,12 +637,8 @@ class TokenStream */ class TokenBuf { public: - TokenBuf() : base(NULL), limit(NULL), ptr(NULL) { } - - void init(const jschar *buf, size_t length) { - base = ptr = buf; - limit = base + length; - } + TokenBuf(const jschar *buf, size_t length) + : base(buf), limit(buf + length), ptr(buf), ptrWhenPoisoned(NULL) { } bool hasRawChars() const { return ptr < limit; diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 52b64402fca..ae16fe6c0f6 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4970,8 +4970,10 @@ JS_BufferIsCompilableUnit(JSContext *cx, JSBool bytes_are_utf8, JSObject *obj, c result = JS_TRUE; exnState = JS_SaveExceptionState(cx); { - Parser parser(cx); - if (parser.init(chars, length, NULL, 1, cx->findVersion())) { + Parser parser(cx, /* prin = */ NULL, /* originPrin = */ NULL, + chars, length, /* filename = */ NULL, /* lineno = */ 1, cx->findVersion(), + /* cfp = */ NULL, /* foldConstants = */ true, /* compileAndGo = */ false); + if (parser.init()) { older = JS_SetErrorReporter(cx, NULL); if (!parser.parse(obj) && parser.tokenStream.isUnexpectedEOF()) { diff --git a/js/src/jsfun.cpp b/js/src/jsfun.cpp index 2cb664d4e6c..d6b3425baa5 100644 --- a/js/src/jsfun.cpp +++ b/js/src/jsfun.cpp @@ -1086,9 +1086,8 @@ Function(JSContext *cx, unsigned argc, Value *vp) } /* Initialize a tokenstream that reads from the given string. */ - TokenStream ts(cx, principals, originPrincipals); - if (!ts.init(collected_args, args_length, filename, lineno, cx->findVersion())) - return false; + TokenStream ts(cx, principals, originPrincipals, + collected_args, args_length, filename, lineno, cx->findVersion()); /* The argument string may be empty or contain no tokens. */ TokenKind tt = ts.getToken(); diff --git a/js/src/jsreflect.cpp b/js/src/jsreflect.cpp index e6acfad38a7..8407d31081f 100644 --- a/js/src/jsreflect.cpp +++ b/js/src/jsreflect.cpp @@ -3164,9 +3164,10 @@ reflect_parse(JSContext *cx, uint32_t argc, jsval *vp) if (!chars) return JS_FALSE; - Parser parser(cx, NULL, NULL, NULL, false); - - if (!parser.init(chars, length, filename, lineno, cx->findVersion())) + Parser parser(cx, /* prin = */ NULL, /* originPrin = */ NULL, + chars, length, filename, lineno, cx->findVersion(), + /* cfp = */ NULL, /* foldConstants = */ false, /* compileAndGo = */ false); + if (!parser.init()) return JS_FALSE; serialize.setParser(&parser); diff --git a/js/src/jsxml.cpp b/js/src/jsxml.cpp index d27628fd236..a241e34058a 100644 --- a/js/src/jsxml.cpp +++ b/js/src/jsxml.cpp @@ -1764,8 +1764,10 @@ ParseXMLSource(JSContext *cx, JSString *src) } { - Parser parser(cx); - if (parser.init(chars, length, filename, lineno, cx->findVersion())) { + Parser parser(cx, /* prin = */ NULL, /* originPrin = */ NULL, + chars, length, filename, lineno, cx->findVersion(), + /* cfp = */ NULL, /* foldConstants = */ true, /* compileAndGo = */ false); + if (parser.init()) { JSObject *scopeChain = GetCurrentScopeChain(cx); if (!scopeChain) { cx->free_(chars); diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 79023332a28..fbb19c2d8bf 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -3284,9 +3284,13 @@ Parse(JSContext *cx, unsigned argc, jsval *vp) } JSString *scriptContents = JSVAL_TO_STRING(arg0); - js::Parser parser(cx); - parser.init(JS_GetStringCharsZ(cx, scriptContents), JS_GetStringLength(scriptContents), - "", 0, cx->findVersion()); + js::Parser parser(cx, /* prin = */ NULL, /* originPrin = */ NULL, + JS_GetStringCharsZ(cx, scriptContents), JS_GetStringLength(scriptContents), + "", /* lineno = */ 0, cx->findVersion(), + /* cfp = */ NULL, /* foldConstants = */ true, /* compileAndGo = */ false); + if (!parser.init()) + return JS_FALSE; + ParseNode *pn = parser.parse(NULL); if (!pn) return JS_FALSE;