[ruby] should initialize static variables inside %init{}, in which it will not be

excuted concurrently by multiple threads.
This commit is contained in:
Takashi Tamura 2017-02-22 12:25:27 +09:00
commit 04a7c4f8b8
2 changed files with 11 additions and 3 deletions

View file

@ -8,7 +8,7 @@
SWIGINTERN int
SWIG_AsWCharPtrAndSize(VALUE obj, wchar_t **cptr, size_t *psize, int *alloc)
{
static rb_encoding* wstr_enc = rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_WSTRING_ENCODING ));
rb_encoding* wstr_enc = swig_ruby_wstring_encoding;
if (TYPE(obj) == T_STRING) {
VALUE rstr = rb_str_conv_enc(obj, rb_enc_get(obj), wstr_enc);
@ -37,8 +37,8 @@ SWIG_AsWCharPtrAndSize(VALUE obj, wchar_t **cptr, size_t *psize, int *alloc)
SWIGINTERNINLINE VALUE
SWIG_FromWCharPtrAndSize(const wchar_t * carray, size_t size)
{
static rb_encoding* wstr_enc = rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_WSTRING_ENCODING ));
static rb_encoding* rb_enc = rb_to_encoding(rb_str_new_cstr( SWIG_RUBY_INTERNAL_ENCODING ));
rb_encoding* wstr_enc = swig_ruby_wstring_encoding;
rb_encoding* rb_enc = swig_ruby_internal_encoding;
if (carray && size <= LONG_MAX/sizeof(wchar_t)) {
VALUE rstr = rb_str_new( (const char*)carray, %numeric_cast(size*sizeof(wchar_t),long) );

View file

@ -32,11 +32,19 @@ extern "C" {
#define SWIG_RUBY_INTERNAL_ENCODING "UTF-8"
#endif
static rb_encoding *swig_ruby_wstring_encoding;
static rb_encoding *swig_ruby_internal_encoding;
#ifdef __cplusplus
}
#endif
%}
%init{
swig_ruby_wstring_encoding = rb_enc_find( SWIG_RUBY_WSTRING_ENCODING );
swig_ruby_internal_encoding = rb_enc_find( SWIG_RUBY_INTERNAL_ENCODING );
}
%include <rubywstrings.swg>
%include <typemaps/std_wstring.swg>