forked from ruby/ruby.wasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm_deep_call.rb
55 lines (47 loc) · 1.39 KB
/
vm_deep_call.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# This script checks the max number of call frames under WebAssembly restriction
#
# Example runs
# $ ruby vm_deep_call.rb
# $ RUBY_EXE="wasmtime run --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
# $ RUBY_EXE="wasmtime run --env RUBY_FIBER_MACHINE_STACK_SIZE=20971520 --dir /::/ head-wasm32-unknown-wasi-minimal/usr/local/bin/ruby --" ruby vm_deep_call.rb
def vm_rec n
vm_rec n - 1 if n > 0
end
def vm_c_rec n
1.times do
vm_c_rec n - 1 if n > 0
end
end
def vm_rec_fiber n
Fiber.new { vm_rec n }.resume
end
def vm_c_rec_fiber n
Fiber.new { vm_c_rec n }.resume
end
def check(ruby_exe, target, n)
cmd = %Q(#{ruby_exe} -r #{File.expand_path(__FILE__)} -e "#{target}(#{n})")
Kernel.system(cmd, err: File::NULL)
end
def bisect(ruby_exe, target)
min = 0
max = 15000
while min < max
mid = (min + max) / 2
ok = check(ruby_exe, target, mid)
if ok
min = mid + 1
else
max = mid
end
end
min
end
def main
ruby_exe = ENV['RUBY_EXE'] || 'ruby'
puts "How deep the call stack can be"
puts " with only VM calls: " + bisect(ruby_exe, "vm_rec").to_s
puts " with only VM calls in Fiber: " + bisect(ruby_exe, "vm_rec_fiber").to_s
puts " with VM and C calls: " + bisect(ruby_exe, "vm_c_rec").to_s
puts " with VM and C calls in Fiber: " + bisect(ruby_exe, "vm_c_rec_fiber").to_s
end
main if $0 == __FILE__