1313#include "lj_gc.h"
1414#include "lj_err.h"
1515#include "lj_tab.h"
16+ #include "lj_lib.h"
1617
1718/* -- Object hashing ------------------------------------------------------ */
1819
@@ -201,8 +202,13 @@ GCtab * LJ_FASTCALL lj_tab_dup(lua_State *L, const GCtab *kt)
201202}
202203
203204/* Clear a table. */
204- void LJ_FASTCALL lj_tab_clear (GCtab * t )
205+ void LJ_FASTCALL lj_tab_clear (lua_State * L , GCtab * t )
205206{
207+ if (isreadonly (t ))
208+ {
209+ lj_err_msg (L , LJ_ERR_READONLY );
210+ }
211+
206212 clearapart (t );
207213 if (t -> hmask > 0 ) {
208214 Node * node = noderef (t -> node );
@@ -229,6 +235,11 @@ void LJ_FASTCALL lj_tab_free(global_State *g, GCtab *t)
229235/* Resize a table to fit the new array/hash part sizes. */
230236void lj_tab_resize (lua_State * L , GCtab * t , uint32_t asize , uint32_t hbits )
231237{
238+ if (isreadonly (t ))
239+ {
240+ lj_err_msg (L , LJ_ERR_READONLY );
241+ }
242+
232243 Node * oldnode = noderef (t -> node );
233244 uint32_t oldasize = t -> asize ;
234245 uint32_t oldhmask = t -> hmask ;
@@ -434,6 +445,11 @@ cTValue *lj_tab_get(lua_State *L, GCtab *t, cTValue *key)
434445/* Insert new key. Use Brent's variation to optimize the chain length. */
435446TValue * lj_tab_newkey (lua_State * L , GCtab * t , cTValue * key )
436447{
448+ if (isreadonly (t ))
449+ {
450+ lj_err_msg (L , LJ_ERR_READONLY );
451+ }
452+
437453 Node * n = hashkey (t , key );
438454 if (!tvisnil (& n -> val ) || t -> hmask == 0 ) {
439455 Node * nodebase = noderef (t -> node );
@@ -509,6 +525,11 @@ TValue *lj_tab_newkey(lua_State *L, GCtab *t, cTValue *key)
509525
510526TValue * lj_tab_setinth (lua_State * L , GCtab * t , int32_t key )
511527{
528+ if (isreadonly (t ))
529+ {
530+ lj_err_msg (L , LJ_ERR_READONLY );
531+ }
532+
512533 TValue k ;
513534 Node * n ;
514535 k .n = (lua_Number )key ;
@@ -522,6 +543,11 @@ TValue *lj_tab_setinth(lua_State *L, GCtab *t, int32_t key)
522543
523544TValue * lj_tab_setstr (lua_State * L , GCtab * t , const GCstr * key )
524545{
546+ if (isreadonly (t ))
547+ {
548+ lj_err_msg (L , LJ_ERR_READONLY );
549+ }
550+
525551 TValue k ;
526552 Node * n = hashstr (t , key );
527553 do {
@@ -534,6 +560,11 @@ TValue *lj_tab_setstr(lua_State *L, GCtab *t, const GCstr *key)
534560
535561TValue * lj_tab_set (lua_State * L , GCtab * t , cTValue * key )
536562{
563+ if (isreadonly (t ))
564+ {
565+ lj_err_msg (L , LJ_ERR_READONLY );
566+ }
567+
537568 Node * n ;
538569 t -> nomm = 0 ; /* Invalidate negative metamethod cache. */
539570 if (tvisstr (key )) {
@@ -684,3 +715,17 @@ MSize LJ_FASTCALL lj_tab_len_hint(GCtab *t, size_t hint)
684715}
685716#endif
686717
718+ void LJ_FASTCALL lj_tab_setreadonly (GCtab * t , int readOnly )
719+ {
720+ if (readOnly != 0 )
721+ {
722+ markreadonly (t );
723+ } else {
724+ unmarkreadonly (t );
725+ }
726+ }
727+
728+ int LJ_FASTCALL lj_tab_isreadonly (GCtab * t )
729+ {
730+ return isreadonly (t );
731+ }
0 commit comments