hokusai-zero 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Dockerfile +26 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +91 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/ast/genheader +3 -0
- data/ast/include/hashmap.c +1151 -0
- data/ast/include/hashmap.c.license +20 -0
- data/ast/include/hashmap.h +54 -0
- data/ast/src/core/ast.c +448 -0
- data/ast/src/core/ast.h +259 -0
- data/ast/src/core/common.h +24 -0
- data/ast/src/core/component.c +85 -0
- data/ast/src/core/component.h +35 -0
- data/ast/src/core/hml.c +665 -0
- data/ast/src/core/hml.h +11 -0
- data/ast/src/core/input.c +458 -0
- data/ast/src/core/input.h +118 -0
- data/ast/src/core/style.c +101 -0
- data/ast/src/core/style.h +41 -0
- data/ast/src/core/text.c +784 -0
- data/ast/src/core/text.h +93 -0
- data/ast/src/core/util.c +140 -0
- data/ast/src/core/util.h +48 -0
- data/ast/src/hokusai.c +6 -0
- data/ast/src/hokusai.h +6 -0
- data/ast/test/fixtures/test.ui +13 -0
- data/ast/test/greatest.h +1266 -0
- data/ast/test/hokusai.c +14 -0
- data/ast/test/parser.c +234 -0
- data/ast/test/text.c +116 -0
- data/ext/extconf.rb +27 -0
- data/grammar/Cargo.lock +80 -0
- data/grammar/Cargo.toml +26 -0
- data/grammar/binding.gyp +20 -0
- data/grammar/bindings/node/binding.cc +28 -0
- data/grammar/bindings/node/index.js +19 -0
- data/grammar/bindings/rust/build.rs +40 -0
- data/grammar/bindings/rust/lib.rs +52 -0
- data/grammar/corpus/1_document.txt +131 -0
- data/grammar/corpus/2_selectors.txt +58 -0
- data/grammar/corpus/3_spaces.txt +69 -0
- data/grammar/corpus/4_errors.txt +10 -0
- data/grammar/corpus/5_macros.txt +175 -0
- data/grammar/corpus/6_styles.txt +81 -0
- data/grammar/grammar.js +275 -0
- data/grammar/package-lock.json +34 -0
- data/grammar/package.json +33 -0
- data/grammar/src/grammar.json +1269 -0
- data/grammar/src/node-types.json +474 -0
- data/grammar/src/parser.c +5772 -0
- data/grammar/src/scanner.c +258 -0
- data/grammar/src/tree_sitter/parser.h +230 -0
- data/grammar/src/tree_sitter/scanner.h +12 -0
- data/grammar/test.nml +10 -0
- data/hokusai.gemspec +19 -0
- data/ui/examples/assets/DigitalDisplay.ttf +0 -0
- data/ui/examples/assets/OpenSans-Regular.ttf +0 -0
- data/ui/examples/assets/addy.png +0 -0
- data/ui/examples/assets/baby_sean.png +0 -0
- data/ui/examples/assets/football-troll.png +0 -0
- data/ui/examples/assets/gear.png +0 -0
- data/ui/examples/assets/icecold.ttf +0 -0
- data/ui/examples/assets/science-troll.png +0 -0
- data/ui/examples/buddy.rb +31 -0
- data/ui/examples/clock.rb +58 -0
- data/ui/examples/counter.rb +123 -0
- data/ui/examples/dynamic.rb +147 -0
- data/ui/examples/foobar.rb +236 -0
- data/ui/examples/stock.rb +115 -0
- data/ui/examples/stock_decider/option.rb +74 -0
- data/ui/examples/tic_tac_toe.rb +246 -0
- data/ui/lib/lib_hokusai.rb +425 -0
- data/ui/spec/hokusai/ast_spec.rb +88 -0
- data/ui/spec/hokusai/automation/keys_transcoder_spec.rb +50 -0
- data/ui/spec/hokusai/automation/selector_spec.rb +68 -0
- data/ui/spec/hokusai/block_spec.rb +126 -0
- data/ui/spec/hokusai/directives_spec.rb +327 -0
- data/ui/spec/hokusai/e2e/client_spec.rb +58 -0
- data/ui/spec/hokusai/e2e/meta_spec.rb +42 -0
- data/ui/spec/hokusai/publisher_spec.rb +38 -0
- data/ui/spec/hokusai/slots_spec.rb +150 -0
- data/ui/spec/hokusai/util/piece_table_spec.rb +90 -0
- data/ui/spec/hokusai_spec.rb +0 -0
- data/ui/spec/spec_helper.rb +30 -0
- data/ui/src/hokusai/ast.rb +446 -0
- data/ui/src/hokusai/automation/client.rb +167 -0
- data/ui/src/hokusai/automation/constants.rb +98 -0
- data/ui/src/hokusai/automation/converters/selector_converter.rb +61 -0
- data/ui/src/hokusai/automation/driver.rb +54 -0
- data/ui/src/hokusai/automation/driver_command_queue.rb +50 -0
- data/ui/src/hokusai/automation/driver_commands/base.rb +79 -0
- data/ui/src/hokusai/automation/driver_commands/get_attribute.rb +41 -0
- data/ui/src/hokusai/automation/driver_commands/invoke.rb +33 -0
- data/ui/src/hokusai/automation/driver_commands/locate.rb +48 -0
- data/ui/src/hokusai/automation/driver_commands/trigger_keyboard.rb +94 -0
- data/ui/src/hokusai/automation/driver_commands/trigger_mouse.rb +213 -0
- data/ui/src/hokusai/automation/keys_transcoder.rb +128 -0
- data/ui/src/hokusai/automation/selector.rb +39 -0
- data/ui/src/hokusai/automation/server.rb +114 -0
- data/ui/src/hokusai/automation.rb +3 -0
- data/ui/src/hokusai/backends/raylib/config.rb +47 -0
- data/ui/src/hokusai/backends/raylib/font.rb +113 -0
- data/ui/src/hokusai/backends/raylib/keys.rb +124 -0
- data/ui/src/hokusai/backends/raylib.rb +449 -0
- data/ui/src/hokusai/backends/sdl2/Monaco.ttf +0 -0
- data/ui/src/hokusai/backends/sdl2/color.rb +12 -0
- data/ui/src/hokusai/backends/sdl2/config.rb +31 -0
- data/ui/src/hokusai/backends/sdl2/font.rb +127 -0
- data/ui/src/hokusai/backends/sdl2/keys.rb +119 -0
- data/ui/src/hokusai/backends/sdl2.rb +529 -0
- data/ui/src/hokusai/block.rb +237 -0
- data/ui/src/hokusai/blocks/button.rb +100 -0
- data/ui/src/hokusai/blocks/checkbox.rb +51 -0
- data/ui/src/hokusai/blocks/circle.rb +28 -0
- data/ui/src/hokusai/blocks/clipped.rb +23 -0
- data/ui/src/hokusai/blocks/cursor.rb +49 -0
- data/ui/src/hokusai/blocks/dynamic.rb +37 -0
- data/ui/src/hokusai/blocks/empty.rb +10 -0
- data/ui/src/hokusai/blocks/hblock.rb +35 -0
- data/ui/src/hokusai/blocks/image.rb +18 -0
- data/ui/src/hokusai/blocks/input.rb +200 -0
- data/ui/src/hokusai/blocks/label.rb +39 -0
- data/ui/src/hokusai/blocks/panel.rb +126 -0
- data/ui/src/hokusai/blocks/rect.rb +24 -0
- data/ui/src/hokusai/blocks/scissor_begin.rb +18 -0
- data/ui/src/hokusai/blocks/scissor_end.rb +12 -0
- data/ui/src/hokusai/blocks/scrollbar.rb +103 -0
- data/ui/src/hokusai/blocks/selectable.rb +77 -0
- data/ui/src/hokusai/blocks/svg.rb +20 -0
- data/ui/src/hokusai/blocks/text.rb +214 -0
- data/ui/src/hokusai/blocks/titlebar/osx.rb +145 -0
- data/ui/src/hokusai/blocks/toggle.rb +55 -0
- data/ui/src/hokusai/blocks/vblock.rb +35 -0
- data/ui/src/hokusai/commands/base.rb +22 -0
- data/ui/src/hokusai/commands/circle.rb +47 -0
- data/ui/src/hokusai/commands/image.rb +45 -0
- data/ui/src/hokusai/commands/rect.rb +158 -0
- data/ui/src/hokusai/commands/scissor.rb +22 -0
- data/ui/src/hokusai/commands/text.rb +92 -0
- data/ui/src/hokusai/commands.rb +87 -0
- data/ui/src/hokusai/diff.rb +124 -0
- data/ui/src/hokusai/error.rb +3 -0
- data/ui/src/hokusai/event.rb +54 -0
- data/ui/src/hokusai/events/keyboard.rb +84 -0
- data/ui/src/hokusai/events/mouse.rb +172 -0
- data/ui/src/hokusai/font.rb +280 -0
- data/ui/src/hokusai/meta.rb +152 -0
- data/ui/src/hokusai/mounting/loop_entry.rb +230 -0
- data/ui/src/hokusai/mounting/mount_entry.rb +74 -0
- data/ui/src/hokusai/mounting/update_entry.rb +101 -0
- data/ui/src/hokusai/node.rb +98 -0
- data/ui/src/hokusai/node_mounter.rb +102 -0
- data/ui/src/hokusai/painter.rb +214 -0
- data/ui/src/hokusai/publisher.rb +32 -0
- data/ui/src/hokusai/style.rb +72 -0
- data/ui/src/hokusai/types.rb +266 -0
- data/ui/src/hokusai/util/clamping_iterator.rb +202 -0
- data/ui/src/hokusai/util/piece_table.rb +111 -0
- data/ui/src/hokusai/util/selection.rb +145 -0
- data/ui/src/hokusai.rb +120 -0
- data/ui/vendor/.gitkeep +0 -0
- data/vendor/.gitkeep +0 -0
- data/xmake.lua +192 -0
- metadata +222 -0
data/ast/src/core/ast.h
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
#ifndef HOKUSAI_CORE_AST_H
|
2
|
+
#define HOKUSAI_CORE_AST_H
|
3
|
+
|
4
|
+
#include <string.h>
|
5
|
+
#include <stdlib.h>
|
6
|
+
#include <stdio.h>
|
7
|
+
#include <tree_sitter/api.h>
|
8
|
+
#include "../../include/hashmap.h"
|
9
|
+
#include "style.h"
|
10
|
+
|
11
|
+
extern TSLanguage* tree_sitter_hml();
|
12
|
+
|
13
|
+
/** @struct HmlFuncCall
|
14
|
+
* @brief represents a function call, used in events or props
|
15
|
+
* @var function
|
16
|
+
* the name of the function to call
|
17
|
+
* @var args_len
|
18
|
+
* the number of args to pass to the function
|
19
|
+
* @var args
|
20
|
+
* an opaque array of pointers to arguments.
|
21
|
+
*/
|
22
|
+
typedef struct HmlAstFuncCall
|
23
|
+
{
|
24
|
+
char* function;
|
25
|
+
int args_len;
|
26
|
+
void** args;
|
27
|
+
char** strargs;
|
28
|
+
} hoku_ast_func_call;
|
29
|
+
|
30
|
+
/** @struct HmlProp
|
31
|
+
* @brief a prop on the component ex (:size="get_size")
|
32
|
+
* @var name
|
33
|
+
* the name of the prop
|
34
|
+
* @var call
|
35
|
+
* the function call for this prop
|
36
|
+
*/
|
37
|
+
typedef struct HmlAstProp
|
38
|
+
{
|
39
|
+
char* name;
|
40
|
+
bool computed;
|
41
|
+
hoku_ast_func_call* call;
|
42
|
+
} hoku_ast_prop;
|
43
|
+
|
44
|
+
/** @struct HmlEvent
|
45
|
+
* @brief an event triggered by input ex (@click="submit")
|
46
|
+
* @var name
|
47
|
+
* the name of the event
|
48
|
+
* @var call
|
49
|
+
* the function to handle this event
|
50
|
+
*/
|
51
|
+
typedef struct HmlAstEvent
|
52
|
+
{
|
53
|
+
char* name;
|
54
|
+
hoku_ast_func_call* call;
|
55
|
+
} hoku_ast_event;
|
56
|
+
|
57
|
+
/** @struct HmlCondition
|
58
|
+
* @brief an condition on which to render the component
|
59
|
+
* @var not
|
60
|
+
* inverses the function result
|
61
|
+
* @var call
|
62
|
+
* a call which should return a boolean for the render
|
63
|
+
*/
|
64
|
+
typedef struct HmlAstCondition
|
65
|
+
{
|
66
|
+
bool not;
|
67
|
+
hoku_ast_func_call* call;
|
68
|
+
} hoku_ast_condition;
|
69
|
+
|
70
|
+
/** @struct HmlLoop
|
71
|
+
* @brief render the child in a loop
|
72
|
+
* @var name
|
73
|
+
* the property to pass down
|
74
|
+
* @var list_name
|
75
|
+
* the method to return an iterable
|
76
|
+
*/
|
77
|
+
typedef struct HmlAstLoop
|
78
|
+
{
|
79
|
+
char* name;
|
80
|
+
char* list_name;
|
81
|
+
} hoku_ast_loop;
|
82
|
+
|
83
|
+
/** @struct HmlList
|
84
|
+
* @brief a linked list for siblings and children of a component.
|
85
|
+
* @var component
|
86
|
+
* the comonent with siblings or children
|
87
|
+
* @var next_sibling
|
88
|
+
* the next sibling of <component>
|
89
|
+
* @var next_child
|
90
|
+
* the next child of <component>
|
91
|
+
*/
|
92
|
+
typedef struct HmlAstList
|
93
|
+
{
|
94
|
+
struct HmlAst* next_sibling;
|
95
|
+
struct HmlAst* next_child;
|
96
|
+
} hoku_ast_list;
|
97
|
+
|
98
|
+
typedef struct HmlAstClassList
|
99
|
+
{
|
100
|
+
char* name;
|
101
|
+
struct HmlAstClassList* next;
|
102
|
+
} hoku_ast_class_list;
|
103
|
+
|
104
|
+
/** @struct HmlAst
|
105
|
+
* @brief a component tree
|
106
|
+
* @var type
|
107
|
+
* the type name of the component
|
108
|
+
* @var has_slot
|
109
|
+
* boolean representing if the component has open slots
|
110
|
+
* @var cond
|
111
|
+
* a HmlCondition that decides if the component should be rendered
|
112
|
+
* @var props
|
113
|
+
* a hashmap of properties
|
114
|
+
* @var events
|
115
|
+
* a hashmap of events
|
116
|
+
* @var parent
|
117
|
+
* the parent of this component
|
118
|
+
* @var next_sibling
|
119
|
+
* the next sibling of this component
|
120
|
+
* @var next_child
|
121
|
+
* the next child of this component
|
122
|
+
*/
|
123
|
+
typedef struct HmlAst
|
124
|
+
{
|
125
|
+
char* type;
|
126
|
+
char* id;
|
127
|
+
char* error;
|
128
|
+
bool has_slot;
|
129
|
+
int child_len;
|
130
|
+
struct HmlStyle* styles;
|
131
|
+
struct HmlAstClassList* style_list;
|
132
|
+
struct HmlAstClassList* class_list;
|
133
|
+
struct HmlAstCondition* cond;
|
134
|
+
struct HmlAstLoop* loop;
|
135
|
+
struct hashmap* props;
|
136
|
+
struct hashmap* events;
|
137
|
+
struct HmlAst* parent;
|
138
|
+
struct HmlAstList* relations;
|
139
|
+
struct HmlAstList* else_relations;
|
140
|
+
bool else_active;
|
141
|
+
} hoku_ast;
|
142
|
+
|
143
|
+
/**
|
144
|
+
Marks this ast as errored
|
145
|
+
@param ast the ast to error
|
146
|
+
@param error the description of the error
|
147
|
+
@return 0 for success, -1 for error
|
148
|
+
*/
|
149
|
+
int hoku_ast_set_error(hoku_ast* ast, char* error, TSNode node, char* template);
|
150
|
+
|
151
|
+
/**
|
152
|
+
Returns the first errored ast if one exists
|
153
|
+
@param ast the ast to search
|
154
|
+
@return NULL for no errored asts, the errored ast otherwise
|
155
|
+
*/
|
156
|
+
hoku_ast* hoku_errored_ast(hoku_ast* ast);
|
157
|
+
|
158
|
+
/**
|
159
|
+
Prepends an style name to the ast's style list
|
160
|
+
@param ast the ast to prepend to
|
161
|
+
@param name the style name to prepend
|
162
|
+
@return 0 for success, -1 for error
|
163
|
+
*/
|
164
|
+
int hoku_ast_style_list_prepend(hoku_ast* ast, char* name);
|
165
|
+
|
166
|
+
/**
|
167
|
+
Prepends an class name to the ast's class list
|
168
|
+
@param ast the ast to prepend to
|
169
|
+
@param name the class name to prepend
|
170
|
+
@return 0 for success, -1 for error
|
171
|
+
*/
|
172
|
+
int hoku_ast_class_list_prepend(hoku_ast* ast, char* name);
|
173
|
+
|
174
|
+
/**
|
175
|
+
Does the ast include this class name?
|
176
|
+
@param ast the ast to check
|
177
|
+
@param name the class name to check for
|
178
|
+
@return false if ast does not include the class name, true otherwise
|
179
|
+
*/
|
180
|
+
bool hoku_ast_class_list_includes(hoku_ast* ast, char* name);
|
181
|
+
|
182
|
+
/**
|
183
|
+
Append a sibling to the ast node
|
184
|
+
@param first the source node
|
185
|
+
@param second the sibling to the append
|
186
|
+
@return 0 for success, -1 for error
|
187
|
+
*/
|
188
|
+
int hoku_ast_append_sibling(hoku_ast** first, hoku_ast* second);
|
189
|
+
|
190
|
+
/**
|
191
|
+
Append a child to the ast node
|
192
|
+
@param parent the source node
|
193
|
+
@param child the child to append
|
194
|
+
@return 0 for success, -1 for error
|
195
|
+
*/
|
196
|
+
int hoku_ast_append_child(hoku_ast** parent, hoku_ast* child);
|
197
|
+
|
198
|
+
/**
|
199
|
+
Prepend sibling to the ast node
|
200
|
+
@param first the source node
|
201
|
+
@param second the sibling to prepend
|
202
|
+
@return 0 for success, -1 for error
|
203
|
+
*/
|
204
|
+
int hoku_ast_prepend_sibling(hoku_ast** first, hoku_ast* second);
|
205
|
+
|
206
|
+
/**
|
207
|
+
Prepend child to the ast node
|
208
|
+
@param parent the source node
|
209
|
+
@param child the child to prepend
|
210
|
+
@return 0 for success, -1 for error
|
211
|
+
*/
|
212
|
+
int hoku_ast_prepend_child(hoku_ast** parent, hoku_ast* child);
|
213
|
+
|
214
|
+
/**
|
215
|
+
Counts the number of props on this ast
|
216
|
+
@param component the ast to check
|
217
|
+
@return the number of props
|
218
|
+
*/
|
219
|
+
int hoku_ast_props_count(hoku_ast* component);
|
220
|
+
|
221
|
+
/**
|
222
|
+
Adds a prop to this ast node
|
223
|
+
@param component the target ast
|
224
|
+
@param prop the prop to add
|
225
|
+
@return 0 for success, -1 for error
|
226
|
+
*/
|
227
|
+
int hoku_ast_add_prop(hoku_ast* component, hoku_ast_prop* prop);
|
228
|
+
|
229
|
+
/**
|
230
|
+
Fetches a prop from this ast node
|
231
|
+
|
232
|
+
@param component the target ast
|
233
|
+
@param prop the prop to fetch
|
234
|
+
@return a pointer to the prop or NULL
|
235
|
+
|
236
|
+
Example usage
|
237
|
+
|
238
|
+
hoku_ast_prop* prop = hoku_ast_get_prop(ast_node, &(hoku_ast_prop){.name="prop"});
|
239
|
+
*/
|
240
|
+
hoku_ast_prop* hoku_ast_get_prop(hoku_ast* component, hoku_ast_prop* prop);
|
241
|
+
int hoku_ast_cond_init(hoku_ast_condition** cond, hoku_ast_func_call* call);
|
242
|
+
int hoku_ast_loop_init(hoku_ast_loop** loop, char* name, char* list_name);
|
243
|
+
int hoku_ast_events_count(hoku_ast* component);
|
244
|
+
int hoku_ast_add_event(hoku_ast* component, hoku_ast_event* event);
|
245
|
+
hoku_ast_event* hoku_ast_get_event(hoku_ast* component, hoku_ast_event* event);
|
246
|
+
|
247
|
+
int hoku_ast_init(hoku_ast** component, char* type);
|
248
|
+
void hoku_ast_free(hoku_ast* component);
|
249
|
+
|
250
|
+
int hoku_ast_func_call_init(hoku_ast_func_call** call, char* name);
|
251
|
+
void hoku_ast_func_call_free(hoku_ast_func_call* init);
|
252
|
+
|
253
|
+
int hoku_ast_event_init(hoku_ast_event** event, char* name);
|
254
|
+
void hoku_ast_event_free(hoku_ast_event* event);
|
255
|
+
|
256
|
+
int hoku_ast_prop_init(hoku_ast_prop** prop, char* name);
|
257
|
+
void hoku_ast_prop_free(hoku_ast_prop* prop);
|
258
|
+
|
259
|
+
#endif
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#ifndef HOKU_CORE_COMMON_H
|
2
|
+
#define HOKU_CORE_COMMON_H
|
3
|
+
|
4
|
+
typedef struct HmlVec2
|
5
|
+
{
|
6
|
+
float x;
|
7
|
+
float y;
|
8
|
+
} hoku_vec2;
|
9
|
+
|
10
|
+
typedef struct HmlDoubleVec2
|
11
|
+
{
|
12
|
+
double x;
|
13
|
+
double y;
|
14
|
+
} hoku_dvec2;
|
15
|
+
|
16
|
+
typedef struct HmlRect
|
17
|
+
{
|
18
|
+
float x;
|
19
|
+
float y;
|
20
|
+
float w;
|
21
|
+
float h;
|
22
|
+
} hoku_rect;
|
23
|
+
|
24
|
+
#endif
|
@@ -0,0 +1,85 @@
|
|
1
|
+
#ifndef HOKU_CORE_COMPONENT
|
2
|
+
#define HOKU_CORE_COMPONENT
|
3
|
+
|
4
|
+
#include "component.h"
|
5
|
+
|
6
|
+
uint64_t hoku_method_hash(const void* item, uint64_t seed0, uint64_t seed1)
|
7
|
+
{
|
8
|
+
hoku_method* method = (hoku_method*) item;
|
9
|
+
return hashmap_sip(method->name, strlen(method->name), seed0, seed1);
|
10
|
+
}
|
11
|
+
|
12
|
+
int hoku_method_compare(const void* a, const void* b, void* udata)
|
13
|
+
{
|
14
|
+
const hoku_method* ea = (hoku_method*) a;
|
15
|
+
const hoku_method* eb = (hoku_method*) b;
|
16
|
+
return strcmp(ea->name, eb->name);
|
17
|
+
}
|
18
|
+
|
19
|
+
int hoku_method_init(hoku_method** method, char* name, hoku_method_func function)
|
20
|
+
{
|
21
|
+
hoku_method* init = malloc(sizeof(hoku_method));
|
22
|
+
if (method == NULL) return -1;
|
23
|
+
|
24
|
+
init->name = strdup(name);
|
25
|
+
if (init->name == NULL)
|
26
|
+
{
|
27
|
+
free(init);
|
28
|
+
return -1;
|
29
|
+
}
|
30
|
+
|
31
|
+
init->function = function;
|
32
|
+
*method = init;
|
33
|
+
return 0;
|
34
|
+
}
|
35
|
+
|
36
|
+
void hoku_method_free(hoku_method* method)
|
37
|
+
{
|
38
|
+
free(method->name);
|
39
|
+
free(method);
|
40
|
+
}
|
41
|
+
|
42
|
+
int hoku_component_add_method(hoku_component* component, char* name, hoku_method_func function)
|
43
|
+
{
|
44
|
+
if (hashmap_get(component->methods, &(hoku_method){.name=name}) != NULL) return -2;
|
45
|
+
|
46
|
+
hoku_method* init;
|
47
|
+
if (hoku_method_init(&init, name, function) == -1) return -1;
|
48
|
+
if (hashmap_set(component->methods, init) == NULL)
|
49
|
+
{
|
50
|
+
free(init);
|
51
|
+
return -1;
|
52
|
+
}
|
53
|
+
|
54
|
+
return 0;
|
55
|
+
}
|
56
|
+
|
57
|
+
hoku_method* hoku_component_get_method(hoku_component* component, char* name)
|
58
|
+
{
|
59
|
+
return hashmap_get(component->methods, &(hoku_method){.name=name});
|
60
|
+
}
|
61
|
+
|
62
|
+
int hoku_component_init(hoku_component** component, void* state)
|
63
|
+
{
|
64
|
+
hoku_component* init = malloc(sizeof(hoku_component));
|
65
|
+
if (init == NULL) return -1;
|
66
|
+
|
67
|
+
struct hashmap* hm = hashmap_new(sizeof(hoku_method), 0, 0, 0, hoku_method_hash, hoku_method_compare, NULL, hoku_method_free);
|
68
|
+
if (hm == NULL)
|
69
|
+
{
|
70
|
+
free(init);
|
71
|
+
return -1;
|
72
|
+
}
|
73
|
+
init->methods = hm;
|
74
|
+
init->state = state;
|
75
|
+
*component = init;
|
76
|
+
return 0;
|
77
|
+
}
|
78
|
+
|
79
|
+
void hoku_component_free(hoku_component* component)
|
80
|
+
{
|
81
|
+
hashmap_free(component->methods);
|
82
|
+
free(component->state);
|
83
|
+
free(component);
|
84
|
+
}
|
85
|
+
#endif
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#ifndef HOKU_CORE_COMPONENT_H
|
2
|
+
#define HOKU_CORE_COMPONENT_H
|
3
|
+
|
4
|
+
#include <stdlib.h>
|
5
|
+
#include <string.h>
|
6
|
+
#include "../../include/hashmap.h"
|
7
|
+
|
8
|
+
enum HOKU_METHOD_RETURN_TYPE
|
9
|
+
{
|
10
|
+
HOKU_METHOD_RETURN_NONE,
|
11
|
+
HOKU_METHOD_RETURN_BOOL,
|
12
|
+
HOKU_METHOD_RETURN_STR,
|
13
|
+
HOKU_METHOD_RETURN_INT,
|
14
|
+
HOKU_METHOD_RETURN_OPAQUE
|
15
|
+
};
|
16
|
+
|
17
|
+
typedef struct HmlComponent
|
18
|
+
{
|
19
|
+
void* state;
|
20
|
+
struct hashmap* methods;
|
21
|
+
} hoku_component;
|
22
|
+
|
23
|
+
typedef void* (*hoku_method_func)(hoku_component*, void** args, int args_len);
|
24
|
+
|
25
|
+
typedef struct HmlMethod
|
26
|
+
{
|
27
|
+
char* name;
|
28
|
+
hoku_method_func function;
|
29
|
+
} hoku_method;
|
30
|
+
|
31
|
+
int hoku_component_init(hoku_component** component, void* state);
|
32
|
+
int hoku_component_add_method(hoku_component* component, char* name, hoku_method_func function);
|
33
|
+
hoku_method* hoku_component_get_method(hoku_component* component, char* name);
|
34
|
+
|
35
|
+
#endif
|