Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
script.h
Go to the documentation of this file.
1
8#ifdef FLECS_SCRIPT
9
18#ifndef FLECS_META
19#define FLECS_META
20#endif
21
22#ifndef FLECS_DOC
23#define FLECS_DOC
24#endif
25
26#ifndef FLECS_PARSER
27#define FLECS_PARSER
28#endif
29
30#ifndef FLECS_SCRIPT_H
31#define FLECS_SCRIPT_H
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37#define FLECS_SCRIPT_FUNCTION_ARGS_MAX (16)
38
39FLECS_API
41
42FLECS_API
43extern ECS_DECLARE(EcsScriptTemplate);
44
45FLECS_API
47
48FLECS_API
50
51FLECS_API
53
54/* Script template. */
55typedef struct ecs_script_template_t ecs_script_template_t;
56
58typedef struct ecs_script_var_t {
59 const char *name;
60 ecs_value_t value;
61 const ecs_type_info_t *type_info;
62 int32_t sp;
63 bool is_const;
65
67typedef struct ecs_script_vars_t {
68 struct ecs_script_vars_t *parent;
69 int32_t sp;
70
71 ecs_hashmap_t var_index;
72 ecs_vec_t vars;
73
74 const ecs_world_t *world;
75 struct ecs_stack_t *stack;
76 ecs_stack_cursor_t *cursor;
77 ecs_allocator_t *allocator;
79
81typedef struct ecs_script_t {
82 ecs_world_t *world;
83 const char *name;
84 const char *code;
86
87/* Runtime for executing scripts */
88typedef struct ecs_script_runtime_t ecs_script_runtime_t;
89
93typedef struct EcsScript {
94 ecs_script_t *script;
95 ecs_script_template_t *template_; /* Only set for template scripts */
97
99typedef struct ecs_function_ctx_t {
100 ecs_world_t *world;
101 ecs_entity_t function;
102 void *ctx;
104
107 const ecs_function_ctx_t *ctx,
108 int32_t argc,
109 const ecs_value_t *argv,
110 ecs_value_t *result);
111
114 const char *name;
115 ecs_entity_t type;
117
121typedef struct EcsScriptConstVar {
122 ecs_value_t value;
123 const ecs_type_info_t *type_info;
125
129typedef struct EcsScriptFunction {
130 ecs_entity_t return_type;
131 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
133 void *ctx;
135
141typedef struct EcsScriptMethod {
142 ecs_entity_t return_type;
143 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
145 void *ctx;
147
148/* Parsing & running scripts */
149
155
170FLECS_API
172 ecs_world_t *world,
173 const char *name,
174 const char *code,
175 const ecs_script_eval_desc_t *desc);
176
188FLECS_API
190 const ecs_script_t *script,
191 const ecs_script_eval_desc_t *desc);
192
202FLECS_API
205
221FLECS_API
223 ecs_world_t *world,
224 const char *name,
225 const char *code);
226
236FLECS_API
238 ecs_world_t *world,
239 const char *filename);
240
254FLECS_API
255ecs_script_runtime_t* ecs_script_runtime_new(void);
256
262FLECS_API
264 ecs_script_runtime_t *runtime);
265
274FLECS_API
277 ecs_strbuf_t *buf,
278 bool colors);
279
287FLECS_API
290 bool colors);
291
292
293/* Managed scripts (script associated with entity that outlives the function) */
294
296typedef struct ecs_script_desc_t {
297 ecs_entity_t entity; /* Set to customize entity handle associated with script */
298 const char *filename; /* Set to load script from file */
299 const char *code; /* Set to parse script from string */
301
312FLECS_API
314 ecs_world_t *world,
315 const ecs_script_desc_t *desc);
316
317#define ecs_script(world, ...)\
318 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
319
327FLECS_API
329 ecs_world_t *world,
331 ecs_entity_t instance,
332 const char *code);
333
340FLECS_API
342 ecs_world_t *world,
344 ecs_entity_t instance);
345
346
347/* Script variables */
348
365FLECS_API
367 ecs_world_t *world);
368
375FLECS_API
377 ecs_script_vars_t *vars);
378
390FLECS_API
392 ecs_script_vars_t *parent);
393
402FLECS_API
404 ecs_script_vars_t *vars);
405
418FLECS_API
420 ecs_script_vars_t *vars,
421 const char *name);
422
438FLECS_API
440 ecs_script_vars_t *vars,
441 const char *name,
442 ecs_entity_t type);
443
444#define ecs_script_vars_define(vars, name, type)\
445 ecs_script_vars_define_id(vars, name, ecs_id(type))
446
456FLECS_API
458 const ecs_script_vars_t *vars,
459 const char *name);
460
473FLECS_API
475 const ecs_script_vars_t *vars,
476 int32_t sp);
477
483FLECS_API
485 const ecs_script_vars_t *vars);
486
495FLECS_API
497 ecs_script_vars_t *vars,
498 int32_t count);
499
532FLECS_API
534 const ecs_iter_t *it,
535 ecs_script_vars_t *vars,
536 int offset);
537
538
539/* Standalone expression evaluation */
540
542typedef struct ecs_expr_eval_desc_t {
543 const char *name;
544 const char *expr;
547 ecs_entity_t (*lookup_action)(
548 const ecs_world_t*,
549 const char *value,
550 void *ctx);
555
560
564
565 ecs_script_runtime_t *runtime;
567
582FLECS_API
583const char* ecs_expr_run(
584 ecs_world_t *world,
585 const char *ptr,
586 ecs_value_t *value,
587 const ecs_expr_eval_desc_t *desc);
588
598FLECS_API
600 ecs_world_t *world,
601 const char *expr,
602 const ecs_expr_eval_desc_t *desc);
603
618FLECS_API
620 const ecs_script_t *script,
621 ecs_value_t *value,
622 const ecs_expr_eval_desc_t *desc);
623
636FLECS_API
638 ecs_world_t *world,
639 const char *str,
640 const ecs_script_vars_t *vars);
641
642
643/* Global const variables */
644
646typedef struct ecs_const_var_desc_t {
647 /* Variable name. */
648 const char *name;
649
650 /* Variable parent (namespace). */
651 ecs_entity_t parent;
652
653 /* Variable type. */
654 ecs_entity_t type;
655
656 /* Pointer to value of variable. The value will be copied to an internal
657 * storage and does not need to be kept alive. */
658 void *value;
660
667FLECS_API
669 ecs_world_t *world,
671
672#define ecs_const_var(world, ...)\
673 ecs_const_var_init(world, &(ecs_const_var_desc_t)__VA_ARGS__)
674
675/* Functions */
676
698
706FLECS_API
708 ecs_world_t *world,
709 const ecs_function_desc_t *desc);
710
711#define ecs_function(world, ...)\
712 ecs_function_init(world, &(ecs_function_desc_t)__VA_ARGS__)
713
726FLECS_API
728 ecs_world_t *world,
729 const ecs_function_desc_t *desc);
730
731#define ecs_method(world, ...)\
732 ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
733
734
735/* Value serialization */
736
746FLECS_API
748 const ecs_world_t *world,
749 ecs_entity_t type,
750 const void *data);
751
761FLECS_API
763 const ecs_world_t *world,
764 ecs_entity_t type,
765 const void *data,
766 ecs_strbuf_t *buf);
767
779FLECS_API
781 const ecs_world_t *world,
782 ecs_entity_t type,
783 const void *data);
784
794FLECS_API
796 const ecs_world_t *world,
797 ecs_entity_t type,
798 const void *data,
799 ecs_strbuf_t *buf);
800
801typedef struct ecs_expr_node_t ecs_expr_node_t;
802
811FLECS_API
813 ecs_world_t *world);
814
815#ifdef __cplusplus
816}
817#endif
818
819#endif
820
823#endif
FLECS_API void ecs_script_runtime_free(ecs_script_runtime_t *runtime)
Free script runtime.
FLECS_API void ecs_script_vars_print(const ecs_script_vars_t *vars)
Print variables.
FLECS_API void ecs_script_vars_set_size(ecs_script_vars_t *vars, int32_t count)
Preallocate space for variables.
FLECS_API int ecs_ptr_to_expr_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
Serialize value into expression buffer.
FLECS_API ecs_script_t * ecs_expr_parse(ecs_world_t *world, const char *expr, const ecs_expr_eval_desc_t *desc)
Parse expression.
FLECS_API void ecs_script_vars_fini(ecs_script_vars_t *vars)
Free variable scope.
struct ecs_expr_eval_desc_t ecs_expr_eval_desc_t
Used with ecs_expr_run().
FLECS_API ecs_script_runtime_t * ecs_script_runtime_new(void)
Create runtime for script.
struct EcsScript EcsScript
Script component.
FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
Serialize value into expression string.
FLECS_API ecs_entity_t ecs_function_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new function.
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)
Parse script.
struct EcsScriptConstVar EcsScriptConstVar
Const component.
FLECS_API char * ecs_ptr_to_str(const ecs_world_t *world, ecs_entity_t type, const void *data)
Similar as ecs_ptr_to_expr(), but serializes values to string.
FLECS_API int ecs_script_update(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance, const char *code)
Update script with new code.
FLECS_API char * ecs_script_ast_to_str(ecs_script_t *script, bool colors)
Convert script AST to string.
void(* ecs_function_callback_t)(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result)
Script function callback.
Definition script.h:106
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
struct ecs_const_var_desc_t ecs_const_var_desc_t
Used with ecs_const_var_init.
FLECS_API ecs_script_vars_t * ecs_script_vars_pop(ecs_script_vars_t *vars)
Pop variable scope.
FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code)
Parse script.
FLECS_API ecs_script_var_t * ecs_script_vars_from_sp(const ecs_script_vars_t *vars, int32_t sp)
Lookup a variable by stack pointer.
FLECS_API void ecs_script_clear(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance)
Clear all entities associated with script.
FLECS_API ecs_script_var_t * ecs_script_vars_declare(ecs_script_vars_t *vars, const char *name)
Declare a variable.
FLECS_API ecs_script_var_t * ecs_script_vars_define_id(ecs_script_vars_t *vars, const char *name, ecs_entity_t type)
Define a variable.
FLECS_API void ecs_script_vars_from_iter(const ecs_iter_t *it, ecs_script_vars_t *vars, int offset)
Convert iterator to vars This operation converts an iterator to a variable array.
FLECS_API void ecs_script_free(ecs_script_t *script)
Free script.
FLECS_API int ecs_script_ast_to_buf(ecs_script_t *script, ecs_strbuf_t *buf, bool colors)
Convert script AST to string.
FLECS_API ecs_entity_t ecs_method_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new method.
struct ecs_script_t ecs_script_t
Script object.
FLECS_API int ecs_ptr_to_str_buf(const ecs_world_t *world, ecs_entity_t type, const void *data, ecs_strbuf_t *buf)
Serialize value into string buffer.
FLECS_API int ecs_script_eval(const ecs_script_t *script, const ecs_script_eval_desc_t *desc)
Evaluate script.
struct ecs_script_desc_t ecs_script_desc_t
Used with ecs_script_init()
FLECS_API ecs_script_vars_t * ecs_script_vars_init(ecs_world_t *world)
Create new variable scope.
FLECS_API int ecs_expr_eval(const ecs_script_t *script, ecs_value_t *value, const ecs_expr_eval_desc_t *desc)
Evaluate expression.
struct ecs_function_desc_t ecs_function_desc_t
Used with ecs_function_init and ecs_method_init.
FLECS_API ecs_script_vars_t * ecs_script_vars_push(ecs_script_vars_t *parent)
Push new variable scope.
struct ecs_script_eval_desc_t ecs_script_eval_desc_t
Used with ecs_script_parse() and ecs_script_eval()
struct ecs_script_vars_t ecs_script_vars_t
Script variable scope.
FLECS_API ecs_entity_t ecs_const_var_init(ecs_world_t *world, ecs_const_var_desc_t *desc)
Create a const variable that can be accessed by scripts.
FLECS_API void FlecsScriptImport(ecs_world_t *world)
Script module import function.
FLECS_API char * ecs_script_string_interpolate(ecs_world_t *world, const char *str, const ecs_script_vars_t *vars)
Evaluate interpolated expressions in string.
struct EcsScriptFunction EcsScriptFunction
Function component.
struct ecs_function_ctx_t ecs_function_ctx_t
Script function context.
struct ecs_script_parameter_t ecs_script_parameter_t
Function argument type.
struct ecs_script_var_t ecs_script_var_t
Script variable.
FLECS_API int ecs_script_run_file(ecs_world_t *world, const char *filename)
Parse script file.
FLECS_API ecs_script_var_t * ecs_script_vars_lookup(const ecs_script_vars_t *vars, const char *name)
Lookup a variable.
struct EcsScriptMethod EcsScriptMethod
Method component.
FLECS_API const char * ecs_expr_run(ecs_world_t *world, const char *ptr, ecs_value_t *value, const ecs_expr_eval_desc_t *desc)
Run expression.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:375
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:419
script_builder script(const char *name=nullptr) const
Build script.
Definition mixin.inl:31
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:112
Const component.
Definition script.h:121
Function component.
Definition script.h:129
Method component.
Definition script.h:141
Script component.
Definition script.h:93
Used with ecs_const_var_init.
Definition script.h:646
Used with ecs_expr_run().
Definition script.h:542
bool disable_folding
Disable constant folding (slower evaluation, faster parsing)
Definition script.h:554
ecs_entity_t type
Type of parsed value (optional)
Definition script.h:546
const ecs_script_vars_t * vars
Variables accessible in expression.
Definition script.h:545
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:565
bool allow_unresolved_identifiers
Allow for unresolved identifiers when parsing.
Definition script.h:563
const char * name
Script name.
Definition script.h:543
const char * expr
Full expression string.
Definition script.h:544
bool disable_dynamic_variable_binding
This option instructs the expression runtime to lookup variables by stack pointer instead of by name,...
Definition script.h:559
void * lookup_ctx
Context passed to lookup function.
Definition script.h:551
Script function context.
Definition script.h:99
Used with ecs_function_init and ecs_method_init.
Definition script.h:678
ecs_entity_t return_type
Function return type.
Definition script.h:690
const char * name
Function name.
Definition script.h:680
ecs_script_parameter_t params[(16)]
Function parameters.
Definition script.h:687
void * ctx
Context passed to function implementation.
Definition script.h:696
ecs_function_callback_t callback
Function implementation.
Definition script.h:693
ecs_entity_t parent
Parent of function.
Definition script.h:684
Iterator.
Definition flecs.h:1128
Used with ecs_script_init()
Definition script.h:296
Used with ecs_script_parse() and ecs_script_eval()
Definition script.h:151
ecs_script_runtime_t * runtime
Reusable runtime (optional)
Definition script.h:153
ecs_script_vars_t * vars
Variables used by script.
Definition script.h:152
Function argument type.
Definition script.h:113
Script object.
Definition script.h:81
Script variable.
Definition script.h:58
Script variable scope.
Definition script.h:67
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:982
Utility to hold a value of a dynamic type.
Definition flecs.h:995