crystalruby 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +32 -2
- data/lib/crystalruby/template.rb +0 -1
- data/lib/crystalruby/typebuilder.rb +0 -112
- data/lib/crystalruby/types/union_type.rb +5 -1
- data/lib/crystalruby/version.rb +1 -1
- data/lib/crystalruby.rb +14 -8
- data/logo.png +0 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8dc48667be65ee6e6d36ba7efcfc87dfefb133a06cb375a22297db9643221e3
|
4
|
+
data.tar.gz: 40753b3741ae9fb5dec86019d35968027517280a08ae6bffbcf8f2e207fe96b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1682a4405fd2baf66a93fc913af2146fa11e9806b90c8d6ac6d0eebc496b30415204474bd841377e3ea409401608f2907f58d7b717b21520cd4b1fe04d825d86
|
7
|
+
data.tar.gz: 8682ebdcdd83bda6ab29261cdfc627afe1bd211343bc4caa95c9f08cbb30fba663f1aebcf4df1535e27d7a84377449d5028afcce145cc0a91c153721e2d261f1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,19 @@
|
|
1
|
-
|
1
|
+
<table>
|
2
|
+
<tr>
|
3
|
+
<td><img src="logo.png" alt="logo" width="150"></td>
|
4
|
+
<td>
|
5
|
+
<h1 align="center">crystalruby</h1>
|
6
|
+
<p align="center">
|
7
|
+
<a href="https://rubygems.org/gems/crystalruby">
|
8
|
+
<img alt="GEM Version" src="https://img.shields.io/gem/v/crystalruby?color=168AFE&include_prereleases&logo=ruby&logoColor=FE1616">
|
9
|
+
</a><br>
|
10
|
+
<a href="https://rubygems.org/gems/crystalruby">
|
11
|
+
<img alt="GEM Downloads" src="https://img.shields.io/gem/dt/crystalruby?color=168AFE&logo=ruby&logoColor=FE1616">
|
12
|
+
</a>
|
13
|
+
</p>
|
14
|
+
</td>
|
15
|
+
</tr>
|
16
|
+
</table>
|
2
17
|
|
3
18
|
`crystalruby` is a gem that allows you to write Crystal code, inlined in Ruby. All you need is a modern crystal compiler installed on your system.
|
4
19
|
|
@@ -196,6 +211,22 @@ ArgumentError: Expected Bool but was Int at line 1, column 15
|
|
196
211
|
from crystalruby.rb:303:in `block in compile!'
|
197
212
|
```
|
198
213
|
|
214
|
+
## Named Types
|
215
|
+
|
216
|
+
You can name your types, for more succinct method signatures.
|
217
|
+
The type names will be mirrored in the generated Crystal code.
|
218
|
+
E.g.
|
219
|
+
|
220
|
+
```ruby
|
221
|
+
|
222
|
+
IntArrOrBoolArr = crtype{ Array(Bool) | Array(Int32) }
|
223
|
+
|
224
|
+
crystalize [a: IntArrOrBoolArr] => json{ IntArrOrBoolArr }
|
225
|
+
def method_with_named_types(a)
|
226
|
+
return a
|
227
|
+
end
|
228
|
+
```
|
229
|
+
|
199
230
|
## Exceptions
|
200
231
|
|
201
232
|
Exceptions thrown in Crystal code can be caught in Ruby.
|
@@ -294,7 +325,6 @@ It should support escape hatches to allow it to coexist with code that performs
|
|
294
325
|
The library is currently in its infancy. Planned additions are:
|
295
326
|
|
296
327
|
- Replace existing checksum process, with one that combines results of inline and external crystal to more accurately detect when recompilation is necessary.
|
297
|
-
- Support for automatic serialization of nested data structures (holding _ONLY_ primitives), using JSON as our serialization protocol (prioritizing portability over raw serialization performance. JSON generation and parsing is bundled into the stdlib in both languages).
|
298
328
|
- Simple mixin/concern that utilises `FFI::Struct` for bi-directional passing of Ruby objects and Crystal objects (by value).
|
299
329
|
- Install command to generate a sample build script, and supports build command (which simply verifies then invokes this script)
|
300
330
|
- Call Ruby from Crystal using FFI callbacks (implement `.expose_to_crystal`)
|
data/lib/crystalruby/template.rb
CHANGED
@@ -65,115 +65,3 @@ module CrystalRuby
|
|
65
65
|
end
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
69
|
-
# class UnionType
|
70
|
-
# attr_reader :inner
|
71
|
-
|
72
|
-
# def initialize(*inner)
|
73
|
-
# @inner = inner
|
74
|
-
# end
|
75
|
-
|
76
|
-
# def |(other)
|
77
|
-
# UnionType.new(*inner, *other.inner)
|
78
|
-
# end
|
79
|
-
|
80
|
-
# def inspect
|
81
|
-
# elements = inner.map(&:inspect).join(" | ")
|
82
|
-
# end
|
83
|
-
# end
|
84
|
-
|
85
|
-
# class Type
|
86
|
-
# attr_reader :inner, :contains
|
87
|
-
|
88
|
-
# def initialize(name)
|
89
|
-
# @name = name
|
90
|
-
# @contains = contains
|
91
|
-
# @inner = [self]
|
92
|
-
# end
|
93
|
-
|
94
|
-
# def |(other)
|
95
|
-
# UnionType.new(*inner, *other.inner)
|
96
|
-
# end
|
97
|
-
|
98
|
-
# def inspect
|
99
|
-
# if @contains
|
100
|
-
# "#{@name}(#{@contains.inspect})"
|
101
|
-
# else
|
102
|
-
# @name
|
103
|
-
# end
|
104
|
-
# end
|
105
|
-
# end
|
106
|
-
|
107
|
-
# module_function
|
108
|
-
|
109
|
-
# %w[
|
110
|
-
# Bool Uint8 Uint16 Uint32 Uint64 Int8 Int16 Int32 Int64 Float32 Float64 String Time Symbol
|
111
|
-
# Null
|
112
|
-
# ].map do |t|
|
113
|
-
# cls = Class.new(Type)
|
114
|
-
# const_set(t, cls)
|
115
|
-
# define_method(t.downcase) do
|
116
|
-
# cls.new(t)
|
117
|
-
# end
|
118
|
-
# end
|
119
|
-
|
120
|
-
# def build(&blk)
|
121
|
-
# instance_exec(&blk)
|
122
|
-
# end
|
123
|
-
|
124
|
-
# def hash(key_type, value_type)
|
125
|
-
# Hash.new(key_type, value_type)
|
126
|
-
# end
|
127
|
-
|
128
|
-
# def array(type)
|
129
|
-
# Array.new(type)
|
130
|
-
# end
|
131
|
-
|
132
|
-
# def tuple(*types)
|
133
|
-
# Tuple.new(*types)
|
134
|
-
# end
|
135
|
-
|
136
|
-
# def named_tuple(type_hash)
|
137
|
-
# NamedTuple.new(type_hash)
|
138
|
-
# end
|
139
|
-
|
140
|
-
# def NamedTuple(type_hash)
|
141
|
-
# NamedTuple.new(type_hash)
|
142
|
-
# end
|
143
|
-
|
144
|
-
# class Hash < Type
|
145
|
-
# HASH_KEY_TYPES = %w[String Symbol].freeze
|
146
|
-
# def initialize(key_type, value_type)
|
147
|
-
# super("Hash")
|
148
|
-
# @key_type = key_type
|
149
|
-
# @value_type = value_type
|
150
|
-
# raise "Invalid key type" unless [Uint8, Uint16, Uint32, Uint64, Int8, Int16, Int32, Int64,
|
151
|
-
# String].include?(key_type)
|
152
|
-
# raise "Invalid value type" unless value_type.is_a?(Type)
|
153
|
-
# end
|
154
|
-
# end
|
155
|
-
|
156
|
-
# class Array < Type
|
157
|
-
# def initialize(value_type)
|
158
|
-
# super("Array")
|
159
|
-
# @value_type = value_type
|
160
|
-
# raise "Invalid value type" unless value_type.is_a?(Type)
|
161
|
-
# end
|
162
|
-
# end
|
163
|
-
|
164
|
-
# class NamedTuple < Type
|
165
|
-
# def initialize(types_hash)
|
166
|
-
# raise "keys must be symbols" unless types_hash.keys.all? { |k| k.is_a?(Symbol) }
|
167
|
-
# raise "Invalid value type" unless types_hash.values.all? { |v| v.is_a?(Type) }
|
168
|
-
|
169
|
-
# super("NamedTuple")
|
170
|
-
# @types_hash = types_hash
|
171
|
-
# end
|
172
|
-
# end
|
173
|
-
|
174
|
-
# class Tuple < Type
|
175
|
-
# def initialize(*value_types)
|
176
|
-
# super("Tuple")
|
177
|
-
# raise "Invalid value type" unless value_types.all? { |v| v.is_a?(Type) }
|
178
|
-
# end
|
179
|
-
# end
|
@@ -27,7 +27,11 @@ module CrystalRuby
|
|
27
27
|
def interpret!(raw)
|
28
28
|
union_types.each do |type|
|
29
29
|
if type.interprets?(raw)
|
30
|
-
|
30
|
+
begin
|
31
|
+
return type.interpret!(raw)
|
32
|
+
rescue
|
33
|
+
# Pass
|
34
|
+
end
|
31
35
|
end
|
32
36
|
end
|
33
37
|
raise "Invalid deserialized value #{raw} for type #{inspect}"
|
data/lib/crystalruby/version.rb
CHANGED
data/lib/crystalruby.rb
CHANGED
@@ -112,7 +112,9 @@ module CrystalRuby
|
|
112
112
|
fn_ret_type: return_type[:crystal_type],
|
113
113
|
lib_fn_args: arg_types.map { |k, arg_type| "_#{k}: #{arg_type[:lib_type]}" }.join(","),
|
114
114
|
lib_fn_ret_type: return_type[:lib_type],
|
115
|
-
convert_lib_args:
|
115
|
+
convert_lib_args: arg_types.map do |k, arg_type|
|
116
|
+
"#{k} = #{arg_type[:convert_lib_to_crystal_type]["_#{k}"]}"
|
117
|
+
end.join("\n "),
|
116
118
|
arg_names: args.keys.join(","),
|
117
119
|
convert_return_type: return_type[:convert_crystal_to_lib_type]["return_value"],
|
118
120
|
error_value: return_type[:error_value]
|
@@ -212,7 +214,10 @@ module CrystalRuby
|
|
212
214
|
FileUtils.mkdir_p "#{config.crystal_src_dir}/#{config.crystal_codegen_dir}"
|
213
215
|
FileUtils.mkdir_p "#{config.crystal_lib_dir}"
|
214
216
|
unless File.exist?("#{config.crystal_src_dir}/#{config.crystal_main_file}")
|
215
|
-
IO.write(
|
217
|
+
IO.write(
|
218
|
+
"#{config.crystal_src_dir}/#{config.crystal_main_file}",
|
219
|
+
"require \"./#{config.crystal_codegen_dir}/index\"\n"
|
220
|
+
)
|
216
221
|
end
|
217
222
|
return if File.exist?("#{config.crystal_src_dir}/shard.yml")
|
218
223
|
|
@@ -295,12 +300,11 @@ module CrystalRuby
|
|
295
300
|
extend FFI::Library
|
296
301
|
ffi_lib "#{config.crystal_lib_dir}/#{config.crystal_lib_name}"
|
297
302
|
attach_function :attach_rb_error_handler, [:pointer], :int
|
298
|
-
const_set(:ErrorCallback, FFI::Function.new(:void, [
|
303
|
+
const_set(:ErrorCallback, FFI::Function.new(:void, %i[string string]) do |error_type, message|
|
299
304
|
error_type = error_type.to_sym
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
raise error_type.new(message)
|
305
|
+
is_exception_type = Object.const_defined?(error_type) && Object.const_get(error_type).ancestors.include?(Exception)
|
306
|
+
error_type = is_exception_type ? Object.const_get(error_type) : RuntimeError
|
307
|
+
raise error_type.new(message)
|
304
308
|
end)
|
305
309
|
attach_rb_error_handler(ErrorCallback)
|
306
310
|
end
|
@@ -313,7 +317,9 @@ module CrystalRuby
|
|
313
317
|
end
|
314
318
|
|
315
319
|
def self.write_function(owner, name:, body:, &compile_callback)
|
316
|
-
|
320
|
+
unless defined?(@compiled)
|
321
|
+
@compiled = File.exist?("#{config.crystal_src_dir}/#{config.crystal_codegen_dir}/index.cr")
|
322
|
+
end
|
317
323
|
@block_store ||= []
|
318
324
|
@block_store << { owner: owner, name: name, body: body, compile_callback: compile_callback }
|
319
325
|
FileUtils.mkdir_p("#{config.crystal_src_dir}/#{config.crystal_codegen_dir}")
|
data/logo.png
ADDED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crystalruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wouter Coppieters
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- lib/crystalruby/types/union_type.rb
|
108
108
|
- lib/crystalruby/version.rb
|
109
109
|
- lib/module.rb
|
110
|
+
- logo.png
|
110
111
|
- sig/crystalruby.rbs
|
111
112
|
homepage: https://github.com/wouterken/crystalruby
|
112
113
|
licenses:
|