Flecs v4.1
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
39/* Must be the same as EcsPrimitiveKindLast */
40#define FLECS_SCRIPT_VECTOR_FUNCTION_COUNT (18)
41
42FLECS_API
44
45FLECS_API
46extern ECS_DECLARE(EcsScriptTemplate);
47
48FLECS_API
50
51FLECS_API
53
54FLECS_API
56
57FLECS_API
58extern ECS_DECLARE(EcsScriptVectorType);
59
60/* Script template. */
61typedef struct ecs_script_template_t ecs_script_template_t;
62
71
73typedef struct ecs_script_vars_t {
75 int32_t sp;
77 ecs_hashmap_t var_index;
78 ecs_vec_t vars;
81 struct ecs_stack_t *stack;
82 ecs_stack_cursor_t *cursor;
83 ecs_allocator_t *allocator;
85
87typedef struct ecs_script_t {
89 const char *name;
90 const char *code;
92
95
99typedef struct EcsScript {
100 char *filename;
101 char *code;
102 char *error;
104 ecs_script_template_t *template_;
105 ecs_vec_t observers;
107
114
117 const ecs_function_ctx_t *ctx,
118 int32_t argc,
119 const ecs_value_t *argv,
120 ecs_value_t *result);
121
124 const ecs_function_ctx_t *ctx,
125 int32_t argc,
126 const ecs_value_t *argv,
127 ecs_value_t *result,
128 int32_t elem_count);
129
135
139typedef struct EcsScriptConstVar {
140 ecs_value_t value;
141 const ecs_type_info_t *type_info;
143
145 ecs_entity_t return_type;
146 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
148 ecs_vector_function_callback_t vector_callbacks[FLECS_SCRIPT_VECTOR_FUNCTION_COUNT];
149 void *ctx;
150 void *binding_ctx;
151 ecs_ctx_free_t binding_ctx_free;
152};
153
158
165
166/* Parsing and running scripts */
167
173
180
200FLECS_API
202 ecs_world_t *world,
203 const char *name,
204 const char *code,
205 const ecs_script_eval_desc_t *desc,
207
224FLECS_API
226 const ecs_script_t *script,
227 const ecs_script_eval_desc_t *desc,
229
239FLECS_API
242
259FLECS_API
261 ecs_world_t *world,
262 const char *name,
263 const char *code,
265
275FLECS_API
277 ecs_world_t *world,
278 const char *filename);
279
293FLECS_API
295
301FLECS_API
303 ecs_script_runtime_t *runtime);
304
314FLECS_API
317 ecs_strbuf_t *buf,
318 bool colors);
319
328FLECS_API
331 bool colors);
332
333
334/* Managed scripts (script associated with entity that outlives the function) */
335
342
354FLECS_API
356 ecs_world_t *world,
357 const ecs_script_desc_t *desc);
358
359#define ecs_script(world, ...)\
360 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
361
370FLECS_API
372 ecs_world_t *world,
374 ecs_entity_t instance,
375 const char *code);
376
383FLECS_API
385 ecs_world_t *world,
387 ecs_entity_t instance);
388
389
390/* Script variables */
391
409FLECS_API
411 ecs_world_t *world);
412
419FLECS_API
421 ecs_script_vars_t *vars);
422
434FLECS_API
436 ecs_script_vars_t *parent);
437
446FLECS_API
448 ecs_script_vars_t *vars);
449
462FLECS_API
464 ecs_script_vars_t *vars,
465 const char *name);
466
482FLECS_API
484 ecs_script_vars_t *vars,
485 const char *name,
486 ecs_entity_t type);
487
488#define ecs_script_vars_define(vars, name, type)\
489 ecs_script_vars_define_id(vars, name, ecs_id(type))
490
500FLECS_API
502 const ecs_script_vars_t *vars,
503 const char *name);
504
517FLECS_API
519 const ecs_script_vars_t *vars,
520 int32_t sp);
521
527FLECS_API
529 const ecs_script_vars_t *vars);
530
539FLECS_API
541 ecs_script_vars_t *vars,
542 int32_t count);
543
576FLECS_API
578 const ecs_iter_t *it,
579 ecs_script_vars_t *vars,
580 int offset);
581
582
583/* Standalone expression evaluation */
584
586typedef struct ecs_expr_eval_desc_t {
587 const char *name;
588 const char *expr;
591 ecs_entity_t (*lookup_action)(
592 const ecs_world_t*,
593 const char *value,
594 void *ctx);
599
604
608
613 bool (*unresolved_identifier_action)(
614 const ecs_world_t*,
615 const char *value,
616 void *ctx);
618
633FLECS_API
634const char* ecs_expr_run(
635 ecs_world_t *world,
636 const char *ptr,
637 ecs_value_t *value,
638 const ecs_expr_eval_desc_t *desc);
639
649FLECS_API
651 ecs_world_t *world,
652 const char *expr,
653 const ecs_expr_eval_desc_t *desc);
654
669FLECS_API
671 const ecs_script_t *script,
672 ecs_value_t *value,
673 const ecs_expr_eval_desc_t *desc);
674
688FLECS_API
690 ecs_world_t *world,
691 const char *str,
692 const ecs_script_vars_t *vars);
693
694
695/* Global const variables */
696
712
719FLECS_API
721 ecs_world_t *world,
723
724#define ecs_const_var(world, ...)\
725 ecs_const_var_init(world, &(ecs_const_var_desc_t)__VA_ARGS__)
726
727
736FLECS_API
738 const ecs_world_t *world,
739 ecs_entity_t var);
740
759FLECS_API
761 const ecs_world_t *world,
762 const char *name,
763 ecs_entity_t type,
764 ecs_size_t size,
765 void *out);
766
783#define ecs_const_var_get_t(world, name, T)\
784 (*ECS_CAST(T*, ecs_const_var_get_w_type(\
785 world, name, ecs_id(T), ECS_SIZEOF(T), &(T){0})))
786
793FLECS_API
795 ecs_world_t *world,
796 ecs_entity_t var);
797
798/* Functions */
799
805
876
884FLECS_API
886 ecs_world_t *world,
887 const ecs_function_desc_t *desc);
888
889#define ecs_function(world, ...)\
890 ecs_function_init(world, &(ecs_function_desc_t)__VA_ARGS__)
891
892FLECS_API
893int ecs_function_call(
894 ecs_world_t *world,
895 ecs_entity_t function,
896 int32_t argc,
897 const ecs_value_t *argv,
898 ecs_value_t *result);
899
912FLECS_API
914 ecs_world_t *world,
915 const ecs_function_desc_t *desc);
916
917#define ecs_method(world, ...)\
918 ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
919
920FLECS_API
921int ecs_method_call(
922 ecs_world_t *world,
923 ecs_entity_t method,
924 const ecs_value_t *instance,
925 int32_t argc,
926 const ecs_value_t *argv,
927 ecs_value_t *result);
928
929
930/* Value serialization */
931
941FLECS_API
943 const ecs_world_t *world,
944 ecs_entity_t type,
945 const void *data);
946
956FLECS_API
958 const ecs_world_t *world,
959 ecs_entity_t type,
960 const void *data,
961 ecs_strbuf_t *buf);
962
974FLECS_API
976 const ecs_world_t *world,
977 ecs_entity_t type,
978 const void *data);
979
989FLECS_API
991 const ecs_world_t *world,
992 ecs_entity_t type,
993 const void *data,
994 ecs_strbuf_t *buf);
995
996typedef struct ecs_expr_node_t ecs_expr_node_t;
997
1006FLECS_API
1008 ecs_world_t *world);
1009
1010#ifdef __cplusplus
1011}
1012#endif
1013
1014#endif
1015
1018#endif
FLECS_API void ecs_script_runtime_free(ecs_script_runtime_t *runtime)
Free script runtime.
FLECS_API int ecs_script_eval(const ecs_script_t *script, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
Evaluate script.
FLECS_API void ecs_script_vars_print(const ecs_script_vars_t *vars)
Print variables.
struct ecs_vector_fn_callbacks_t ecs_vector_fn_callbacks_t
Vector function callbacks for different element types.
void(* ecs_vector_function_callback_t)(const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, ecs_value_t *result, int32_t elem_count)
Script vector function callback.
Definition script.h:123
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 ecs_script_eval_result_t ecs_script_eval_result_t
Used to capture error output from script evaluation.
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 void ecs_const_var_modified(ecs_world_t *world, ecs_entity_t var)
Mark const var as modified.
FLECS_API ecs_entity_t ecs_function_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new function.
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 to 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:116
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 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_value_t ecs_const_var_get(const ecs_world_t *world, ecs_entity_t var)
Return the value for a const 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_const_var_get_w_type(const ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, void *out)
Return pointer to the value of a const 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.
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_run(ecs_world_t *world, const char *name, const char *code, ecs_script_eval_result_t *result)
Parse 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().
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.
struct ecs_script_vars_t ecs_script_vars_t
Script variable scope.
struct ecs_script_runtime_t ecs_script_runtime_t
Runtime for executing scripts.
Definition script.h:94
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 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.
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:393
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:437
script_builder script(const char *name=nullptr) const
Build a script.
Definition mixin.inl:31
#define ECS_DECLARE(id)
Forward declare an entity, tag, prefab, or any other entity identifier.
Definition flecs_c.h:25
#define ECS_COMPONENT_DECLARE(id)
Forward declare a component.
Definition flecs_c.h:65
void(* ecs_ctx_free_t)(void *ctx)
Function to clean up context data.
Definition flecs.h:658
Const component.
Definition script.h:139
Script component.
Definition script.h:99
ecs_vec_t observers
Observers for referenced components.
Definition script.h:105
ecs_script_template_t * template_
Only set for template scripts.
Definition script.h:104
char * error
Set if script evaluation had errors.
Definition script.h:102
char * code
Script source code.
Definition script.h:101
ecs_script_t * script
Parsed script object.
Definition script.h:103
char * filename
Script filename.
Definition script.h:100
Used with ecs_const_var_init().
Definition script.h:698
ecs_entity_t parent
Variable parent (namespace).
Definition script.h:703
const char * name
Variable name.
Definition script.h:700
ecs_entity_t type
Variable type.
Definition script.h:706
void * value
Pointer to value of variable.
Definition script.h:710
Used with ecs_expr_run().
Definition script.h:586
bool disable_folding
Disable constant folding (slower evaluation, faster parsing).
Definition script.h:598
void * script_visitor
For internal usage.
Definition script.h:611
ecs_entity_t type
Type of parsed value (optional).
Definition script.h:590
const ecs_script_vars_t * vars
Variables accessible in expression.
Definition script.h:589
ecs_script_runtime_t * runtime
Reusable runtime (optional).
Definition script.h:609
bool allow_unresolved_identifiers
Allow for unresolved identifiers when parsing.
Definition script.h:607
const char * name
Script name.
Definition script.h:587
const char * expr
Full expression string.
Definition script.h:588
bool disable_dynamic_variable_binding
This option instructs the expression runtime to lookup variables by stack pointer instead of by name,...
Definition script.h:603
void * lookup_ctx
Context passed to lookup function.
Definition script.h:595
Script function context.
Definition script.h:109
void * ctx
User context.
Definition script.h:112
ecs_entity_t function
The function entity.
Definition script.h:111
ecs_world_t * world
The world.
Definition script.h:110
Used with ecs_function_init() and ecs_method_init().
Definition script.h:807
ecs_entity_t return_type
Function return type.
Definition script.h:819
const char * name
Function name.
Definition script.h:809
ecs_script_parameter_t params[(16)]
Function parameters.
Definition script.h:816
void * ctx
Context passed to function implementation.
Definition script.h:874
ecs_function_callback_t callback
Function implementation.
Definition script.h:822
ecs_vector_function_callback_t vector_callbacks[(18)]
Vector function implementations.
Definition script.h:871
ecs_entity_t parent
Parent of function.
Definition script.h:813
Iterator.
Definition flecs.h:1190
Used with ecs_script_init().
Definition script.h:337
const char * code
Set to parse script from string.
Definition script.h:340
ecs_entity_t entity
Set to customize entity handle associated with script.
Definition script.h:338
const char * filename
Set to load script from file.
Definition script.h:339
Used with ecs_script_parse() and ecs_script_eval().
Definition script.h:169
ecs_script_runtime_t * runtime
Reusable runtime (optional).
Definition script.h:171
ecs_script_vars_t * vars
Variables used by script.
Definition script.h:170
Used to capture error output from script evaluation.
Definition script.h:175
char * error
Error message, or NULL if no error.
Definition script.h:176
int32_t line
Line number (1-based) of first error, or 0 if not available.
Definition script.h:177
int32_t column
Column number (1-based) of first error, or 0 if not available.
Definition script.h:178
Function argument type.
Definition script.h:131
const char * name
Parameter name.
Definition script.h:132
ecs_entity_t type
Parameter type.
Definition script.h:133
Script object.
Definition script.h:87
const char * code
Script source code.
Definition script.h:90
const char * name
Script name.
Definition script.h:89
ecs_world_t * world
The world.
Definition script.h:88
Script variable.
Definition script.h:64
bool is_const
Whether the variable is constant.
Definition script.h:69
ecs_value_t value
Variable value.
Definition script.h:66
const char * name
Variable name.
Definition script.h:65
const ecs_type_info_t * type_info
Type information.
Definition script.h:67
int32_t sp
Stack pointer.
Definition script.h:68
Script variable scope.
Definition script.h:73
int32_t sp
Stack pointer for this scope.
Definition script.h:75
ecs_allocator_t * allocator
General purpose allocator.
Definition script.h:83
struct ecs_script_vars_t * parent
Parent variable scope.
Definition script.h:74
struct ecs_stack_t * stack
Stack allocator for variable storage.
Definition script.h:81
ecs_stack_cursor_t * cursor
Cursor into the stack allocator.
Definition script.h:82
ecs_hashmap_t var_index
Index for variable name lookups.
Definition script.h:77
const ecs_world_t * world
The world.
Definition script.h:80
ecs_vec_t vars
Vector of variables in this scope.
Definition script.h:78
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1050
Value of a dynamic type.
Definition flecs.h:1066
Vector function callbacks for different element types.
Definition script.h:801
ecs_vector_function_callback_t i8
Callback for i8 element type.
Definition script.h:802
ecs_vector_function_callback_t i32
Callback for i32 element type.
Definition script.h:803