2017-03-16 18:10:45 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/asmjs/asm-scanner.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
#include "src/parsing/scanner-character-streams.h"
|
|
|
|
#include "src/parsing/scanner.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
#define TOK(t) AsmJsScanner::kToken_##t
|
|
|
|
|
|
|
|
class AsmJsScannerTest : public ::testing::Test {
|
|
|
|
protected:
|
2017-08-22 10:53:10 +00:00
|
|
|
void SetupScanner(const char* source) {
|
|
|
|
stream = ScannerStream::ForTesting(source);
|
|
|
|
scanner.reset(new AsmJsScanner(stream.get()));
|
2017-03-16 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Skip(AsmJsScanner::token_t t) {
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK_EQ(t, scanner->Token());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkipGlobal() {
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsGlobal());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkipLocal() {
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsLocal());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-10-18 09:06:55 +00:00
|
|
|
void CheckForEnd() { CHECK_EQ(scanner->Token(), AsmJsScanner::kEndOfInput); }
|
2017-03-16 18:10:45 +00:00
|
|
|
|
|
|
|
void CheckForParseError() {
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_EQ(scanner->Token(), AsmJsScanner::kParseError);
|
2017-03-16 18:10:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
std::unique_ptr<Utf16CharacterStream> stream;
|
|
|
|
std::unique_ptr<AsmJsScanner> scanner;
|
2017-03-16 18:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, SimpleFunction) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("function foo() { return; }");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(function));
|
2017-08-22 10:53:10 +00:00
|
|
|
DCHECK_EQ("foo", scanner->GetIdentifierString());
|
2017-03-16 18:10:45 +00:00
|
|
|
SkipGlobal();
|
|
|
|
Skip('(');
|
|
|
|
Skip(')');
|
|
|
|
Skip('{');
|
|
|
|
// clang-format off
|
|
|
|
Skip(TOK(return));
|
|
|
|
// clang-format on
|
|
|
|
Skip(';');
|
|
|
|
Skip('}');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, JSKeywords) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(
|
2017-03-16 18:10:45 +00:00
|
|
|
"arguments break case const continue\n"
|
|
|
|
"default do else eval for function\n"
|
|
|
|
"if new return switch var while\n");
|
|
|
|
Skip(TOK(arguments));
|
|
|
|
Skip(TOK(break));
|
|
|
|
Skip(TOK(case));
|
|
|
|
Skip(TOK(const));
|
|
|
|
Skip(TOK(continue));
|
|
|
|
Skip(TOK(default));
|
|
|
|
Skip(TOK(do));
|
|
|
|
Skip(TOK(else));
|
|
|
|
Skip(TOK(eval));
|
|
|
|
Skip(TOK(for));
|
|
|
|
Skip(TOK(function));
|
|
|
|
Skip(TOK(if));
|
|
|
|
Skip(TOK(new));
|
|
|
|
// clang-format off
|
|
|
|
Skip(TOK(return));
|
|
|
|
// clang-format on
|
|
|
|
Skip(TOK(switch));
|
|
|
|
Skip(TOK(var));
|
|
|
|
Skip(TOK(while));
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, JSOperatorsSpread) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(
|
2017-03-16 18:10:45 +00:00
|
|
|
"+ - * / % & | ^ ~ << >> >>>\n"
|
|
|
|
"< > <= >= == !=\n");
|
|
|
|
Skip('+');
|
|
|
|
Skip('-');
|
|
|
|
Skip('*');
|
|
|
|
Skip('/');
|
|
|
|
Skip('%');
|
|
|
|
Skip('&');
|
|
|
|
Skip('|');
|
|
|
|
Skip('^');
|
|
|
|
Skip('~');
|
|
|
|
Skip(TOK(SHL));
|
|
|
|
Skip(TOK(SAR));
|
|
|
|
Skip(TOK(SHR));
|
|
|
|
Skip('<');
|
|
|
|
Skip('>');
|
|
|
|
Skip(TOK(LE));
|
|
|
|
Skip(TOK(GE));
|
|
|
|
Skip(TOK(EQ));
|
|
|
|
Skip(TOK(NE));
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, JSOperatorsTight) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(
|
2017-03-16 18:10:45 +00:00
|
|
|
"+-*/%&|^~<<>> >>>\n"
|
|
|
|
"<><=>= ==!=\n");
|
|
|
|
Skip('+');
|
|
|
|
Skip('-');
|
|
|
|
Skip('*');
|
|
|
|
Skip('/');
|
|
|
|
Skip('%');
|
|
|
|
Skip('&');
|
|
|
|
Skip('|');
|
|
|
|
Skip('^');
|
|
|
|
Skip('~');
|
|
|
|
Skip(TOK(SHL));
|
|
|
|
Skip(TOK(SAR));
|
|
|
|
Skip(TOK(SHR));
|
|
|
|
Skip('<');
|
|
|
|
Skip('>');
|
|
|
|
Skip(TOK(LE));
|
|
|
|
Skip(TOK(GE));
|
|
|
|
Skip(TOK(EQ));
|
|
|
|
Skip(TOK(NE));
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, UsesOfAsm) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("'use asm' \"use asm\"\n");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(UseAsm));
|
|
|
|
Skip(TOK(UseAsm));
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, DefaultGlobalScope) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("var x = x + x;");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK_EQ("x", scanner->GetIdentifierString());
|
|
|
|
AsmJsScanner::token_t x = scanner->Token();
|
2017-03-16 18:10:45 +00:00
|
|
|
SkipGlobal();
|
|
|
|
Skip('=');
|
|
|
|
Skip(x);
|
|
|
|
Skip('+');
|
|
|
|
Skip(x);
|
|
|
|
Skip(';');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, GlobalScope) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("var x = x + x;");
|
|
|
|
scanner->EnterGlobalScope();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK_EQ("x", scanner->GetIdentifierString());
|
|
|
|
AsmJsScanner::token_t x = scanner->Token();
|
2017-03-16 18:10:45 +00:00
|
|
|
SkipGlobal();
|
|
|
|
Skip('=');
|
|
|
|
Skip(x);
|
|
|
|
Skip('+');
|
|
|
|
Skip(x);
|
|
|
|
Skip(';');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, LocalScope) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("var x = x + x;");
|
|
|
|
scanner->EnterLocalScope();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK_EQ("x", scanner->GetIdentifierString());
|
|
|
|
AsmJsScanner::token_t x = scanner->Token();
|
2017-03-16 18:10:45 +00:00
|
|
|
SkipLocal();
|
|
|
|
Skip('=');
|
|
|
|
Skip(x);
|
|
|
|
Skip('+');
|
|
|
|
Skip(x);
|
|
|
|
Skip(';');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, Numbers) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("1 1.2 0x1f 1.e3");
|
2017-03-16 18:10:45 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsUnsigned());
|
|
|
|
CHECK_EQ(1, scanner->AsUnsigned());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsDouble());
|
|
|
|
CHECK_EQ(1.2, scanner->AsDouble());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsUnsigned());
|
|
|
|
CHECK_EQ(31, scanner->AsUnsigned());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsDouble());
|
|
|
|
CHECK_EQ(1.0e3, scanner->AsDouble());
|
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
2017-04-26 12:07:28 +00:00
|
|
|
TEST_F(AsmJsScannerTest, UnsignedNumbers) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("0x7fffffff 0x80000000 0xffffffff 0x100000000");
|
2017-04-26 12:07:28 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsUnsigned());
|
|
|
|
CHECK_EQ(0x7fffffff, scanner->AsUnsigned());
|
|
|
|
scanner->Next();
|
2017-04-26 12:07:28 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsUnsigned());
|
|
|
|
CHECK_EQ(0x80000000, scanner->AsUnsigned());
|
|
|
|
scanner->Next();
|
2017-04-26 12:07:28 +00:00
|
|
|
|
2017-08-22 10:53:10 +00:00
|
|
|
CHECK(scanner->IsUnsigned());
|
|
|
|
CHECK_EQ(0xffffffff, scanner->AsUnsigned());
|
|
|
|
scanner->Next();
|
2017-04-26 12:07:28 +00:00
|
|
|
|
|
|
|
// Numeric "unsigned" literals with a payload of more than 32-bit are rejected
|
|
|
|
// by asm.js in all contexts, we hence consider `0x100000000` to be an error.
|
|
|
|
CheckForParseError();
|
|
|
|
}
|
|
|
|
|
2017-03-16 18:10:45 +00:00
|
|
|
TEST_F(AsmJsScannerTest, BadNumber) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(".123fe");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('.');
|
|
|
|
CheckForParseError();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, Rewind1) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("+ - * /");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('+');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('+');
|
|
|
|
Skip('-');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('-');
|
|
|
|
Skip('*');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('*');
|
|
|
|
Skip('/');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('/');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, Comments) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(
|
2017-03-16 18:10:45 +00:00
|
|
|
"var // This is a test /* */ eval\n"
|
|
|
|
"var /* test *** test */ eval\n"
|
|
|
|
"function /* this */ ^");
|
|
|
|
Skip(TOK(var));
|
|
|
|
Skip(TOK(var));
|
|
|
|
Skip(TOK(eval));
|
|
|
|
Skip(TOK(function));
|
|
|
|
Skip('^');
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, TrailingCComment) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("var /* test\n");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
|
|
|
CheckForParseError();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, Seeking) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner("var eval do arguments function break\n");
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
size_t old_pos = scanner->Position();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(eval));
|
|
|
|
Skip(TOK(do));
|
|
|
|
Skip(TOK(arguments));
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(arguments));
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Rewind();
|
|
|
|
scanner->Seek(old_pos);
|
2017-04-18 12:10:56 +00:00
|
|
|
Skip(TOK(eval));
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(do));
|
|
|
|
Skip(TOK(arguments));
|
|
|
|
Skip(TOK(function));
|
|
|
|
Skip(TOK(break));
|
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(AsmJsScannerTest, Newlines) {
|
2017-08-22 10:53:10 +00:00
|
|
|
SetupScanner(
|
2017-03-16 18:10:45 +00:00
|
|
|
"var x = 1\n"
|
|
|
|
"var y = 2\n");
|
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('=');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Next();
|
|
|
|
CHECK(scanner->IsPrecededByNewline());
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip(TOK(var));
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Next();
|
2017-03-16 18:10:45 +00:00
|
|
|
Skip('=');
|
2017-08-22 10:53:10 +00:00
|
|
|
scanner->Next();
|
|
|
|
CHECK(scanner->IsPrecededByNewline());
|
2017-03-16 18:10:45 +00:00
|
|
|
CheckForEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|