-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbindings.cc
More file actions
115 lines (94 loc) · 3.82 KB
/
Copy pathbindings.cc
File metadata and controls
115 lines (94 loc) · 3.82 KB
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
#include "src/bindings.h"
#include <cstdio>
#include <fstream>
#include <iostream>
#include <sstream>
namespace jsvm {
const intptr_t kExternalReferences[] = {
reinterpret_cast<intptr_t>(Print),
0, // terminator
};
// --single-threaded drops the background compile and concurrent-marking
// threads. Anything set here MUST also be set by mksnapshot_tool: the snapshot
// carries a hash of the flags and will not deserialize under a different set.
const char kV8Flags[] = "--single-threaded";
v8::Local<v8::String> Str(v8::Isolate* isolate, const std::string& s) {
return v8::String::NewFromUtf8(isolate, s.data(), v8::NewStringType::kNormal,
static_cast<int>(s.size()))
.ToLocalChecked();
}
std::string ToStdString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
if (value.IsEmpty()) return "";
v8::String::Utf8Value utf8(isolate, value);
return *utf8 ? std::string(*utf8, utf8.length()) : std::string();
}
void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
v8::HandleScope scope(isolate);
std::string line;
for (int i = 0; i < args.Length(); ++i) {
if (i > 0) line += ' ';
line += ToStdString(isolate, args[i]);
}
line += '\n';
fwrite(line.data(), 1, line.size(), stdout);
}
void InstallGlobals(v8::Isolate* isolate, v8::Local<v8::ObjectTemplate> global) {
global->Set(isolate, "print", v8::FunctionTemplate::New(isolate, Print));
}
void ReportException(v8::Isolate* isolate, v8::TryCatch* try_catch) {
v8::HandleScope scope(isolate);
const std::string exception = ToStdString(isolate, try_catch->Exception());
v8::Local<v8::Message> message = try_catch->Message();
if (message.IsEmpty()) {
// Thrown before any script was running, or termination with no message.
fprintf(stderr, "%s\n", exception.empty() ? "uncaught exception"
: exception.c_str());
return;
}
v8::Local<v8::Context> context = isolate->GetCurrentContext();
// VERSION RISK: ScriptOrigin lost its leading Isolate* parameter around
// V8 10.x, and GetScriptOrigin()/ResourceName() have moved before. If this
// does not compile, `message->GetScriptResourceName()` is the older spelling.
const std::string file =
ToStdString(isolate, message->GetScriptOrigin().ResourceName());
const int line = message->GetLineNumber(context).FromMaybe(0);
fprintf(stderr, "%s:%d: %s\n", file.empty() ? "<eval>" : file.c_str(), line,
exception.c_str());
// Source line plus a caret, the way d8 does it.
v8::Local<v8::String> source_line;
if (message->GetSourceLine(context).ToLocal(&source_line)) {
const std::string text = ToStdString(isolate, source_line);
fprintf(stderr, "%s\n", text.c_str());
const int start = message->GetStartColumn(context).FromMaybe(0);
const int end = message->GetEndColumn(context).FromMaybe(start + 1);
std::string caret(static_cast<size_t>(start), ' ');
caret.append(static_cast<size_t>(end > start ? end - start : 1), '^');
fprintf(stderr, "%s\n", caret.c_str());
}
v8::Local<v8::Value> stack;
if (try_catch->StackTrace(context).ToLocal(&stack)) {
const std::string text = ToStdString(isolate, stack);
if (!text.empty()) fprintf(stderr, "%s\n", text.c_str());
}
}
bool ReadFile(const std::string& path, std::string* out) {
std::ifstream in(path, std::ios::binary);
if (!in) return false;
std::ostringstream ss;
ss << in.rdbuf();
*out = ss.str();
return true;
}
bool WriteFile(const std::string& path, const char* data, size_t len) {
std::ofstream out(path, std::ios::binary | std::ios::trunc);
if (!out) return false;
out.write(data, static_cast<std::streamsize>(len));
return out.good();
}
std::string ReadStdin() {
std::ostringstream ss;
ss << std::cin.rdbuf();
return ss.str();
}
} // namespace jsvm