Skip to content

Commit

Permalink
[asm.js] Use token position instead of stream position.
Browse files Browse the repository at this point in the history
This switches the parser to use token positions (i.e. {Position})
instead of stream positions (i.e. {GetPosition}) everywhere. Access to
the latter is being removed as it is unsupported when the scanner is in
rewind state anyways. This prevents "skipping" a token when seeking.

R=bradnelson@chromium.org
BUG=v8:6127

Change-Id: I9c13dd20a981061a2bccc4fb57e5c57d2a64ac5c
Reviewed-on: https://chromium-review.googlesource.com/480300
Reviewed-by: Brad Nelson <bradnelson@chromium.org>
Commit-Queue: Brad Nelson <bradnelson@chromium.org>
Cr-Commit-Position: refs/heads/master@{#44699}
  • Loading branch information
Michael Starzinger authored and Commit Bot committed Apr 18, 2017
1 parent f4a5754 commit 5930e0a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
21 changes: 11 additions & 10 deletions src/asmjs/asm-parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ namespace wasm {
#define FAIL_AND_RETURN(ret, msg) \
failed_ = true; \
failure_message_ = msg; \
failure_location_ = scanner_.GetPosition(); \
failure_location_ = static_cast<int>(scanner_.Position()); \
if (FLAG_trace_asm_parser) { \
PrintF("[asm.js failure: %s, token: '%s', see: %s:%d]\n", msg, \
scanner_.Name(scanner_.Token()).c_str(), __FILE__, __LINE__); \
} \
return ret;
#else
#define FAIL_AND_RETURN(ret, msg) \
failed_ = true; \
failure_message_ = msg; \
failure_location_ = scanner_.GetPosition(); \
#define FAIL_AND_RETURN(ret, msg) \
failed_ = true; \
failure_message_ = msg; \
failure_location_ = static_cast<int>(scanner_.Position()); \
return ret;
#endif

Expand Down Expand Up @@ -1302,7 +1302,8 @@ void AsmJsParser::SwitchStatement() {
pending_label_ = 0;
// TODO(bradnelson): Make less weird.
std::vector<int32_t> cases;
GatherCases(&cases); // Skips { implicitly.
GatherCases(&cases);
EXPECT_TOKEN('{');
size_t count = cases.size() + 1;
for (size_t i = 0; i < count; ++i) {
BareBegin(BlockKind::kOther);
Expand Down Expand Up @@ -1958,17 +1959,17 @@ AsmType* AsmJsParser::BitwiseORExpression() {
call_coercion_deferred_ = nullptr;
// TODO(bradnelson): Make it prettier.
bool zero = false;
int old_pos;
size_t old_pos;
size_t old_code;
if (a->IsA(AsmType::Intish()) && CheckForZero()) {
old_pos = scanner_.GetPosition();
old_pos = scanner_.Position();
old_code = current_function_builder_->GetPosition();
scanner_.Rewind();
zero = true;
}
RECURSEn(b = BitwiseXORExpression());
// Handle |0 specially.
if (zero && old_pos == scanner_.GetPosition()) {
if (zero && old_pos == scanner_.Position()) {
current_function_builder_->StashCode(nullptr, old_code);
a = AsmType::Signed();
continue;
Expand Down Expand Up @@ -2440,7 +2441,7 @@ void AsmJsParser::ValidateFloatCoercion() {
}

void AsmJsParser::GatherCases(std::vector<int32_t>* cases) {
int start = scanner_.GetPosition();
size_t start = scanner_.Position();
int depth = 0;
for (;;) {
if (Peek('{')) {
Expand Down
4 changes: 2 additions & 2 deletions src/asmjs/asm-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AsmJsParser {
explicit AsmJsParser(Isolate* isolate, Zone* zone, Handle<Script> script,
int start, int end);
bool Run();
const char* failure_message() const { return failure_message_.c_str(); }
const char* failure_message() const { return failure_message_; }
int failure_location() const { return failure_location_; }
WasmModuleBuilder* module_builder() { return module_builder_; }
const AsmTyper::StdlibSet* stdlib_uses() const { return &stdlib_uses_; }
Expand Down Expand Up @@ -115,7 +115,7 @@ class AsmJsParser {

// Error Handling related
bool failed_;
std::string failure_message_;
const char* failure_message_;
int failure_location_;

// Module Related.
Expand Down
7 changes: 1 addition & 6 deletions src/asmjs/asm-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,7 @@ std::string AsmJsScanner::Name(token_t token) const {
}
#endif

int AsmJsScanner::GetPosition() const {
DCHECK(!rewind_);
return static_cast<int>(stream_->pos());
}

void AsmJsScanner::Seek(int pos) {
void AsmJsScanner::Seek(size_t pos) {
stream_->Seek(pos);
preceding_token_ = kUninitialized;
token_ = kUninitialized;
Expand Down
7 changes: 3 additions & 4 deletions src/asmjs/asm-scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,9 @@ class V8_EXPORT_PRIVATE AsmJsScanner {
std::string Name(token_t token) const;
#endif

// Get current position (to use with Seek).
int GetPosition() const;
// Restores old position (token after that position).
void Seek(int pos);
// Restores old position (token after that position). Note that it is not
// allowed to rewind right after a seek, because previous tokens are unknown.
void Seek(size_t pos);

// Select whether identifiers are resolved in global or local scope,
// and which scope new identifiers are added to.
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/asmjs/asm-scanner-unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,15 @@ TEST_F(AsmJsScannerTest, TrailingCComment) {
TEST_F(AsmJsScannerTest, Seeking) {
SetupSource("var eval do arguments function break\n");
Skip(TOK(var));
int old_pos = scanner.GetPosition();
size_t old_pos = scanner.Position();
Skip(TOK(eval));
Skip(TOK(do));
Skip(TOK(arguments));
scanner.Rewind();
Skip(TOK(arguments));
scanner.Rewind();
scanner.Seek(old_pos);
Skip(TOK(eval));
Skip(TOK(do));
Skip(TOK(arguments));
Skip(TOK(function));
Expand Down

0 comments on commit 5930e0a

Please sign in to comment.