From 149658c42b11b4b75851c366623f6b04793d2a44 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 25 Jan 2024 09:49:33 -0800 Subject: [PATCH] Check for rb_enc_interned_str_cstr and fall back Truffle Ruby doesn't implement this function yet, so we need to check for it and then fall back if it's not there. --- ext/sqlite3/extconf.rb | 4 ++++ ext/sqlite3/statement.c | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ext/sqlite3/extconf.rb b/ext/sqlite3/extconf.rb index 733e22d5..c648d9e9 100644 --- a/ext/sqlite3/extconf.rb +++ b/ext/sqlite3/extconf.rb @@ -109,6 +109,10 @@ def configure_extension abort_could_not_find(libname) unless find_library(libname, "sqlite3_libversion_number", "sqlite3.h") + # Truffle Ruby doesn't support this yet: + # https://github.com/oracle/truffleruby/issues/3408 + have_func("rb_enc_interned_str_cstr") + # Functions defined in 1.9 but not 1.8 have_func("rb_proc_arity") diff --git a/ext/sqlite3/statement.c b/ext/sqlite3/statement.c index b8511bf8..37d6c374 100644 --- a/ext/sqlite3/statement.c +++ b/ext/sqlite3/statement.c @@ -364,6 +364,23 @@ column_count(VALUE self) return INT2NUM(sqlite3_column_count(ctx->st)); } +#if HAVE_RB_ENC_INTERNED_STR_CSTR +static VALUE +interned_utf8_cstr(const char * str) +{ + return rb_enc_interned_str_cstr(str, rb_utf8_encoding()); +} +#else +static VALUE +interned_utf8_cstr(const char * str) +{ + VALUE rb_str = rb_utf8_str_new_cstr(str); + rb_obj_freeze(rb_str); + rb_funcall(rb_str, rb_intern("-@"), 0); + return rb_str; +} +#endif + /* call-seq: stmt.column_name(index) * * Get the column name at +index+. 0 based. @@ -382,7 +399,7 @@ column_name(VALUE self, VALUE index) VALUE ret = Qnil; if (name) { - ret = rb_enc_interned_str_cstr(name, rb_utf8_encoding()); + ret = interned_utf8_cstr(name); } return ret; }