Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -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 .
|
||||
@@ -0,0 +1,39 @@
|
||||
require 'bundler/inline'
|
||||
|
||||
gemfile do
|
||||
source 'https://rubygems.org'
|
||||
gem 'ffi'
|
||||
end
|
||||
|
||||
require 'ffi'
|
||||
|
||||
# extension for shared libraries varies by platform - see vlib/dl/dl.v
|
||||
# get_shared_library_extension()
|
||||
def shared_library_extension
|
||||
if Gem.win_platform?
|
||||
'.dll'
|
||||
elsif RUBY_PLATFORM =~ /darwin/ # MacOS
|
||||
'.dylib'
|
||||
else
|
||||
'.so'
|
||||
end
|
||||
end
|
||||
|
||||
module Lib
|
||||
extend FFI::Library
|
||||
|
||||
begin
|
||||
ffi_lib File.join(File.dirname(__FILE__), 'test' + shared_library_extension)
|
||||
rescue LoadError
|
||||
abort("No shared library test#{shared_library_extension} found. Check examples/call_v_from_ruby/README.md")
|
||||
end
|
||||
|
||||
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)
|
||||
@@ -0,0 +1,14 @@
|
||||
// vtest build: present_ruby? // the example only makes sense to be compiled, when ruby is installed
|
||||
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)
|
||||
}
|
||||
Reference in new issue
Block a user