All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
js_value.h
1#pragma once
2
3#include <furi.h>
4#include "js_modules.h"
5
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10typedef enum {
11 // literal types
12 JsValueTypeAny, //<! Literal term
13 JsValueTypeAnyArray, //<! Literal term, after ensuring that it's an array
14 JsValueTypeAnyObject, //<! Literal term, after ensuring that it's an object
15 JsValueTypeFunction, //<! Literal term, after ensuring that it's a function
16
17 // primitive types
18 JsValueTypeRawPointer, //<! Unchecked `void*`
19 JsValueTypeInt32, //<! Number cast to `int32_t`
20 JsValueTypeDouble, //<! Number cast to `double`
21 JsValueTypeString, //<! Any string cast to `const char*`
22 JsValueTypeBool, //<! Bool cast to `bool`
23
24 // types with children
25 JsValueTypeEnum, //<! String with predefined possible values cast to a C enum via a mapping
26 JsValueTypeObject, //<! Object with predefined recursive fields cast to several C values
27
28 JsValueTypeMask = 0xff,
29
30 // enum sizes
31 JsValueTypeEnumSize1 = (1 << 8),
32 JsValueTypeEnumSize2 = (2 << 8),
33 JsValueTypeEnumSize4 = (4 << 8),
34
35 // flags
36 JsValueTypePermitNull = (1 << 16), //<! If the value is absent, assign default value
37} JsValueType;
38
39#define JS_VALUE_TYPE_ENUM_SIZE(x) ((x) << 8)
40
41typedef struct {
42 const char* string_value;
43 size_t num_value;
45
46typedef union {
47 void* ptr_val;
48 int32_t int32_val;
49 double double_val;
50 const char* str_val;
51 size_t enum_val;
52 bool bool_val;
54
56
57typedef struct {
58 JsValueType type;
59 JsValueDefaultValue default_value;
60
61 size_t n_children;
62 union {
63 const JsValueEnumVariant* enum_variants;
64 const JsValueObjectField* object_fields;
65 };
67
69 const char* field_name;
70 const JsValueDeclaration* value;
71};
72
73typedef struct {
74 size_t n_children;
75 const JsValueDeclaration* arguments;
77
78#define JS_VALUE_ENUM(c_type, variants) \
79 { \
80 .type = JsValueTypeEnum | JS_VALUE_TYPE_ENUM_SIZE(sizeof(c_type)), \
81 .n_children = COUNT_OF(variants), \
82 .enum_variants = variants, \
83 }
84
85#define JS_VALUE_ENUM_W_DEFAULT(c_type, variants, default) \
86 { \
87 .type = JsValueTypeEnum | JsValueTypePermitNull | \
88 JS_VALUE_TYPE_ENUM_SIZE(sizeof(c_type)), \
89 .default_value.enum_val = default, \
90 .n_children = COUNT_OF(variants), \
91 .enum_variants = variants, \
92 }
93
94#define JS_VALUE_OBJECT(fields) \
95 { \
96 .type = JsValueTypeObject, \
97 .n_children = COUNT_OF(fields), \
98 .object_fields = fields, \
99 }
100
101#define JS_VALUE_OBJECT_W_DEFAULTS(fields) \
102 { \
103 .type = JsValueTypeObject | JsValueTypePermitNull, \
104 .n_children = COUNT_OF(fields), \
105 .object_fields = fields, \
106 }
107
108#define JS_VALUE_SIMPLE(t) {.type = t}
109
110#define JS_VALUE_SIMPLE_W_DEFAULT(t, name, val) \
111 {.type = (t) | JsValueTypePermitNull, .default_value.name = (val)}
112
113#define JS_VALUE_ARGS(args) \
114 { \
115 .n_children = COUNT_OF(args), \
116 .arguments = args, \
117 }
118
119typedef enum {
120 JsValueParseFlagNone = 0,
121 JsValueParseFlagReturnOnError =
122 (1
123 << 0), //<! Sets mjs error string to a description of the parsing error and returns from the JS function
124} JsValueParseFlag;
125
126typedef enum {
127 JsValueParseStatusOk, //<! Parsing completed successfully
128 JsValueParseStatusJsError, //<! Parsing failed due to incorrect JS input
129} JsValueParseStatus;
130
131typedef enum {
132 JsValueParseSourceValue,
133 JsValueParseSourceArguments,
134} JsValueParseSource;
135
136typedef struct {
137 JsValueParseSource source;
138 union {
139 const JsValueDeclaration* value_decl;
140 const JsValueArguments* argument_decl;
141 };
143
144#define JS_VALUE_PARSE_SOURCE_VALUE(declaration) \
145 ((JsValueParseDeclaration){.source = JsValueParseSourceValue, .value_decl = declaration})
146#define JS_VALUE_PARSE_SOURCE_ARGS(declaration) \
147 ((JsValueParseDeclaration){ \
148 .source = JsValueParseSourceArguments, .argument_decl = declaration})
149
154size_t js_value_buffer_size(const JsValueParseDeclaration declaration);
155
181JsValueParseStatus js_value_parse(
182 struct mjs* mjs,
183 const JsValueParseDeclaration declaration,
184 JsValueParseFlag flags,
185 mjs_val_t* buffer,
186 size_t buf_size,
187 mjs_val_t* source,
188 size_t n_c_vals,
189 ...);
190
191#define JS_VALUE_PARSE(mjs, declaration, flags, status_ptr, value_ptr, ...) \
192 void* _args[] = {__VA_ARGS__}; \
193 size_t _n_args = COUNT_OF(_args); \
194 size_t _temp_buf_len = js_value_buffer_size(declaration); \
195 mjs_val_t _temp_buffer[_temp_buf_len]; \
196 *(status_ptr) = js_value_parse( \
197 mjs, declaration, flags, _temp_buffer, _temp_buf_len, value_ptr, _n_args, __VA_ARGS__);
198
199#define JS_VALUE_PARSE_ARGS_OR_RETURN(mjs, declaration, ...) \
200 JsValueParseStatus _status; \
201 JS_VALUE_PARSE( \
202 mjs, \
203 JS_VALUE_PARSE_SOURCE_ARGS(declaration), \
204 JsValueParseFlagReturnOnError, \
205 &_status, \
206 NULL, \
207 __VA_ARGS__); \
208 if(_status != JsValueParseStatusOk) return;
209
210#ifdef __cplusplus
211}
212#endif
Definition js_value.h:73
Definition js_value.h:57
Definition js_value.h:41
Definition js_value.h:68
Definition js_value.h:136
Definition mjs_core.h:63
Definition js_value.h:46