Skip to content

Commit 1858a8e

Browse files
committed
Add specs for ruby_vm_at_exit()
1 parent 6510efc commit 1858a8e

2 files changed

Lines changed: 17 additions & 0 deletions

File tree

optional/capi/ext/kernel_spec.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ruby.h"
2+
#include "ruby/vm.h"
23
#include "rubyspec.h"
34

45
#include <errno.h>
@@ -337,6 +338,15 @@ static VALUE kernel_spec_rb_set_end_proc(VALUE self, VALUE io) {
337338
return Qnil;
338339
}
339340

341+
static void at_exit_hook(ruby_vm_t *vm) {
342+
puts("ruby_vm_at_exit hook ran");
343+
}
344+
345+
static VALUE kernel_spec_ruby_vm_at_exit(VALUE self) {
346+
ruby_vm_at_exit(at_exit_hook);
347+
return self;
348+
}
349+
340350
static VALUE kernel_spec_rb_f_sprintf(VALUE self, VALUE ary) {
341351
return rb_f_sprintf((int)RARRAY_LEN(ary), RARRAY_PTR(ary));
342352
}
@@ -434,6 +444,7 @@ void Init_kernel_spec(void) {
434444
rb_define_method(cls, "rb_yield_splat", kernel_spec_rb_yield_splat, 1);
435445
rb_define_method(cls, "rb_exec_recursive", kernel_spec_rb_exec_recursive, 1);
436446
rb_define_method(cls, "rb_set_end_proc", kernel_spec_rb_set_end_proc, 1);
447+
rb_define_method(cls, "ruby_vm_at_exit", kernel_spec_ruby_vm_at_exit, 0);
437448
rb_define_method(cls, "rb_f_sprintf", kernel_spec_rb_f_sprintf, 1);
438449
rb_define_method(cls, "rb_str_format", kernel_spec_rb_str_format, 3);
439450
rb_define_method(cls, "rb_make_backtrace", kernel_spec_rb_make_backtrace, 0);

optional/capi/kernel_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,12 @@ def proc_caller
703703
end
704704
end
705705

706+
describe "ruby_vm_at_exit" do
707+
it "runs a C function after the VM is terminated" do
708+
ruby_exe("require #{kernel_path.inspect}; CApiKernelSpecs.new.ruby_vm_at_exit").should == "ruby_vm_at_exit hook ran\n"
709+
end
710+
end
711+
706712
describe "rb_f_sprintf" do
707713
it "returns a string according to format and arguments" do
708714
@s.rb_f_sprintf(["%d %f %s", 10, 2.5, "test"]).should == "10 2.500000 test"

0 commit comments

Comments
 (0)