From 357ac0bb5acd4a0636eaface76a2c5ed09dacbd3 Mon Sep 17 00:00:00 2001 From: "Everton J. Carpes" Date: Mon, 7 Aug 2023 01:07:00 -0300 Subject: [PATCH] examples: show how to call a simple v module from ruby (#19073) (#19073) This is a copy/adaptation of the python example (#13105) --- cmd/tools/modules/testing/common.v | 1 + examples/call_v_from_ruby/README.md | 7 +++++++ examples/call_v_from_ruby/test.rb | 23 +++++++++++++++++++++++ examples/call_v_from_ruby/test.v | 13 +++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 examples/call_v_from_ruby/README.md create mode 100644 examples/call_v_from_ruby/test.rb create mode 100644 examples/call_v_from_ruby/test.v diff --git a/cmd/tools/modules/testing/common.v b/cmd/tools/modules/testing/common.v index 334571094d..c0c0ccfb7b 100644 --- a/cmd/tools/modules/testing/common.v +++ b/cmd/tools/modules/testing/common.v @@ -244,6 +244,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession { if testing.github_job != 'ubuntu-tcc' { skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases skip_files << 'examples/call_v_from_python/test.v' // the example only makes sense to be compiled, when python is installed + skip_files << 'examples/call_v_from_ruby/test.v' // the example only makes sense to be compiled, when ruby is installed // the ttf_test.v is not interactive, but needs X11 headers to be installed, which is done only on ubuntu-tcc for now skip_files << 'vlib/x/ttf/ttf_test.v' skip_files << 'vlib/vweb/vweb_app_test.v' // imports the `sqlite` module, which in turn includes sqlite3.h diff --git a/examples/call_v_from_ruby/README.md b/examples/call_v_from_ruby/README.md new file mode 100644 index 0000000000..4a0a43c8fa --- /dev/null +++ b/examples/call_v_from_ruby/README.md @@ -0,0 +1,7 @@ +A simple example to show how to call a function written in v from ruby + +Step 1: Compile the v code to a shared library using `v -d no_backtrace -shared test.v` + +Step 2: Run the ruby file using `ruby test.rb` + +Note: you do not need `-d no_backtrace` if you use gcc or clang . diff --git a/examples/call_v_from_ruby/test.rb b/examples/call_v_from_ruby/test.rb new file mode 100644 index 0000000000..6ea33748a8 --- /dev/null +++ b/examples/call_v_from_ruby/test.rb @@ -0,0 +1,23 @@ +require 'bundler/inline' + +gemfile do + source 'https://rubygems.org' + gem 'ffi' +end + +require 'ffi' + +module Lib + extend FFI::Library + + ffi_lib File.join(File.dirname(__FILE__), 'test.so') + + attach_function :square, [:int], :int + attach_function :sqrt_of_sum_of_squares, [:double, :double], :double +end + +puts "Lib.square(10) result is #{Lib.square(10)}" +raise 'Cannot validate V square().' unless Lib.square(10) == 100 + +raise 'Cannot validate V sqrt_of_sum_of_squares().' unless \ + Lib.sqrt_of_sum_of_squares(1.1, 2.2) == Math.sqrt(1.1*1.1 + 2.2*2.2) diff --git a/examples/call_v_from_ruby/test.v b/examples/call_v_from_ruby/test.v new file mode 100644 index 0000000000..87d7e92230 --- /dev/null +++ b/examples/call_v_from_ruby/test.v @@ -0,0 +1,13 @@ +module test + +import math + +[export: 'square'] +fn square(i int) int { + return i * i +} + +[export: 'sqrt_of_sum_of_squares'] +fn sqrt_of_sum_of_squares(x f64, y f64) f64 { + return math.sqrt(x * x + y * y) +}