Skip to content

Commit 9c7d15d

Browse files
committed
Define rb_encoding as const OnigEncodingType like MRI
* Save the loaded libtruffleruby so we can easily call to it from Ruby.
1 parent cdb0ba7 commit 9c7d15d

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

lib/cext/include/ruby/encoding.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ void RB_ENC_CODERANGE_SET(VALUE obj, int cr);
108108
#define ENC_CODERANGE_AND(a, b) RB_ENC_CODERANGE_AND(a, b)
109109
#define ENCODING_CODERANGE_SET(obj, encindex, cr) RB_ENCODING_CODERANGE_SET(obj, encindex, cr)
110110

111-
typedef struct rb_encoding {
112-
char *name;
113-
} rb_encoding;
111+
typedef const OnigEncodingType rb_encoding;
114112

115113
int rb_char_to_option_kcode(int c, int *option, int *kcode);
116114

lib/truffle/truffle/cext.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ module Truffle::CExt
7272
RUBY_ECONV_PARTIAL_INPUT = Encoding::Converter::PARTIAL_INPUT
7373
RUBY_ECONV_AFTER_OUTPUT = Encoding::Converter::AFTER_OUTPUT
7474

75+
SET_LIBTRUFFLERUBY = -> libtruffleruby do
76+
LIBTRUFFLERUBY = libtruffleruby
77+
end
78+
79+
def self.register_libtruffleruby(libtruffleruby)
80+
SET_LIBTRUFFLERUBY.call(libtruffleruby)
81+
end
82+
7583
def supported?
7684
Interop.mime_type_supported?('application/x-sulong-library')
7785
end

lib/truffle/truffle/cext_structs.rb

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def self.get_encoding_from_native(rbencoding_ptr)
430430
def initialize(encoding)
431431
@encoding = encoding
432432
@pointer = nil
433-
@name = nil
433+
@name = Truffle::CExt::LIBTRUFFLERUBY.RSTRING_PTR_IMPL(Primitive.cext_wrap(encoding.name))
434434
end
435435

436436
private
@@ -445,12 +445,11 @@ def polyglot_members(internal)
445445

446446
def polyglot_read_member(name)
447447
raise Truffle::Interop::UnknownIdentifierException unless name == 'name'
448-
@name or raise '@name not set'
448+
@name
449449
end
450450

451451
def polyglot_write_member(name, value)
452-
raise Truffle::Interop::UnknownIdentifierException unless name == 'name'
453-
@name = value
452+
raise Truffle::Interop::UnsupportedMessageException
454453
end
455454

456455
def polyglot_remove_member(name)
@@ -466,7 +465,7 @@ def polyglot_member_readable?(name)
466465
end
467466

468467
def polyglot_member_modifiable?(name)
469-
name == 'name'
468+
false
470469
end
471470

472471
def polyglot_member_removable?(name)
@@ -498,24 +497,15 @@ def polyglot_pointer?
498497
end
499498

500499
def polyglot_to_native
501-
name = @name or raise '@name not set'
502-
unless Truffle::Interop.pointer?(name)
503-
Truffle::Interop.to_native(name)
504-
raise "#{name.inspect} could not be converted to native" unless Truffle::Interop.pointer?(name)
505-
end
506-
name_address = Truffle::Interop.as_pointer(name)
507-
508500
ENCODING_CACHE_MUTEX.synchronize do
509501
unless @pointer
510-
@pointer = Truffle::FFI::MemoryPointer.new(:pointer, 1)
511-
@pointer.write_pointer name_address
512-
513-
NATIVE_CACHE[@pointer.address] = self
502+
@pointer = Truffle::CExt::LIBTRUFFLERUBY.rb_encoding_to_native(@name)
503+
NATIVE_CACHE[Truffle::Interop.as_pointer(@pointer)] = self
514504
end
515505
end
516506
end
517507

518508
def polyglot_as_pointer
519-
@pointer.address
509+
Truffle::Interop.as_pointer(@pointer)
520510
end
521511
end

src/main/c/cext/encoding.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33

44
// Encoding, rb_enc_*
55

6-
POLYGLOT_DECLARE_STRUCT(rb_encoding)
6+
POLYGLOT_DECLARE_TYPE(rb_encoding)
77

88
// returns Truffle::CExt::RbEncoding, takes Encoding or String
99
rb_encoding* rb_to_encoding(VALUE encoding) {
1010
encoding = RUBY_CEXT_INVOKE("rb_convert_to_encoding", encoding); // Convert to Encoding
11-
rb_encoding *enc = polyglot_as_rb_encoding(RUBY_CEXT_INVOKE_NO_WRAP("rb_to_encoding", encoding));
12-
enc->name = RSTRING_PTR(RUBY_INVOKE(encoding, "name"));
13-
return enc;
11+
return polyglot_as_rb_encoding(RUBY_CEXT_INVOKE_NO_WRAP("rb_to_encoding", encoding));
12+
}
13+
14+
rb_encoding* rb_encoding_to_native(char* name) {
15+
OnigEncodingType* native = calloc(1, sizeof(rb_encoding)); // calloc() to zero-fill
16+
native->name = name;
17+
return native;
1418
}
1519

1620
rb_encoding* rb_default_external_encoding(void) {

src/main/java/org/truffleruby/language/loader/FeatureLoader.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@
4242

4343
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4444
import com.oracle.truffle.api.TruffleFile;
45-
import com.oracle.truffle.api.interop.ArityException;
45+
import com.oracle.truffle.api.interop.InteropException;
4646
import com.oracle.truffle.api.interop.InteropLibrary;
47-
import com.oracle.truffle.api.interop.UnsupportedMessageException;
48-
import com.oracle.truffle.api.interop.UnsupportedTypeException;
4947
import com.oracle.truffle.api.object.DynamicObject;
5048
import com.oracle.truffle.api.profiles.ConditionProfile;
5149
import com.oracle.truffle.api.source.Source;
@@ -423,9 +421,13 @@ public void ensureCExtImplementationLoaded(String feature, RequireNode requireNo
423421
final Object initFunction = requireNode
424422
.findFunctionInLibrary(library, "rb_tr_init", rubyLibPath);
425423

424+
final InteropLibrary interop = InteropLibrary.getFactory().getUncached();
426425
try {
427-
InteropLibrary.getFactory().getUncached(initFunction).execute(initFunction, truffleCExt);
428-
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
426+
// rb_tr_init(Truffle::CExt)
427+
interop.execute(initFunction, truffleCExt);
428+
// Truffle::CExt.register_libtruffleruby(libtruffleruby)
429+
interop.invokeMember(truffleCExt, "register_libtruffleruby", library);
430+
} catch (InteropException e) {
429431
throw new JavaException(e);
430432
}
431433
} finally {

0 commit comments

Comments
 (0)