[v2,5/5] Lua: Forward image properties via table to scripts

Message ID 1515769128-29657-5-git-send-email-stefan@herbrechtsmeier.net
State Changes Requested
Headers show
Series
  • [v2,1/5] dict: Rename dictionary struct and its key to distinguish it from simple lists
Related show

Commit Message

Stefan Herbrechtsmeier Jan. 12, 2018, 2:58 p.m.
From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
---

Changes in v2: None

 corelib/lua_interface.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

Comments

Christian Storm Jan. 15, 2018, 7:35 a.m. | #1
Hi Stefan,

> From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
> 
> Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
> ---
> 
> Changes in v2: None
> 
>  corelib/lua_interface.c | 24 ++++++++++++++++++++++++
>  1 file changed, 24 insertions(+)
> 
> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
> index 872cb2f..28bb4f5 100644
> --- a/corelib/lua_interface.c
> +++ b/corelib/lua_interface.c
> @@ -439,6 +439,10 @@ static int l_istream_read(lua_State* L)
>  
>  static void update_table(lua_State* L, struct img_type *img)
>  {
> +	struct dict_entry *property;
> +	struct dict_list_elem *elem;
> +	int i;
> +
>  	if (L && img) {
>  		luaL_checktype(L, -1, LUA_TTABLE);
>  
> @@ -464,6 +468,26 @@ static void update_table(lua_State* L, struct img_type *img)
>  		LUA_PUSH_IMG_NUMBER(img, "size", size);
>  		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
>  
> +		lua_pushstring(L, "properties");
> +		lua_newtable (L);
> +		LIST_FOREACH(property, &img->properties, next) {
> +			lua_pushstring(L, dict_entry_get_key(property));
> +			elem = LIST_FIRST(&property->list);
> +			if (LIST_NEXT(elem, next) == LIST_END(&property->list)) {
> +				lua_pushstring(L, elem->value);
> +			} else {
> +				i = 1;

You're using i just here, maybe rephrase this to
  int i = 1;
Same is true for the other two variables. This is,
however, just a matter of taste, though...


> +				lua_newtable (L);
> +				LIST_FOREACH(elem, &property->list, next) {
> +					lua_pushnumber(L, i++);
> +					lua_pushstring(L, elem->value);
> +					lua_settable(L, -3);
> +				}
> +			}
> +			lua_settable(L, -3);
> +		}
> +		lua_settable(L, -3);
> +
>  #ifdef CONFIG_HANDLER_IN_LUA
>  		if (is_type(L, LUA_TYPE_HANDLER)) {
>  			lua_pushstring(L, "copy2file");
> -- 
> 2.7.4
> 


Besten Gruß,
   Christian
Stefan Herbrechtsmeier Jan. 15, 2018, 7:25 p.m. | #2
Hi Christian,

Am 15.01.2018 um 08:35 schrieb Christian Storm:
>> From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
>>
>> Signed-off-by: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>
>> ---
>>
>> Changes in v2: None
>>
>>   corelib/lua_interface.c | 24 ++++++++++++++++++++++++
>>   1 file changed, 24 insertions(+)
>>
>> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
>> index 872cb2f..28bb4f5 100644
>> --- a/corelib/lua_interface.c
>> +++ b/corelib/lua_interface.c
>> @@ -439,6 +439,10 @@ static int l_istream_read(lua_State* L)
>>   
>>   static void update_table(lua_State* L, struct img_type *img)
>>   {
>> +	struct dict_entry *property;
>> +	struct dict_list_elem *elem;
>> +	int i;
>> +
>>   	if (L && img) {
>>   		luaL_checktype(L, -1, LUA_TTABLE);
>>   
>> @@ -464,6 +468,26 @@ static void update_table(lua_State* L, struct img_type *img)
>>   		LUA_PUSH_IMG_NUMBER(img, "size", size);
>>   		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
>>   
>> +		lua_pushstring(L, "properties");
>> +		lua_newtable (L);
>> +		LIST_FOREACH(property, &img->properties, next) {
>> +			lua_pushstring(L, dict_entry_get_key(property));
>> +			elem = LIST_FIRST(&property->list);
>> +			if (LIST_NEXT(elem, next) == LIST_END(&property->list)) {
>> +				lua_pushstring(L, elem->value);
>> +			} else {
>> +				i = 1;
> You're using i just here, maybe rephrase this to
>    int i = 1;
> Same is true for the other two variables. This is,
> however, just a matter of taste, though...

@Stefano: What are your preferences?


>> +				lua_newtable (L);
>> +				LIST_FOREACH(elem, &property->list, next) {
>> +					lua_pushnumber(L, i++);
>> +					lua_pushstring(L, elem->value);
>> +					lua_settable(L, -3);
>> +				}
>> +			}
>> +			lua_settable(L, -3);
>> +		}
>> +		lua_settable(L, -3);
>> +
>>   #ifdef CONFIG_HANDLER_IN_LUA
>>   		if (is_type(L, LUA_TYPE_HANDLER)) {
>>   			lua_pushstring(L, "copy2file");
>> -- 
>> 2.7.4
>>

Best regards,
   Stefan
Stefano Babic Jan. 15, 2018, 9:19 p.m. | #3
Hi Stefan,

On 15/01/2018 20:25, Stefan Herbrechtsmeier wrote:
> Hi Christian,

> 

> Am 15.01.2018 um 08:35 schrieb Christian Storm:

>>> From: Stefan Herbrechtsmeier <stefan.herbrechtsmeier@weidmueller.com>

>>>

>>> Signed-off-by: Stefan Herbrechtsmeier

>>> <stefan.herbrechtsmeier@weidmueller.com>

>>> ---

>>>

>>> Changes in v2: None

>>>

>>>   corelib/lua_interface.c | 24 ++++++++++++++++++++++++

>>>   1 file changed, 24 insertions(+)

>>>

>>> diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c

>>> index 872cb2f..28bb4f5 100644

>>> --- a/corelib/lua_interface.c

>>> +++ b/corelib/lua_interface.c

>>> @@ -439,6 +439,10 @@ static int l_istream_read(lua_State* L)

>>>     static void update_table(lua_State* L, struct img_type *img)

>>>   {

>>> +    struct dict_entry *property;

>>> +    struct dict_list_elem *elem;

>>> +    int i;

>>> +

>>>       if (L && img) {

>>>           luaL_checktype(L, -1, LUA_TTABLE);

>>>   @@ -464,6 +468,26 @@ static void update_table(lua_State* L, struct

>>> img_type *img)

>>>           LUA_PUSH_IMG_NUMBER(img, "size", size);

>>>           LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);

>>>   +        lua_pushstring(L, "properties");

>>> +        lua_newtable (L);

>>> +        LIST_FOREACH(property, &img->properties, next) {

>>> +            lua_pushstring(L, dict_entry_get_key(property));

>>> +            elem = LIST_FIRST(&property->list);

>>> +            if (LIST_NEXT(elem, next) == LIST_END(&property->list)) {

>>> +                lua_pushstring(L, elem->value);

>>> +            } else {

>>> +                i = 1;

>> You're using i just here, maybe rephrase this to

>>    int i = 1;

>> Same is true for the other two variables. This is,

>> however, just a matter of taste, though...

> 

> @Stefano: What are your preferences?


This is a pure matter of taste. Christian prefers to declare variable
inside the code, I am used to instantiate at the beginning (but
sometimes I do as Christian, too). I do not refuse patches due to one or
the other style, and I let the contributor to choose the preferred way.
IMHO both are readable.

> 

> 

>>> +                lua_newtable (L);

>>> +                LIST_FOREACH(elem, &property->list, next) {

>>> +                    lua_pushnumber(L, i++);

>>> +                    lua_pushstring(L, elem->value);

>>> +                    lua_settable(L, -3);

>>> +                }

>>> +            }

>>> +            lua_settable(L, -3);

>>> +        }

>>> +        lua_settable(L, -3);

>>> +

>>>   #ifdef CONFIG_HANDLER_IN_LUA

>>>           if (is_type(L, LUA_TYPE_HANDLER)) {

>>>               lua_pushstring(L, "copy2file");

>>> -- 

>>> 2.7.4

>>>

> 

> Best regards,

>   Stefan

> 


Best regards,
Stefano

-- 
=====================================================================
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sbabic@denx.de
=====================================================================

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 872cb2f..28bb4f5 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -439,6 +439,10 @@  static int l_istream_read(lua_State* L)
 
 static void update_table(lua_State* L, struct img_type *img)
 {
+	struct dict_entry *property;
+	struct dict_list_elem *elem;
+	int i;
+
 	if (L && img) {
 		luaL_checktype(L, -1, LUA_TTABLE);
 
@@ -464,6 +468,26 @@  static void update_table(lua_State* L, struct img_type *img)
 		LUA_PUSH_IMG_NUMBER(img, "size", size);
 		LUA_PUSH_IMG_NUMBER(img, "checksum", checksum);
 
+		lua_pushstring(L, "properties");
+		lua_newtable (L);
+		LIST_FOREACH(property, &img->properties, next) {
+			lua_pushstring(L, dict_entry_get_key(property));
+			elem = LIST_FIRST(&property->list);
+			if (LIST_NEXT(elem, next) == LIST_END(&property->list)) {
+				lua_pushstring(L, elem->value);
+			} else {
+				i = 1;
+				lua_newtable (L);
+				LIST_FOREACH(elem, &property->list, next) {
+					lua_pushnumber(L, i++);
+					lua_pushstring(L, elem->value);
+					lua_settable(L, -3);
+				}
+			}
+			lua_settable(L, -3);
+		}
+		lua_settable(L, -3);
+
 #ifdef CONFIG_HANDLER_IN_LUA
 		if (is_type(L, LUA_TYPE_HANDLER)) {
 			lua_pushstring(L, "copy2file");