diff --git a/distr/flecs.c b/distr/flecs.c index 5fffb6688..a1552ce24 100644 --- a/distr/flecs.c +++ b/distr/flecs.c @@ -55649,10 +55649,8 @@ void flecs_meta_entity_parent( *(ecs_entity_t*)result->ptr = ecs_get_parent(ctx->world, entity); } -#ifdef FLECS_DOC - static -void flecs_meta_entity_doc_name( +void flecs_meta_entity_has( const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, @@ -55660,21 +55658,118 @@ void flecs_meta_entity_doc_name( { (void)argc; ecs_entity_t entity = *(ecs_entity_t*)argv[0].ptr; - *(char**)result->ptr = ecs_os_strdup(ecs_doc_get_name(ctx->world, entity)); + ecs_id_t id = *(ecs_id_t*)argv[1].ptr; + *(ecs_bool_t*)result->ptr = ecs_has_id(ctx->world, entity, id); } +static +void flecs_meta_core_pair( + const ecs_function_ctx_t *ctx, + int32_t argc, + const ecs_value_t *argv, + ecs_value_t *result) +{ + (void)argc; + (void)ctx; + ecs_entity_t first = *(ecs_entity_t*)argv[0].ptr; + ecs_entity_t second = *(ecs_entity_t*)argv[1].ptr; + *(ecs_id_t*)result->ptr = ecs_pair(first, second); +} + +#ifdef FLECS_DOC + +#define FLECS_DOC_FUNC(name)\ + static\ + void flecs_meta_entity_doc_##name(\ + const ecs_function_ctx_t *ctx,\ + int32_t argc,\ + const ecs_value_t *argv,\ + ecs_value_t *result)\ + {\ + (void)argc;\ + ecs_entity_t entity = *(ecs_entity_t*)argv[0].ptr;\ + *(char**)result->ptr = \ + ecs_os_strdup(ecs_doc_get_##name(ctx->world, entity));\ + } + +FLECS_DOC_FUNC(name) +FLECS_DOC_FUNC(uuid) +FLECS_DOC_FUNC(brief) +FLECS_DOC_FUNC(detail) +FLECS_DOC_FUNC(link) +FLECS_DOC_FUNC(color) + +#undef FLECS_DOC_FUNC + static void flecs_script_register_builtin_doc_functions( ecs_world_t *world) { - ecs_entity_t name = ecs_method(world, { - .name = "doc_name", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_doc_name - }); + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_name", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_name + }); + + ecs_doc_set_brief(world, m, "Returns entity doc name"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_uuid", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_uuid + }); + + ecs_doc_set_brief(world, m, "Returns entity doc uuid"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_brief", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_brief + }); + + ecs_doc_set_brief(world, m, "Returns entity doc brief description"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_detail", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_detail + }); + + ecs_doc_set_brief(world, m, "Returns entity doc detailed description"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_link", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_link + }); + + ecs_doc_set_brief(world, m, "Returns entity doc link"); + } - ecs_doc_set_brief(world, name, "Returns entity doc name"); + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_color", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_color + }); + + ecs_doc_set_brief(world, m, "Returns entity doc color"); + } } #else @@ -55691,32 +55786,67 @@ void flecs_script_register_builtin_doc_functions( void flecs_script_register_builtin_functions( ecs_world_t *world) { - ecs_entity_t name = ecs_method(world, { - .name = "name", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_name - }); + { + ecs_entity_t m = ecs_method(world, { + .name = "name", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_name + }); - ecs_doc_set_brief(world, name, "Returns entity name"); + ecs_doc_set_brief(world, m, "Returns entity name"); + } - ecs_entity_t path = ecs_method(world, { - .name = "path", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_path - }); + { + ecs_entity_t m = ecs_method(world, { + .name = "path", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_path + }); - ecs_doc_set_brief(world, path, "Returns entity path"); + ecs_doc_set_brief(world, m, "Returns entity path"); + } - ecs_entity_t parent = ecs_method(world, { - .name = "parent", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_entity_t), - .callback = flecs_meta_entity_parent - }); + { + ecs_entity_t m = ecs_method(world, { + .name = "parent", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_entity_t), + .callback = flecs_meta_entity_parent + }); + + ecs_doc_set_brief(world, m, "Returns entity parent"); + } - ecs_doc_set_brief(world, parent, "Returns entity parent"); + { + ecs_entity_t m = ecs_method(world, { + .name = "has", + .parent = ecs_id(ecs_entity_t), + .params = { + { .name = "component", .type = ecs_id(ecs_id_t) } + }, + .return_type = ecs_id(ecs_bool_t), + .callback = flecs_meta_entity_has + }); + + ecs_doc_set_brief(world, m, "Returns whether entity has component"); + } + + { + ecs_entity_t m = ecs_function(world, { + .name = "pair", + .parent = ecs_entity(world, { .name = "core"}), + .params = { + { .name = "first", .type = ecs_id(ecs_entity_t) }, + { .name = "second", .type = ecs_id(ecs_entity_t) } + }, + .return_type = ecs_id(ecs_id_t), + .callback = flecs_meta_core_pair + }); + + ecs_doc_set_brief(world, m, "Returns a pair identifier"); + } flecs_script_register_builtin_doc_functions(world); } @@ -55902,7 +56032,7 @@ void FlecsScriptMathImport( /* Exponential and logarithmic functions */ FLECS_MATH_FUNC_DEF_F64(exp, "Compute exponential function"); - FLECS_MATH_FUNC_DEF_F64_F32(ldexp, "Generate value from significand and exponent"); + FLECS_MATH_FUNC_DEF_F64_F32(ldexp, "Generate value from significant and exponent"); FLECS_MATH_FUNC_DEF_F64(log, "Compute natural logarithm"); FLECS_MATH_FUNC_DEF_F64(log10, "Compute common logarithm"); FLECS_MATH_FUNC_DEF_F64(exp2, "Compute binary exponential function"); diff --git a/distr/flecs.h b/distr/flecs.h index d53b1ce52..e7212edea 100644 --- a/distr/flecs.h +++ b/distr/flecs.h @@ -136,27 +136,17 @@ */ // #define FLECS_KEEP_ASSERT -/** \def FLECS_CPP_NO_AUTO_REGISTRATION - * When set, the C++ API will require that components are registered before they - * are used. This is useful in multithreaded applications, where components need - * to be registered beforehand, and to catch issues in projects where component - * registration is mandatory. Disabling automatic component registration also - * slightly improves performance. - * The C API is not affected by this feature. - */ -// #define FLECS_CPP_NO_AUTO_REGISTRATION - /** @def FLECS_CPP_NO_AUTO_REGISTRATION * When set, the C++ API will require that components are registered before they * are used. This is useful in multithreaded applications, where components need - * to be registered beforehand, and to catch issues in projects where component + * to be registered beforehand, and to catch issues in projects where component * registration is mandatory. Disabling automatic component registration also * slightly improves performance. * The C API is not affected by this feature. */ // #define FLECS_CPP_NO_AUTO_REGISTRATION -/** \def FLECS_CUSTOM_BUILD +/** @def FLECS_CUSTOM_BUILD * This macro lets you customize which addons to build flecs with. * Without any addons Flecs is just a minimal ECS storage, but addons add * features such as systems, scheduling and reflection. If an addon is disabled, @@ -273,7 +263,7 @@ #define FLECS_ID_DESC_MAX (32) #endif -/** \def FLECS_EVENT_DESC_MAX +/** @def FLECS_EVENT_DESC_MAX * Maximum number of events in ecs_observer_desc_t */ #ifndef FLECS_EVENT_DESC_MAX #define FLECS_EVENT_DESC_MAX (8) @@ -283,19 +273,19 @@ * Maximum number of query variables per query */ #define FLECS_VARIABLE_COUNT_MAX (64) -/** \def FLECS_TERM_COUNT_MAX +/** @def FLECS_TERM_COUNT_MAX * Maximum number of terms in queries. Should not exceed 64. */ #ifndef FLECS_TERM_COUNT_MAX #define FLECS_TERM_COUNT_MAX 32 #endif -/** \def FLECS_TERM_ARG_COUNT_MAX +/** @def FLECS_TERM_ARG_COUNT_MAX * Maximum number of arguments for a term. */ #ifndef FLECS_TERM_ARG_COUNT_MAX #define FLECS_TERM_ARG_COUNT_MAX (16) #endif -/** \def FLECS_QUERY_VARIABLE_COUNT_MAX +/** @def FLECS_QUERY_VARIABLE_COUNT_MAX * Maximum number of query variables per query. Should not exceed 128. */ #ifndef FLECS_QUERY_VARIABLE_COUNT_MAX #define FLECS_QUERY_VARIABLE_COUNT_MAX (64) diff --git a/docs/FlecsScript.md b/docs/FlecsScript.md index 73ee89b55..ba62fa1a8 100644 --- a/docs/FlecsScript.md +++ b/docs/FlecsScript.md @@ -957,6 +957,79 @@ ecs_method(world, { }); ``` +### Builtin functions and constants +The following table lists builtin core functions in the `flecs.script.core` namespace: + +| **Function Name** | **Description** | **Return Type** | **Arguments** | +|-------------------|-----------------------------|-----------------|-----------------------------| +| `pair` | Returns a pair identifier | `id` | (`entity`, `entity`) | + +The following table lists builtin methods on the `flecs.meta.entity` type: + +| **Method Name** | **Description** | **Return Type** | **Arguments** | +|-------------------|----------------------------------------|-----------------|----------------------| +| `name` | Returns entity name | `string` | `()` | +| `path` | Returns entity path | `string` | `()` | +| `parent` | Returns entity parent | `entity` | `()` | +| `has` | Returns whether entity has component | `bool` | `(id)` | + +The following table lists doc methods on the `flecs.meta.entity` type: + +| **Method Name** | **Description** | **Return Type** | **Arguments** | +|-------------------|------------------------------------------|------------------|----------------------| +| `doc_name` | Returns entity doc name | `string` | `()` | +| `doc_uuid` | Returns entity doc uuid | `string` | `()` | +| `doc_brief` | Returns entity doc brief description | `string` | `()` | +| `doc_detail` | Returns entity doc detailed description | `string` | `()` | +| `doc_link` | Returns entity doc link | `string` | `()` | +| `doc_color` | Returns entity doc color | `string` | `()` | + +To use the doc functions, make sure to use a Flecs build compiled with `FLECS_DOC` (enabled by default). + +The following table lists math functions in the `flecs.script.math` namespace: + +| **Function Name** | **Description** | **Return Type** | **Arguments** | +|--------------------|-----------------------------------------|-----------------|---------------------| +| `cos` | Compute cosine | `f64` | `(f64)` | +| `sin` | Compute sine | `f64` | `(f64)` | +| `tan` | Compute tangent | `f64` | `(f64)` | +| `acos` | Compute arc cosine | `f64` | `(f64)` | +| `asin` | Compute arc sine | `f64` | `(f64)` | +| `atan` | Compute arc tangent | `f64` | `(f64)` | +| `atan2` | Compute arc tangent with two parameters | `f64` | `(f64, f64)` | +| `cosh` | Compute hyperbolic cosine | `f64` | `(f64)` | +| `sinh` | Compute hyperbolic sine | `f64` | `(f64)` | +| `tanh` | Compute hyperbolic tangent | `f64` | `(f64)` | +| `acosh` | Compute area hyperbolic cosine | `f64` | `(f64)` | +| `asinh` | Compute area hyperbolic sine | `f64` | `(f64)` | +| `atanh` | Compute area hyperbolic tangent | `f64` | `(f64)` | +| `exp` | Compute exponential function | `f64` | `(f64)` | +| `ldexp` | Generate value from significant and exponent | `f64` | `(f64, f32)` | +| `log` | Compute natural logarithm | `f64` | `(f64)` | +| `log10` | Compute common logarithm | `f64` | `(f64)` | +| `exp2` | Compute binary exponential function | `f64` | `(f64)` | +| `log2` | Compute binary logarithm | `f64` | `(f64)` | +| `pow` | Raise to power | `f64` | `(f64, f64)` | +| `sqrt` | Compute square root | `f64` | `(f64)` | +| `sqr` | Compute square | `f64` | `(f64)` | +| `ceil` | Round up value | `f64` | `(f64)` | +| `floor` | Round down value | `f64` | `(f64)` | +| `round` | Round to nearest | `f64` | `(f64)` | +| `abs` | Compute absolute value | `f64` | `(f64)` | + +The following table lists the constants in the `flecs.script.math` namespace: + +| **Function Name** | **Description** | **Type** | **Value** | +|-------------------|-------------------------------------------|----------|----------------------| +| `E` | Euler's number | `f64` | `2.71828182845904523536028747135266250` | +| `PI` | Ratio of circle circumference to diameter | `f64` | `3.14159265358979323846264338327950288` | + +To use the math functions, make sure to use a Flecs build compiled with the `FLECS_SCRIPT_MATH` addon (disabled by default) and that the module is imported: + +```c +ECS_IMPORT(world, FlecsScriptMath); +``` + ## Templates Templates are parameterized scripts that can be used to create procedural assets. Templates can be created with the `template` keyword. Example: diff --git a/include/flecs.h b/include/flecs.h index 9ea399413..8219c9925 100644 --- a/include/flecs.h +++ b/include/flecs.h @@ -134,27 +134,17 @@ */ // #define FLECS_KEEP_ASSERT -/** \def FLECS_CPP_NO_AUTO_REGISTRATION - * When set, the C++ API will require that components are registered before they - * are used. This is useful in multithreaded applications, where components need - * to be registered beforehand, and to catch issues in projects where component - * registration is mandatory. Disabling automatic component registration also - * slightly improves performance. - * The C API is not affected by this feature. - */ -// #define FLECS_CPP_NO_AUTO_REGISTRATION - /** @def FLECS_CPP_NO_AUTO_REGISTRATION * When set, the C++ API will require that components are registered before they * are used. This is useful in multithreaded applications, where components need - * to be registered beforehand, and to catch issues in projects where component + * to be registered beforehand, and to catch issues in projects where component * registration is mandatory. Disabling automatic component registration also * slightly improves performance. * The C API is not affected by this feature. */ // #define FLECS_CPP_NO_AUTO_REGISTRATION -/** \def FLECS_CUSTOM_BUILD +/** @def FLECS_CUSTOM_BUILD * This macro lets you customize which addons to build flecs with. * Without any addons Flecs is just a minimal ECS storage, but addons add * features such as systems, scheduling and reflection. If an addon is disabled, @@ -271,7 +261,7 @@ #define FLECS_ID_DESC_MAX (32) #endif -/** \def FLECS_EVENT_DESC_MAX +/** @def FLECS_EVENT_DESC_MAX * Maximum number of events in ecs_observer_desc_t */ #ifndef FLECS_EVENT_DESC_MAX #define FLECS_EVENT_DESC_MAX (8) @@ -281,19 +271,19 @@ * Maximum number of query variables per query */ #define FLECS_VARIABLE_COUNT_MAX (64) -/** \def FLECS_TERM_COUNT_MAX +/** @def FLECS_TERM_COUNT_MAX * Maximum number of terms in queries. Should not exceed 64. */ #ifndef FLECS_TERM_COUNT_MAX #define FLECS_TERM_COUNT_MAX 32 #endif -/** \def FLECS_TERM_ARG_COUNT_MAX +/** @def FLECS_TERM_ARG_COUNT_MAX * Maximum number of arguments for a term. */ #ifndef FLECS_TERM_ARG_COUNT_MAX #define FLECS_TERM_ARG_COUNT_MAX (16) #endif -/** \def FLECS_QUERY_VARIABLE_COUNT_MAX +/** @def FLECS_QUERY_VARIABLE_COUNT_MAX * Maximum number of query variables per query. Should not exceed 128. */ #ifndef FLECS_QUERY_VARIABLE_COUNT_MAX #define FLECS_QUERY_VARIABLE_COUNT_MAX (64) diff --git a/src/addons/script/functions_builtin.c b/src/addons/script/functions_builtin.c index 94ae9c32f..68f0ed371 100644 --- a/src/addons/script/functions_builtin.c +++ b/src/addons/script/functions_builtin.c @@ -44,10 +44,8 @@ void flecs_meta_entity_parent( *(ecs_entity_t*)result->ptr = ecs_get_parent(ctx->world, entity); } -#ifdef FLECS_DOC - static -void flecs_meta_entity_doc_name( +void flecs_meta_entity_has( const ecs_function_ctx_t *ctx, int32_t argc, const ecs_value_t *argv, @@ -55,21 +53,118 @@ void flecs_meta_entity_doc_name( { (void)argc; ecs_entity_t entity = *(ecs_entity_t*)argv[0].ptr; - *(char**)result->ptr = ecs_os_strdup(ecs_doc_get_name(ctx->world, entity)); + ecs_id_t id = *(ecs_id_t*)argv[1].ptr; + *(ecs_bool_t*)result->ptr = ecs_has_id(ctx->world, entity, id); +} + +static +void flecs_meta_core_pair( + const ecs_function_ctx_t *ctx, + int32_t argc, + const ecs_value_t *argv, + ecs_value_t *result) +{ + (void)argc; + (void)ctx; + ecs_entity_t first = *(ecs_entity_t*)argv[0].ptr; + ecs_entity_t second = *(ecs_entity_t*)argv[1].ptr; + *(ecs_id_t*)result->ptr = ecs_pair(first, second); } +#ifdef FLECS_DOC + +#define FLECS_DOC_FUNC(name)\ + static\ + void flecs_meta_entity_doc_##name(\ + const ecs_function_ctx_t *ctx,\ + int32_t argc,\ + const ecs_value_t *argv,\ + ecs_value_t *result)\ + {\ + (void)argc;\ + ecs_entity_t entity = *(ecs_entity_t*)argv[0].ptr;\ + *(char**)result->ptr = \ + ecs_os_strdup(ecs_doc_get_##name(ctx->world, entity));\ + } + +FLECS_DOC_FUNC(name) +FLECS_DOC_FUNC(uuid) +FLECS_DOC_FUNC(brief) +FLECS_DOC_FUNC(detail) +FLECS_DOC_FUNC(link) +FLECS_DOC_FUNC(color) + +#undef FLECS_DOC_FUNC + static void flecs_script_register_builtin_doc_functions( ecs_world_t *world) { - ecs_entity_t name = ecs_method(world, { - .name = "doc_name", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_doc_name - }); - - ecs_doc_set_brief(world, name, "Returns entity doc name"); + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_name", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_name + }); + + ecs_doc_set_brief(world, m, "Returns entity doc name"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_uuid", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_uuid + }); + + ecs_doc_set_brief(world, m, "Returns entity doc uuid"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_brief", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_brief + }); + + ecs_doc_set_brief(world, m, "Returns entity doc brief description"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_detail", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_detail + }); + + ecs_doc_set_brief(world, m, "Returns entity doc detailed description"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_link", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_link + }); + + ecs_doc_set_brief(world, m, "Returns entity doc link"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "doc_color", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_doc_color + }); + + ecs_doc_set_brief(world, m, "Returns entity doc color"); + } } #else @@ -86,32 +181,67 @@ void flecs_script_register_builtin_doc_functions( void flecs_script_register_builtin_functions( ecs_world_t *world) { - ecs_entity_t name = ecs_method(world, { - .name = "name", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_name - }); - - ecs_doc_set_brief(world, name, "Returns entity name"); - - ecs_entity_t path = ecs_method(world, { - .name = "path", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_string_t), - .callback = flecs_meta_entity_path - }); - - ecs_doc_set_brief(world, path, "Returns entity path"); - - ecs_entity_t parent = ecs_method(world, { - .name = "parent", - .parent = ecs_id(ecs_entity_t), - .return_type = ecs_id(ecs_entity_t), - .callback = flecs_meta_entity_parent - }); - - ecs_doc_set_brief(world, parent, "Returns entity parent"); + { + ecs_entity_t m = ecs_method(world, { + .name = "name", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_name + }); + + ecs_doc_set_brief(world, m, "Returns entity name"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "path", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_string_t), + .callback = flecs_meta_entity_path + }); + + ecs_doc_set_brief(world, m, "Returns entity path"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "parent", + .parent = ecs_id(ecs_entity_t), + .return_type = ecs_id(ecs_entity_t), + .callback = flecs_meta_entity_parent + }); + + ecs_doc_set_brief(world, m, "Returns entity parent"); + } + + { + ecs_entity_t m = ecs_method(world, { + .name = "has", + .parent = ecs_id(ecs_entity_t), + .params = { + { .name = "component", .type = ecs_id(ecs_id_t) } + }, + .return_type = ecs_id(ecs_bool_t), + .callback = flecs_meta_entity_has + }); + + ecs_doc_set_brief(world, m, "Returns whether entity has component"); + } + + { + ecs_entity_t m = ecs_function(world, { + .name = "pair", + .parent = ecs_entity(world, { .name = "core"}), + .params = { + { .name = "first", .type = ecs_id(ecs_entity_t) }, + { .name = "second", .type = ecs_id(ecs_entity_t) } + }, + .return_type = ecs_id(ecs_id_t), + .callback = flecs_meta_core_pair + }); + + ecs_doc_set_brief(world, m, "Returns a pair identifier"); + } flecs_script_register_builtin_doc_functions(world); } diff --git a/src/addons/script/functions_math.c b/src/addons/script/functions_math.c index b3e7c8cb2..54be522c3 100644 --- a/src/addons/script/functions_math.c +++ b/src/addons/script/functions_math.c @@ -179,7 +179,7 @@ void FlecsScriptMathImport( /* Exponential and logarithmic functions */ FLECS_MATH_FUNC_DEF_F64(exp, "Compute exponential function"); - FLECS_MATH_FUNC_DEF_F64_F32(ldexp, "Generate value from significand and exponent"); + FLECS_MATH_FUNC_DEF_F64_F32(ldexp, "Generate value from significant and exponent"); FLECS_MATH_FUNC_DEF_F64(log, "Compute natural logarithm"); FLECS_MATH_FUNC_DEF_F64(log10, "Compute common logarithm"); FLECS_MATH_FUNC_DEF_F64(exp2, "Compute binary exponential function"); diff --git a/test/script/project.json b/test/script/project.json index e5720c106..42c20736d 100644 --- a/test/script/project.json +++ b/test/script/project.json @@ -577,7 +577,15 @@ "entity_path_expr", "entity_parent_func", "entity_name_func", + "entity_has_func", + "entity_has_func_w_pair", + "entity_has_func_w_pair_pair_invalid", "entity_doc_name_func", + "entity_doc_uuid_func", + "entity_doc_brief_func", + "entity_doc_detail_func", + "entity_doc_link_func", + "entity_doc_color_func", "entity_path_func", "entity_chain_func", "var_parent_func", diff --git a/test/script/src/Expr.c b/test/script/src/Expr.c index b2eb347f2..75b71c577 100644 --- a/test/script/src/Expr.c +++ b/test/script/src/Expr.c @@ -3194,6 +3194,99 @@ void Expr_entity_name_func(void) { ecs_fini(world); } +void Expr_entity_has_func(void) { + ecs_world_t *world = ecs_init(); + + ECS_COMPONENT(world, Position); + ECS_COMPONENT(world, Velocity); + + ecs_entity_t e = ecs_entity(world, { .name = "e" }); + test_assert(e != 0); + + ecs_add(world, e, Position); + + { + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "e.has(Position)", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_bool_t)); + test_assert(v.ptr != NULL); + test_bool(*(bool*)v.ptr, true); + ecs_value_free(world, v.type, v.ptr); + } + + { + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "e.has(Velocity)", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_bool_t)); + test_assert(v.ptr != NULL); + test_bool(*(bool*)v.ptr, false); + ecs_value_free(world, v.type, v.ptr); + } + + ecs_fini(world); +} + +void Expr_entity_has_func_w_pair(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Likes); + ECS_TAG(world, Apples); + ECS_TAG(world, Pears); + + ecs_entity_t e = ecs_entity(world, { .name = "e" }); + test_assert(e != 0); + + ecs_add_pair(world, e, Likes, Apples); + + { + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "e.has(flecs.script.core.pair(Likes, Apples))", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_bool_t)); + test_assert(v.ptr != NULL); + test_bool(*(bool*)v.ptr, true); + ecs_value_free(world, v.type, v.ptr); + } + + { + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "e.has(flecs.script.core.pair(Likes, Pears))", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_bool_t)); + test_assert(v.ptr != NULL); + test_bool(*(bool*)v.ptr, false); + ecs_value_free(world, v.type, v.ptr); + } + + ecs_fini(world); +} + +void Expr_entity_has_func_w_pair_pair_invalid(void) { + ecs_world_t *world = ecs_init(); + + ECS_TAG(world, Likes); + ECS_TAG(world, Apples); + ECS_TAG(world, Pears); + + ecs_entity_t e = ecs_entity(world, { .name = "e" }); + test_assert(e != 0); + + ecs_add_pair(world, e, Likes, Apples); + + { + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + ecs_log_set_level(-4); + test_assert(ecs_expr_run(world, + "e.has(flecs.script.core.pair(flecs.script.core.pair(Likes, Apples), Pears))", + &v, &desc) == NULL); + } + + ecs_fini(world); +} + void Expr_entity_doc_name_func(void) { ecs_world_t *world = ecs_init(); @@ -3202,14 +3295,149 @@ void Expr_entity_doc_name_func(void) { ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); test_assert(foo != 0); - ecs_doc_set_name(world, foo, "FooDoc"); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); ecs_value_t v = {0}; ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; test_assert(ecs_expr_run(world, "parent.foo.doc_name()", &v, &desc) != NULL); test_assert(v.type == ecs_id(ecs_string_t)); test_assert(v.ptr != NULL); - test_str(*(char**)v.ptr, "FooDoc"); + test_str(*(char**)v.ptr, "FooName"); + ecs_value_free(world, v.type, v.ptr); + + ecs_fini(world); +} + +void Expr_entity_doc_uuid_func(void) { + ecs_world_t *world = ecs_init(); + + ecs_entity_t parent = ecs_entity(world, { .name = "parent" }); + test_assert(parent != 0); + + ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); + test_assert(foo != 0); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); + + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "parent.foo.doc_uuid()", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_string_t)); + test_assert(v.ptr != NULL); + test_str(*(char**)v.ptr, "FooUuid"); + ecs_value_free(world, v.type, v.ptr); + + ecs_fini(world); +} + +void Expr_entity_doc_brief_func(void) { + ecs_world_t *world = ecs_init(); + + ecs_entity_t parent = ecs_entity(world, { .name = "parent" }); + test_assert(parent != 0); + + ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); + test_assert(foo != 0); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); + + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "parent.foo.doc_brief()", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_string_t)); + test_assert(v.ptr != NULL); + test_str(*(char**)v.ptr, "FooBrief"); + ecs_value_free(world, v.type, v.ptr); + + ecs_fini(world); +} + +void Expr_entity_doc_detail_func(void) { + ecs_world_t *world = ecs_init(); + + ecs_entity_t parent = ecs_entity(world, { .name = "parent" }); + test_assert(parent != 0); + + ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); + test_assert(foo != 0); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); + + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "parent.foo.doc_detail()", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_string_t)); + test_assert(v.ptr != NULL); + test_str(*(char**)v.ptr, "FooDetail"); + ecs_value_free(world, v.type, v.ptr); + + ecs_fini(world); +} + +void Expr_entity_doc_link_func(void) { + ecs_world_t *world = ecs_init(); + + ecs_entity_t parent = ecs_entity(world, { .name = "parent" }); + test_assert(parent != 0); + + ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); + test_assert(foo != 0); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); + + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "parent.foo.doc_link()", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_string_t)); + test_assert(v.ptr != NULL); + test_str(*(char**)v.ptr, "FooLink"); + ecs_value_free(world, v.type, v.ptr); + + ecs_fini(world); +} + +void Expr_entity_doc_color_func(void) { + ecs_world_t *world = ecs_init(); + + ecs_entity_t parent = ecs_entity(world, { .name = "parent" }); + test_assert(parent != 0); + + ecs_entity_t foo = ecs_entity(world, { .name = "parent.foo" }); + test_assert(foo != 0); + ecs_doc_set_name(world, foo, "FooName"); + ecs_doc_set_uuid(world, foo, "FooUuid"); + ecs_doc_set_brief(world, foo, "FooBrief"); + ecs_doc_set_detail(world, foo, "FooDetail"); + ecs_doc_set_link(world, foo, "FooLink"); + ecs_doc_set_color(world, foo, "FooColor"); + + ecs_value_t v = {0}; + ecs_expr_eval_desc_t desc = { .disable_folding = disable_folding }; + test_assert(ecs_expr_run(world, "parent.foo.doc_color()", &v, &desc) != NULL); + test_assert(v.type == ecs_id(ecs_string_t)); + test_assert(v.ptr != NULL); + test_str(*(char**)v.ptr, "FooColor"); ecs_value_free(world, v.type, v.ptr); ecs_fini(world); diff --git a/test/script/src/main.c b/test/script/src/main.c index 1794c3815..23a320941 100644 --- a/test/script/src/main.c +++ b/test/script/src/main.c @@ -564,7 +564,15 @@ void Expr_entity_expr(void); void Expr_entity_path_expr(void); void Expr_entity_parent_func(void); void Expr_entity_name_func(void); +void Expr_entity_has_func(void); +void Expr_entity_has_func_w_pair(void); +void Expr_entity_has_func_w_pair_pair_invalid(void); void Expr_entity_doc_name_func(void); +void Expr_entity_doc_uuid_func(void); +void Expr_entity_doc_brief_func(void); +void Expr_entity_doc_detail_func(void); +void Expr_entity_doc_link_func(void); +void Expr_entity_doc_color_func(void); void Expr_entity_path_func(void); void Expr_entity_chain_func(void); void Expr_var_parent_func(void); @@ -3046,10 +3054,42 @@ bake_test_case Expr_testcases[] = { "entity_name_func", Expr_entity_name_func }, + { + "entity_has_func", + Expr_entity_has_func + }, + { + "entity_has_func_w_pair", + Expr_entity_has_func_w_pair + }, + { + "entity_has_func_w_pair_pair_invalid", + Expr_entity_has_func_w_pair_pair_invalid + }, { "entity_doc_name_func", Expr_entity_doc_name_func }, + { + "entity_doc_uuid_func", + Expr_entity_doc_uuid_func + }, + { + "entity_doc_brief_func", + Expr_entity_doc_brief_func + }, + { + "entity_doc_detail_func", + Expr_entity_doc_detail_func + }, + { + "entity_doc_link_func", + Expr_entity_doc_link_func + }, + { + "entity_doc_color_func", + Expr_entity_doc_color_func + }, { "entity_path_func", Expr_entity_path_func @@ -4170,7 +4210,7 @@ static bake_test_suite suites[] = { "Expr", Expr_setup, NULL, - 224, + 232, Expr_testcases, 1, Expr_params