-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (102 loc) · 3.24 KB
/
main.cpp
File metadata and controls
136 lines (102 loc) · 3.24 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#ifdef ENABLE_JS_RUNTIME
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include "ffi/NativeScriptException.h"
#include "runtime/Bundle.h"
#include "runtime/Runtime.h"
#include "runtime/RuntimeConfig.h"
#include "segappend.h"
#include "ffi/Tasks.h"
#include "BundleLoader.h"
using namespace nativescript;
void bootFromBytecode(std::string baseDir, const void* data, size_t size) {
RuntimeConfig.BaseDir = baseDir;
auto runtime = Runtime();
runtime.Init();
// TODO
// runtime.ExecuteBytecode(data, size);
runtime.RunLoop();
Tasks::Drain();
}
void bootFromModuleSpec(std::string baseDir, std::string spec) {
RuntimeConfig.BaseDir = baseDir;
RuntimeConfig.ApplicationPath = baseDir;
// In CLI mode, map "~" imports to the directory of the entry file when
// possible so app bundles that use "~/package.json" resolve consistently.
std::error_code ec;
std::filesystem::path specPath(spec);
auto absoluteSpecPath = std::filesystem::absolute(specPath, ec);
if (!ec && !absoluteSpecPath.empty()) {
RuntimeConfig.ApplicationPath = absoluteSpecPath.parent_path().string();
}
auto runtime = Runtime();
runtime.Init();
try {
runtime.RunModule(spec);
} catch (const nativescript::NativeScriptException& e) {
std::cerr << "Uncaught Exception: " << e.Description() << std::endl;
std::exit(1);
}
runtime.RunLoop();
Tasks::Drain();
}
int main(int argc, char** argv) {
RuntimeConfig.LogToSystemConsole = true;
RuntimeConfig.Arguments.clear();
if (argv != nullptr && argc > 0) {
RuntimeConfig.Arguments.reserve(static_cast<size_t>(argc));
for (int i = 0; i < argc; i++) {
RuntimeConfig.Arguments.emplace_back(argv[i] != nullptr ? argv[i] : "");
}
}
#ifdef __APPLE__
std::string bytecodePath = getBytecodePathFromBundle();
if (!bytecodePath.empty()) {
std::string bundlePath = getBundlePath();
std::ifstream file(bytecodePath, std::ios::binary);
if (!file.is_open()) {
std::cout << "Failed to open bytecode file" << std::endl;
return 1;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
file.seekg(0, std::ios::beg);
std::vector<uint8_t> data(size);
file.read((char*)data.data(), size);
file.close();
bootFromBytecode(bundlePath, data.data(), size);
return 0;
}
#endif // __APPLE__
const uint8_t* segmentData;
size_t segmentSize;
auto status = segappend_load_segment("__nativescript_start",
(void**)&segmentData, &segmentSize);
std::string cwd = std::filesystem::current_path().string();
if (status == segappend_ok) {
size_t bytecode_size = *(size_t*)segmentData;
segmentData += sizeof(size_t);
bootFromBytecode(cwd, segmentData, bytecode_size);
} else {
std::string mainPath = resolveMainPath();
if (!mainPath.empty()) {
bootFromModuleSpec(cwd, mainPath);
return 0;
}
if (argc < 3) {
std::cout << "Usage: " << argv[0] << " run <js file>" << std::endl;
return 1;
}
std::string cmd = argv[1];
if (cmd == "run") {
bootFromModuleSpec(cwd, argv[2]);
} else {
std::cout << "Unknown command: " << cmd << std::endl;
return 1;
}
}
return 0;
}
#endif // ENABLE_JS_RUNTIME