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"
10
11#include <string>
12
13namespace ccf::js::modules
14{
16 {
17 protected:
19
20 public:
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
64 "Module '{}' found in KV (table: {})",
65 module_name_kv,
67 return parsed_module;
68 }
69 };
70}
Definition context.h:45
JSWrappedValue eval(const char *input, size_t input_len, const char *filename, int eval_flags) const
Definition context.cpp:418
std::pair< std::string, std::optional< std::string > > error_message()
Definition context.cpp:180
Runtime & runtime()
Definition context.h:77
Definition kv_module_loader.h:16
ccf::Modules::ReadOnlyHandle * modules_handle
Definition kv_module_loader.h:18
KvModuleLoader(ccf::Modules::ReadOnlyHandle *mh)
Definition kv_module_loader.h:21
std::optional< js::core::JSWrappedValue > get_module(std::string_view module_name, js::core::Context &ctx) override
Definition kv_module_loader.h:23
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:291
#define CCF_APP_FAIL
Definition logger.h:294
Definition module_loader_interface.h:19