Remove version templating in YARP

This commit is contained in:
Kevin Newton 2023-08-25 17:54:01 -04:00
parent aea7e91828
commit ca9a44795b
Notes: git 2023-08-25 22:21:12 +00:00
12 changed files with 18 additions and 146 deletions

View File

@ -533,7 +533,6 @@ require_relative "yarp/node"
require_relative "yarp/ripper_compat"
require_relative "yarp/serialize"
require_relative "yarp/pack"
require_relative "yarp/version"
if RUBY_ENGINE == "ruby" and !ENV["YARP_FFI_BACKEND"]
require "yarp/yarp"

View File

@ -172,10 +172,8 @@ module YARP
# the YARP module.
private_constant :LibRubyParser
library_version = LibRubyParser.yp_version.read_string
if library_version != YARP::VERSION
raise "The YARP library version (#{library_version}) does not match the expected version (#{YARP::VERSION})"
end
# The version constant is set by reading the result of calling yp_version.
VERSION = LibRubyParser.yp_version.read_string
def self.dump_internal(source, source_size, filepath)
LibRubyParser::YPBuffer.with do |buffer|

View File

@ -1,16 +1,8 @@
# frozen_string_literal: true
if File.exist?(File.expand_path("version.rb", __dir__))
# CRuby
require_relative "version"
else
# Within the gem/local repository
require_relative "lib/yarp/version"
end
Gem::Specification.new do |spec|
spec.name = "yarp"
spec.version = YARP::VERSION
spec.version = "0.8.0"
spec.authors = ["Shopify"]
spec.email = ["ruby@shopify.com"]
@ -75,7 +67,6 @@ Gem::Specification.new do |spec|
"lib/yarp/pack.rb",
"lib/yarp/ripper_compat.rb",
"lib/yarp/serialize.rb",
"lib/yarp/version.rb",
"src/diagnostic.c",
"src/enc/yp_big5.c",
"src/enc/yp_euc_jp.c",

View File

@ -1,100 +0,0 @@
# frozen_string_literal: true
require "yarp"
require "ripper"
require "pp"
require "test/unit"
require "tempfile"
puts "Using YARP backend: #{YARP::BACKEND}" if ENV["YARP_FFI_BACKEND"]
module YARP
module Assertions
private
def assert_equal_nodes(expected, actual, compare_location: true, parent: nil)
assert_equal expected.class, actual.class
case expected
when Array
assert_equal(
expected.size,
actual.size,
-> { "Arrays were different sizes. Parent: #{parent.pretty_inspect}" }
)
expected.zip(actual).each do |(expected_element, actual_element)|
assert_equal_nodes(
expected_element,
actual_element,
compare_location: compare_location,
parent: actual
)
end
when YARP::SourceFileNode
deconstructed_expected = expected.deconstruct_keys(nil)
deconstructed_actual = actual.deconstruct_keys(nil)
assert_equal deconstructed_expected.keys, deconstructed_actual.keys
# Filepaths can be different if test suites were run
# on different machines.
# We accommodate for this by comparing the basenames,
# and not the absolute filepaths
assert_equal deconstructed_expected.except(:filepath), deconstructed_actual.except(:filepath)
assert_equal File.basename(deconstructed_expected[:filepath]), File.basename(deconstructed_actual[:filepath])
when YARP::Node
deconstructed_expected = expected.deconstruct_keys(nil)
deconstructed_actual = actual.deconstruct_keys(nil)
assert_equal deconstructed_expected.keys, deconstructed_actual.keys
deconstructed_expected.each_key do |key|
assert_equal_nodes(
deconstructed_expected[key],
deconstructed_actual[key],
compare_location: compare_location,
parent: actual
)
end
when YARP::Location
assert_operator actual.start_offset, :<=, actual.end_offset, -> {
"start_offset > end_offset for #{actual.inspect}, parent is #{parent.pretty_inspect}"
}
if compare_location
assert_equal(
expected.start_offset,
actual.start_offset,
-> { "Start locations were different. Parent: #{parent.pretty_inspect}" }
)
assert_equal(
expected.end_offset,
actual.end_offset,
-> { "End locations were different. Parent: #{parent.pretty_inspect}" }
)
end
else
assert_equal expected, actual
end
end
def assert_valid_locations(value, parent: nil)
case value
when Array
value.each do |element|
assert_valid_locations(element, parent: value)
end
when YARP::Node
value.deconstruct_keys(nil).each_value do |field|
assert_valid_locations(field, parent: value)
end
when YARP::Location
assert_operator value.start_offset, :<=, value.end_offset, -> {
"start_offset > end_offset for #{value.inspect}, parent is #{parent.pretty_inspect}"
}
end
end
end
end
Test::Unit::TestCase.include(YARP::Assertions)

View File

@ -1 +0,0 @@
#include "ruby/config.h"

View File

@ -545,12 +545,12 @@ RUBY_FUNC_EXPORTED void
Init_yarp(void) {
// Make sure that the YARP library version matches the expected version.
// Otherwise something was compiled incorrectly.
if (strcmp(yp_version(), YP_VERSION) != 0) {
if (strcmp(yp_version(), EXPECTED_YARP_VERSION) != 0) {
rb_raise(
rb_eRuntimeError,
"The YARP library version (%s) does not match the expected version (%s)",
yp_version(),
YP_VERSION
EXPECTED_YARP_VERSION
);
}
@ -566,6 +566,9 @@ Init_yarp(void) {
rb_cYARPParseWarning = rb_define_class_under(rb_cYARP, "ParseWarning", rb_cObject);
rb_cYARPParseResult = rb_define_class_under(rb_cYARP, "ParseResult", rb_cObject);
// Define the version string here so that we can use the constants defined
// in yarp.h.
rb_define_const(rb_cYARP, "VERSION", rb_str_new2(EXPECTED_YARP_VERSION));
rb_define_const(rb_cYARP, "BACKEND", ID2SYM(rb_intern("CExtension")));
// First, the functions that have to do with lexing and parsing.

View File

@ -1,6 +1,8 @@
#ifndef YARP_EXT_NODE_H
#define YARP_EXT_NODE_H
#define EXPECTED_YARP_VERSION "0.8.0"
#include <ruby.h>
#include <ruby/encoding.h>
#include "yarp.h"

View File

@ -1,9 +0,0 @@
#ifndef YARP_VERSION_H
#define YARP_VERSION_H
#define YP_VERSION_MAJOR <%= YARP_VERSION_MAJOR %>
#define YP_VERSION_MINOR <%= YARP_VERSION_MINOR %>
#define YP_VERSION_PATCH <%= YARP_VERSION_PATCH %>
#define YP_VERSION "<%= YARP_VERSION %>"
#endif // YARP_VERSION_H

View File

@ -59,9 +59,9 @@ public class Loader {
expect((byte) 'R');
expect((byte) 'P');
expect((byte) <%= YARP_VERSION_MAJOR %>);
expect((byte) <%= YARP_VERSION_MINOR %>);
expect((byte) <%= YARP_VERSION_PATCH %>);
expect((byte) 0);
expect((byte) 8);
expect((byte) 0);
// This loads the name of the encoding. We don't actually do anything
// with it just yet.

View File

@ -13,6 +13,10 @@ end
module YARP
module Serialize
MAJOR_VERSION = 0
MINOR_VERSION = 8
PATCH_VERSION = 0
def self.load(input, serialized)
Loader.new(Source.new(input), serialized).load_result
end
@ -78,9 +82,7 @@ module YARP
def load_nodes
raise "Invalid serialization" if io.read(4) != "YARP"
if io.read(3).unpack("C3") != [<%= YARP_VERSION_MAJOR %>, <%= YARP_VERSION_MINOR %>, <%= YARP_VERSION_PATCH %>]
raise "Invalid serialization version"
end
raise "Invalid serialization" if io.read(3).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION]
@encoding = load_encoding
@input = input.force_encoding(@encoding).freeze

View File

@ -4,17 +4,6 @@ require "erb"
require "fileutils"
require "yaml"
if File.exist?(File.expand_path("../lib/yarp/version.rb", __dir__))
# Within the gem/local repository
require_relative "../lib/yarp/version"
else
# Within CRuby
require_relative "../../lib/yarp/version"
end
YARP_VERSION = YARP::VERSION
YARP_VERSION_MAJOR, YARP_VERSION_MINOR, YARP_VERSION_PATCH = YARP_VERSION.split(".")
COMMON_FLAGS = 1
class Param
@ -323,7 +312,6 @@ end
TEMPLATES = [
"ext/yarp/api_node.c",
"include/yarp/ast.h",
"include/yarp/version.h",
"java/org/yarp/Loader.java",
"java/org/yarp/Nodes.java",
"java/org/yarp/AbstractNodeVisitor.java",

View File

@ -1,5 +1,4 @@
#include "yarp.h"
#include "yarp/version.h"
// The YARP version and the serialization format.
const char *