From 2f5eb6ab368b062dbbde39b3cee6eae23c5452ff Mon Sep 17 00:00:00 2001 From: Amir Rajan Date: Mon, 6 Sep 2021 14:32:04 -0500 Subject: Synced with version 2.26 --- samples/12_c_extensions/README.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'samples/12_c_extensions/README.md') diff --git a/samples/12_c_extensions/README.md b/samples/12_c_extensions/README.md index 8950e53..aef3864 100644 --- a/samples/12_c_extensions/README.md +++ b/samples/12_c_extensions/README.md @@ -350,6 +350,34 @@ dragonruby-bind --ffi-module=CoolStuff bridge.c Then one can use `include FFI::CoolStuff` instead. +### Type checks + +C extensions expect the right types in the right place! + +Given the following C code: + +```c +void take_int(int x) { ... } +void take_struct(struct S) { ... } +``` + +the next calls from the Ruby side + +```ruby +take_int(15.0) +take_struct(42) +``` + +may not work as you would expect. +In the case of `take_int`, you'll likely see some garbage instead of "expected" `15`. +The call to `take_struct` will likely crash. + +To prevent this from happening, `dragonruby-bind` emits code that does type checking: +if you use the wrong types DragonRuby will throw an exception. + +If the type checking takes CPU cycles out of your game (or if you feel brave) you can +disable type checks via `--no-typecheck` CLI argument when emitting C bindings. + ### Pitfalls There is no so-called marshalling when it comes to structs. When you read or -- cgit v1.2.3