<commit_before>package vm

import (
	"sync"
)

type stack struct {
	Data   []*Pointer
	thread *thread
	// Although every thread has its own stack, vm's main thread still can be accessed by other threads.
	// This is why we need a lock in stack
	// TODO: Find a way to fix this instead of put lock on every stack.
	*sync.RWMutex
}

func (s *stack) set(index int, pointer *Pointer) {
	s.Lock()

	defer s.Unlock()

	s.Data[index] = pointer
}

func (s *stack) push(v *Pointer) {
	s.Lock()
	defer s.Unlock()

	if len(s.Data) <= s.thread.sp {
		s.Data = append(s.Data, v)
	} else {
		s.Data[s.thread.sp] = v
	}

	s.thread.sp++
}

func (s *stack) pop() *Pointer {
	s.Lock()
	defer s.Unlock()

	if len(s.Data) < 1 {
		panic("Nothing to pop!")
	}

	if s.thread.sp < 0 {
		panic("SP is not normal!")
	}

	if s.thread.sp > 0 {
		s.thread.sp--
	}

	v := s.Data[s.thread.sp]
	s.Data[s.thread.sp] = nil
	return v
}

func (s *stack) top() *Pointer {
	s.RLock()
	defer s.RUnlock()

	if len(s.Data) == 0 {
		return nil
	}

	if s.thread.sp > 0 {
		return s.Data[s.thread.sp-1]
	}

	return s.Data[0]
}<commit_msg>Remove defer usage from stack<commit_after>package vm

import (
	"sync"
)

type stack struct {
	Data   []*Pointer
	thread *thread
	// Although every thread has its own stack, vm's main thread still can be accessed by other threads.
	// This is why we need a lock in stack
	// TODO: Find a way to fix this instead of put lock on every stack.
	sync.RWMutex
}

func (s *stack) set(index int, pointer *Pointer) {
	s.Lock()

	s.Data[index] = pointer

	s.Unlock()
}

func (s *stack) push(v *Pointer) {
	s.Lock()

	if len(s.Data) <= s.thread.sp {
		s.Data = append(s.Data, v)
	} else {
		s.Data[s.thread.sp] = v
	}

	s.thread.sp++
	s.Unlock()
}

func (s *stack) pop() *Pointer {
	s.Lock()

	if len(s.Data) < 1 {
		panic("Nothing to pop!")
	}

	if s.thread.sp < 0 {
		panic("SP is not normal!")
	}

	if s.thread.sp > 0 {
		s.thread.sp--
	}

	v := s.Data[s.thread.sp]
	s.Data[s.thread.sp] = nil
	s.Unlock()
	return v
}

func (s *stack) top() *Pointer {
	var r *Pointer
	s.RLock()

	if len(s.Data) == 0 {
		r = nil
	} else if s.thread.sp > 0 {
		r = s.Data[s.thread.sp-1]
	} else {
		r = s.Data[0]
	}

	s.RUnlock()

	return r
}
{'fraction_non_alphanumeric': 0.16327453640886477, 'fraction_numerical': 0.006331976481230212, 'mean_word_length': 2.7114093959731544, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1912409', 'n_tokens_mistral': 971, 'n_tokens_neox': 923, 'n_words': 273}