[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Locking Table Member Additions
- From: "Leinen, Rick" <RLeinen@...>
- Date: Tue, 17 Jun 2014 17:04:29 +0000
Thanks for your reply Sean.
Yes, I know Lua typically starts arrays with a 1. Old habits die hard. :-) I put the nil assignment there as a flag. Maybe that's not necessary. I'll explore that.
Yes, creating the table in C is more work. I have a couple of reasons for exploring this option.
1) Lua is new to our company. We are hoping that it will allow us to place the functionality of the product in a Lua script rather than hardcoded in C/C++. This will make it much easier to change or add functionality, even in the field. As a result, there may be occasion where our field service engineers may make modifications. Since none of the staff are coding experts, I want to hide as much of the nitty-gritty stuff as possible. That is the reason I would like to lock the members of the table. Less chance for mistakes.
2) The Kinetis microcontroller I am targeting has 1MB of internal flash and 256KB of internal SRAM. Neither is a lot, but I have much more flash. By defining the table in C, I am hoping that I will be using less RAM since the Lua script will be smaller. This may prove out to be negligible, but that is the thinking at the time.
I'll have to study your suggestions a bit, as I am fairly new to Lua, especially the C side.
Thanks again,
Rick Leinen
Engineering Manager, R&D Projects
Lighting and Energy Solutions
T: 503 404-5561 F: 503 404-5661 C: 503 860-6305
-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of Sean Conner
Sent: Monday, June 16, 2014 5:50 PM
To: Lua mailing list
Subject: Re: Locking Table Member Additions
It was thus said that the Great Leinen, Rick once stated:
>
> I have written "C" functions to manage a Lua table that is to be used
> by a Lua script to pass parameters to a "C" function where the
> parameters are sent out a proprietary CAN stack. The Lua table is created from a "C"
> function before the first lua_pcall is called. Initially the table is
> as
> follows:
>
> messagePayload =
> {
> isRelativeLevel = 0,
> isFadeRate = 1,
> isWaitForGo = 0,
> isHighestTakePrecedence = 0,
> priority = 8,
> fade = 10,
> channelListCount = 0,
> Dimmers =
> {
> [0] = nil,
> }
> }
While Lua can deal with arrays starting with 0, it is traditional to start with 1. Also, you don't need to initialize the first element in the array; just doing a lua_createtable(L,0,0) is enough for that.
> One of the functions that can be called from "C" adds dimmer/level
> pairs to the Dimmers table. For example, if the LuaScript called the "C"
> function with a dimmer number of 1 and a level of 128, the table would
> be modified as follows:
>
> messagePayload =
> {
> isRelativeLevel = 0,
> isFadeRate = 1,
> isWaitForGo = 0,
> isHighestTakePrecedence = 0,
> priority = 8,
> fade = 10,
> channelListCount = 0,
> Dimmers =
> {
> [0] = {dim = 1, lev = 128}
> }
> }
Is there some reason why this can't be done in Lua? Is there some background processing that happens you set the Dimmers array? Because that seems like a bunch of extra work dealing with that in C.
> Additional dimmer/level pairs can be added.
>
> My question is this; I would like to be able to modify the values of
> members of the table from Lua, but I do not want Lua to be able to
> change the members or structure of the table, only the "C" functions
> are allowed to do this. Is this possible?
For the most part, yes. Just associate a metatable with a function
__newindex() that either does nothing, or raises an error (depending on taste). This will prevent new fields from being added to the table.
Existing members can be removed, however, by being set to 'nil'. If you don't want to deal with that, then you either need to create a proxy table with a metatable with both __index() and __newindex() methods to manage the fields.
But a question: why? Adding members should not affect your code, and it would allow the programmer to associate additional information.
Without knowing more about the problem, it seems like constructing the table in C is more work than just doing it in Lua.
-spc