finish table_insert interface
This commit is contained in:
		
							parent
							
								
									319779a16c
								
							
						
					
					
						commit
						3edff81d89
					
				
							
								
								
									
										5
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								main.c
									
									
									
									
									
								
							@ -32,7 +32,12 @@ static size_t native_print(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, L
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
static size_t table_insert(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, LRegSet *regset) {
 | 
					static size_t table_insert(LVM *lvm, void *ud, size_t argn, set_LValueU *heap, LRegSet *regset) {
 | 
				
			||||||
	LTable *tbl = (LTable*) (regset->regs[0].u & ~LTAG_MASK);
 | 
						LTable *tbl = (LTable*) (regset->regs[0].u & ~LTAG_MASK);
 | 
				
			||||||
 | 
						if(argn == 2) {
 | 
				
			||||||
		ltable_insert(tbl, regset->regs[1], 0);
 | 
							ltable_insert(tbl, regset->regs[1], 0);
 | 
				
			||||||
 | 
						} else if(argn > 2) {
 | 
				
			||||||
 | 
							size_t idx = lvalue_to_int32(regset->regs[1]);
 | 
				
			||||||
 | 
							ltable_insert(tbl, regset->regs[2], idx);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										19
									
								
								table.h
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								table.h
									
									
									
									
									
								
							@ -241,9 +241,20 @@ static inline size_t ltable_len(LTable *self) {
 | 
				
			|||||||
	return ret;
 | 
						return ret;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline void ltable_insert(LTable *self, LValue val, size_t index) {
 | 
					static inline bool ltable_insert(LTable *self, LValue val, size_t index) {
 | 
				
			||||||
	lrwl_write_lock(&self->lock);
 | 
						lrwl_write_lock(&self->lock);
 | 
				
			||||||
	index = ltable_len_nolock(self) + 1;
 | 
						size_t len = ltable_len_nolock(self);
 | 
				
			||||||
	ltable_set_nolock(self, lvalue_from_int32(index), val);
 | 
						if(index == 0) {
 | 
				
			||||||
	lrwl_write_unlock(&self->lock);
 | 
							index = len + 1;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						bool success = false;
 | 
				
			||||||
 | 
						if(index <= len + 1) {
 | 
				
			||||||
 | 
							for(size_t i = len; i >= index; i--) {
 | 
				
			||||||
 | 
								ltable_set_nolock(self, lvalue_from_int32(i + 1), ltable_get_nolock(self, lvalue_from_int32(i)));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							ltable_set_nolock(self, lvalue_from_int32(index), val);
 | 
				
			||||||
 | 
							success = true;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						lrwl_write_unlock(&self->lock);
 | 
				
			||||||
 | 
						return success;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user