1#include <async_await.h>
2#include <stdio.h>
3
4
5
6
7
8
9
10
11
12
13const char *code =
14 "const score = await fetch_score(\"Alice\")\n"
15 "\n"
16 "Alice {\n"
17 " Score: {score}\n"
18 "}\n"
19 ;
20
21typedef struct {
22 int32_t value;
23} Score;
24
25
26ecs_script_future_t *pending_request = NULL;
27
28void fetch_score(
30 int32_t argc,
32 ecs_script_future_t *future)
33{
34 (void)ctx;
35 (void)argc;
36
37 printf("fetch_score(\"%s\"): request sent\n", *(char**)argv[0].ptr);
38
39
40
41 pending_request = future;
42}
43
44
45void ServerProgress(
47{
48 (void)it;
49
50 if (!pending_request) {
51 return;
52 }
53
54
55
56
57 printf("server: responding with 120\n");
58 int32_t score = 120;
60 ecs_script_future_resolve(pending_request, &value);
61 ecs_script_future_release(pending_request);
62 pending_request = NULL;
63}
64
65int main(int argc, char *argv[]) {
67
69
72 .members = {
74 }
75 });
76
78
79
80
81 ecs_async_function(world, {
82 .name = "fetch_score",
84 .params = {
86 },
87 .callback = fetch_score
88 });
89
92 printf("script failed to parse\n");
93 return -1;
94 }
95
96
97
98 ecs_script_task_t *task = ecs_script_task_new(
script, NULL);
99
100
101 ecs_script_task_status_t status;
102 while ((status = ecs_script_task_resume(task, NULL)) ==
103 EcsScriptTaskPending)
104 {
105 printf("script is waiting\n");
107 }
108
109 if (status != EcsScriptTaskDone) {
110 printf("script failed to run\n");
111 return -1;
112 }
113
114 ecs_script_task_free(task);
116
118 printf("Alice: %d\n", s->value);
119
121
122
123
124
125
126
127}
FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
Component ID for EcsDocDescription.
FLECS_API bool ecs_progress(ecs_world_t *world, ecs_ftime_t delta_time)
Progress a world.
FLECS_API void ecs_script_free(ecs_script_t *script)
Free script.
FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
Parse script.
#define ECS_SYSTEM(world, id, phase,...)
Declare and define a system.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
script_builder script(const char *name=nullptr) const
Build a script.
#define ECS_COMPONENT(world, id)
Declare and define a component.
#define ecs_get(world, entity, T)
Get a component.
ecs_entity_t ecs_lookup(const ecs_world_t *world, const char *path)
Look up an entity by its path.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.