CCF
Loading...
Searching...
No Matches
common_context.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
13
14namespace ccf::js
15{
16 // This is intended to extend a js::core::Context with various CCF-specific
17 // extensions, expected to be accessible in every execution context (eg -
18 // ccf.bufToStr converters, ccf.crypto helpers). This is
19 // implemented as a CRTP mixin so that you could build your own hierarchy.
20 template <typename Base>
21 class WithCommonExtensions : public Base
22 {
23 public:
25 {
26 // override Math.random
27 Base::add_extension(
28 std::make_shared<ccf::js::extensions::MathRandomExtension>());
29
30 // add console.[debug|log|...]
31 Base::add_extension(
32 std::make_shared<ccf::js::extensions::ConsoleExtension>());
33
34 // add ccf.[strToBuf|bufToStr|...]
35 Base::add_extension(
36 std::make_shared<ccf::js::extensions::ConvertersExtension>());
37
38 // add ccf.crypto.*
39 Base::add_extension(
40 std::make_shared<ccf::js::extensions::CryptoExtension>());
41
42 // add snp_attestation.*
43 Base::add_extension(
44 std::make_shared<ccf::js::extensions::SnpAttestationExtension>());
45
46 // add ccf.gov.*
47 Base::add_extension(
48 std::make_shared<ccf::js::extensions::GovExtension>());
49 }
50 };
51
52 template <typename Base>
53 class WithKVExtension : public Base
54 {
55 public:
56 WithKVExtension(TxAccess acc, ccf::kv::Tx* tx) : Base(acc)
57 {
58 // add ccf.kv.*
59 Base::add_extension(
60 std::make_shared<ccf::js::extensions::KvExtension>(tx));
61 }
62
65 const std::vector<ccf::js::core::JSWrappedValue>& argv) override
66 {
67 auto ret = Base::inner_call(f, argv);
68 auto* extension =
69 Base::template get_extension<ccf::js::extensions::KvExtension>();
70 if (extension != nullptr)
71 {
72 extension->rethrow_trapped_exceptions();
73 }
74
75 return ret;
76 }
77 };
78
81}
Definition common_context.h:22
WithCommonExtensions(TxAccess acc)
Definition common_context.h:24
Definition common_context.h:54
ccf::js::core::JSWrappedValue inner_call(const ccf::js::core::JSWrappedValue &f, const std::vector< ccf::js::core::JSWrappedValue > &argv) override
Definition common_context.h:63
WithKVExtension(TxAccess acc, ccf::kv::Tx *tx)
Definition common_context.h:56
Definition tx.h:200
Definition bundle.h:12
TxAccess
Definition tx_access.h:10
Definition wrapped_value.h:13