-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
114 lines (94 loc) · 3.42 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include "utils/FileUtils.h"
#include "runtime/ThreadPool.h"
#include "runtime/ClassLoader.h"
#include "runtime/heap/JavaClass.h"
#include "interpret/Interpret.h"
#include <boost/program_options.hpp>
using namespace std;
using namespace boost;
using namespace boost::program_options;
std::mutex mtx;
using namespace std;
int main(int argc, char **argv)
{
mtx.lock();
try {
options_description desc("新哥的java \n"
"author: zhukovasky\n"
"Usage: jvm [-options] [args...]\n"
" e.g jvm --help \n"
" \n");
desc.add_options()
("help", "帮助命令")
("classpath",value<string>(),
"jvm --classpath xxx; \n")
("xjre",value<string>(),"xjrehome")
("javaclass",value<string>(),"")
;
variables_map vm;
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
return 1;
}
std::string classpath="";
std::string xjre="";
std::string javaClassName="";
if (vm.count("classpath")) {
classpath=vm["classpath"].as<string>();
}
if(vm.count("javaclass")){
javaClassName=vm["javaclass"].as<string>();
}
if(vm.count("xjre")){
xjre=vm["xjre"].as<string>();
}
else{
cout<< desc <<endl;
exit(0);
}
// cout<<classpath<<endl;
// cout<<javaClassName<<endl;
// exit(0);
// Runtime::JavaRuntimeEnv javaRuntimeEnv;
// javaRuntimeEnv.initRuntime();
Runtime::ThreadPool threadPool;
threadPool.start(1);
std::future<void> result= threadPool.submit([=]() -> void{
// cout<<"hello"<<endl;
Runtime::ClassPath *classPath=new Runtime::ClassPath();
classPath->parseBootAndExtClassPath(xjre);
classPath->parseUserClassPath(classpath);
Runtime::ClassLoader *classLoader=new Runtime::ClassLoader(classPath);
//
if(!Utils::StringUtils::endsWith(javaClassName,".class")){
cerr<<"java虚拟机加载文件出错"<<endl;
exit(0);
}
std::string javaName=Utils::StringUtils::replaceAll(Utils::StringUtils::replaceAll(javaClassName, ".", "/"),"/class",".class");
Runtime::JavaClass* javaClass=classLoader->loadClass(javaName);
Runtime::Heap::Method *mainMethod=javaClass->getMainMethod();
if(mainMethod== nullptr){
cerr<<"java虚拟机找不到主方法"<<endl;
exit(0);
}
Interpret::Interpret *interpret;
interpret->execByteCode(mainMethod);
//执行引擎执行java
});
result.share().get();
result.get();
// std::vector<std::thread> threadVector=threadPool.threads;
// std::vector<std::thread>::iterator threadIterator;
// for (threadIterator=threadVector.begin(); threadIterator!=threadVector.end() ; ++threadIterator) {
// threadIterator.operator*();
// }
threadPool.workQueue;
mtx.unlock();
}catch (std::exception& e){
std::cout<<e.what()<<std::endl;
}
return 0;
}