crystalruby 0.2.3 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -0
  3. data/Dockerfile +23 -2
  4. data/README.md +395 -198
  5. data/Rakefile +4 -3
  6. data/crystalruby.gemspec +2 -2
  7. data/examples/adder/adder.rb +1 -1
  8. data/exe/crystalruby +1 -0
  9. data/lib/crystalruby/adapter.rb +143 -73
  10. data/lib/crystalruby/arc_mutex.rb +47 -0
  11. data/lib/crystalruby/compilation.rb +32 -3
  12. data/lib/crystalruby/config.rb +41 -37
  13. data/lib/crystalruby/function.rb +216 -73
  14. data/lib/crystalruby/library.rb +157 -51
  15. data/lib/crystalruby/reactor.rb +63 -44
  16. data/lib/crystalruby/source_reader.rb +92 -0
  17. data/lib/crystalruby/template.rb +16 -5
  18. data/lib/crystalruby/templates/function.cr +11 -10
  19. data/lib/crystalruby/templates/index.cr +53 -66
  20. data/lib/crystalruby/templates/inline_chunk.cr +1 -1
  21. data/lib/crystalruby/templates/ruby_interface.cr +34 -0
  22. data/lib/crystalruby/templates/top_level_function.cr +62 -0
  23. data/lib/crystalruby/templates/top_level_ruby_interface.cr +33 -0
  24. data/lib/crystalruby/typebuilder.rb +11 -55
  25. data/lib/crystalruby/typemaps.rb +92 -67
  26. data/lib/crystalruby/types/concerns/allocator.rb +80 -0
  27. data/lib/crystalruby/types/fixed_width/named_tuple.cr +80 -0
  28. data/lib/crystalruby/types/fixed_width/named_tuple.rb +86 -0
  29. data/lib/crystalruby/types/fixed_width/proc.cr +45 -0
  30. data/lib/crystalruby/types/fixed_width/proc.rb +79 -0
  31. data/lib/crystalruby/types/fixed_width/tagged_union.cr +53 -0
  32. data/lib/crystalruby/types/fixed_width/tagged_union.rb +113 -0
  33. data/lib/crystalruby/types/fixed_width/tuple.cr +82 -0
  34. data/lib/crystalruby/types/fixed_width/tuple.rb +92 -0
  35. data/lib/crystalruby/types/fixed_width.cr +138 -0
  36. data/lib/crystalruby/types/fixed_width.rb +205 -0
  37. data/lib/crystalruby/types/primitive.cr +21 -0
  38. data/lib/crystalruby/types/primitive.rb +117 -0
  39. data/lib/crystalruby/types/primitive_types/bool.cr +34 -0
  40. data/lib/crystalruby/types/primitive_types/bool.rb +11 -0
  41. data/lib/crystalruby/types/primitive_types/nil.cr +35 -0
  42. data/lib/crystalruby/types/primitive_types/nil.rb +16 -0
  43. data/lib/crystalruby/types/primitive_types/numbers.cr +37 -0
  44. data/lib/crystalruby/types/primitive_types/numbers.rb +28 -0
  45. data/lib/crystalruby/types/primitive_types/symbol.cr +55 -0
  46. data/lib/crystalruby/types/primitive_types/symbol.rb +35 -0
  47. data/lib/crystalruby/types/primitive_types/time.cr +35 -0
  48. data/lib/crystalruby/types/primitive_types/time.rb +25 -0
  49. data/lib/crystalruby/types/type.cr +64 -0
  50. data/lib/crystalruby/types/type.rb +249 -30
  51. data/lib/crystalruby/types/variable_width/array.cr +74 -0
  52. data/lib/crystalruby/types/variable_width/array.rb +88 -0
  53. data/lib/crystalruby/types/variable_width/hash.cr +146 -0
  54. data/lib/crystalruby/types/variable_width/hash.rb +117 -0
  55. data/lib/crystalruby/types/variable_width/string.cr +36 -0
  56. data/lib/crystalruby/types/variable_width/string.rb +18 -0
  57. data/lib/crystalruby/types/variable_width.cr +23 -0
  58. data/lib/crystalruby/types/variable_width.rb +46 -0
  59. data/lib/crystalruby/types.rb +32 -13
  60. data/lib/crystalruby/version.rb +2 -2
  61. data/lib/crystalruby.rb +13 -6
  62. metadata +42 -22
  63. data/lib/crystalruby/types/array.rb +0 -15
  64. data/lib/crystalruby/types/bool.rb +0 -3
  65. data/lib/crystalruby/types/hash.rb +0 -17
  66. data/lib/crystalruby/types/named_tuple.rb +0 -28
  67. data/lib/crystalruby/types/nil.rb +0 -3
  68. data/lib/crystalruby/types/numbers.rb +0 -5
  69. data/lib/crystalruby/types/string.rb +0 -3
  70. data/lib/crystalruby/types/symbol.rb +0 -3
  71. data/lib/crystalruby/types/time.rb +0 -8
  72. data/lib/crystalruby/types/tuple.rb +0 -17
  73. data/lib/crystalruby/types/type_serializer/json.rb +0 -41
  74. data/lib/crystalruby/types/type_serializer.rb +0 -37
  75. data/lib/crystalruby/types/typedef.rb +0 -57
  76. data/lib/crystalruby/types/union_type.rb +0 -43
  77. data/lib/module.rb +0 -3
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crystalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.1
5
5
  platform: ruby
6
+ original_platform: ''
6
7
  authors:
7
8
  - Wouter Coppieters
8
- autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-09 00:00:00.000000000 Z
11
+ date: 2024-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: digest
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: method_source
56
+ name: prism
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -87,35 +87,57 @@ files:
87
87
  - exe/crystalruby
88
88
  - lib/crystalruby.rb
89
89
  - lib/crystalruby/adapter.rb
90
+ - lib/crystalruby/arc_mutex.rb
90
91
  - lib/crystalruby/compilation.rb
91
92
  - lib/crystalruby/config.rb
92
93
  - lib/crystalruby/function.rb
93
94
  - lib/crystalruby/library.rb
94
95
  - lib/crystalruby/reactor.rb
96
+ - lib/crystalruby/source_reader.rb
95
97
  - lib/crystalruby/template.rb
96
98
  - lib/crystalruby/templates/function.cr
97
99
  - lib/crystalruby/templates/index.cr
98
100
  - lib/crystalruby/templates/inline_chunk.cr
101
+ - lib/crystalruby/templates/ruby_interface.cr
102
+ - lib/crystalruby/templates/top_level_function.cr
103
+ - lib/crystalruby/templates/top_level_ruby_interface.cr
99
104
  - lib/crystalruby/typebuilder.rb
100
105
  - lib/crystalruby/typemaps.rb
101
106
  - lib/crystalruby/types.rb
102
- - lib/crystalruby/types/array.rb
103
- - lib/crystalruby/types/bool.rb
104
- - lib/crystalruby/types/hash.rb
105
- - lib/crystalruby/types/named_tuple.rb
106
- - lib/crystalruby/types/nil.rb
107
- - lib/crystalruby/types/numbers.rb
108
- - lib/crystalruby/types/string.rb
109
- - lib/crystalruby/types/symbol.rb
110
- - lib/crystalruby/types/time.rb
111
- - lib/crystalruby/types/tuple.rb
107
+ - lib/crystalruby/types/concerns/allocator.rb
108
+ - lib/crystalruby/types/fixed_width.cr
109
+ - lib/crystalruby/types/fixed_width.rb
110
+ - lib/crystalruby/types/fixed_width/named_tuple.cr
111
+ - lib/crystalruby/types/fixed_width/named_tuple.rb
112
+ - lib/crystalruby/types/fixed_width/proc.cr
113
+ - lib/crystalruby/types/fixed_width/proc.rb
114
+ - lib/crystalruby/types/fixed_width/tagged_union.cr
115
+ - lib/crystalruby/types/fixed_width/tagged_union.rb
116
+ - lib/crystalruby/types/fixed_width/tuple.cr
117
+ - lib/crystalruby/types/fixed_width/tuple.rb
118
+ - lib/crystalruby/types/primitive.cr
119
+ - lib/crystalruby/types/primitive.rb
120
+ - lib/crystalruby/types/primitive_types/bool.cr
121
+ - lib/crystalruby/types/primitive_types/bool.rb
122
+ - lib/crystalruby/types/primitive_types/nil.cr
123
+ - lib/crystalruby/types/primitive_types/nil.rb
124
+ - lib/crystalruby/types/primitive_types/numbers.cr
125
+ - lib/crystalruby/types/primitive_types/numbers.rb
126
+ - lib/crystalruby/types/primitive_types/symbol.cr
127
+ - lib/crystalruby/types/primitive_types/symbol.rb
128
+ - lib/crystalruby/types/primitive_types/time.cr
129
+ - lib/crystalruby/types/primitive_types/time.rb
130
+ - lib/crystalruby/types/type.cr
112
131
  - lib/crystalruby/types/type.rb
113
- - lib/crystalruby/types/type_serializer.rb
114
- - lib/crystalruby/types/type_serializer/json.rb
115
- - lib/crystalruby/types/typedef.rb
116
- - lib/crystalruby/types/union_type.rb
132
+ - lib/crystalruby/types/variable_width.cr
133
+ - lib/crystalruby/types/variable_width.rb
134
+ - lib/crystalruby/types/variable_width/array.cr
135
+ - lib/crystalruby/types/variable_width/array.rb
136
+ - lib/crystalruby/types/variable_width/hash.cr
137
+ - lib/crystalruby/types/variable_width/hash.rb
138
+ - lib/crystalruby/types/variable_width/string.cr
139
+ - lib/crystalruby/types/variable_width/string.rb
117
140
  - lib/crystalruby/version.rb
118
- - lib/module.rb
119
141
  - logo.png
120
142
  - sig/crystalruby.rbs
121
143
  homepage: https://github.com/wouterken/crystalruby
@@ -125,7 +147,6 @@ metadata:
125
147
  homepage_uri: https://github.com/wouterken/crystalruby
126
148
  source_code_uri: https://github.com/wouterken/crystalruby
127
149
  changelog_uri: https://github.com/wouterken/crystalruby/CHANGELOG.md
128
- post_install_message:
129
150
  rdoc_options: []
130
151
  require_paths:
131
152
  - lib
@@ -140,8 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
161
  - !ruby/object:Gem::Version
141
162
  version: '0'
142
163
  requirements: []
143
- rubygems_version: 3.4.19
144
- signing_key:
164
+ rubygems_version: 3.6.0.dev
145
165
  specification_version: 4
146
166
  summary: Embed Crystal code directly in Ruby.
147
167
  test_files: []
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby::Types
4
- Array = Type.new(
5
- :Array,
6
- error: "Array type must have a type parameter. E.g. Array(Float64)"
7
- )
8
-
9
- def self.Array(type)
10
- Type.validate!(type)
11
- Type.new("Array", inner_types: [type], accept_if: [::Array]) do |a|
12
- a.map! { |v| type.interpret!(v) }
13
- end
14
- end
15
- end
@@ -1,3 +0,0 @@
1
- module CrystalRuby::Types
2
- Bool = Type.new(:Bool, accept_if: [::TrueClass, ::FalseClass])
3
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby::Types
4
- Hash = Type.new(
5
- :Hash,
6
- error: "Hash type must have 2 type parameters. E.g. Hash(Float64, String)"
7
- )
8
-
9
- def self.Hash(key_type, value_type)
10
- Type.validate!(key_type)
11
- Type.validate!(value_type)
12
- Type.new("Hash", inner_types: [key_type, value_type], accept_if: [::Hash]) do |h|
13
- h.transform_keys! { |k| key_type.interpret!(k) }
14
- h.transform_values! { |v| value_type.interpret!(v) }
15
- end
16
- end
17
- end
@@ -1,28 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby::Types
4
- NamedTuple = Type.new(
5
- :NamedTuple,
6
- error: "NamedTuple type must contain one or more symbol -> type pairs. E.g. NamedTuple(hello: Int32, world: String)"
7
- )
8
-
9
- def self.NamedTuple(types_hash)
10
- types_hash.keys.each do |key|
11
- raise "NamedTuple keys must be symbols" unless key.is_a?(::Symbol) || key.respond_to?(:to_sym)
12
- end
13
- types_hash.values.each do |value_type|
14
- Type.validate!(value_type)
15
- end
16
- keys = types_hash.keys.map(&:to_sym)
17
- values = types_hash.values
18
- Type.new("NamedTuple", inner_types: values, inner_keys: keys, accept_if: [::Hash]) do |h|
19
- h.transform_keys! { |k| k.to_sym }
20
- raise "Invalid keys for named tuple" unless h.keys.length == keys.length
21
- raise "Invalid keys for named tuple" unless h.keys.all? { |k| keys.include?(k) }
22
-
23
- h.each do |key, value|
24
- h[key] = values[keys.index(key)].interpret!(value)
25
- end
26
- end
27
- end
28
- end
@@ -1,3 +0,0 @@
1
- module CrystalRuby::Types
2
- Nil = Type.new(:Nil, accept_if: [::NilClass])
3
- end
@@ -1,5 +0,0 @@
1
- module CrystalRuby::Types
2
- %i[Uint8 Uint16 Uint32 Uint64 Int8 Int16 Int32 Int64 Float32 Float64].each do |type_name|
3
- const_set type_name, Type.new(type_name, accept_if: [::Numeric])
4
- end
5
- end
@@ -1,3 +0,0 @@
1
- module CrystalRuby::Types
2
- String = Type.new(:String, accept_if: [::String])
3
- end
@@ -1,3 +0,0 @@
1
- module CrystalRuby::Types
2
- Symbol = Type.new(:Symbol, accept_if: [::String, ::Symbol])
3
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby::Types
4
- require "date"
5
- Time = Type.new(:Time, accept_if: [::Time, ::String]) do |v|
6
- DateTime.parse(v)
7
- end
8
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby::Types
4
- Tuple = Type.new(
5
- :Tuple,
6
- error: "Tuple type must contain one or more types E.g. Tuple(Int32, String)"
7
- )
8
-
9
- def self.Tuple(*types)
10
- types.each do |value_type|
11
- Type.validate!(value_type)
12
- end
13
- Type.new("Tuple", inner_types: types, accept_if: [::Array]) do |a|
14
- a.map!.with_index { |v, i| inner_types[i].interpret!(v) }
15
- end
16
- end
17
- end
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "json"
4
-
5
- module CrystalRuby::Types
6
- class TypeSerializer
7
- class JSON < TypeSerializer
8
- def lib_type
9
- "UInt8*"
10
- end
11
-
12
- def crystal_type
13
- type_expr
14
- end
15
-
16
- def error_value
17
- '"{}".to_unsafe'
18
- end
19
-
20
- def ffi_type
21
- :string
22
- end
23
-
24
- def prepare_argument(arg)
25
- arg.to_json
26
- end
27
-
28
- def prepare_retval(retval)
29
- @typedef.interpret!(::JSON.parse(retval))
30
- end
31
-
32
- def lib_to_crystal_type_expr(expr)
33
- "(#{type_expr}).from_json(String.new(%s))" % expr
34
- end
35
-
36
- def crystal_to_lib_type_expr(expr)
37
- "%s.to_json.to_unsafe" % expr
38
- end
39
- end
40
- end
41
- end
@@ -1,37 +0,0 @@
1
- module CrystalRuby::Types
2
- class TypeSerializer
3
- include FFI::DataConverter
4
- def self.for(format)
5
- case format
6
- when :json then JSON
7
- else raise "Unknown type format: #{format}"
8
- end
9
- end
10
-
11
- def error_value
12
- 0
13
- end
14
-
15
- def initialize(typedef)
16
- @typedef = typedef
17
- end
18
-
19
- def type_expr
20
- @typedef.type_expr
21
- end
22
-
23
- def type_defn
24
- @typedef.type_defn
25
- end
26
-
27
- def anonymous?
28
- @typedef.anonymous?
29
- end
30
-
31
- def name
32
- @typedef.name
33
- end
34
- end
35
- end
36
-
37
- require_relative "type_serializer/json"
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "type_serializer"
4
-
5
- module CrystalRuby
6
- module Types
7
- class Typedef; end
8
-
9
- def self.Typedef(type)
10
- return type if type.is_a?(Class) && type < Typedef
11
-
12
- Class.new(Typedef) do
13
- define_singleton_method(:union_types) do
14
- [self]
15
- end
16
-
17
- define_singleton_method(:anonymous?) do
18
- name.nil?
19
- end
20
-
21
- define_singleton_method(:valid?) do
22
- type.valid?
23
- end
24
-
25
- define_singleton_method(:type) do
26
- type
27
- end
28
-
29
- define_singleton_method(:type_expr) do
30
- anonymous? ? type.type_expr : name
31
- end
32
-
33
- define_singleton_method(:type_defn) do
34
- type.type_expr
35
- end
36
-
37
- define_singleton_method(:|) do |other|
38
- raise "Cannot union non-crystal type #{other}" unless other.is_a?(Type) || other.is_a?(Typedef)
39
-
40
- UnionType.new(*union_types, *other.union_types)
41
- end
42
-
43
- define_singleton_method(:inspect) do
44
- "<#{name || "AnonymousType"} #{type.inspect}>"
45
- end
46
-
47
- define_singleton_method(:serialize_as) do |format|
48
- TypeSerializer.for(format).new(self)
49
- end
50
-
51
- define_singleton_method(:interpret!) do |raw|
52
- type.interpret!(raw)
53
- end
54
- end
55
- end
56
- end
57
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CrystalRuby
4
- module Types
5
- class UnionType < Type
6
- attr_accessor :name, :union_types
7
-
8
- def initialize(*union_types)
9
- self.name = "UnionType"
10
- self.union_types = union_types
11
- end
12
-
13
- def valid?
14
- union_types.all?(&:valid?)
15
- end
16
-
17
- def error
18
- union_types.map(&:error).join(", ")
19
- end
20
-
21
- def inspect
22
- union_types.map(&:inspect).join(" | ")
23
- end
24
-
25
- def interprets?(raw)
26
- union_types.any? { |type| type.interprets?(raw) }
27
- end
28
-
29
- def interpret!(raw)
30
- union_types.each do |type|
31
- next unless type.interprets?(raw)
32
-
33
- begin
34
- return type.interpret!(raw)
35
- rescue StandardError
36
- # Pass
37
- end
38
- end
39
- raise "Invalid deserialized value #{raw} for type #{inspect}"
40
- end
41
- end
42
- end
43
- end
data/lib/module.rb DELETED
@@ -1,3 +0,0 @@
1
- class Module
2
- prepend CrystalRuby::Adapter
3
- end