summaryrefslogtreecommitdiff
path: root/yarp
diff options
context:
space:
mode:
authorVinicius Stock <vinicius.stock@shopify.com>2023-09-06 15:21:07 -0400
committergit <svn-admin@ruby-lang.org>2023-09-06 19:50:21 +0000
commitacd626a58345247a2d98c3cff1233008a6e81c61 (patch)
treedcc737a6f63b0946bd035e8c1c31d48438ae5c42 /yarp
parentf1422e4cecdbff12148b4b94e1f00646251f2dae (diff)
[ruby/yarp] Consider source encoding for `slice`
https://github.com/ruby/yarp/commit/8f59fc27cd Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
Diffstat (limited to 'yarp')
-rw-r--r--yarp/extension.c2
-rw-r--r--yarp/extension.h2
-rw-r--r--yarp/templates/ext/yarp/api_node.c.erb6
-rw-r--r--yarp/templates/lib/yarp/serialize.rb.erb8
4 files changed, 12 insertions, 6 deletions
diff --git a/yarp/extension.c b/yarp/extension.c
index de925f1509..8d36cd4427 100644
--- a/yarp/extension.c
+++ b/yarp/extension.c
@@ -347,7 +347,7 @@ parse_input(yp_string_t *input, const char *filepath) {
yp_node_t *node = yp_parse(&parser);
rb_encoding *encoding = rb_enc_find(parser.encoding.name);
- VALUE source = yp_source_new(&parser);
+ VALUE source = yp_source_new(&parser, encoding);
VALUE result_argv[] = {
yp_ast_new(&parser, node, encoding),
parser_comments(&parser, source),
diff --git a/yarp/extension.h b/yarp/extension.h
index ae7db77ca0..ccfb6f9454 100644
--- a/yarp/extension.h
+++ b/yarp/extension.h
@@ -7,7 +7,7 @@
#include <ruby/encoding.h>
#include "yarp.h"
-VALUE yp_source_new(yp_parser_t *parser);
+VALUE yp_source_new(yp_parser_t *parser, rb_encoding *encoding);
VALUE yp_token_new(yp_parser_t *parser, yp_token_t *token, rb_encoding *encoding, VALUE source);
VALUE yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding);
diff --git a/yarp/templates/ext/yarp/api_node.c.erb b/yarp/templates/ext/yarp/api_node.c.erb
index 8fb2d2e507..0d075112c8 100644
--- a/yarp/templates/ext/yarp/api_node.c.erb
+++ b/yarp/templates/ext/yarp/api_node.c.erb
@@ -38,8 +38,8 @@ yp_string_new(yp_string_t *string, rb_encoding *encoding) {
// Create a YARP::Source object from the given parser.
VALUE
-yp_source_new(yp_parser_t *parser) {
- VALUE source = rb_str_new((const char *) parser->start, parser->end - parser->start);
+yp_source_new(yp_parser_t *parser, rb_encoding *encoding) {
+ VALUE source = rb_enc_str_new((const char *) parser->start, parser->end - parser->start, encoding);
VALUE offsets = rb_ary_new_capa(parser->newline_list.size);
for (size_t index = 0; index < parser->newline_list.size; index++) {
@@ -78,7 +78,7 @@ yp_node_stack_pop(yp_node_stack_node_t **stack) {
VALUE
yp_ast_new(yp_parser_t *parser, yp_node_t *node, rb_encoding *encoding) {
- VALUE source = yp_source_new(parser);
+ VALUE source = yp_source_new(parser, encoding);
ID *constants = calloc(parser->constant_pool.size, sizeof(ID));
for (size_t index = 0; index < parser->constant_pool.capacity; index++) {
diff --git a/yarp/templates/lib/yarp/serialize.rb.erb b/yarp/templates/lib/yarp/serialize.rb.erb
index 2f6d6421a9..c8d7f422cd 100644
--- a/yarp/templates/lib/yarp/serialize.rb.erb
+++ b/yarp/templates/lib/yarp/serialize.rb.erb
@@ -18,7 +18,13 @@ module YARP
PATCH_VERSION = 0
def self.load(input, serialized)
- Loader.new(Source.new(input), serialized).load_result
+ input = input.dup
+ source = Source.new(input)
+ loader = Loader.new(source, serialized)
+ result = loader.load_result
+
+ input.force_encoding(loader.encoding)
+ result
end
def self.load_tokens(source, serialized)