nodewrap 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. data/COPYING +59 -0
  2. data/LEGAL +6 -0
  3. data/LGPL +515 -0
  4. data/LICENSE +6 -0
  5. data/README +31 -0
  6. data/TODO +9 -0
  7. data/example/README +5 -0
  8. data/example/simple_client.rb +12 -0
  9. data/example/simple_server.rb +11 -0
  10. data/example/triangle_client.rb +7 -0
  11. data/example/triangle_server.rb +24 -0
  12. data/ext/MANIFEST +11 -0
  13. data/ext/builtins.h +25 -0
  14. data/ext/cached/ruby-1.6.3/COPYING +340 -0
  15. data/ext/cached/ruby-1.6.3/README +3 -0
  16. data/ext/cached/ruby-1.6.3/classpath.c +3 -0
  17. data/ext/cached/ruby-1.6.3/classpath.h +8 -0
  18. data/ext/cached/ruby-1.6.3/evalinfo.h +62 -0
  19. data/ext/cached/ruby-1.6.3/global_entry.h +15 -0
  20. data/ext/cached/ruby-1.6.3/insns_info.c +40 -0
  21. data/ext/cached/ruby-1.6.3/insns_info.h +19 -0
  22. data/ext/cached/ruby-1.6.3/node_type_descrip.c +150 -0
  23. data/ext/cached/ruby-1.6.3/nodeinfo.c +3182 -0
  24. data/ext/cached/ruby-1.6.3/nodeinfo.h +66 -0
  25. data/ext/cached/ruby-1.6.4/COPYING +340 -0
  26. data/ext/cached/ruby-1.6.4/README +3 -0
  27. data/ext/cached/ruby-1.6.4/classpath.c +3 -0
  28. data/ext/cached/ruby-1.6.4/classpath.h +8 -0
  29. data/ext/cached/ruby-1.6.4/evalinfo.h +62 -0
  30. data/ext/cached/ruby-1.6.4/global_entry.h +15 -0
  31. data/ext/cached/ruby-1.6.4/insns_info.c +40 -0
  32. data/ext/cached/ruby-1.6.4/insns_info.h +19 -0
  33. data/ext/cached/ruby-1.6.4/node_type_descrip.c +150 -0
  34. data/ext/cached/ruby-1.6.4/nodeinfo.c +3182 -0
  35. data/ext/cached/ruby-1.6.4/nodeinfo.h +66 -0
  36. data/ext/cached/ruby-1.6.5/COPYING +56 -0
  37. data/ext/cached/ruby-1.6.5/GPL +340 -0
  38. data/ext/cached/ruby-1.6.5/LEGAL +325 -0
  39. data/ext/cached/ruby-1.6.5/LGPL +504 -0
  40. data/ext/cached/ruby-1.6.5/README +3 -0
  41. data/ext/cached/ruby-1.6.5/classpath.c +3 -0
  42. data/ext/cached/ruby-1.6.5/classpath.h +8 -0
  43. data/ext/cached/ruby-1.6.5/evalinfo.h +63 -0
  44. data/ext/cached/ruby-1.6.5/global_entry.h +15 -0
  45. data/ext/cached/ruby-1.6.5/insns_info.c +40 -0
  46. data/ext/cached/ruby-1.6.5/insns_info.h +19 -0
  47. data/ext/cached/ruby-1.6.5/node_type_descrip.c +150 -0
  48. data/ext/cached/ruby-1.6.5/nodeinfo.c +3182 -0
  49. data/ext/cached/ruby-1.6.5/nodeinfo.h +66 -0
  50. data/ext/cached/ruby-1.6.7/COPYING +56 -0
  51. data/ext/cached/ruby-1.6.7/GPL +340 -0
  52. data/ext/cached/ruby-1.6.7/LEGAL +308 -0
  53. data/ext/cached/ruby-1.6.7/LGPL +504 -0
  54. data/ext/cached/ruby-1.6.7/README +3 -0
  55. data/ext/cached/ruby-1.6.7/classpath.c +3 -0
  56. data/ext/cached/ruby-1.6.7/classpath.h +8 -0
  57. data/ext/cached/ruby-1.6.7/evalinfo.h +63 -0
  58. data/ext/cached/ruby-1.6.7/global_entry.h +15 -0
  59. data/ext/cached/ruby-1.6.7/insns_info.c +40 -0
  60. data/ext/cached/ruby-1.6.7/insns_info.h +19 -0
  61. data/ext/cached/ruby-1.6.7/node_type_descrip.c +150 -0
  62. data/ext/cached/ruby-1.6.7/nodeinfo.c +3182 -0
  63. data/ext/cached/ruby-1.6.7/nodeinfo.h +66 -0
  64. data/ext/cached/ruby-1.6.8/COPYING +56 -0
  65. data/ext/cached/ruby-1.6.8/GPL +340 -0
  66. data/ext/cached/ruby-1.6.8/LEGAL +308 -0
  67. data/ext/cached/ruby-1.6.8/LGPL +504 -0
  68. data/ext/cached/ruby-1.6.8/README +3 -0
  69. data/ext/cached/ruby-1.6.8/classpath.c +3 -0
  70. data/ext/cached/ruby-1.6.8/classpath.h +8 -0
  71. data/ext/cached/ruby-1.6.8/evalinfo.h +63 -0
  72. data/ext/cached/ruby-1.6.8/global_entry.h +15 -0
  73. data/ext/cached/ruby-1.6.8/insns_info.c +40 -0
  74. data/ext/cached/ruby-1.6.8/insns_info.h +19 -0
  75. data/ext/cached/ruby-1.6.8/node_type_descrip.c +150 -0
  76. data/ext/cached/ruby-1.6.8/nodeinfo.c +3182 -0
  77. data/ext/cached/ruby-1.6.8/nodeinfo.h +66 -0
  78. data/ext/cached/ruby-1.8.0/COPYING +56 -0
  79. data/ext/cached/ruby-1.8.0/GPL +340 -0
  80. data/ext/cached/ruby-1.8.0/LEGAL +371 -0
  81. data/ext/cached/ruby-1.8.0/LGPL +504 -0
  82. data/ext/cached/ruby-1.8.0/README +3 -0
  83. data/ext/cached/ruby-1.8.0/classpath.c +27 -0
  84. data/ext/cached/ruby-1.8.0/classpath.h +14 -0
  85. data/ext/cached/ruby-1.8.0/evalinfo.h +65 -0
  86. data/ext/cached/ruby-1.8.0/global_entry.h +10 -0
  87. data/ext/cached/ruby-1.8.0/insns_info.c +40 -0
  88. data/ext/cached/ruby-1.8.0/insns_info.h +19 -0
  89. data/ext/cached/ruby-1.8.0/node_type_descrip.c +150 -0
  90. data/ext/cached/ruby-1.8.0/nodeinfo.c +3199 -0
  91. data/ext/cached/ruby-1.8.0/nodeinfo.h +66 -0
  92. data/ext/cached/ruby-1.8.1/COPYING +56 -0
  93. data/ext/cached/ruby-1.8.1/GPL +340 -0
  94. data/ext/cached/ruby-1.8.1/LEGAL +371 -0
  95. data/ext/cached/ruby-1.8.1/LGPL +504 -0
  96. data/ext/cached/ruby-1.8.1/README +3 -0
  97. data/ext/cached/ruby-1.8.1/classpath.c +27 -0
  98. data/ext/cached/ruby-1.8.1/classpath.h +14 -0
  99. data/ext/cached/ruby-1.8.1/evalinfo.h +59 -0
  100. data/ext/cached/ruby-1.8.1/global_entry.h +10 -0
  101. data/ext/cached/ruby-1.8.1/insns_info.c +40 -0
  102. data/ext/cached/ruby-1.8.1/insns_info.h +19 -0
  103. data/ext/cached/ruby-1.8.1/node_type_descrip.c +150 -0
  104. data/ext/cached/ruby-1.8.1/nodeinfo.c +3199 -0
  105. data/ext/cached/ruby-1.8.1/nodeinfo.h +66 -0
  106. data/ext/cached/ruby-1.8.2/COPYING +56 -0
  107. data/ext/cached/ruby-1.8.2/GPL +340 -0
  108. data/ext/cached/ruby-1.8.2/LEGAL +371 -0
  109. data/ext/cached/ruby-1.8.2/LGPL +504 -0
  110. data/ext/cached/ruby-1.8.2/README +3 -0
  111. data/ext/cached/ruby-1.8.2/classpath.c +45 -0
  112. data/ext/cached/ruby-1.8.2/classpath.h +17 -0
  113. data/ext/cached/ruby-1.8.2/evalinfo.h +60 -0
  114. data/ext/cached/ruby-1.8.2/global_entry.h +10 -0
  115. data/ext/cached/ruby-1.8.2/insns_info.c +40 -0
  116. data/ext/cached/ruby-1.8.2/insns_info.h +19 -0
  117. data/ext/cached/ruby-1.8.2/node_type_descrip.c +150 -0
  118. data/ext/cached/ruby-1.8.2/nodeinfo.c +3199 -0
  119. data/ext/cached/ruby-1.8.2/nodeinfo.h +66 -0
  120. data/ext/cached/ruby-1.8.3/COPYING +56 -0
  121. data/ext/cached/ruby-1.8.3/GPL +340 -0
  122. data/ext/cached/ruby-1.8.3/LEGAL +370 -0
  123. data/ext/cached/ruby-1.8.3/LGPL +504 -0
  124. data/ext/cached/ruby-1.8.3/README +3 -0
  125. data/ext/cached/ruby-1.8.3/classpath.c +45 -0
  126. data/ext/cached/ruby-1.8.3/classpath.h +17 -0
  127. data/ext/cached/ruby-1.8.3/evalinfo.h +61 -0
  128. data/ext/cached/ruby-1.8.3/global_entry.h +10 -0
  129. data/ext/cached/ruby-1.8.3/insns_info.c +40 -0
  130. data/ext/cached/ruby-1.8.3/insns_info.h +19 -0
  131. data/ext/cached/ruby-1.8.3/node_type_descrip.c +150 -0
  132. data/ext/cached/ruby-1.8.3/nodeinfo.c +3199 -0
  133. data/ext/cached/ruby-1.8.3/nodeinfo.h +66 -0
  134. data/ext/cached/ruby-1.8.4/COPYING +56 -0
  135. data/ext/cached/ruby-1.8.4/GPL +340 -0
  136. data/ext/cached/ruby-1.8.4/LEGAL +370 -0
  137. data/ext/cached/ruby-1.8.4/LGPL +504 -0
  138. data/ext/cached/ruby-1.8.4/README +3 -0
  139. data/ext/cached/ruby-1.8.4/classpath.c +45 -0
  140. data/ext/cached/ruby-1.8.4/classpath.h +17 -0
  141. data/ext/cached/ruby-1.8.4/evalinfo.h +61 -0
  142. data/ext/cached/ruby-1.8.4/global_entry.h +10 -0
  143. data/ext/cached/ruby-1.8.4/insns_info.c +40 -0
  144. data/ext/cached/ruby-1.8.4/insns_info.h +19 -0
  145. data/ext/cached/ruby-1.8.4/node_type_descrip.c +150 -0
  146. data/ext/cached/ruby-1.8.4/nodeinfo.c +3199 -0
  147. data/ext/cached/ruby-1.8.4/nodeinfo.h +66 -0
  148. data/ext/cached/ruby-1.8.5/COPYING +56 -0
  149. data/ext/cached/ruby-1.8.5/GPL +340 -0
  150. data/ext/cached/ruby-1.8.5/LEGAL +370 -0
  151. data/ext/cached/ruby-1.8.5/LGPL +504 -0
  152. data/ext/cached/ruby-1.8.5/README +3 -0
  153. data/ext/cached/ruby-1.8.5/classpath.c +45 -0
  154. data/ext/cached/ruby-1.8.5/classpath.h +17 -0
  155. data/ext/cached/ruby-1.8.5/evalinfo.h +61 -0
  156. data/ext/cached/ruby-1.8.5/global_entry.h +10 -0
  157. data/ext/cached/ruby-1.8.5/insns_info.c +40 -0
  158. data/ext/cached/ruby-1.8.5/insns_info.h +19 -0
  159. data/ext/cached/ruby-1.8.5/node_type_descrip.c +150 -0
  160. data/ext/cached/ruby-1.8.5/nodeinfo.c +3187 -0
  161. data/ext/cached/ruby-1.8.5/nodeinfo.h +64 -0
  162. data/ext/cached/ruby-1.8.6/COPYING +56 -0
  163. data/ext/cached/ruby-1.8.6/GPL +340 -0
  164. data/ext/cached/ruby-1.8.6/LEGAL +370 -0
  165. data/ext/cached/ruby-1.8.6/LGPL +504 -0
  166. data/ext/cached/ruby-1.8.6/README +3 -0
  167. data/ext/cached/ruby-1.8.6/classpath.c +45 -0
  168. data/ext/cached/ruby-1.8.6/classpath.h +17 -0
  169. data/ext/cached/ruby-1.8.6/evalinfo.h +61 -0
  170. data/ext/cached/ruby-1.8.6/global_entry.h +10 -0
  171. data/ext/cached/ruby-1.8.6/insns_info.c +40 -0
  172. data/ext/cached/ruby-1.8.6/insns_info.h +19 -0
  173. data/ext/cached/ruby-1.8.6/node_type_descrip.c +150 -0
  174. data/ext/cached/ruby-1.8.6/nodeinfo.c +3187 -0
  175. data/ext/cached/ruby-1.8.6/nodeinfo.h +64 -0
  176. data/ext/classpath.c +42 -0
  177. data/ext/classpath.c.rpp +28 -0
  178. data/ext/classpath.h +17 -0
  179. data/ext/classpath.h.rpp +35 -0
  180. data/ext/evalinfo.h +21 -0
  181. data/ext/evalinfo.h.rpp +49 -0
  182. data/ext/extconf.rb +68 -0
  183. data/ext/generate.rb +201 -0
  184. data/ext/generate_cached.rb +49 -0
  185. data/ext/global_entry.h +10 -0
  186. data/ext/global_entry.h.rpp +25 -0
  187. data/ext/insns_info.c +6079 -0
  188. data/ext/insns_info.c.rpp +210 -0
  189. data/ext/insns_info.h +819 -0
  190. data/ext/insns_info.h.rpp +50 -0
  191. data/ext/node_type_descrip.c +148 -0
  192. data/ext/node_type_descrip.c.rpp +72 -0
  193. data/ext/node_type_descrip.h +17 -0
  194. data/ext/node_type_descrip.rb +169 -0
  195. data/ext/nodeinfo.c +3164 -0
  196. data/ext/nodeinfo.c.rpp +519 -0
  197. data/ext/nodeinfo.h +67 -0
  198. data/ext/nodeinfo.h.rpp +27 -0
  199. data/ext/nodewrap.c +2576 -0
  200. data/ext/nodewrap.h +20 -0
  201. data/ext/read_node_h.rb +19 -0
  202. data/ext/ruby_source_dir.rb +15 -0
  203. data/ext/ruby_version.h +12 -0
  204. data/ext/ruby_version_code.rb +16 -0
  205. data/ext/rubypp.rb +97 -0
  206. data/ext/test.rb +15 -0
  207. data/generate_rdoc.rb +33 -0
  208. data/install.rb +1022 -0
  209. data/lib/as_code.rb +347 -0
  210. data/lib/as_expression.rb +657 -0
  211. data/lib/bytedecoder.rb +848 -0
  212. data/lib/classtree.rb +54 -0
  213. data/lib/methodsig.rb +266 -0
  214. data/lib/node_to_a.rb +34 -0
  215. data/lib/nodepp.rb +71 -0
  216. data/lib/nwdebug.rb +18 -0
  217. data/lib/nwdump.rb +53 -0
  218. data/lib/nwobfusc.rb +57 -0
  219. data/lib/procsig.rb +182 -0
  220. data/lib/test.rb +8 -0
  221. data/metaconfig +10 -0
  222. data/post-config.rb +53 -0
  223. data/post-setup.rb +9 -0
  224. data/pre-config.rb +1 -0
  225. data/run_tests.rb +48 -0
  226. data/test/expression_samples.rb +158 -0
  227. data/test/node_samples.rb +122 -0
  228. data/test/test.rb +39 -0
  229. data/test/test2.rb +7 -0
  230. data/test/test3.rb +5 -0
  231. data/test/test4.rb +5 -0
  232. data/test/test_as_code.rb +249 -0
  233. data/test/test_as_expression.rb +222 -0
  234. data/test/test_methodsig.rb +189 -0
  235. data/test/test_nodewrap.rb +369 -0
  236. metadata +286 -0
@@ -0,0 +1,122 @@
1
+ Common_Node_Samples = {
2
+ :method => "",
3
+ :fbody => "",
4
+ # cfunc not dumpable
5
+ :ifunc => "",
6
+ :scope => "",
7
+ :block => "",
8
+ :if => "if true then 1 end",
9
+ :case => "case 1 when 1 then true end",
10
+ :when => "case 1 when 1 then true end",
11
+ :opt_n => "",
12
+ :while => "a = 0; while a != 1; a += 1; end",
13
+ :until => "a = 0; until a == 1; a += 1; end",
14
+ :iter => "loop { break }",
15
+ :for => "a = 0; for i in 1..42; a += 1; end; a",
16
+ :break => "a = 24; for i in 1..42; break if i == 24; a += 1; end; a",
17
+ :next => "[1].each { next }",
18
+ :redo => "a = false; [1].each { if not a; a = true; redo; end }",
19
+ :retry => "a = true; begin; raise 'foo' if a; rescue; a = false; retry; end",
20
+ :begin => "begin; rescue; end",
21
+ :rescue => "begin; raise 'foo'; rescue; end",
22
+ :resbody => "begin; raise 'foo'; rescue; end",
23
+ :ensure => "begin; ensure; 10; end",
24
+ :and => "true and false",
25
+ :or => "if 1 or 2 then 3; end",
26
+ :not => "not false",
27
+ :masgn => "a, b, c = 4, 5, 6",
28
+ :lasgn => "x = 1",
29
+ :dasgn => "",
30
+ :dasgn_curr => "",
31
+ :gasgn => "$a = 1",
32
+ :iasgn => "@a = 1",
33
+ :cdecl => "",
34
+ :op_asgn1 => "{}[10] = 1",
35
+ :op_asgn2 => "{}.default = 1",
36
+ :op_asgn_and => "",
37
+ :op_asgn_or => "",
38
+ :call => "$stdout.sync",
39
+ :fcall => "", # see iter
40
+ :vcall => "",
41
+ :array => "[1, 2, 3]",
42
+ :zarray => "[]",
43
+ :hash => "{ 1 => 2 }",
44
+ :lvar => "",
45
+ :dvar => "",
46
+ :gvar => "$a == 1",
47
+ :ivar => "@a == 42", # must be diff from iasgn
48
+ :const => "",
49
+ :cvar => "@@a == 42",
50
+ :cvar2 => "",
51
+ :nth_ref => "$1 == nil",
52
+ :back_ref => "$` == nil",
53
+ :match => "",
54
+ :match2 => "",
55
+ :match3 => "",
56
+ :lit => "17",
57
+ :str => "\"foo\"",
58
+ :dstr => "\"#{8}\"",
59
+ :xstr => "`ls` if false",
60
+ :dxstr => "`\#{foo}` if false",
61
+ :evstr => "",
62
+ :dregx => "/\#{42}/",
63
+ :dregx_i => "/\#{42}/i", # same node type as dregx
64
+ :dregx_once => "/\#{42}/o",
65
+ :args => "",
66
+ :argscat => "",
67
+ :argspush => "''[0,*[1]] = 'foo'",
68
+ :restargs => "",
69
+ :block_arg => "", # def foo(&block)
70
+ :block_pass => "",
71
+ :defn => "",
72
+ :defs => "",
73
+ :alias => "",
74
+ :valias => "",
75
+ :undef => "",
76
+ :class => "",
77
+ :module => "",
78
+ :sclass => "class << self; end",
79
+ :colon2 => "Object::Kernel",
80
+ :colon3 => "::Object",
81
+ :cref => "",
82
+ :dot2 => "1..2",
83
+ :dot3 => "2...3",
84
+ :flip2 => "if 'foo' =~ /foo/ .. /bar/; end", # TODO
85
+ :flip3 => "if 'foo' =~ /foo/ ... /bar/; end", # TODO
86
+ :attrset => "",
87
+ :self => "self.nil?",
88
+ :nil => "nil",
89
+ :true => "true",
90
+ :false => "false",
91
+ :defined => "a = 1; defined?(a)",
92
+ :newline => "\n",
93
+ :postexe => "", # END { }
94
+ :alloca => "",
95
+ :dmethod => "",
96
+ :bmethod => "",
97
+ :memo => "",
98
+ :attrasgn => "{}.default = 42",
99
+ # last is just a placeholder
100
+
101
+ :crash1 => "self.class.ancestors.include?(Object)",
102
+ :crash2 => "Object === self",
103
+ }
104
+
105
+ Method_Node_Samples = Common_Node_Samples.dup.update({
106
+ :cvasgn => "@@a = 42", #TODO: not sure which of these is the decl and which is the asgn
107
+ :cvdecl => "@@a = 42",
108
+ :super => "super()",
109
+ :zsuper => "super",
110
+ :return => "",
111
+ :yield => "yield",
112
+ })
113
+
114
+ Proc_Node_Samples = Common_Node_Samples.dup.update({
115
+ :cvasgn => "",
116
+ :cvdecl => "",
117
+ :super => "", # can't call super from proc
118
+ :zsuper => "", # can't call super from proc
119
+ :return => "", # can't return from proc
120
+ :yield => "",
121
+ })
122
+
@@ -0,0 +1,39 @@
1
+ require 'nodewrap'
2
+
3
+ def foo; Object; end
4
+
5
+ m = method(:foo)
6
+ n = m.body
7
+ d = Marshal.dump(n)
8
+ n2 = Marshal.load(d)
9
+
10
+ c = Class.new
11
+ c.class_eval { add_method :foo, n2, Noex::PUBLIC }
12
+
13
+ obj2 = c.new
14
+
15
+ # puts method(:foo).body.body.disasm
16
+ # p method(:foo).body.body.to_a
17
+ #
18
+ # puts c.method(:foo).body.body.disasm
19
+ # p c.method(:foo).body.body.to_a
20
+ #
21
+ # i = method(:foo).body.body
22
+ # puts VM::InstructionSequence.load(i.to_a).disasm
23
+ # p VM::InstructionSequence.load(i.to_a).to_a
24
+
25
+ p obj2.foo
26
+
27
+ # obj2.method(:foo).body.body.eval
28
+
29
+ # require 'nodewrap'
30
+ #
31
+ # i = VM::InstructionSequence.compile('Object')
32
+ # s = Marshal.dump(i)
33
+ # i2 = Marshal.load(s)
34
+ #
35
+ # c = Class.new
36
+ # c.class_eval { add_method :foo, i2, Noex::PUBLIC }
37
+ # o = c.new
38
+ # p o.foo
39
+
@@ -0,0 +1,7 @@
1
+ $stdout.sync = true
2
+ is = VM::InstructionSequence.compile('1 + 1')
3
+ puts is.disasm
4
+ a = is.to_a
5
+ is2 = VM::InstructionSequence.load(a)
6
+ puts is2.disasm
7
+
@@ -0,0 +1,5 @@
1
+ i = VM::InstructionSequence.new('for a in []; end')
2
+ puts i.disasm
3
+ # a = i.to_a
4
+ # i2 = VM::InstructionSequence.load(a)
5
+ # puts i2.disasm
@@ -0,0 +1,5 @@
1
+ i = VM::InstructionSequence.new('a, b, c = 4, 5, 6')
2
+ # puts i.disasm
3
+ # a = i.to_a
4
+ # i2 = VM::InstructionSequence.load(a)
5
+ # puts i2.disasm
@@ -0,0 +1,249 @@
1
+ require 'test/unit'
2
+ require 'test/unit/ui/console/testrunner'
3
+ require 'timeout'
4
+
5
+ dir = File.dirname(__FILE__)
6
+ $:.unshift(dir) if not $:.include?(dir)
7
+ $:.unshift("#{dir}/../lib") if not $:.include?("#{dir}/../lib")
8
+ $:.unshift("#{dir}/../ext") if not $:.include?("#{dir}/../ext")
9
+
10
+ require 'nodewrap'
11
+ require 'as_code'
12
+
13
+ require "expression_samples"
14
+
15
+ $stdout.sync = true
16
+ $stderr.sync = true
17
+
18
+ class TC_As_Code < Test::Unit::TestCase
19
+ MAJOR = Config::CONFIG['MAJOR'].to_i
20
+ MINOR = Config::CONFIG['MINOR'].to_i
21
+ TEENY = Config::CONFIG['TEENY'].to_i
22
+ RUBY_VERSION_CODE = MAJOR * 100 + MINOR * 10 + TEENY
23
+
24
+ extend Test::Unit::Assertions
25
+
26
+ # Some of the samples use this
27
+ def foo(*a, &b)
28
+ return b.call(1, 2) if b
29
+ return a
30
+ end
31
+
32
+ EXPRESSION_SAMPLES.each do |name, code|
33
+ p = proc {
34
+ p_orig = eval("proc { #{code} }")
35
+ code_new = p_orig.body.as_code
36
+ # p code, code_new
37
+ p_new = eval("proc { #{code_new} }")
38
+ result_orig = result_new = nil
39
+ exc_orig = exc_new = nil
40
+ timeout(1) { begin; result_orig = p_orig.call; rescue; exc_orig = $!; end }
41
+ timeout(1) { begin; result_new = p_new.call; rescue; exc_new = $!; end }
42
+ assert_equal(exc_orig.class, exc_new.class)
43
+ if exc_orig and exc_new then
44
+ assert_equal(exc_orig.message, exc_new.message)
45
+ end
46
+ assert_equal(result_orig, result_new)
47
+ }
48
+ define_method "test_#{name}", p
49
+ end
50
+
51
+ def initialize(test_method_name)
52
+ # TODO: This seems to be the only way to get tests defined with #
53
+ # define_method to run on 1.8.1 and earlier.
54
+ @method_name = test_method_name
55
+ @test_passed = true
56
+ end
57
+
58
+ def method_no_args
59
+ 42
60
+ end
61
+
62
+ def test_method_no_args_as_code
63
+ m = method(:method_no_args)
64
+ assert_equal "def method_no_args()\n 42\nend", m.as_code
65
+ end
66
+
67
+ def method_one_arg(a)
68
+ 42
69
+ end
70
+
71
+ def test_method_one_arg_as_code
72
+ m = method(:method_one_arg)
73
+ assert_equal "def method_one_arg(a)\n 42\nend", m.as_code
74
+ end
75
+
76
+ def method_two_args(a, b)
77
+ 42
78
+ end
79
+
80
+ def test_method_two_args_as_code
81
+ m = method(:method_two_args)
82
+ assert_equal "def method_two_args(a, b)\n 42\nend", m.as_code
83
+ end
84
+
85
+ def method_rest_arg(*rest)
86
+ 42
87
+ end
88
+
89
+ def test_method_rest_arg_as_code
90
+ m = method(:method_rest_arg)
91
+ assert_equal "def method_rest_arg(*rest)\n 42\nend", m.as_code
92
+ end
93
+
94
+ def method_block_arg(&block)
95
+ 42
96
+ end
97
+
98
+ def test_method_block_arg_as_code
99
+ m = method(:method_block_arg)
100
+ assert_equal "def method_block_arg(&block)\n 42\nend", m.as_code
101
+ end
102
+
103
+ def method_rest_and_block_arg(*rest, &block)
104
+ 42
105
+ end
106
+
107
+ def test_method_rest_and_block_arg_as_code
108
+ m = method(:method_rest_and_block_arg)
109
+ assert_equal "def method_rest_and_block_arg(*rest, &block)\n 42\nend", m.as_code
110
+ end
111
+
112
+ def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)
113
+ 42
114
+ end
115
+
116
+ def test_method_two_args_and_rest_and_block_arg_as_code
117
+ m = method(:method_two_args_and_rest_and_block_arg)
118
+ assert_equal "def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)\n 42\nend", m.as_code
119
+ end
120
+
121
+ def method_with_body(a, b)
122
+ a + b
123
+ end
124
+
125
+ def test_method_with_body_as_code
126
+ m = method(:method_with_body)
127
+ assert_equal "def method_with_body(a, b)\n a + b\nend", m.as_code
128
+ end
129
+
130
+ def method_begin_ensure_as_code(a, b, *rest, &block)
131
+ begin
132
+ if not a and not b then
133
+ raise "Need more input!"
134
+ end
135
+ return a + b
136
+ ensure
137
+ puts "In ensure block"
138
+ end
139
+ end
140
+
141
+ def test_method_begin_ensure_as_code
142
+ m = method(:method_begin_ensure_as_code)
143
+ if RUBY_VERSION_CODE >= 190 then
144
+ ret = ''
145
+ else
146
+ ret = 'return '
147
+ end
148
+
149
+ assert_equal <<-END.chomp, m.as_code(3)
150
+ def method_begin_ensure_as_code(a, b, *rest, &block)
151
+ begin
152
+ (raise("Need more input!")) if (not a and not b)
153
+ #{ret}a + b
154
+ ensure
155
+ puts("In ensure block")
156
+ end
157
+ end
158
+ END
159
+ end
160
+
161
+ def test_proc_no_args_as_code
162
+ p = proc { }
163
+ assert_equal "proc do\nend", p.as_code
164
+ end
165
+
166
+ def test_proc_empty_args_as_code
167
+ if not defined?(VM) then
168
+ # indistinguishable from proc { } on YARV
169
+ p = proc { || }
170
+ assert_equal "proc do ||\nend", p.as_code
171
+ end
172
+ end
173
+
174
+ def test_proc_one_arg_as_code
175
+ p = proc { |a| }
176
+ assert_equal "proc do |a|\nend", p.as_code
177
+ end
178
+
179
+ def test_proc_one_array_arg_as_expression
180
+ p = proc { |a,| }
181
+ assert_equal "proc do |a,|\nend", p.as_code
182
+ end
183
+
184
+ def test_proc_two_args_as_code
185
+ p = proc { |a, b| }
186
+ assert_equal "proc do |a, b|\nend", p.as_code
187
+ end
188
+
189
+ def test_proc_rest_arg_as_code
190
+ p = proc { |*rest| }
191
+ assert_equal "proc do |*rest|\nend", p.as_code
192
+ end
193
+
194
+ def test_proc_two_args_and_rest_arg_as_code
195
+ p = proc { |a, b, *rest| }
196
+ assert_equal "proc do |a, b, *rest|\nend", p.as_code
197
+ end
198
+
199
+ def test_proc_with_body_as_code
200
+ p = proc { |a, b| a + b }
201
+ assert_equal "proc do |a, b|\n a + b\nend", p.as_code
202
+ end
203
+
204
+ def setup
205
+ @foo = 42
206
+ end
207
+
208
+ # 1.7 and later
209
+ def foo(*args)
210
+ return args
211
+ end
212
+
213
+ def remove_foo
214
+ self.class.remove_foo
215
+ end
216
+
217
+ # 1.6
218
+ def self.foo(*args)
219
+ return args
220
+ end
221
+
222
+ def self.remove_foo
223
+ self.class_eval { remove_const(:FOO) if const_defined?(:FOO) }
224
+ end
225
+
226
+ @@foo = 10
227
+
228
+ FOO = 57
229
+ end
230
+
231
+ if __FILE__ == $0 then
232
+ if Test::Unit.const_defined?(:AutoRunner) then
233
+ exit Test::Unit::AutoRunner.run
234
+ else
235
+ if ARGV.empty? then
236
+ suite = TC_As_Code.suite
237
+ else
238
+ suite = Test::Unit::TestSuite.new('TC_As_Code')
239
+ TC_As_Code.suite.tests.each do |test|
240
+ ARGV.each do |arg|
241
+ suite << test if /#{arg}/ =~ test.name
242
+ end
243
+ end
244
+ end
245
+ result = Test::Unit::UI::Console::TestRunner.run(suite)
246
+ exit(result.error_count + result.failure_count)
247
+ end
248
+ end
249
+
@@ -0,0 +1,222 @@
1
+ require 'test/unit'
2
+ require 'test/unit/ui/console/testrunner'
3
+ require 'timeout'
4
+
5
+ dir = File.dirname(__FILE__)
6
+ $:.unshift(dir) if not $:.include?(dir)
7
+ $:.unshift("#{dir}/../lib") if not $:.include?("#{dir}/../lib")
8
+ $:.unshift("#{dir}/../ext") if not $:.include?("#{dir}/../ext")
9
+
10
+ require 'nodewrap'
11
+ require 'as_expression'
12
+
13
+ require "expression_samples"
14
+
15
+ $stdout.sync = true
16
+ $stderr.sync = true
17
+
18
+ class TC_As_Expression < Test::Unit::TestCase
19
+ extend Test::Unit::Assertions
20
+
21
+ # Some of the samples use this
22
+ def foo(*a, &b)
23
+ return b.call(1, 2) if b
24
+ return a
25
+ end
26
+
27
+ EXPRESSION_SAMPLES.each do |name, code|
28
+ p = proc {
29
+ p_orig = eval("proc { #{code} }")
30
+ code_new = p_orig.body.as_expression
31
+ # p code, code_new
32
+ p_new = eval("proc { #{code_new} }")
33
+ result_orig = result_new = nil
34
+ exc_orig = exc_new = nil
35
+ timeout(1) { begin; result_orig = p_orig.call; rescue; exc_orig = $!; end }
36
+ timeout(1) { begin; result_new = p_new.call; rescue; exc_new = $!; end }
37
+ assert_equal(result_orig, result_new)
38
+ assert_equal(exc_orig.class, exc_new.class)
39
+ if exc_orig and exc_new then
40
+ assert_equal(exc_orig.message, exc_new.message)
41
+ end
42
+ }
43
+ define_method "test_#{name}", p
44
+ end
45
+
46
+ def initialize(test_method_name)
47
+ # TODO: This seems to be the only way to get tests defined with #
48
+ # define_method to run on 1.8.1 and earlier.
49
+ @method_name = test_method_name
50
+ @test_passed = true
51
+ end
52
+
53
+ def method_no_args
54
+ 42
55
+ end
56
+
57
+ def test_method_no_args_as_expression
58
+ m = method(:method_no_args)
59
+ assert_equal 'def method_no_args(); 42; end', m.as_expression
60
+ end
61
+
62
+ def method_one_arg(a)
63
+ 42
64
+ end
65
+
66
+ def test_method_one_arg_as_expression
67
+ m = method(:method_one_arg)
68
+ assert_equal 'def method_one_arg(a); 42; end', m.as_expression
69
+ end
70
+
71
+ def method_two_args(a, b)
72
+ 42
73
+ end
74
+
75
+ def test_method_two_args_as_expression
76
+ m = method(:method_two_args)
77
+ assert_equal 'def method_two_args(a, b); 42; end', m.as_expression
78
+ end
79
+
80
+ def method_rest_arg(*rest)
81
+ 42
82
+ end
83
+
84
+ def test_method_rest_arg_as_expression
85
+ m = method(:method_rest_arg)
86
+ assert_equal 'def method_rest_arg(*rest); 42; end', m.as_expression
87
+ end
88
+
89
+ def method_block_arg(&block)
90
+ 42
91
+ end
92
+
93
+ def test_method_block_arg_as_expression
94
+ m = method(:method_block_arg)
95
+ assert_equal 'def method_block_arg(&block); 42; end', m.as_expression
96
+ end
97
+
98
+ def method_rest_and_block_arg(*rest, &block)
99
+ 42
100
+ end
101
+
102
+ def test_method_rest_and_block_arg_as_expression
103
+ m = method(:method_rest_and_block_arg)
104
+ assert_equal 'def method_rest_and_block_arg(*rest, &block); 42; end', m.as_expression
105
+ end
106
+
107
+ def method_two_args_and_rest_and_block_arg(a, b, *rest, &block)
108
+ 42
109
+ end
110
+
111
+ def test_method_two_args_and_rest_and_block_arg_as_expression
112
+ m = method(:method_two_args_and_rest_and_block_arg)
113
+ assert_equal 'def method_two_args_and_rest_and_block_arg(a, b, *rest, &block); 42; end', m.as_expression
114
+ end
115
+
116
+ def method_two_default_args(a = 1, b = 2)
117
+ 42
118
+ end
119
+
120
+ def test_method_two_default_args_as_expression
121
+ m = method(:method_two_default_args)
122
+ assert_equal('def method_two_default_args(a=1, b=2); 42; end', m.as_expression)
123
+ end
124
+
125
+ def method_with_body(a, b)
126
+ a + b
127
+ end
128
+
129
+ def test_method_with_body_as_expression
130
+ m = method(:method_with_body)
131
+ assert_equal 'def method_with_body(a, b); a + b; end', m.as_expression
132
+ end
133
+
134
+ def test_proc_no_args_as_expression
135
+ p = proc { }
136
+ assert_equal 'proc { }', p.as_expression
137
+ end
138
+
139
+ def test_proc_empty_args_as_expression
140
+ if not defined?(VM) then
141
+ # indistinguishable from proc { } on YARV
142
+ p = proc { || }
143
+ assert_equal 'proc { || }', p.as_expression
144
+ end
145
+ end
146
+
147
+ def test_proc_one_arg_as_expression
148
+ p = proc { |a| }
149
+ assert_equal 'proc { |a| }', p.as_expression
150
+ end
151
+
152
+ def test_proc_one_array_arg_as_expression
153
+ p = proc { |a,| }
154
+ assert_equal 'proc { |a,| }', p.as_expression
155
+ end
156
+
157
+ def test_proc_two_args_as_expression
158
+ p = proc { |a, b| }
159
+ assert_equal 'proc { |a, b| }', p.as_expression
160
+ end
161
+
162
+ def test_proc_rest_arg_as_expression
163
+ p = proc { |*rest| }
164
+ assert_equal 'proc { |*rest| }', p.as_expression
165
+ end
166
+
167
+ def test_proc_two_args_and_rest_arg_as_expression
168
+ p = proc { |a, b, *rest| }
169
+ assert_equal 'proc { |a, b, *rest| }', p.as_expression
170
+ end
171
+
172
+ def test_proc_with_body_as_expression
173
+ p = proc { |a, b| a + b }
174
+ assert_equal 'proc { |a, b| a + b }', p.as_expression
175
+ end
176
+
177
+ def setup
178
+ @foo = 42
179
+ end
180
+
181
+ # 1.7 and later
182
+ def foo(*args)
183
+ return args
184
+ end
185
+
186
+ def remove_foo
187
+ self.class.remove_foo
188
+ end
189
+
190
+ # 1.6
191
+ def self.foo(*args)
192
+ return args
193
+ end
194
+
195
+ def self.remove_foo
196
+ self.class_eval { remove_const(:FOO) if const_defined?(:FOO) }
197
+ end
198
+
199
+ @@foo = 10
200
+
201
+ FOO = 57
202
+ end
203
+
204
+ if __FILE__ == $0 then
205
+ if Test::Unit.const_defined?(:AutoRunner) then
206
+ exit Test::Unit::AutoRunner.run
207
+ else
208
+ if ARGV.empty? then
209
+ suite = TC_As_Expression.suite
210
+ else
211
+ suite = Test::Unit::TestSuite.new('TC_As_Expression')
212
+ TC_As_Expression.suite.tests.each do |test|
213
+ ARGV.each do |arg|
214
+ suite << test if /#{arg}/ =~ test.name
215
+ end
216
+ end
217
+ end
218
+ result = Test::Unit::UI::Console::TestRunner.run(suite)
219
+ exit(result.error_count + result.failure_count)
220
+ end
221
+ end
222
+