Summary: Release script objects for removed units and cities
                 Project: Freeciv
            Submitted by: englabenny
            Submitted on: söndag 2010-04-11 den 22:10
                Category: general
                Priority: 5 - Normal
                  Status: Ready For Test
                 Privacy: Public
             Assigned to: englabenny
        Originator Email: 
             Open/Closed: Open
         Discussion Lock: Any
         Planned Release: 2.3.0



Executive summary:

No more access to wild pointers through saved values of dead units or
destroyed cities in Lua.

Script may use Unit:is_alive() or City:exists() to be safe from script

Performance cost is only two Lua table lookups per deleted unit or deleted
city. (For the common case that the object was not exported as a tolua


The script API exposes freeciv objects by exporting wrapped pointers,
for example pointers to struct unit or struct city.
We must take care when these objects are removed and the pointers
become invalid ("wild").

In lua we can "store" a city in a variable:

    > city = find.city(nil, 108)
    > print(city)
    <City #108 Cityname>

The lua value bound to 'city' is a userdata ("full userdata") whose
payload is the pointer to the freeciv C object.

Conceptually we have this

    city: [userdata of type 'City' with [void *object: 0x012334]]

When script_release_exported_object(..) is used, 'city' now represents
a typed but empty value:

    city: [userdata of type 'City' with [void *object: NULL]]

'city' is not 'nil' in Lua. However, when 'city' is used in the API,
it is unboxed by tolua and resolves to a NULL pointer.

This means that dead units and destroyed cities:

* Have a "valid" lua value (not nil) that represents a NULL value in C
* Will be NULL when used as API function parameters
* Do not allow access to object fields ("city.id" -> script error)

The only valid methods you can call on lua value storing a
destroyed city or dead unit are:


All other field or method use will raise a script error.


File Attachments:

Date: söndag 2010-04-11 den 22:10  Name:
2010041201-Release-script-objects-for-removed-units-and-c.patch  Size: 7 kB  
By: englabenny



Reply to this item at:


  Meddelandet skickades via/av Gna!

Freeciv-dev mailing list

Reply via email to