CCF
Loading...
Searching...
No Matches
jsengine.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/json.h"
6#include "ccf/ds/openapi.h"
7#include "ccf/service/map.h"
8
9namespace ccf
10{
12 {
13 struct Defaults
14 {
15 static constexpr size_t max_heap_bytes = 100 * 1024 * 1024;
16 static constexpr size_t max_stack_bytes = 1024 * 1024;
17 static constexpr uint64_t max_execution_time_ms = 5000;
18 static constexpr bool log_exception_details = false;
19 static constexpr bool return_exception_details = false;
20 static constexpr size_t max_cached_interpreters = 10;
21 };
22
40 };
41
42#define FOREACH_JSENGINE_FIELD(XX) \
43 XX(max_heap_bytes, decltype(JSRuntimeOptions::max_heap_bytes)) \
44 XX(max_stack_bytes, decltype(JSRuntimeOptions::max_stack_bytes)) \
45 XX(max_execution_time_ms, decltype(JSRuntimeOptions::max_execution_time_ms)) \
46 XX(log_exception_details, decltype(JSRuntimeOptions::log_exception_details)) \
47 XX( \
48 return_exception_details, \
49 decltype(JSRuntimeOptions::return_exception_details)) \
50 XX( \
51 max_cached_interpreters, \
52 decltype(JSRuntimeOptions::max_cached_interpreters))
53
54 // Manually implemented to_json and from_json, so that we are maximally
55 // permissive in deserialisation (use defaults), but maximally verbose in
56 // serialisation (describe all fields)
57 inline void to_json(nlohmann::json& j, const JSRuntimeOptions& options)
58 {
59 j = nlohmann::json::object();
60#define XX(field, field_type) j[#field] = options.field;
61
63#undef XX
64 }
65
66 inline void from_json(const nlohmann::json& j, JSRuntimeOptions& options)
67 {
68#define XX(field, field_type) \
69 { \
70 const auto it = j.find(#field); \
71 if (it != j.end()) \
72 { \
73 options.field = it->get<field_type>(); \
74 } \
75 }
76
78#undef XX
79 }
80
81 inline std::string schema_name(const JSRuntimeOptions*)
82 {
83 return "JSRuntimeOptions";
84 }
85
86 inline void fill_json_schema(nlohmann::json& schema, const JSRuntimeOptions*)
87 {
88 schema = nlohmann::json::object();
89 schema["type"] = "object";
90
91 auto properties = nlohmann::json::object();
92 {
93#define XX(field, field_type) \
94 properties[#field] = ccf::ds::openapi::components_ref_object( \
95 ccf::ds::json::schema_name<field_type>());
96
98#undef XX
99 }
100
101 schema["properties"] = properties;
102 }
103
104#undef FOREACH_JSENGINE_FIELD
105
107
108 namespace Tables
109 {
110 static constexpr auto JSENGINE = "public:ccf.gov.js_runtime_options";
111 }
112}
Definition value.h:32
#define FOREACH_JSENGINE_FIELD(XX)
Definition jsengine.h:42
Definition app_interface.h:14
void fill_json_schema(nlohmann::json &schema, const ClaimsDigest *)
Definition claims_digest.h:64
std::string schema_name(const ClaimsDigest *)
Definition claims_digest.h:59
void from_json(const nlohmann::json &j, ClaimsDigest &hash)
Definition claims_digest.h:54
void to_json(nlohmann::json &j, const ClaimsDigest &hash)
Definition claims_digest.h:49
#define XX(num, name, string)
Definition jsengine.h:14
static constexpr bool return_exception_details
Definition jsengine.h:19
static constexpr size_t max_heap_bytes
Definition jsengine.h:15
static constexpr bool log_exception_details
Definition jsengine.h:18
static constexpr size_t max_cached_interpreters
Definition jsengine.h:20
static constexpr uint64_t max_execution_time_ms
Definition jsengine.h:17
static constexpr size_t max_stack_bytes
Definition jsengine.h:16
Definition jsengine.h:12
bool return_exception_details
return exception details in the response NOTE: this is a security risk as it may leak sensitive infor...
Definition jsengine.h:37
size_t max_stack_bytes
stack size for QuickJS runtime
Definition jsengine.h:26
size_t max_heap_bytes
heap size for QuickJS runtime
Definition jsengine.h:24
uint64_t max_execution_time_ms
max execution time for QuickJS
Definition jsengine.h:28
bool log_exception_details
emit exception details to the log NOTE: this is a security risk as it may leak sensitive information ...
Definition jsengine.h:33
size_t max_cached_interpreters
how many interpreters may be cached in-memory for future reuse
Definition jsengine.h:39