Skip to content

Commit 088c98d

Browse files
committed
[script] Incremental updates
1 parent 3da1b7d commit 088c98d

20 files changed

Lines changed: 15252 additions & 145 deletions

distr/flecs.c

Lines changed: 2965 additions & 70 deletions
Large diffs are not rendered by default.

distr/flecs.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17073,6 +17073,41 @@ char* ecs_script_ast_to_str(
1707317073
ecs_script_t *script,
1707417074
bool colors);
1707517075

17076+
/** Used with ecs_script_ast_to_buf_w_desc() and ecs_script_ast_to_str_w_desc(). */
17077+
typedef struct ecs_script_ast_to_str_desc_t {
17078+
bool colors; /**< Include ANSI color codes in the output. */
17079+
bool annotations; /**< Include incremental update annotations. When set,
17080+
* nodes are annotated with the source position, prop
17081+
* dependency masks, slots and scope slot ranges used
17082+
* for incrementally updating template instances. */
17083+
} ecs_script_ast_to_str_desc_t;
17084+
17085+
/** Convert script AST to string.
17086+
* Same as ecs_script_ast_to_buf(), but with additional options.
17087+
*
17088+
* @param script The script.
17089+
* @param buf The buffer to write to.
17090+
* @param desc String conversion options.
17091+
* @return Zero if success, non-zero if failed.
17092+
*/
17093+
FLECS_API
17094+
int ecs_script_ast_to_buf_w_desc(
17095+
ecs_script_t *script,
17096+
ecs_strbuf_t *buf,
17097+
const ecs_script_ast_to_str_desc_t *desc);
17098+
17099+
/** Convert script AST to string.
17100+
* Same as ecs_script_ast_to_str(), but with additional options.
17101+
*
17102+
* @param script The script.
17103+
* @param desc String conversion options.
17104+
* @return The string if success, NULL if failed.
17105+
*/
17106+
FLECS_API
17107+
char* ecs_script_ast_to_str_w_desc(
17108+
ecs_script_t *script,
17109+
const ecs_script_ast_to_str_desc_t *desc);
17110+
1707617111
/* Managed scripts (script associated with entity that outlives the function) */
1707717112

1707817113
/** Used with ecs_script_init(). */

include/flecs/addons/script.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,41 @@ char* ecs_script_ast_to_str(
327327
ecs_script_t *script,
328328
bool colors);
329329

330+
/** Used with ecs_script_ast_to_buf_w_desc() and ecs_script_ast_to_str_w_desc(). */
331+
typedef struct ecs_script_ast_to_str_desc_t {
332+
bool colors; /**< Include ANSI color codes in the output. */
333+
bool annotations; /**< Include incremental update annotations. When set,
334+
* nodes are annotated with the source position, prop
335+
* dependency masks, slots and scope slot ranges used
336+
* for incrementally updating template instances. */
337+
} ecs_script_ast_to_str_desc_t;
338+
339+
/** Convert script AST to string.
340+
* Same as ecs_script_ast_to_buf(), but with additional options.
341+
*
342+
* @param script The script.
343+
* @param buf The buffer to write to.
344+
* @param desc String conversion options.
345+
* @return Zero if success, non-zero if failed.
346+
*/
347+
FLECS_API
348+
int ecs_script_ast_to_buf_w_desc(
349+
ecs_script_t *script,
350+
ecs_strbuf_t *buf,
351+
const ecs_script_ast_to_str_desc_t *desc);
352+
353+
/** Convert script AST to string.
354+
* Same as ecs_script_ast_to_str(), but with additional options.
355+
*
356+
* @param script The script.
357+
* @param desc String conversion options.
358+
* @return The string if success, NULL if failed.
359+
*/
360+
FLECS_API
361+
char* ecs_script_ast_to_str_w_desc(
362+
ecs_script_t *script,
363+
const ecs_script_ast_to_str_desc_t *desc);
364+
330365

331366
/* Managed scripts (script associated with entity that outlives the function) */
332367

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ flecs_src = files(
6969
'src/addons/query_dsl/parser.c',
7070
'src/addons/rest.c',
7171
'src/addons/script/template.c',
72+
'src/addons/script/template_check.c',
7273
'src/addons/script/ast.c',
7374
'src/addons/script/function.c',
7475
'src/addons/script/functions_builtin.c',

src/addons/script/ast.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef enum ecs_script_node_kind_t {
3434
typedef struct ecs_script_node_t {
3535
ecs_script_node_kind_t kind;
3636
const char *pos;
37+
uint64_t depends_on;
38+
bool contains_entities;
3739
} ecs_script_node_t;
3840

3941
struct ecs_script_scope_t {
@@ -45,6 +47,13 @@ struct ecs_script_scope_t {
4547
/* Array with component ids that are added in scope. Used to limit
4648
* archetype moves. */
4749
ecs_vec_t components; /* vec<ecs_id_t> */
50+
51+
int32_t slot_start;
52+
int32_t slot_end;
53+
int32_t dyn_start;
54+
int32_t dyn_end;
55+
int32_t control_start;
56+
int32_t control_end;
4857
};
4958

5059
typedef struct ecs_script_id_t {
@@ -66,6 +75,9 @@ typedef struct ecs_script_id_t {
6675
* for entities that are defined inside of templates, which have different
6776
* values for each instantiation. */
6877
bool dynamic;
78+
79+
int32_t target_slot;
80+
int32_t id_slot;
6981
} ecs_script_id_t;
7082

7183
typedef struct ecs_script_tag_t {
@@ -107,12 +119,17 @@ struct ecs_script_entity_t {
107119
ecs_script_entity_t *parent;
108120
ecs_entity_t eval;
109121
ecs_entity_t eval_kind;
122+
123+
int32_t slot;
124+
int32_t dyn_slot;
125+
uint64_t name_depends_on;
110126
};
111127

112128
typedef struct ecs_script_with_t {
113129
ecs_script_node_t node;
114130
ecs_script_scope_t *expressions;
115131
ecs_script_scope_t *scope;
132+
uint64_t expr_depends_on;
116133
} ecs_script_with_t;
117134

118135
typedef struct ecs_script_inherit_t {
@@ -124,6 +141,7 @@ typedef struct ecs_script_pair_scope_t {
124141
ecs_script_node_t node;
125142
ecs_script_id_t id;
126143
ecs_script_scope_t *scope;
144+
uint64_t expr_depends_on;
127145
} ecs_script_pair_scope_t;
128146

129147
typedef struct ecs_script_using_t {
@@ -160,6 +178,8 @@ typedef struct ecs_script_if_t {
160178
ecs_script_scope_t *if_true;
161179
ecs_script_scope_t *if_false;
162180
ecs_expr_node_t *expr;
181+
uint64_t expr_depends_on;
182+
int32_t control_slot;
163183
} ecs_script_if_t;
164184

165185
typedef struct ecs_script_for_range_t {
@@ -168,6 +188,12 @@ typedef struct ecs_script_for_range_t {
168188
ecs_expr_node_t *from;
169189
ecs_expr_node_t *to;
170190
ecs_script_scope_t *scope;
191+
uint64_t expr_depends_on;
192+
193+
/* Set when the loop contains a pair with a dynamic element that is added
194+
* to a persistent entity. Such pairs can't be tracked per slot, so the
195+
* matching wildcard is cleared before the loop is re-evaluated. */
196+
bool has_clear_ids;
171197
} ecs_script_for_range_t;
172198

173199
typedef struct ecs_script_include_t {

src/addons/script/expr/visit_eval.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,10 +955,20 @@ int flecs_expr_new_visit_eval(
955955
ecs_script_eval_visitor_t temp_v = {0};
956956
ecs_script_eval_desc_t desc = {0};
957957

958+
if (v && v->template) {
959+
if (ecs_script_visit_node(v, node->entity)) {
960+
return -1;
961+
}
962+
963+
*(ecs_entity_t*)out->value.ptr = 0;
964+
out->value.type = ecs_id(ecs_entity_t);
965+
return 0;
966+
}
967+
958968
if (!v) {
959-
/* Safe const cast, script won't modify variables since it only contains
969+
/* Safe const cast, script won't modify variables since it only contains
960970
* an entity statement. */
961-
desc.vars = ctx->desc ?
971+
desc.vars = ctx->desc ?
962972
ECS_CONST_CAST(ecs_script_vars_t*, ctx->desc->vars) : NULL;
963973
flecs_script_eval_visit_init(
964974
(const ecs_script_impl_t*)ctx->script, &temp_v, &desc);

src/addons/script/parser.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,16 @@ template_stmt: {
722722

723723
Parse(
724724
// template SpaceShip {
725-
case '{':
726-
return flecs_script_scope(parser, template->scope, pos);
725+
case '{': {
726+
pos = flecs_script_scope(parser, template->scope, pos);
727+
if (!pos) {
728+
goto error;
729+
}
730+
if (flecs_script_template_check(parser, template)) {
731+
goto error;
732+
}
733+
return pos;
734+
}
727735

728736
// template SpaceShip\n
729737
EcsTokEndOfStatement:

0 commit comments

Comments
 (0)