Skip to content

Commit 1abccc9

Browse files
committed
Merge pull request #27 from elite-lang/dev
大幅完善插件包机制
2 parents 74ab534 + 91bfd3d commit 1abccc9

File tree

15 files changed

+253
-17
lines changed

15 files changed

+253
-17
lines changed

Builder/src/Worker.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: sxf
33
* @Date: 2015-11-11 16:00:38
44
* @Last Modified by: sxf
5-
* @Last Modified time: 2015-12-25 10:26:37
5+
* @Last Modified time: 2015-12-26 19:19:22
66
*/
77

88

@@ -21,7 +21,7 @@ void Worker::Init(LexInterface* l, Parser* p, ScriptRunner* s, CodeGen* c) {
2121
// 初始化词法分析器与语法分析器
2222
l->Init(NULL);
2323
p->BuildParser();
24-
c->Init();
24+
2525
}
2626

2727
void Worker::Run(const char* input, const char* output) {
@@ -54,12 +54,17 @@ Worker* Worker::CreateDefault(const char* lex_cfg,
5454
p->setLex(l);
5555
p->setScriptRunner(s);
5656

57+
// 向脚本引擎中注入更多接口
58+
c->Init(NULL);
59+
s->setCodeGenContext(c->getContext());
60+
5761
// 配置词法分析器和语法分析器
5862
l->ReadConfig(lex_cfg);
5963
p->AddBNF(parser_cfg);
6064

6165
// 配置外置插件
6266
s->setUpLoader(package_path);
67+
6368

6469
return Create(l, p, s, c);
6570
}

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ include_directories(src extlib/include
3030
ExIconv/include
3131
)
3232

33+
3334
## 链接目录
3435
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/extlib/lib)
3536

@@ -54,6 +55,10 @@ endif()
5455
target_link_libraries(elite builder meta red scanner lex oolua_d lua exiconv
5556
${LLVM_LIBS} ${dl_lib_link})
5657

58+
59+
## 合并Headers
60+
include(cmake/CombineHeader.cmake)
61+
5762
## 下载第三方库
5863
include(cmake/ThirdPartyBuild.cmake)
5964

ExIconv/src/FileUtils.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: sxf
33
* @Date: 2015-12-11 18:48:19
44
* @Last Modified by: sxf
5-
* @Last Modified time: 2015-12-25 14:37:11
5+
* @Last Modified time: 2015-12-25 19:54:42
66
*/
77

88
#include "FileUtils.h"
@@ -87,6 +87,25 @@ int FileUtils::dir_traversal(const std::string& path, IFileTraversal& ifile, tra
8787
}
8888

8989
int FileUtils::dir_recursive_traversal(const std::string& path, IFileTraversal& ifile, traversal_type t) {
90+
std::error_code ec;
91+
recursive_directory_iterator di(path, ec);
92+
recursive_directory_iterator end;
93+
while (di != end) {
94+
auto entry = *di;
95+
bool pd = true;
96+
if (t == only_file)
97+
pd = is_regular_file(entry.path());
98+
if (t == only_dir)
99+
pd = is_directory(entry.path());
100+
if (pd) {
101+
ifile.Work(
102+
llvm::sys::path::parent_path(entry.path()).str(),
103+
llvm::sys::path::filename(entry.path()).str(),
104+
llvm::sys::path::extension(entry.path()).str()
105+
);
106+
}
107+
di.increment(ec);
108+
}
90109
return 0;
91110
}
92111

MetaScriptRunner/include/MetaScriptRunner.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: sxf
33
* @Date: 2015-11-05 21:21:50
44
* @Last Modified by: sxf
5-
* @Last Modified time: 2015-12-25 18:46:07
5+
* @Last Modified time: 2015-12-26 14:36:28
66
*/
77

88

@@ -17,6 +17,7 @@ namespace OOLUA {
1717
class Script;
1818
}
1919
class PackageLoader;
20+
class CodeGenContext;
2021

2122
/**
2223
* @brief 元脚本解析器类
@@ -87,10 +88,13 @@ class MetaScriptRunner : public ScriptRunner
8788

8889
void setUpLoader(const string& path);
8990

91+
void setCodeGenContext(CodeGenContext* ctx) { context = ctx; }
92+
CodeGenContext* getCodeGenContext() { return context; }
9093
protected:
9194
OOLUA::Script* vm;
9295
Node* root;
9396
PackageLoader* loader;
97+
CodeGenContext* context;
9498

9599
MetaScriptRunner();
96100
~MetaScriptRunner();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* @Author: sxf
3+
* @Date: 2015-12-25 20:40:43
4+
* @Last Modified by: sxf
5+
* @Last Modified time: 2015-12-26 16:37:59
6+
*/
7+
8+
9+
#ifndef PACKAGE_JIT_H
10+
#define PACKAGE_JIT_H
11+
12+
#include <string>
13+
14+
class PackageJIT_private;
15+
class MetaScriptRunner;
16+
namespace llvm {
17+
class ExecutionEngine;
18+
};
19+
20+
class PackageJIT
21+
{
22+
public:
23+
static void LoadPlugin(const std::string& path, const std::string& name, MetaScriptRunner* msr);
24+
static void AddSymbol(const std::string& name, void* ptr);
25+
static PackageJIT_private* getInstance();
26+
27+
private:
28+
PackageJIT();
29+
~PackageJIT();
30+
static PackageJIT_private* priv;
31+
};
32+
33+
34+
#endif // PACKAGE_JIT_H

MetaScriptRunner/src/EPackage.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
* @Author: sxf
33
* @Date: 2015-12-24 17:15:29
44
* @Last Modified by: sxf
5-
* @Last Modified time: 2015-12-25 18:47:00
5+
* @Last Modified time: 2015-12-26 19:12:39
66
*/
77

88
#include "EPackage.h"
99
#include "cJSON.h"
1010
#include "FileUtils.h"
1111
#include "PathUtils.h"
1212
#include "MetaScriptRunner.h"
13+
#include "PackageJIT.h"
1314
#include <iostream>
1415
using namespace std;
1516

1617
class FileTraversal : public IFileTraversal {
1718
public:
18-
FileTraversal(MetaScriptRunner* msr) {
19+
FileTraversal(MetaScriptRunner* msr, EPackage& epkg)
20+
: epkg(epkg) {
1921
this->msr = msr;
2022
}
2123

@@ -27,8 +29,12 @@ class FileTraversal : public IFileTraversal {
2729
if (suffix == ".lua") {
2830
msr->run_file(filepath);
2931
}
32+
if (suffix == ".epbc") {
33+
PackageJIT::LoadPlugin(filepath, epkg.getName(), msr);
34+
}
3035
}
3136
MetaScriptRunner* msr;
37+
EPackage& epkg;
3238
};
3339

3440

@@ -46,7 +52,7 @@ EPackage::~EPackage() {
4652

4753
void EPackage::Load() {
4854
cout << "Load Package: " << base_path << endl;
49-
FileTraversal ft(msr);
55+
FileTraversal ft(msr, *this);
5056
FileUtils::dir_traversal(base_path, ft, FileUtils::only_file);
5157
}
5258

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* @Author: sxf
3+
* @Date: 2015-12-26 09:51:14
4+
* @Last Modified by: sxf
5+
* @Last Modified time: 2015-12-26 19:21:50
6+
*/
7+
8+
#include "PackageJIT.h"
9+
#include <string>
10+
#include <iostream>
11+
#include "MetaScriptRunner.h"
12+
13+
#include "llvm/ExecutionEngine/GenericValue.h"
14+
#include "llvm/ExecutionEngine/Interpreter.h"
15+
#include "llvm/IR/Constants.h"
16+
#include "llvm/IR/DerivedTypes.h"
17+
#include "llvm/IR/IRBuilder.h"
18+
#include "llvm/IR/Instructions.h"
19+
#include "llvm/IR/LLVMContext.h"
20+
#include "llvm/IR/Module.h"
21+
#include <llvm/IRReader/IRReader.h>
22+
#include <llvm/Support/SourceMgr.h>
23+
#include "llvm/Support/ManagedStatic.h"
24+
#include "llvm/Support/TargetSelect.h"
25+
#include <llvm/Support/MemoryBuffer.h>
26+
#include "llvm/Support/raw_ostream.h"
27+
#include <llvm/Support/DynamicLibrary.h>
28+
29+
using namespace llvm;
30+
using namespace std;
31+
32+
class PackageJIT_private
33+
{
34+
public:
35+
PackageJIT_private() {
36+
InitializeNativeTarget();
37+
}
38+
~PackageJIT_private() {
39+
delete EE;
40+
}
41+
LLVMContext context;
42+
ExecutionEngine* EE = NULL;
43+
44+
void initEE(std::unique_ptr<Module> Owner) {
45+
if (EE == NULL)
46+
EE = EngineBuilder(std::move(Owner)).create();
47+
else
48+
EE->addModule(std::move(Owner));
49+
}
50+
51+
void LoadPlugin(const std::string& path, const std::string& name, MetaScriptRunner* msr) {
52+
53+
SMDiagnostic error;
54+
std::unique_ptr<Module> Owner = getLazyIRFileModule(path, error, context);
55+
if(Owner == nullptr) {
56+
cout << "Load Error: " << path << endl;
57+
Owner->dump();
58+
return;
59+
}
60+
61+
initEE(std::move(Owner));
62+
63+
string func_name = name + "_elite_plugin_init";
64+
Function* func = EE->FindFunctionNamed(func_name.c_str());
65+
66+
std::vector<GenericValue> args;
67+
args.push_back(GenericValue(msr->getCodeGenContext()));
68+
GenericValue gv = EE->runFunction(func, args);
69+
// plugin_init_func init = (plugin_init_func)(intptr_t)(EE->getPointerToFunction(func));
70+
// init(msr->getCodeGenContext());
71+
}
72+
73+
};
74+
75+
76+
77+
PackageJIT::PackageJIT() {
78+
}
79+
80+
PackageJIT::~PackageJIT() {
81+
delete priv;
82+
}
83+
84+
85+
void PackageJIT::LoadPlugin(const std::string& path, const std::string& name, MetaScriptRunner* msr) {
86+
getInstance()->LoadPlugin(path, name, msr);
87+
}
88+
89+
void PackageJIT::AddSymbol(const std::string& name, void* ptr) {
90+
llvm::sys::DynamicLibrary::AddSymbol(name, ptr);
91+
}
92+
93+
PackageJIT_private* PackageJIT::getInstance() {
94+
if (priv == NULL)
95+
priv = new PackageJIT_private();
96+
return priv;
97+
}
98+
99+
PackageJIT_private* PackageJIT::priv = NULL;

MetaScriptRunner/src/PackageLoader.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
* @Author: sxf
33
* @Date: 2015-12-24 17:21:09
44
* @Last Modified by: sxf
5-
* @Last Modified time: 2015-12-25 18:37:44
5+
* @Last Modified time: 2015-12-26 19:21:56
66
*/
77

88
#include "PackageLoader.h"
99
#include "FileUtils.h"
1010
#include "PathUtils.h"
1111
#include "MetaScriptRunner.h"
12+
#include "PackageJIT.h"
1213
#include <iostream>
1314
using namespace std;
1415

@@ -57,11 +58,16 @@ void PackageLoader::FindAll() {
5758
FileUtils::dir_traversal(base_path, dft, FileUtils::only_dir);
5859
}
5960

61+
static void HelloWorld() {
62+
cout << "Hello World" << endl;
63+
}
64+
6065

6166
/**
6267
* @brief 加载全部默认需要加载的包
6368
*/
6469
void PackageLoader::LoadDefault() {
70+
PackageJIT::AddSymbol("HelloWorld", (void*)HelloWorld);
6571
for (auto& p : packages) {
6672
auto pkg = p.second;
6773
if (pkg->isDefaultLoad() && !pkg->isLoaded())

cmake/CombineHeader.cmake

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
3+
ADD_CUSTOM_TARGET(header
4+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../ExIconv/include header
5+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../Lex/include header
6+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../LR_Scanner/includes header
7+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../RedApple/includes header
8+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../Builder/include header
9+
COMMAND ${CMAKE_COMMAND} -E copy_directory ../MetaScriptRunner/include header
10+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
11+
COMMENT "Generating headers"
12+
VERBATIM)

0 commit comments

Comments
 (0)