Skip to content

Commit

Permalink
Merge branch 'ruby:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
el841 authored Mar 8, 2024
2 parents 6c4b850 + 86ce78d commit e18684d
Show file tree
Hide file tree
Showing 26 changed files with 1,021 additions and 314 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,32 @@ jobs:
make:
strategy:
matrix:
os: [ubuntu-22.04, ubuntu-20.04]
test_task: [check]
arch: ['']
configure: ['cppflags=-DVM_CHECK_MODE']
# specifying other jobs with `include` to avoid redundant tests
include:
- test_task: check
arch: i686
os: ubuntu-22.04
- test_task: check
configure: '--disable-yjit'
os: ubuntu-22.04
- test_task: check
configure: '--enable-shared --enable-load-relative'
os: ubuntu-22.04
- test_task: test-bundler-parallel
os: ubuntu-22.04
- test_task: test-bundled-gems
os: ubuntu-22.04
fail-fast: false

env:
GITPULLOPTIONS: --no-tags origin ${{ github.ref }}
RUBY_DEBUG: ci

runs-on: ubuntu-20.04
runs-on: ${{ matrix.os }}

if: >-
${{!(false
Expand Down
55 changes: 45 additions & 10 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ module LibRubyParser # :nodoc:
# size_t -> :size_t
# void -> :void
#
def self.resolve_type(type)
def self.resolve_type(type, callbacks)
type = type.strip
type.end_with?("*") ? :pointer : type.delete_prefix("const ").to_sym

if !type.end_with?("*")
type.delete_prefix("const ").to_sym
else
type = type.delete_suffix("*").rstrip
callbacks.include?(type.to_sym) ? type.to_sym : :pointer
end
end

# Read through the given header file and find the declaration of each of the
# given functions. For each one, define a function with the same name and
# signature as the C function.
def self.load_exported_functions_from(header, *functions)
def self.load_exported_functions_from(header, *functions, callbacks)
File.foreach(File.expand_path("../../include/#{header}", __dir__)) do |line|
# We only want to attempt to load exported functions.
next unless line.start_with?("PRISM_EXPORTED_FUNCTION ")
Expand All @@ -55,24 +61,28 @@ def self.load_exported_functions_from(header, *functions)

# Resolve the type of the argument by dropping the name of the argument
# first if it is present.
arg_types.map! { |type| resolve_type(type.sub(/\w+$/, "")) }
arg_types.map! { |type| resolve_type(type.sub(/\w+$/, ""), callbacks) }

# Attach the function using the FFI library.
attach_function name, arg_types, resolve_type(return_type)
attach_function name, arg_types, resolve_type(return_type, [])
end

# If we didn't find all of the functions, raise an error.
raise "Could not find functions #{functions.inspect}" unless functions.empty?
end

callback :pm_parse_stream_fgets_t, [:pointer, :int, :pointer], :pointer

load_exported_functions_from(
"prism.h",
"pm_version",
"pm_serialize_parse",
"pm_serialize_parse_stream",
"pm_serialize_parse_comments",
"pm_serialize_lex",
"pm_serialize_parse_lex",
"pm_parse_success_p"
"pm_parse_success_p",
[:pm_parse_stream_fgets_t]
)

load_exported_functions_from(
Expand All @@ -81,7 +91,8 @@ def self.load_exported_functions_from(header, *functions)
"pm_buffer_init",
"pm_buffer_value",
"pm_buffer_length",
"pm_buffer_free"
"pm_buffer_free",
[]
)

load_exported_functions_from(
Expand All @@ -90,7 +101,8 @@ def self.load_exported_functions_from(header, *functions)
"pm_string_free",
"pm_string_source",
"pm_string_length",
"pm_string_sizeof"
"pm_string_sizeof",
[]
)

# This object represents a pm_buffer_t. We only use it as an opaque pointer,
Expand Down Expand Up @@ -215,13 +227,36 @@ def parse(code, **options)
end

# Mirror the Prism.parse_file API by using the serialization API. This uses
# native strings instead of Ruby strings because it allows us to use mmap when
# it is available.
# native strings instead of Ruby strings because it allows us to use mmap
# when it is available.
def parse_file(filepath, **options)
options[:filepath] = filepath
LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) }
end

# Mirror the Prism.parse_stream API by using the serialization API.
def parse_stream(stream, **options)
LibRubyParser::PrismBuffer.with do |buffer|
source = +""
callback = -> (string, size, _) {
raise "Expected size to be >= 0, got: #{size}" if size <= 0

if !(line = stream.gets(size - 1)).nil?
source << line
string.write_string("#{line}\x00", line.bytesize + 1)
end
}

# In the pm_serialize_parse_stream function it accepts a pointer to the
# IO object as a void* and then passes it through to the callback as the
# third argument, but it never touches it itself. As such, since we have
# access to the IO object already through the closure of the lambda, we
# can pass a null pointer here and not worry.
LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, dump_options(options))
Prism.load(source, buffer.read)
end
end

# Mirror the Prism.parse_comments API by using the serialization API.
def parse_comments(code, **options)
LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) }
Expand Down
114 changes: 80 additions & 34 deletions prism/extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,24 @@ parser_warnings(pm_parser_t *parser, rb_encoding *encoding, VALUE source) {
return warnings;
}

/**
* Create a new parse result from the given parser, value, encoding, and source.
*/
static VALUE
parse_result_create(pm_parser_t *parser, VALUE value, rb_encoding *encoding, VALUE source) {
VALUE result_argv[] = {
value,
parser_comments(parser, source),
parser_magic_comments(parser, source),
parser_data_loc(parser, source),
parser_errors(parser, encoding, source),
parser_warnings(parser, encoding, source),
source
};

return rb_class_new_instance(7, result_argv, rb_cPrismParseResult);
}

/******************************************************************************/
/* Lexing Ruby code */
/******************************************************************************/
Expand Down Expand Up @@ -610,19 +628,11 @@ parse_lex_input(pm_string_t *input, const pm_options_t *options, bool return_nod
value = parse_lex_data.tokens;
}

VALUE result_argv[] = {
value,
parser_comments(&parser, source),
parser_magic_comments(&parser, source),
parser_data_loc(&parser, source),
parser_errors(&parser, parse_lex_data.encoding, source),
parser_warnings(&parser, parse_lex_data.encoding, source),
source
};

VALUE result = parse_result_create(&parser, value, parse_lex_data.encoding, source);
pm_node_destroy(&parser, node);
pm_parser_free(&parser);
return rb_class_new_instance(7, result_argv, rb_cPrismParseResult);

return result;
}

/**
Expand Down Expand Up @@ -682,17 +692,8 @@ parse_input(pm_string_t *input, const pm_options_t *options) {
rb_encoding *encoding = rb_enc_find(parser.encoding->name);

VALUE source = pm_source_new(&parser, encoding);
VALUE result_argv[] = {
pm_ast_new(&parser, node, encoding, source),
parser_comments(&parser, source),
parser_magic_comments(&parser, source),
parser_data_loc(&parser, source),
parser_errors(&parser, encoding, source),
parser_warnings(&parser, encoding, source),
source
};

VALUE result = rb_class_new_instance(7, result_argv, rb_cPrismParseResult);
VALUE value = pm_ast_new(&parser, node, encoding, source);
VALUE result = parse_result_create(&parser, value, encoding, source) ;

pm_node_destroy(&parser, node);
pm_parser_free(&parser);
Expand Down Expand Up @@ -751,6 +752,60 @@ parse(int argc, VALUE *argv, VALUE self) {
return value;
}

/**
* An implementation of fgets that is suitable for use with Ruby IO objects.
*/
static char *
parse_stream_fgets(char *string, int size, void *stream) {
RUBY_ASSERT(size > 0);

VALUE line = rb_funcall((VALUE) stream, rb_intern("gets"), 1, INT2FIX(size - 1));
if (NIL_P(line)) {
return NULL;
}

const char *cstr = StringValueCStr(line);
size_t length = strlen(cstr);

memcpy(string, cstr, length);
string[length] = '\0';

return string;
}

/**
* call-seq:
* Prism::parse_stream(stream, **options) -> ParseResult
*
* Parse the given object that responds to `gets` and return a ParseResult
* instance. The options that are supported are the same as Prism::parse.
*/
static VALUE
parse_stream(int argc, VALUE *argv, VALUE self) {
VALUE stream;
VALUE keywords;
rb_scan_args(argc, argv, "1:", &stream, &keywords);

pm_options_t options = { 0 };
extract_options(&options, Qnil, keywords);

pm_parser_t parser;
pm_buffer_t buffer;

pm_node_t *node = pm_parse_stream(&parser, &buffer, (void *) stream, parse_stream_fgets, &options);
rb_encoding *encoding = rb_enc_find(parser.encoding->name);

VALUE source = pm_source_new(&parser, encoding);
VALUE value = pm_ast_new(&parser, node, encoding, source);
VALUE result = parse_result_create(&parser, value, encoding, source);

pm_node_destroy(&parser, node);
pm_buffer_free(&buffer);
pm_parser_free(&parser);

return result;
}

/**
* call-seq:
* Prism::parse_file(filepath, **options) -> ParseResult
Expand Down Expand Up @@ -992,26 +1047,16 @@ integer_parse(VALUE self, VALUE source) {
pm_integer_t integer = { 0 };
pm_integer_parse(&integer, PM_INTEGER_BASE_UNKNOWN, start, start + length);

VALUE number = UINT2NUM(integer.head.value);
size_t shift = 0;

for (pm_integer_word_t *node = integer.head.next; node != NULL; node = node->next) {
VALUE receiver = rb_funcall(UINT2NUM(node->value), rb_intern("<<"), 1, ULONG2NUM(++shift * 32));
number = rb_funcall(receiver, rb_intern("|"), 1, number);
}

if (integer.negative) number = rb_funcall(number, rb_intern("-@"), 0);

pm_buffer_t buffer = { 0 };
pm_integer_string(&buffer, &integer);

VALUE string = rb_str_new(pm_buffer_value(&buffer), pm_buffer_length(&buffer));
pm_buffer_free(&buffer);
pm_integer_free(&integer);

VALUE result = rb_ary_new_capa(2);
rb_ary_push(result, number);
rb_ary_push(result, pm_integer_new(&integer));
rb_ary_push(result, string);
pm_integer_free(&integer);

return result;
}
Expand Down Expand Up @@ -1271,6 +1316,7 @@ Init_prism(void) {
rb_define_singleton_method(rb_cPrism, "lex", lex, -1);
rb_define_singleton_method(rb_cPrism, "lex_file", lex_file, -1);
rb_define_singleton_method(rb_cPrism, "parse", parse, -1);
rb_define_singleton_method(rb_cPrism, "parse_stream", parse_stream, -1);
rb_define_singleton_method(rb_cPrism, "parse_file", parse_file, -1);
rb_define_singleton_method(rb_cPrism, "parse_comments", parse_comments, -1);
rb_define_singleton_method(rb_cPrism, "parse_file_comments", parse_file_comments, -1);
Expand Down
1 change: 1 addition & 0 deletions prism/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
VALUE pm_source_new(const pm_parser_t *parser, rb_encoding *encoding);
VALUE pm_token_new(const pm_parser_t *parser, const pm_token_t *token, rb_encoding *encoding, VALUE source);
VALUE pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encoding, VALUE source);
VALUE pm_integer_new(const pm_integer_t *integer);

void Init_prism_api_node(void);
void Init_prism_pack(void);
Expand Down
3 changes: 3 additions & 0 deletions prism/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ typedef struct pm_lex_mode {
* a tilde heredoc.
*/
size_t common_whitespace;

/** True if the previous token ended with a line continuation. */
bool line_continuation;
} heredoc;
} as;

Expand Down
Loading

0 comments on commit e18684d

Please sign in to comment.