Skip to content

Commit

Permalink
Implement Thread#thread_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
herwinw committed Jan 9, 2024
1 parent efd0f4f commit 116ed9c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/natalie/thread_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class ThreadObject : public Object {
bool has_thread_variable(Env *, Value) const;
Value thread_variable_get(Env *, Value);
Value thread_variable_set(Env *, Value, Value);
Value thread_variables(Env *) const;

void set_sleeping(bool sleeping) { m_sleeping = sleeping; }
bool is_sleeping() const { return m_sleeping; }
Expand Down
1 change: 1 addition & 0 deletions lib/natalie/compiler/binding_gen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,7 @@ def generate_name
gen.binding('Thread', 'thread_variable?', 'ThreadObject', 'has_thread_variable', argc: 1, pass_env: true, pass_block: false, return_type: :bool)
gen.binding('Thread', 'thread_variable_get', 'ThreadObject', 'thread_variable_get', argc: 1, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Thread', 'thread_variable_set', 'ThreadObject', 'thread_variable_set', argc: 2, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Thread', 'thread_variables', 'ThreadObject', 'thread_variables', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Thread', 'to_s', 'ThreadObject', 'to_s', argc: 0, pass_env: true, pass_block: false, aliases: ['inspect'], return_type: :Object)
gen.binding('Thread', 'value', 'ThreadObject', 'value', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
gen.binding('Thread', 'wakeup', 'ThreadObject', 'wakeup', argc: 0, pass_env: true, pass_block: false, return_type: :Object)
Expand Down
6 changes: 6 additions & 0 deletions src/thread_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ Value ThreadObject::thread_variable_set(Env *env, Value key, Value value) {
return m_thread_variables->refeq(env, key, value);
}

Value ThreadObject::thread_variables(Env *env) const {
if (!m_thread_variables)
return new ArrayObject;
return m_thread_variables->keys(env);
}

Value ThreadObject::list(Env *env) {
std::lock_guard<std::mutex> lock(g_thread_mutex);
auto ary = new ArrayObject { s_list.size() };
Expand Down

0 comments on commit 116ed9c

Please sign in to comment.