diff --git a/libraries/AP_Scripting/docs/docs.lua b/libraries/AP_Scripting/docs/docs.lua index 9ee386b504..7eeae5de6e 100644 --- a/libraries/AP_Scripting/docs/docs.lua +++ b/libraries/AP_Scripting/docs/docs.lua @@ -1395,9 +1395,6 @@ function camera:take_picture(instance) end ---@class (exact) AP_Camera__camera_state_t_ud local AP_Camera__camera_state_t_ud = {} ----@return AP_Camera__camera_state_t_ud -function AP_Camera__camera_state_t() end - -- get field ---@return Vector2f_ud function AP_Camera__camera_state_t_ud:tracking_p1() end diff --git a/libraries/AP_Scripting/generator/src/main.c b/libraries/AP_Scripting/generator/src/main.c index 92278a34a1..aeb99bafa5 100644 --- a/libraries/AP_Scripting/generator/src/main.c +++ b/libraries/AP_Scripting/generator/src/main.c @@ -33,6 +33,7 @@ char keyword_creation[] = "creation"; char keyword_manual_operator[] = "manual_operator"; char keyword_operator_getter[] = "operator_getter"; + // attributes (should include the leading ' ) char keyword_attr_enum[] = "'enum"; char keyword_attr_literal[] = "'literal"; @@ -1290,6 +1291,24 @@ void end_dependency(FILE *f, const char *dependency) { } } +int should_emit_creation(struct userdata * data) { + if (data->creation || data->methods) { + // Custom creation or methods, if not specifically disabled + return !(data->creation && data->creation_args == -1); + } else { + // Don't expose creation function for items with only read-only fields + struct userdata_field * field = data->fields; + while(field) { + if (field->access_flags & ACCESS_FLAG_WRITE) { + return TRUE; + break; + } + field = field->next; + } + return FALSE; + } +} + void emit_headers(FILE *f) { struct header *node = headers; while (node) { @@ -1314,7 +1333,7 @@ void emit_userdata_allocators(void) { fprintf(source, "}\n"); // New method used externally, includes argcheck, overridden by custom creation function if provided - if (node->creation == NULL) { + if (node->creation == NULL && should_emit_creation(node)) { fprintf(source, "\n"); fprintf(source, "int lua_new_%s(lua_State *L) {\n", node->sanatized_name); @@ -1399,7 +1418,7 @@ void emit_userdata_declarations(void) { while (node) { start_dependency(header, node->dependency); fprintf(header, "%s * new_%s(lua_State *L);\n", node->name, node->sanatized_name); - if (node->creation == NULL) { + if (node->creation == NULL && should_emit_creation(node)) { fprintf(header, "int lua_new_%s(lua_State *L);\n", node->sanatized_name); } fprintf(header, "%s * check_%s(lua_State *L, int arg);\n", node->name, node->sanatized_name); @@ -1720,6 +1739,7 @@ void emit_field(const struct userdata_field *field, const char* object_name, con if (use_switch) { fprintf(source, " case 1:\n"); } + switch (field->type.type) { case TYPE_BOOLEAN: fprintf(source, "%slua_pushinteger(L, %s%s%s%s);\n", indent, object_name, object_access, field->name, index_string); @@ -1764,6 +1784,7 @@ void emit_field(const struct userdata_field *field, const char* object_name, con fprintf(source, " case 2: {\n"); } emit_checker(field->type, write_arg_number, 0, indent); + fprintf(source, "%s%s%s%s%s = data_%i;\n", indent, object_name, object_access, field->name, index_string, write_arg_number); fprintf(source, "%sreturn 0;\n", indent); if (use_switch) { @@ -2585,24 +2606,7 @@ void emit_userdata_new_funcs(void) { fprintf(source, " const lua_CFunction fun;\n"); fprintf(source, "} new_userdata[] = {\n"); while (data) { - // Dont expose creation function for all read only items - int expose_creation = FALSE; - if (data->creation || data->methods) { - // Custom creation or methods, if not specifically disabled - expose_creation = !(data->creation && data->creation_args == -1); - } else { - // Feilds only - struct userdata_field * field = data->fields; - while(field) { - if (field->access_flags & ACCESS_FLAG_WRITE) { - expose_creation = TRUE; - break; - } - field = field->next; - } - } - - if (expose_creation) { + if (should_emit_creation(data)) { start_dependency(source, data->dependency); if (data->creation) { // expose custom creation function to user (not used internally) @@ -2888,8 +2892,7 @@ void emit_docs(struct userdata *node, int is_userdata, int emit_creation) { // local userdata fprintf(docs, "local %s = {}\n\n", name); - int creation_disabled = (node->creation && node->creation_args == -1); - if (emit_creation && (!node->creation || !creation_disabled)) { + if (emit_creation && should_emit_creation(node)) { // creation function if (node->creation != NULL) { for (int i = 0; i < node->creation_args; ++i) { @@ -2910,7 +2913,6 @@ void emit_docs(struct userdata *node, int is_userdata, int emit_creation) { } fprintf(docs, ") end\n\n"); } - } } else { // global