This repository has been archived by the owner on Aug 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
es.cc
146 lines (137 loc) · 3.91 KB
/
es.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <codecvt>
#include <locale>
#include <es/parser/parser.h>
#include <es/types/property_descriptor_object_conversion.h>
#include <es/enter_code.h>
#include <es/eval.h>
#include <es/utils/helper.h>
#include <es/gc/heap.h>
#include <es/impl.h>
#ifdef PERF
#include <gperftools/profiler.h>
#endif
using namespace es;
// NOTE(zhuzilin) There are some copy and paste from stackoverflow...
std::u16string ReadUTF8FileToUTF16String(std::string filename) {
// https://stackoverflow.com/a/2602258/5163915
std::ifstream file(filename);
std::stringstream buffer;
buffer << file.rdbuf();
std::string utf8_file_content = buffer.str();
// https://stackoverflow.com/a/7235204/5163915
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
std::u16string utf16_file_content = convert.from_bytes(utf8_file_content);
return utf16_file_content;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cout << "no filename presented" << "\n";
return 0;
}
std::string filename(argv[1]);
std::u16string source = ReadUTF8FileToUTF16String(filename);
#ifdef PERF
ProfilerStart("parse_program.profile");
#endif
Parser parser(source);
AST* ast = parser.ParseProgram();
#ifdef PERF
ProfilerStop();
#endif
if (ast->IsIllegal()) {
size_t start = ast->start();
for (size_t i = 0; i < 10; i++) {
if (start == 0 || character::IsLineTerminator(source[start - 1]))
break;
start--;
}
size_t end = ast->end();
for (size_t i = 0; i < 10; i++) {
if (end == source.size() || character::IsLineTerminator(source[end]))
break;
end++;
}
std::cout << "\033[1;31m" << "ParserError: " << "\033[0m"
<< log::ToString(source.substr(start, ast->start() - start))
<< "\033[1;31m" << log::ToString(ast->source()) << "\033[0m"
<< log::ToString(source.substr(ast->end(), end - ast->end())) << "\n";
return 0;
}
#ifdef PERF
ProfilerStart("optimize_ast.profile");
#endif
ast = Optimize(ast);
#ifdef PERF
ProfilerStop();
#endif
#ifdef PARSER_ONLY
std::cout << "finish parsing" << std::endl;
return 0;
#endif
Init();
Handle<Error> e = Error::Ok();
#ifdef PERF
ProfilerStart("eval_program.profile");
#endif
EnterGlobalCode(e, ast);
if (unlikely(!e.val()->IsOk())) {
std::cout << "\033[1;31m" << "\033[2menter\033[0m global failed: " << e.ToString() << "\033[0m" << "\n";
return 0;
}
Completion res = EvalProgram(ast);
#ifdef PERF
ProfilerStop();
#endif
switch (res.type()) {
case Completion::THROW: {
std::cout << "\033[1;31m" << "Uncaught ";
Handle<JSValue> val;
if (res.value().val()->IsError()) {
Handle<Error> error = static_cast<Handle<Error>>(res.value());
val = error.val()->value();
switch (error.val()->error_type()) {
case Error::E_EVAL:
std::cout << "EvalError: ";
break;
case Error::E_RANGE:
std::cout << "RangeError: ";
break;
case Error::E_REFERENCE:
std::cout << "ReferenceError: ";
break;
case Error::E_SYNTAX:
std::cout << "SyntaxError: ";
break;
case Error::E_TYPE:
std::cout << "TypeError: ";
break;
case Error::E_URI:
std::cout << "URIError: ";
break;
default:
break;
}
} else {
val = res.value();
}
log::Debugger::TurnOff();
Handle<Error> e = Error::Ok();
Handle<String> msg = ToString(e, val);
if (unlikely(!e.val()->IsOk()))
std::cout << val.ToString() << "\033[0m" << "\n";
else
std::cout << log::ToString(msg.val()->data()) << "\033[0m" << "\n";
break;
}
default:
break;
}
#ifdef STATS
std::cout << "Heap stats in the end:" << std::endl;
Stats();
#endif
}