CCF
Loading...
Searching...
No Matches
kv_bytecode_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#include "ccf/version.h"
10
11#include <string>
12
13namespace ccf::js::modules
14{
16 {
17 protected:
19
21
22 public:
25 ccf::ModulesQuickJsVersion::ReadOnlyHandle* modules_version_handle) :
27 {
28 const auto version_in_kv = modules_version_handle->get();
29 const auto version_in_binary = std::string(ccf::quickjs_version);
30 if (version_in_kv != version_in_binary)
31 {
33 "Ignoring bytecode table, which was written for QuickJS {} (this "
34 "node is running QuickJS {})",
35 version_in_kv,
36 version_in_binary);
37 version_ok = false;
38 }
39 else
40 {
41 version_ok = true;
42 }
43 }
44
45 virtual std::optional<js::core::JSWrappedValue> get_module(
46 std::string_view module_name, js::core::Context& ctx) override
47 {
48 if (!version_ok)
49 {
50 return std::nullopt;
51 }
52
53 std::string module_name_kv(module_name);
54 if (module_name_kv[0] != '/')
55 {
56 module_name_kv.insert(0, "/");
57 }
58
59 CCF_APP_TRACE("Looking for module '{}' bytecode in KV", module_name_kv);
60
61 auto module_bytecode = modules_bytecode_handle->get(module_name_kv);
62 if (!module_bytecode.has_value())
63 {
64 CCF_APP_TRACE("Module '{}' not found", module_name_kv);
65 return std::nullopt;
66 }
67
68 auto module_val = ctx.read_object(
69 module_bytecode->data(), module_bytecode->size(), JS_READ_OBJ_BYTECODE);
70
71 const bool failed_deser = module_val.is_exception();
72
73 if (failed_deser)
74 {
75 auto [reason, trace] = ctx.error_message();
76
77 auto& rt = ctx.runtime();
78 if (rt.log_exception_details)
79 {
80 CCF_APP_FAIL("{}: {}", reason, trace.value_or("<no trace>"));
81 }
82
83 throw std::runtime_error(fmt::format(
84 "Failed to deserialize bytecode for module '{}': {}",
85 module_name,
86 reason));
87 }
88
90 "Module '{}' bytecode found in KV (table: {})",
91 module_name_kv,
93 return module_val;
94 }
95 };
96}
Definition context.h:46
JSWrappedValue read_object(const uint8_t *buf, size_t buf_len, int flags) const
Definition context.cpp:424
std::pair< std::string, std::optional< std::string > > error_message()
Definition context.cpp:177
Runtime & runtime()
Definition context.h:78
Definition kv_bytecode_module_loader.h:16
KvBytecodeModuleLoader(ccf::ModulesQuickJsBytecode::ReadOnlyHandle *mbh, ccf::ModulesQuickJsVersion::ReadOnlyHandle *modules_version_handle)
Definition kv_bytecode_module_loader.h:23
bool version_ok
Definition kv_bytecode_module_loader.h:20
virtual std::optional< js::core::JSWrappedValue > get_module(std::string_view module_name, js::core::Context &ctx) override
Definition kv_bytecode_module_loader.h:45
ccf::ModulesQuickJsBytecode::ReadOnlyHandle * modules_bytecode_handle
Definition kv_bytecode_module_loader.h:18
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
Definition value_handle.h:14
std::optional< V > get()
Definition value_handle.h:31
#define CCF_APP_TRACE
Definition logger.h:359
#define CCF_APP_INFO
Definition logger.h:366
#define CCF_APP_FAIL
Definition logger.h:367
Definition module_loader_interface.h:19
bool is_exception() const
Definition wrapped_value.cpp:156