-
Notifications
You must be signed in to change notification settings - Fork 238
/
ast_test.cc
62 lines (58 loc) · 2.62 KB
/
ast_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include "gtest/gtest.h"
#include "v8.h"
#include "libplatform/libplatform.h"
#include "v8_test_fixture.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parser.h"
#include "src/parsing/parsing.h"
#include "src/parsing/scanner-character-streams.h"
#include "src/common/globals.h"
using namespace v8;
namespace i = v8::internal;
class AstTest : public V8TestFixture {
};
TEST_F(AstTest, ParseInfo) {
const Isolate::Scope isolate_scope(isolate_);
const v8::HandleScope handle_scope(isolate_);
i::Isolate* internal_isolate = asInternal(isolate_);
i::Factory* factory = internal_isolate->factory();
i::Handle<i::String> source = factory->NewStringFromStaticChars(
"const msg = 'testing'");
i::Handle<i::Script> script = factory->NewScript(source);
i::UnoptimizedCompileState state(internal_isolate);
i::UnoptimizedCompileFlags compile_flags = i::UnoptimizedCompileFlags::ForScriptCompile(
internal_isolate, *script);
i::ParseInfo parse_info(internal_isolate, compile_flags, &state);
EXPECT_TRUE(compile_flags.is_toplevel());
EXPECT_FALSE(compile_flags.is_eval());
//EXPECT_FALSE(parse_info.is_strict_mode());
//EXPECT_FALSE(compile_flags.is_native());
EXPECT_FALSE(compile_flags.is_module());
bool result = i::parsing::ParseProgram(&parse_info,
script, internal_isolate, i::parsing::ReportStatisticsMode::kYes);
EXPECT_TRUE(result);
i::Scope* scope = parse_info.literal()->scope();
EXPECT_EQ(scope->start_position(), 0) << "start position was not 0";
EXPECT_EQ(scope->end_position(), 21) << "end position was not 21";
}
TEST_F(AstTest, Parser) {
const Isolate::Scope isolate_scope(isolate_);
const v8::HandleScope handle_scope(isolate_);
i::Isolate* internal_isolate = asInternal(isolate_);
i::Factory* factory = internal_isolate->factory();
i::Handle<i::String> source = factory->NewStringFromStaticChars(
"const msg = 'testing'");
i::Handle<i::Script> script = factory->NewScript(source);
i::UnoptimizedCompileState state(internal_isolate);
i::UnoptimizedCompileFlags compile_flags = i::UnoptimizedCompileFlags::ForScriptCompile(
internal_isolate, *script);
i::ParseInfo parse_info(internal_isolate, compile_flags, &state);
bool result = i::parsing::ParseProgram(&parse_info,
script, internal_isolate, i::parsing::ReportStatisticsMode::kYes);
EXPECT_TRUE(result);
// ParseProgram set parse_info's literal_ which is a AST node
i::FunctionLiteral* function_literal = parse_info.literal();
EXPECT_EQ(function_literal->syntax_kind(), i::FunctionSyntaxKind::kAnonymousExpression);
EXPECT_EQ(function_literal->kind(), i::FunctionKind::kNormalFunction);
}