CCF
Loading...
Searching...
No Matches
src
js
modules
kv_module_loader.h
Go to the documentation of this file.
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the Apache 2.0 License.
3
#pragma once
4
5
#include "
ccf/ds/logger.h
"
6
#include "
ccf/js/modules/module_loader_interface.h
"
7
#include "
ccf/service/tables/modules.h
"
8
#include "
ccf/tx.h
"
9
#include "
ds/internal_logger.h
"
10
11
#include <string>
12
13
namespace
ccf::js::modules
14
{
15
class
KvModuleLoader
:
public
ModuleLoaderInterface
16
{
17
protected
:
18
ccf::Modules::ReadOnlyHandle
*
modules_handle
;
19
20
public
:
21
KvModuleLoader
(
ccf::Modules::ReadOnlyHandle
* mh) :
modules_handle
(mh) {}
22
23
std::optional<js::core::JSWrappedValue>
get_module
(
24
std::string_view module_name,
js::core::Context
& ctx)
override
25
{
26
std::string module_name_kv(module_name);
27
if
(module_name_kv[0] !=
'/'
)
28
{
29
module_name_kv.insert(0,
"/"
);
30
}
31
32
CCF_APP_TRACE
(
"Looking for module '{}' in KV"
, module_name_kv);
33
34
auto
module_str =
modules_handle
->
get
(module_name_kv);
35
if
(!module_str.has_value())
36
{
37
CCF_APP_TRACE
(
"Module '{}' not found"
, module_name_kv);
38
return
std::nullopt;
39
}
40
41
const
auto
* module_name_quickjs = module_name_kv.c_str() + 1;
42
const
char
* buf = module_str->c_str();
43
size_t
buf_len = module_str->size();
44
auto
parsed_module = ctx.
eval
(
45
buf,
46
buf_len,
47
module_name_quickjs,
48
JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
49
if
(parsed_module.is_exception())
50
{
51
auto
[reason, trace] = ctx.
error_message
();
52
53
auto
& rt = ctx.
runtime
();
54
if
(rt.log_exception_details)
55
{
56
CCF_APP_FAIL
(
"{}: {}"
, reason, trace.value_or(
"<no trace>"
));
57
}
58
59
throw
std::runtime_error(fmt::format(
60
"Failed to compile module '{}': {}"
, module_name, reason));
61
}
62
63
CCF_APP_TRACE
(
64
"Module '{}' found in KV (table: {})"
,
65
module_name_kv,
66
modules_handle
->
get_name_of_map
());
67
return
parsed_module;
68
}
69
};
70
}
ccf::js::core::Context
Definition
context.h:45
ccf::js::core::Context::eval
JSWrappedValue eval(const char *input, size_t input_len, const char *filename, int eval_flags) const
Definition
context.cpp:418
ccf::js::core::Context::error_message
std::pair< std::string, std::optional< std::string > > error_message()
Definition
context.cpp:180
ccf::js::core::Context::runtime
Runtime & runtime()
Definition
context.h:77
ccf::js::modules::KvModuleLoader
Definition
kv_module_loader.h:16
ccf::js::modules::KvModuleLoader::modules_handle
ccf::Modules::ReadOnlyHandle * modules_handle
Definition
kv_module_loader.h:18
ccf::js::modules::KvModuleLoader::KvModuleLoader
KvModuleLoader(ccf::Modules::ReadOnlyHandle *mh)
Definition
kv_module_loader.h:21
ccf::js::modules::KvModuleLoader::get_module
std::optional< js::core::JSWrappedValue > get_module(std::string_view module_name, js::core::Context &ctx) override
Definition
kv_module_loader.h:23
ccf::js::modules::ModuleLoaderInterface
Definition
module_loader_interface.h:21
ccf::kv::ReadableMapHandle
Definition
map_handle.h:13
ccf::kv::ReadableMapHandle::get_name_of_map
std::string get_name_of_map() const
Definition
map_handle.h:27
ccf::kv::ReadableMapHandle::get
std::optional< V > get(const K &key)
Definition
map_handle.h:45
internal_logger.h
logger.h
CCF_APP_TRACE
#define CCF_APP_TRACE
Definition
logger.h:291
CCF_APP_FAIL
#define CCF_APP_FAIL
Definition
logger.h:294
module_loader_interface.h
modules.h
ccf::js::modules
Definition
module_loader_interface.h:19
tx.h
Generated by
1.9.8