-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.cc
62 lines (54 loc) · 2.02 KB
/
instance.cc
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
#include "instance.h"
#include "log.h"
namespace wvm
{
Instance::Instance(std::shared_ptr<Module> module_ptr) : module_ptr(module_ptr)
{
}
Instance::~Instance()
{
}
std::shared_ptr<wvm::Runtime> Instance::instantiate()
{
LOG("-> instantiate");
std::shared_ptr<wvm::Runtime> runtime_ptr = std::make_shared<wvm::Runtime>(module_ptr);
/* func */
LOG(" -> func");
for (auto i = 0; i < module_ptr->funcDefs.size(); ++i)
{
const auto typeIdx = module_ptr->funcTypesIndices.at(i);
const auto &funcType = module_ptr->funcTypes.at(typeIdx);
const auto codeEntry = module_ptr->funcDefs.at(i).body.data();
// Wasm types -> RT types (value).
runtime_ptr->rtFuncDescriptor.emplace_back(&funcType, codeEntry);
runtime_ptr->expandWasmTypesToRTValues(
runtime_ptr->rtFuncDescriptor.back().localsDefault,
// 参数类型
funcType.first,
// 内部变量
module_ptr->funcDefs.at(i).locals);
}
/* entry point */
const auto entryFunc = std::find_if(
module_ptr->exports.begin(),
module_ptr->exports.end(),
[](auto &item) -> bool
{
return item.name == "add" && item.extKind == EXT_KIND_FUNC;
});
LOG(" -> entryFuc: ", entryFunc->name);
const auto funcIdx = entryFunc->extIdx;
if (entryFunc != module_ptr->exports.end())
{
runtime_ptr->rtEntryIdx = funcIdx;
}
const auto &inputFuncArgTypes = runtime_ptr->rtFuncDescriptor.at(funcIdx).funcType->first;
runtime_ptr->stack.push_back(
Runtime::RTValueFrame{
runtime_ptr->convertStrToRTVal("1", inputFuncArgTypes[0])});
runtime_ptr->stack.push_back(
Runtime::RTValueFrame{
runtime_ptr->convertStrToRTVal("2", inputFuncArgTypes[1])});
return runtime_ptr;
};
}