rVM 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rvm.rb CHANGED
@@ -1,7 +1,7 @@
1
- require 'rvm/interpreter'
2
- require 'rvm/classes'
3
- require 'rvm/functions'
4
- require 'rvm/languages'
1
+ require File.dirname(__FILE__) +'/rvm/interpreter'
2
+ require File.dirname(__FILE__) +'/rvm/classes'
3
+ require File.dirname(__FILE__) +'/rvm/functions'
4
+ require File.dirname(__FILE__) +'/rvm/languages'
5
5
  require 'timeout'
6
6
 
7
7
  # This is the rVM library. Including it loads the very basic functionalites rVM offers.
@@ -0,0 +1,116 @@
1
+
2
+ # RVM::ActsAsRVMType is a mixin that allows to quickly publish classes and types
3
+ # to the rVM interpreter without going through the hassle of defining each
4
+ # function on it's own and publisheing every varialbe that is watned.
5
+ #
6
+ # == Example
7
+ # class Test
8
+ # # Add the readers and writers for
9
+ # attr_accessor :var
10
+ # include RVM::ActsAsRVMType
11
+ # acts_as_rvm_type
12
+ # register_variable :var
13
+ # register_function :to_s
14
+ # end
15
+
16
+
17
+ module RVM::ActsAsRVMType
18
+
19
+ # Called when included, loads the class methods and the instance methods needed.
20
+ def self.included(mod)
21
+ mod.extend(RVM::ActsAsRVMType::ActsAsRVMTypeMethods)
22
+ end
23
+
24
+
25
+ module ActsAsRVMTypeMethods
26
+ # This function sets up the class to be a registerd class for rVM.
27
+ # It also sets it up to allow the use of +register_variable+ and
28
+ # +register_function+.
29
+ def acts_as_rvm_type
30
+ # registering as class
31
+ extend RVM::Plugin
32
+ plugin_host RVM::Classes
33
+ register_for self.class.to_s.gsub(/(\w)([A-Z])/,"\1_" + "\2".downcase).to_sym
34
+
35
+ #initializing empty functions and variables hashes
36
+ @functions = {}
37
+ @variables = {}
38
+
39
+ #including DSL functions
40
+ extend RVM::ActsAsRVMType::ActsAsRVMTypeInstanceMethods
41
+
42
+ #including methods in class
43
+ include RVM::ActsAsRVMType::ActsAsRVMTypeSingeltonMethods
44
+ end
45
+ end
46
+
47
+ module ActsAsRVMTypeSingeltonMethods
48
+ # This function will set up the variables for a rVM and replaces
49
+ # the current function with a shorter and faster one.
50
+ def variables
51
+ puts self.class
52
+ @variables = {}
53
+ #gets the variables hash from the Test class and maps it to a getter/setter Class
54
+ self.class.object_variables.each do |name, data|
55
+ published_as, writable = data
56
+ @variables[published_as.to_s] = RVM::Interpreter::VariableStorageCallback.new(self, name, writable)
57
+ end
58
+ #redefinces the variables method to remove overhead from mapping
59
+ instance_eval do
60
+ def variables
61
+ @variables
62
+ end
63
+ end
64
+ @variables
65
+ end
66
+
67
+ # This function will set up the variables for a rVM and replaces
68
+ # the current function with a shorter and faster one.
69
+ def functions
70
+ @functions = self.class.object_functions.dup
71
+ instance_eval do
72
+ def functions
73
+ @functions
74
+ end
75
+ end
76
+ @functions ||= @@functions.dup
77
+ end
78
+
79
+ end
80
+
81
+ module ActsAsRVMTypeInstanceMethods
82
+
83
+ # This registers a variable for the rVM interface, it allows the scripts
84
+ # to read and write them. If +writable+ is set this will be a read only
85
+ # variable. It alls an be published to rVM under a different name,
86
+ # by passing +published_as+.
87
+ def register_variable name, writable = true, published_as = name
88
+ @variables[published_as.to_s] = [name.to_sym, writable]
89
+ end
90
+
91
+ # This registers a variable for the rVM interface, it allows the scripts
92
+ # to read and write them. If +writable+ is set this will be a read only
93
+ # variable. It alls an be published to rVM under a different name,
94
+ # by passing +published_as+.
95
+ def register_function name, published_as = "#{name}"
96
+ @functions[published_as.to_s] = Class.new(RVM::Functions::Function) do @name = name; end
97
+ class << @functions[published_as.to_s]
98
+ def execute params, env
99
+ env.read_var_val(:self).send(@name, *params)
100
+ end
101
+
102
+ def signature
103
+ [:any]
104
+ end
105
+ end
106
+ end
107
+
108
+ def object_variables
109
+ @variables
110
+ end
111
+ def object_functions
112
+ @functions
113
+ end
114
+ end
115
+
116
+ end
data/lib/rvm/classes.rb CHANGED
@@ -19,7 +19,6 @@ module RVM
19
19
  # require 'lib/base/classes'
20
20
  # string = RVM::Classes[:string].new
21
21
  module Classes
22
-
23
22
  extend RVM::PluginHost
24
23
  default :string
25
24
  # The Parent for new classes, meant never to be used alone.
@@ -0,0 +1,36 @@
1
+ module RVM
2
+ module Classes
3
+ class Association < Hash
4
+ extend Plugin
5
+ plugin_host Classes
6
+ register_for :association
7
+ @@type = :association
8
+
9
+ def data_type
10
+ :list
11
+ end
12
+
13
+ def variables
14
+ @variables ||= {}
15
+ end
16
+
17
+
18
+ def functions
19
+ @functions ||= {
20
+ 'size' => RVM::Functions[:size],
21
+ 'length' => RVM::Functions[:size],
22
+ 'get' => RVM::Functions[:assoc_get],
23
+ 'set' => RVM::Functions[:assoc_set],
24
+ }
25
+ end
26
+
27
+ def initialize source= {}, sepperator = ' '
28
+ super()
29
+ if source.is_a?(::Hash) # Okay we've to hack here to get sure we get the /real/ String
30
+ self.merge! source
31
+ end
32
+ end
33
+
34
+ end
35
+ end
36
+ end
data/lib/rvm/functions.rb CHANGED
@@ -19,8 +19,7 @@ module RVM
19
19
  if (RVM::Functions::has? m)
20
20
  RVM::Functions[m].execute args, @env
21
21
  else
22
- raise "ook!"
23
- #method_missing_old_functions m, *args, &block
22
+ method_missing_old_functions m, *args, &block
24
23
  end
25
24
  end
26
25
 
@@ -0,0 +1,25 @@
1
+ module RVM
2
+ module Functions
3
+ class AssocGet < RVM::Functions::Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 2
7
+ assoc = params.shift
8
+ key = params.shift
9
+ assoc[key]
10
+ elsif params.length == 1 and (this = env.read_var_val(:self)).is_a?(RVM::Classes[:association])
11
+ this[params.shift]
12
+ else
13
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.class}) EXPECTS 2 or 1 ARGUMENTS BUT GOT #{params.length}")
14
+ end
15
+ end
16
+
17
+ def signature
18
+ [:any]
19
+ end
20
+ end
21
+
22
+ register_for :assoc_get
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module RVM
2
+ module Functions
3
+ class AssocSet < RVM::Functions::Function
4
+ class << self
5
+ def execute params, env
6
+ if params.length == 3
7
+ assoc = params.shift
8
+ key = params.shift
9
+ value = params.shift
10
+ assoc[key] = value
11
+ elsif params.length == 2 and (this = env.read_var_val(:self)).is_a?(RVM::Classes[:association])
12
+ key = params.shift
13
+ value = params.shift
14
+ this[key] = value
15
+ else
16
+ RVM::Classes[:error].new(1,"FUNCTION (#{self.class}) EXPECTS 3 or 2 ARGUMENTS BUT GOT #{params.length}")
17
+ end
18
+ end
19
+
20
+ def signature
21
+ [:any]
22
+ end
23
+ end
24
+
25
+ register_for :assoc_set
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) +'/../array/at'
2
+ require File.dirname(__FILE__) +'/../association/assoc_get'
3
+ module RVM
4
+ module Functions
5
+ class Get < RVM::Functions::Function
6
+ class << self
7
+ FUNCTION_MAP = {
8
+ RVM::Classes::List => RVM::Functions::At,
9
+ Classes::Association => RVM::Functions::AssocGet
10
+ }
11
+ def execute params, env
12
+ if params.length == 2
13
+ if fun = FUNCTION_MAP[params.first.class]
14
+ fun.execute(params, env)
15
+ else
16
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) does not accept #{params.first.class}")
17
+ end
18
+ elsif params.length == 1 and (this = env.read_var_val(:self)).is_a?(RVM::Classes[:association])
19
+ if fun = FUNCTION_MAP[this]
20
+ fun.execute(params, env)
21
+ else
22
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) does not accept #{thiss.class}")
23
+ end
24
+ else
25
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) EXPECTS 2 or 1 ARGUMENTS BUT GOT #{params.length}")
26
+ end
27
+ end
28
+
29
+ def signature
30
+ [:any]
31
+ end
32
+ end
33
+
34
+ register_for :get
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) +'/../array/set_at'
2
+ require File.dirname(__FILE__) +'/../association/assoc_set'
3
+ module RVM
4
+ module Functions
5
+ class Set < RVM::Functions::Function
6
+ class << self
7
+ FUNCTION_MAP = {
8
+ RVM::Classes::List => RVM::Functions::SetAt,
9
+ Classes::Association => RVM::Functions::AssocSet
10
+ }
11
+ def execute params, env
12
+ if params.length == 2
13
+ if fun = FUNCTION_MAP[params.first.class]
14
+ fun.execute(params, env)
15
+ else
16
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) does not accept #{params.first.class}")
17
+ end
18
+ elsif params.length == 1 and (this = env.read_var_val(:self)).is_a?(RVM::Classes[:association])
19
+ if fun = FUNCTION_MAP[this]
20
+ fun.execute(params, env)
21
+ else
22
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) does not accept #{thiss.class}")
23
+ end
24
+ else
25
+ RVM::Classes[:error].new(1, "FUNCTION (#{self.class}) EXPECTS 2 or 1 ARGUMENTS BUT GOT #{params.length}")
26
+ end
27
+ end
28
+
29
+ def signature
30
+ [:any]
31
+ end
32
+ end
33
+
34
+ register_for :set
35
+ end
36
+ end
37
+ end
@@ -1,9 +1,13 @@
1
1
  module RVM
2
2
  module Functions
3
3
  class Size < Function
4
- class << self
4
+ class << self
5
+ COLLECTION_CLASSES = [
6
+ RVM::Classes::List,
7
+ RVM::Classes::Association
8
+ ]
5
9
  def execute params, env
6
- if (this = env.read_var_val(:self)).is_a?(RVM::Classes[:list]) and params.empty?
10
+ if COLLECTION_CLASSES.include?(this = env.read_var_val(:self)) and params.empty?
7
11
  RVM::Classes[:number].new(this.size)
8
12
  elsif params.length == 1
9
13
  RVM::Classes[:number].new(params[0].size)
@@ -11,7 +15,6 @@ module RVM
11
15
  RVM::Classes[:error].new(1,"FUNCTION (#{self.class}) EXPECTS 2 ARGUMENTS BUT GOT #{params.length}")
12
16
  end
13
17
  end
14
-
15
18
  def signature
16
19
  [:any]
17
20
  end
@@ -22,11 +22,28 @@ module RVM
22
22
  RVM::Interpreter::Constant.new(RVM::Classes[type].new(value), pos)
23
23
  end
24
24
 
25
+
26
+ class VariableStorageCallback
27
+ def initialize obj, var, writable = true
28
+ @obj = obj
29
+ @get = var.to_sym
30
+ @set = "#{var}=".to_sym
31
+ @writable = writable
32
+ end
33
+ def val= v
34
+ @obj.send(@set,v) if @writable
35
+ end
36
+ def val
37
+ @obj.send(@get)
38
+ end
39
+ end
40
+
25
41
  class VariableStorage
26
42
  attr_accessor :val
27
- def initialize val, writable = true
43
+ def initialize val = nil, writable = true, &block
28
44
  @writable = writable
29
45
  @val = val
46
+ instance_eval(&block) if block
30
47
  end
31
48
 
32
49
  def val=v
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rVM
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Heinz N. Gies
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-07-16 00:00:00 +02:00
12
+ date: 2008-07-21 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -23,7 +23,9 @@ extra_rdoc_files:
23
23
  - README
24
24
  files:
25
25
  - lib/rvm
26
+ - lib/rvm/acts_as_rvm_type.rb
26
27
  - lib/rvm/classes
28
+ - lib/rvm/classes/association.rb
27
29
  - lib/rvm/classes/block.rb
28
30
  - lib/rvm/classes/boolean.rb
29
31
  - lib/rvm/classes/class.rb
@@ -39,8 +41,14 @@ files:
39
41
  - lib/rvm/functions/array/append.rb
40
42
  - lib/rvm/functions/array/at.rb
41
43
  - lib/rvm/functions/array/set_at.rb
42
- - lib/rvm/functions/array/size.rb
43
44
  - lib/rvm/functions/array.rb
45
+ - lib/rvm/functions/association
46
+ - lib/rvm/functions/association/assoc_get.rb
47
+ - lib/rvm/functions/association/assoc_set.rb
48
+ - lib/rvm/functions/collection
49
+ - lib/rvm/functions/collection/get.rb
50
+ - lib/rvm/functions/collection/set.rb
51
+ - lib/rvm/functions/collection/size.rb
44
52
  - lib/rvm/functions/general
45
53
  - lib/rvm/functions/general/cmp.rb
46
54
  - lib/rvm/functions/general/eq.rb
@@ -120,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
120
128
  requirements: []
121
129
 
122
130
  rubyforge_project: rmush
123
- rubygems_version: 1.1.1
131
+ rubygems_version: 1.2.0
124
132
  signing_key:
125
133
  specification_version: 2
126
134
  summary: A ruby based VM that lets one add secure scripting to ruby applications.