CCF
Loading...
Searching...
No Matches
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"
8#include "ccf/tx.h"
9
10#include <string>
11
12namespace ccf::js::modules
13{
15 {
16 protected:
18
19 public:
21
22 virtual std::optional<js::core::JSWrappedValue> get_module(
23 std::string_view module_name, js::core::Context& ctx) override
24 {
25 std::string module_name_kv(module_name);
26 if (module_name_kv[0] != '/')
27 {
28 module_name_kv.insert(0, "/");
29 }
30
31 CCF_APP_TRACE("Looking for module '{}' in KV", module_name_kv);
32
33 auto module_str = modules_handle->get(module_name_kv);
34 if (!module_str.has_value())
35 {
36 CCF_APP_TRACE("Module '{}' not found", module_name_kv);
37 return std::nullopt;
38 }
39
40 auto module_name_quickjs = module_name_kv.c_str() + 1;
41 const char* buf = module_str->c_str();
42 size_t buf_len = module_str->size();
43 auto parsed_module = ctx.eval(
44 buf,
45 buf_len,
46 module_name_quickjs,
47 JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
48 if (parsed_module.is_exception())
49 {
50 auto [reason, trace] = ctx.error_message();
51
52 auto& rt = ctx.runtime();
53 if (rt.log_exception_details)
54 {
55 CCF_APP_FAIL("{}: {}", reason, trace.value_or("<no trace>"));
56 }
57
58 throw std::runtime_error(fmt::format(
59 "Failed to compile module '{}': {}", module_name, reason));
60 }
61
63 "Module '{}' found in KV (table: {})",
64 module_name_kv,
66 return parsed_module;
67 }
68 };
69}
Definition context.h:46
JSWrappedValue eval(const char *input, size_t input_len, const char *filename, int eval_flags) const
Definition context.cpp:415
std::pair< std::string, std::optional< std::string > > error_message()
Definition context.cpp:177
Runtime & runtime()
Definition context.h:78
Definition kv_module_loader.h:15
virtual std::optional< js::core::JSWrappedValue > get_module(std::string_view module_name, js::core::Context &ctx) override
Definition kv_module_loader.h:22
ccf::Modules::ReadOnlyHandle * modules_handle
Definition kv_module_loader.h:17
KvModuleLoader(ccf::Modules::ReadOnlyHandle *mh)
Definition kv_module_loader.h:20
Definition module_loader_interface.h:21
Definition map_handle.h:13
std::string get_name_of_map() const
Definition map_handle.h:27
std::optional< V > get(const K &key)
Definition map_handle.h:45
#define CCF_APP_TRACE
Definition logger.h:359
#define CCF_APP_FAIL
Definition logger.h:367
Definition module_loader_interface.h:19