Permalink
Browse files

Merge branch 'master' into celluloid

  • Loading branch information...
2 parents 281d0d9 + 499390f commit edabac447b948e24e2c1d705ff333c1598fe7c9f @bakkdoor committed Nov 14, 2012
Showing with 62 additions and 44 deletions.
  1. +22 −9 boot/fancy_ext/thread.rb
  2. +22 −17 lib/argv.fy
  3. +1 −1 lib/compiler/ast/identifier.fy
  4. +9 −6 lib/object.fy
  5. +3 −4 lib/package/list.fy
  6. +5 −7 lib/rbx/thread.fy
View
@@ -1,23 +1,36 @@
class Thread
- def dynamic_vars
- @dynamic_vars ||= []
+ def __dynamic_vars__
+ @dynamic_vars ||= {}
@dynamic_vars
end
- alias_method :set_thread_local, :[]=
+ private :__dynamic_vars__
- def []=(var, val)
- dynamic_vars << var unless dynamic_vars.include? var
- set_thread_local(var, val)
+ def dynamic_vars
+ __dynamic_vars__.keys
+ end
+
+ def get_dynamic_variable(var)
+ var = var.to_s
+ __dynamic_vars__[var]
+ end
+
+ def set_dynamic_variable(var, val)
+ var = var.to_s
+ __dynamic_vars__[var] = val
+ end
+
+ def copy_dynamic_variables_from(other_thread)
+ other_thread.send(:__dynamic_vars__).each do |var, val|
+ self.set_dynamic_variable(var, val)
+ end
end
class << self
alias_method :old_new, :new
def new(*args, &block)
parent = current
old_new(*args) do
- parent.dynamic_vars.each do |v|
- current[v] = parent[v]
- end
+ current.copy_dynamic_variables_from(parent)
block.call
end
end
View
@@ -3,10 +3,16 @@ ARGV documentation: """
arguments passed to a Fancy programm before it starts executing.
"""
-def ARGV for_option: op_name do: block {
- "Runs a given block if an option is in ARGV."
+def ARGV for_option: option_name do: block {
+ """
+ @option_name Name of command-line option.
+ @block @Block@ to be called with value of command-line option @option_name, if given.
+ @return @true, if @option_name was found in @ARGV and @block was called, @false otherwise.
+
+ Calls a given Block if a command-line option is in @ARGV.
+ """
- ARGV index: op_name . if_true: |idx| {
+ if: (ARGV index: option_name) then: |idx| {
if: (block arity > 0) then: {
if: (ARGV[idx + 1]) then: |arg| {
block call: [arg]
@@ -17,25 +23,24 @@ def ARGV for_option: op_name do: block {
block call
ARGV remove_at: idx
}
+ return true
}
+ return false
}
-def ARGV for_options: op_names do: block {
- "Runs a given block if any of the given options is in ARGV."
+def ARGV for_options: option_names do: block {
+ """
+ @option_names @Fancy::Enumerable@ of related command-line option names.
+ @block @Block@ to be called with value of any command-line option in @option_names, if given.
+ @return @true, if any of @option_names was found in @ARGV and @block was called, @false otherwise.
+
+ Calls a given Block if any of the given command-line options are in @ARGV.
+ """
- op_names size times: |i| {
- if: (ARGV index: (op_names[i])) then: |idx| {
- if: (block arity > 0) then: {
- if: (ARGV[idx + 1]) then: |arg| {
- block call: [arg]
- ARGV remove_at: idx
- ARGV remove_at: idx
- }
- } else: {
- block call
- ARGV remove_at: idx
- }
+ option_names each: |option_name| {
+ if: (ARGV for_option: option_name do: block) then: {
return true
}
}
+ return false
}
@@ -148,7 +148,7 @@ class Fancy AST {
thread bytecode: g
g send('current, 0, false)
@varname bytecode: g
- g send(':[], 1, false)
+ g send('dynamic_var:, 1, false)
}
}
}
View
@@ -535,10 +535,13 @@ class Object {
Runs a given @Block@ in a synchronized fashion if called by multiple Threads.
Uses a @Mutex@ in the background for synchronization (created on demand for each @Object@).
+ Calls @block with @self.
"""
@__mutex__ = @__mutex__ || { Mutex new() }
- @__mutex__ synchronize(&block)
+ @__mutex__ synchronize() {
+ block call: [self]
+ }
}
def copy_slots: slots from: object {
@@ -692,18 +695,18 @@ class Object {
{ return value } unless: var_name
unless: block do: {
- Thread current[var_name]: value
+ Thread current set_dynamic_var: var_name to: value
return value
}
- oldval = Thread current[var_name]
+ oldval = Thread current dynamic_var: var_name
try {
- Thread current[var_name]: value
+ Thread current set_dynamic_var: var_name to: value
block call
return value
} finally {
try { ensure_block call } catch {}
- Thread current[var_name]: oldval
+ Thread current set_dynamic_var: var_name to: oldval
}
}
@@ -772,7 +775,7 @@ class Object {
@block @Block@ to be executed while ignoring (catching but not handling) @Exception@s defined in @exception_classes.
Example:
- ignoring: (IOError, ZeroDivisionError) in: {
+ ignoring: (IOError, ZeroDivisionError) do: {
# do something
}
"""
View
@@ -5,13 +5,14 @@ class Fancy Package {
def println {
packages each: |p| {
name, version, url = p
- "#{name} (#{version})" println
+ "#{name} (#{version}) - #{url}" println
}
}
def packages {
packages = []
- try {
+ # ignore file not found, as no packages might have been installed yet.
+ ignoring: IOError do: {
File open: @package_list_file modes: ['read] with: |f| {
f readlines each: |l| {
match l {
@@ -20,8 +21,6 @@ class Fancy Package {
}
}
}
- } catch IOError => e {
- # ignore file not found, as no packages might have been installed yet.
}
packages
}
View
@@ -35,15 +35,13 @@ class Thread {
Thread metaclass ruby_alias: 'main
Thread metaclass ruby_alias: 'pass
Thread metaclass ruby_alias: 'stop
- ruby_alias: 'dynamic_vars
- def [dynamic_var_name] {
- send('[], dynamic_var_name)
- }
+ ruby_alias: 'dynamic_vars
+ alias_method: 'dynamic_var: for_ruby: 'get_dynamic_variable
+ alias_method: 'set_dynamic_var:to: for_ruby: 'set_dynamic_variable
- def [dynamic_var_name]: value {
- send('[]=, dynamic_var_name, value)
- }
+ alias_method: '[] for_ruby: '[]
+ alias_method: '[]: for_ruby: '[]=
def priority: new_prio {
priority=(new_prio)

0 comments on commit edabac4

Please sign in to comment.