ryobg / JContainers

JSON-based data structures for Papyrus - the TESV Skyrim SE scripting language
MIT License
107 stars 23 forks source link

Add `JArray.eraseIndex` to the Lua API #34

Closed RealAntithesis closed 6 years ago

RealAntithesis commented 6 years ago

Hi, I'd like to request the JArray.eraseIndex() function be added to remove JArray entries. Currently, there's no way I can see to do this from within lua. table.remove() doesn't work since the JArray is cdata, not a table. I also had a look at JArray.__newindex using a nil value, but that doesn't seem to do the job.

So I had a look in the source code and came up with the following changes, which seem to work in my testing:

-- Added 'eraseIndex' function from tes_array.h to JArrayNativeFuncs in jc.lua.

local JArrayNativeFuncs = retrieveNativeFunctions('JArray',
    {
      object = {'handle'},
      objectWithSize = {'handle', 'uint32_t'},
      --getInt = {'int32_t', 'handle, index, int32_t'},
      --getFlt = {'float', 'handle, index, float'},
      --setInt = {'void', 'handle, index, int32_t'},
      --setFlt = {'void', 'handle, index, float'},
      valueType = {'int32_t', 'handle, index'}, -- AH CHANGED
      eraseIndex = {'void', 'handle, index'}, -- AH ADDED
    }
  )

  -- Added corresponding lua function to jc.lua
  function JArray.eraseIndex(optr, idx)
    JArrayNativeFuncs.eraseIndex(jc_context, optr.___id, convertIndex(idx))
  end

Thanks.

ryobg commented 6 years ago

Fair enough. Will check around and will make it part of the default API.