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_;
106
113
116 const ecs_function_ctx_t *ctx,
117 int32_t argc,
118 const ecs_value_t *argv,
119 ecs_value_t *result);
120
123 const ecs_function_ctx_t *ctx,
124 int32_t argc,
125 const ecs_value_t *argv,
126 ecs_value_t *result,
127 int32_t elem_count);
128
134
138typedef struct EcsScriptConstVar {
139 ecs_value_t value;
140 const ecs_type_info_t *type_info;
142
144 ecs_entity_t return_type;
145 ecs_vec_t params; /* vec<ecs_script_parameter_t> */
147 ecs_vector_function_callback_t vector_callbacks[FLECS_SCRIPT_VECTOR_FUNCTION_COUNT];
148 void *ctx;
149 void *binding_ctx;
150 ecs_ctx_free_t binding_ctx_free;
151};
152
157
164
165/* Parsing and running scripts */
166
172
179
199FLECS_API
201 ecs_world_t *world,
202 const char *name,
203 const char *code,
204 const ecs_script_eval_desc_t *desc,
206
223FLECS_API
225 const ecs_script_t *script,
226 const ecs_script_eval_desc_t *desc,
228
238FLECS_API
241
258FLECS_API
260 ecs_world_t *world,
261 const char *name,
262 const char *code,
264
274FLECS_API
276 ecs_world_t *world,
277 const char *filename);
278
292FLECS_API
294
300FLECS_API
302 ecs_script_runtime_t *runtime);
303
313FLECS_API
316 ecs_strbuf_t *buf,
317 bool colors);
318
327FLECS_API
330 bool colors);
331
332
333/* Managed scripts (script associated with entity that outlives the function) */
334
341
353FLECS_API
355 ecs_world_t *world,
356 const ecs_script_desc_t *desc);
357
358#define ecs_script(world, ...)\
359 ecs_script_init(world, &(ecs_script_desc_t) __VA_ARGS__)
360
369FLECS_API
371 ecs_world_t *world,
373 ecs_entity_t instance,
374 const char *code);
375
382FLECS_API
384 ecs_world_t *world,
386 ecs_entity_t instance);
387
388
389/* Script variables */
390
408FLECS_API
410 ecs_world_t *world);
411
418FLECS_API
420 ecs_script_vars_t *vars);
421
433FLECS_API
435 ecs_script_vars_t *parent);
436
445FLECS_API
447 ecs_script_vars_t *vars);
448
461FLECS_API
463 ecs_script_vars_t *vars,
464 const char *name);
465
481FLECS_API
483 ecs_script_vars_t *vars,
484 const char *name,
485 ecs_entity_t type);
486
487#define ecs_script_vars_define(vars, name, type)\
488 ecs_script_vars_define_id(vars, name, ecs_id(type))
489
499FLECS_API
501 const ecs_script_vars_t *vars,
502 const char *name);
503
516FLECS_API
518 const ecs_script_vars_t *vars,
519 int32_t sp);
520
526FLECS_API
528 const ecs_script_vars_t *vars);
529
538FLECS_API
540 ecs_script_vars_t *vars,
541 int32_t count);
542
575FLECS_API
577 const ecs_iter_t *it,
578 ecs_script_vars_t *vars,
579 int offset);
580
581
582/* Standalone expression evaluation */
583
585typedef struct ecs_expr_eval_desc_t {
586 const char *name;
587 const char *expr;
590 ecs_entity_t (*lookup_action)(
591 const ecs_world_t*,
592 const char *value,
593 void *ctx);
598
603
607
612 bool (*unresolved_identifier_action)(
613 const ecs_world_t*,
614 const char *value,
615 void *ctx);
617
632FLECS_API
633const char* ecs_expr_run(
634 ecs_world_t *world,
635 const char *ptr,
636 ecs_value_t *value,
637 const ecs_expr_eval_desc_t *desc);
638
648FLECS_API
650 ecs_world_t *world,
651 const char *expr,
652 const ecs_expr_eval_desc_t *desc);
653
668FLECS_API
670 const ecs_script_t *script,
671 ecs_value_t *value,
672 const ecs_expr_eval_desc_t *desc);
673
687FLECS_API
689 ecs_world_t *world,
690 const char *str,
691 const ecs_script_vars_t *vars);
692
693
694/* Global const variables */
695
711
718FLECS_API
720 ecs_world_t *world,
722
723#define ecs_const_var(world, ...)\
724 ecs_const_var_init(world, &(ecs_const_var_desc_t)__VA_ARGS__)
725
726
735FLECS_API
737 const ecs_world_t *world,
738 ecs_entity_t var);
739
740/* Functions */
741
747
818
826FLECS_API
828 ecs_world_t *world,
829 const ecs_function_desc_t *desc);
830
831#define ecs_function(world, ...)\
832 ecs_function_init(world, &(ecs_function_desc_t)__VA_ARGS__)
833
846FLECS_API
848 ecs_world_t *world,
849 const ecs_function_desc_t *desc);
850
851#define ecs_method(world, ...)\
852 ecs_method_init(world, &(ecs_function_desc_t)__VA_ARGS__)
853
854
855/* Value serialization */
856
866FLECS_API
868 const ecs_world_t *world,
869 ecs_entity_t type,
870 const void *data);
871
881FLECS_API
883 const ecs_world_t *world,
884 ecs_entity_t type,
885 const void *data,
886 ecs_strbuf_t *buf);
887
899FLECS_API
901 const ecs_world_t *world,
902 ecs_entity_t type,
903 const void *data);
904
914FLECS_API
916 const ecs_world_t *world,
917 ecs_entity_t type,
918 const void *data,
919 ecs_strbuf_t *buf);
920
921typedef struct ecs_expr_node_t ecs_expr_node_t;
922
931FLECS_API
933 ecs_world_t *world);
934
935#ifdef __cplusplus
936}
937#endif
938
939#endif
940
943#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:122
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 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:115
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_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:385
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:429
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:112
void(* ecs_ctx_free_t)(void *ctx)
Function to clean up context data.
Definition flecs.h:650
Const component.
Definition script.h:138
Script component.
Definition script.h:99
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:697
ecs_entity_t parent
Variable parent (namespace).
Definition script.h:702
const char * name
Variable name.
Definition script.h:699
ecs_entity_t type
Variable type.
Definition script.h:705
void * value
Pointer to value of variable.
Definition script.h:709
Used with ecs_expr_run().
Definition script.h:585
bool disable_folding
Disable constant folding (slower evaluation, faster parsing).
Definition script.h:597
void * script_visitor
For internal usage.
Definition script.h:610
ecs_entity_t type
Type of parsed value (optional).
Definition script.h:589
const ecs_script_vars_t * vars
Variables accessible in expression.
Definition script.h:588
ecs_script_runtime_t * runtime
Reusable runtime (optional).
Definition script.h:608
bool allow_unresolved_identifiers
Allow for unresolved identifiers when parsing.
Definition script.h:606
const char * name
Script name.
Definition script.h:586
const char * expr
Full expression string.
Definition script.h:587
bool disable_dynamic_variable_binding
This option instructs the expression runtime to lookup variables by stack pointer instead of by name,...
Definition script.h:602
void * lookup_ctx
Context passed to lookup function.
Definition script.h:594
Script function context.
Definition script.h:108
void * ctx
User context.
Definition script.h:111
ecs_entity_t function
The function entity.
Definition script.h:110
ecs_world_t * world
The world.
Definition script.h:109
Used with ecs_function_init() and ecs_method_init().
Definition script.h:749
ecs_entity_t return_type
Function return type.
Definition script.h:761
const char * name
Function name.
Definition script.h:751
ecs_script_parameter_t params[(16)]
Function parameters.
Definition script.h:758
void * ctx
Context passed to function implementation.
Definition script.h:816
ecs_function_callback_t callback
Function implementation.
Definition script.h:764
ecs_vector_function_callback_t vector_callbacks[(18)]
Vector function implementations.
Definition script.h:813
ecs_entity_t parent
Parent of function.
Definition script.h:755
Iterator.
Definition flecs.h:1170
Used with ecs_script_init().
Definition script.h:336
const char * code
Set to parse script from string.
Definition script.h:339
ecs_entity_t entity
Set to customize entity handle associated with script.
Definition script.h:337
const char * filename
Set to load script from file.
Definition script.h:338
Used with ecs_script_parse() and ecs_script_eval().
Definition script.h:168
ecs_script_runtime_t * runtime
Reusable runtime (optional).
Definition script.h:170
ecs_script_vars_t * vars
Variables used by script.
Definition script.h:169
Used to capture error output from script evaluation.
Definition script.h:174
char * error
Error message, or NULL if no error.
Definition script.h:175
int32_t line
Line number (1-based) of first error, or 0 if not available.
Definition script.h:176
int32_t column
Column number (1-based) of first error, or 0 if not available.
Definition script.h:177
Function argument type.
Definition script.h:130
const char * name
Parameter name.
Definition script.h:131
ecs_entity_t type
Parameter type.
Definition script.h:132
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:1023
Utility to hold a value of a dynamic type.
Definition flecs.h:1037
Vector function callbacks for different element types.
Definition script.h:743
ecs_vector_function_callback_t i8
Callback for i8 element type.
Definition script.h:744
ecs_vector_function_callback_t i32
Callback for i32 element type.
Definition script.h:745