-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathruntime_42tinyjs.cpp
105 lines (84 loc) · 2.57 KB
/
runtime_42tinyjs.cpp
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
#include "runtime.h"
#include "TinyJS_Functions.h"
#include "TinyJS_MathFunctions.h"
namespace test262_harness_cpp
{
void runtime::create_realm()
{
if (context != nullptr)
{
return;
}
context = new CTinyJS();
}
bool runtime::evaluate(std::string& file, std::string source, std::string& error_type, std::string& error_description)
{
if (context == nullptr)
{
return true;
}
try
{
context->execute(source);
}
catch (CScriptException* exception)
{
switch(exception->errorType)
{
case Error:
error_type = "Error";
break;
case EvalError:
error_type = "EvalError";
break;
case RangeError:
error_type = "RangeError";
break;
case ReferenceError:
error_type = "ReferenceError";
break;
case SyntaxError:
error_type = "SyntaxError";
break;
case TypeError:
error_type = "TypeError";
break;
}
error_description = exception->toString();
auto position = error_description.find_first_of(":");
if (position != std::string::npos)
{
auto parsed_error_type = error_description.substr(0, position);
if (error_type == "")
{
error_type = parsed_error_type;
}
if (error_type == parsed_error_type)
{
error_description = error_description.substr(position + 1);
}
}
return false;
}
catch (std::exception& exception)
{
error_description = exception.what();
return false;
}
catch (...)
{
error_description = "A reason for the error was not returned.";
return false;
}
return true;
}
void runtime::destroy_realm()
{
delete context;
context = nullptr;
}
runtime::~runtime()
{
destroy_realm();
}
}