Commit 8379ca6e

8379ca6e8502Merged

Sam Ruby <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

test(ffi): regression test for integer literal in :ptr arg slot

The codegen arm for non-poly :ptr (compile_ffi_func_call) emits ((void *)<expr>), which makes integer literals usable as pointer values at FFI call sites. This is the wire-shape that lets callers pass sentinel pointer values like SQLITE_TRANSIENT (((sqlite3_destructor_type)-1)) from Ruby as the literal -1 — the same idiom ruby-ffi exposes via FFI::Pointer.new(-1).

The capability already works end-to-end. A round-trip spike against sqlite3_bind_text(stmt, idx, "hello", 5, -1) + step + column_text returns "hello" correctly; generated C is ((void *)-1LL), which sqlite3 recognizes as SQLITE_TRANSIENT and copies the string immediately. Test uses free(0) instead of sqlite3 to keep the regression coverage independent of libsqlite3-dev availability in CI — the meaningful regression is the codegen primitive, not the sqlite path.

Updates examples/ffi/sqlite/sqlite3_lib.rb to reflect that bind_text with SQLITE_TRANSIENT is supported today, pointing at the new test for the underlying primitive.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
8379ca6e8502
Parent
f0063046556d

examples/ffi/sqlite/sqlite3_lib.rbmodifiedView file

@@ -5,10 +5,12 @@
55 # - exec for DDL + INSERT/UPDATE/DELETE
66 # - prepare_v2 / step / finalize + column accessors for SELECT
77 #
8-# Strings are passed inline into SQL; bind_text would also work but
9-# requires the SQLITE_TRANSIENT destructor sentinel which the Spinel FFI
10-# can't construct as a `:ptr` value yet. For string columns we escape
11-# single quotes ourselves (`Sql.q`).
8+# Strings are passed inline into SQL: `Sql.q` doubles single quotes
9+# to handle the demo's well-known inputs. `bind_text` is also
10+# supported — pass `-1` as the destructor argument for
11+# `SQLITE_TRANSIENT` (`((sqlite3_destructor_type)-1)`), the same
12+# wire-shape ruby-ffi exposes via `FFI::Pointer.new(-1)`. See
13+# test/ffi_ptr_int_literal.rb for the underlying primitive.
1214 module SQL
1315 ffi_lib "sqlite3"
1416