homura-runtime 0.3.8 → 0.3.10

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.
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk/opal_compat"
4
+ require "literal" unless defined?(Literal)
5
+
6
+ module Literal
7
+ class OpalBuffer
8
+ def initialize(value = "")
9
+ @value = value.to_s
10
+ end
11
+
12
+ def <<(value)
13
+ @value += value.to_s
14
+ self
15
+ end
16
+
17
+ def to_s
18
+ @value
19
+ end
20
+
21
+ alias to_str to_s
22
+
23
+ def encoding
24
+ @value.encoding
25
+ end
26
+
27
+ def force_encoding(_encoding)
28
+ @value
29
+ end
30
+
31
+ def method_missing(name, ...)
32
+ if @value.respond_to?(name)
33
+ @value.public_send(name, ...)
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ def respond_to_missing?(name, include_private = false)
40
+ @value.respond_to?(name, include_private) || super
41
+ end
42
+ end
43
+ end
44
+
45
+ module Literal::Properties
46
+ private def __define_literal_methods__(new_property)
47
+ extension = __literal_extension__
48
+
49
+ extension.define_method(:initialize) do |*args, **kwargs, &block|
50
+ positional_index = 0
51
+ properties = self.class.literal_properties
52
+
53
+ properties.each do |property|
54
+ value = case property.kind
55
+ when :positional
56
+ if positional_index < args.length
57
+ args[positional_index]
58
+ elsif property.default?
59
+ Literal::Undefined
60
+ elsif property.type === nil
61
+ nil
62
+ else
63
+ Literal::Undefined
64
+ end
65
+ .tap { positional_index += 1 }
66
+ when :*
67
+ args[positional_index..] || []
68
+ when :keyword
69
+ if kwargs.key?(property.name)
70
+ kwargs.delete(property.name)
71
+ elsif property.default?
72
+ Literal::Undefined
73
+ elsif property.type === nil
74
+ nil
75
+ else
76
+ Literal::Undefined
77
+ end
78
+
79
+ when :**
80
+ kwargs
81
+ when :&
82
+ block
83
+ else
84
+ raise "You should never see this error."
85
+ end
86
+
87
+ value = property.default_value(self) if property.default? && Literal::Undefined == value
88
+ value = property.coerce(value, context: self) if property.coercion
89
+ property.check_initializer(self, value)
90
+ instance_variable_set(:"@#{property.name}", value)
91
+ end
92
+
93
+ after_initialize if respond_to?(:after_initialize, true)
94
+ rescue Literal::TypeError => error
95
+ error.set_backtrace(caller(2))
96
+ raise
97
+ end
98
+
99
+ extension.define_method(:to_h) do
100
+ self.class.literal_properties.each.each_with_object({}) do |property, hash|
101
+ hash[property.name] = instance_variable_get(:"@#{property.name}")
102
+ end
103
+ end
104
+
105
+ extension.alias_method(:to_hash, :to_h)
106
+
107
+ define_literal_reader(extension, new_property) if new_property.reader
108
+ define_literal_writer(extension, new_property) if new_property.writer
109
+ define_literal_predicate(extension, new_property) if new_property.predicate
110
+ end
111
+
112
+ private def define_literal_reader(extension, property)
113
+ extension.define_method(property.name) do
114
+ instance_variable_get(:"@#{property.name}")
115
+ end
116
+
117
+ extension.__send__(property.reader, property.name)
118
+ end
119
+
120
+ private def define_literal_writer(extension, property)
121
+ method_name = :"#{property.name}="
122
+ extension.define_method(method_name) do |value|
123
+ property.check_writer(self, value)
124
+ instance_variable_set(:"@#{property.name}", value)
125
+ rescue Literal::TypeError => error
126
+ error.set_backtrace(caller(1))
127
+ raise
128
+ end
129
+
130
+ extension.__send__(property.writer, method_name)
131
+ end
132
+
133
+ private def define_literal_predicate(extension, property)
134
+ method_name = :"#{property.name}?"
135
+ extension.define_method(method_name) do
136
+ !!instance_variable_get(:"@#{property.name}")
137
+ end
138
+
139
+ extension.__send__(property.predicate, method_name)
140
+ end
141
+ end
data/lib/opal_patches.rb CHANGED
@@ -25,10 +25,11 @@
25
25
  # from, and a warning-only behaviour does not affect program output).
26
26
  class Module
27
27
  unless private_method_defined?(:deprecate_constant) ||
28
- method_defined?(:deprecate_constant)
28
+ method_defined?(:deprecate_constant)
29
29
  def deprecate_constant(*_names)
30
30
  self
31
31
  end
32
+
32
33
  private :deprecate_constant
33
34
  end
34
35
  end
@@ -72,13 +73,15 @@ class Module
72
73
  name_str = name.to_s
73
74
  if name_str.include?("::")
74
75
  parts = name_str.split("::")
75
- parts.shift if parts.first.empty? # leading "::Foo::Bar"
76
+ # leading "::Foo::Bar"
77
+ parts.shift if parts.first.empty?
76
78
  current = self
77
79
  parts.each do |part|
78
80
  return false unless current.__homura_const_defined_simple(part, inherit)
79
81
  current = current.const_get(part, inherit)
80
82
  return false unless current.is_a?(Module)
81
83
  end
84
+
82
85
  true
83
86
  else
84
87
  __homura_const_defined_simple(name, inherit)
@@ -156,15 +159,15 @@ module ForwardableAccessor
156
159
  expr
157
160
  .split(".")
158
161
  .each do |part|
159
- current =
160
- if part == "self"
161
- instance
162
- elsif part.start_with?("@")
163
- instance.instance_variable_get(part)
164
- else
165
- current.__send__(part)
166
- end
162
+ current = if part == "self"
163
+ instance
164
+ elsif part.start_with?("@")
165
+ instance.instance_variable_get(part)
166
+ else
167
+ current.__send__(part)
168
+ end
167
169
  end
170
+
168
171
  current
169
172
  end
170
173
  end
@@ -177,17 +180,17 @@ module Forwardable
177
180
  def def_instance_delegator(accessor, method, ali = method)
178
181
  accessor_str = accessor.to_s
179
182
  if accessor_str.start_with?("@") && !accessor_str.include?(".")
180
- define_method ali do |*args, &block|
183
+ define_method(ali) do |*args, &block|
181
184
  instance_variable_get(accessor_str).__send__(method, *args, &block)
182
185
  end
183
186
  elsif accessor_str =~ /\A[A-Za-z_]\w*\z/
184
187
  # Plain identifier (method name) — call via __send__ as before.
185
- define_method ali do |*args, &block|
188
+ define_method(ali) do |*args, &block|
186
189
  __send__(accessor_str).__send__(method, *args, &block)
187
190
  end
188
191
  else
189
192
  # Dot-path expression like 'self.class'. Resolve without eval.
190
- define_method ali do |*args, &block|
193
+ define_method(ali) do |*args, &block|
191
194
  ForwardableAccessor.resolve(self, accessor_str).__send__(
192
195
  method,
193
196
  *args,
@@ -204,15 +207,15 @@ module SingleForwardable
204
207
  def def_single_delegator(accessor, method, ali = method)
205
208
  accessor_str = accessor.to_s
206
209
  if accessor_str.start_with?("@") && !accessor_str.include?(".")
207
- define_singleton_method ali do |*args, &block|
210
+ define_singleton_method(ali) do |*args, &block|
208
211
  instance_variable_get(accessor_str).__send__(method, *args, &block)
209
212
  end
210
213
  elsif accessor_str =~ /\A[A-Za-z_]\w*\z/
211
- define_singleton_method ali do |*args, &block|
214
+ define_singleton_method(ali) do |*args, &block|
212
215
  __send__(accessor_str).__send__(method, *args, &block)
213
216
  end
214
217
  else
215
- define_singleton_method ali do |*args, &block|
218
+ define_singleton_method(ali) do |*args, &block|
216
219
  ForwardableAccessor.resolve(self, accessor_str).__send__(
217
220
  method,
218
221
  *args,
@@ -239,29 +242,30 @@ end
239
242
  # continue to work.
240
243
  begin
241
244
  require "uri"
245
+
242
246
  rescue StandardError
243
247
  nil
244
248
  end
249
+
245
250
  require "cgi"
246
251
 
247
252
  module ::URI
248
253
  unless const_defined?(:DEFAULT_PARSER)
249
- DEFAULT_PARSER =
250
- Module.new do
251
- UNSAFE = Regexp.compile('[^\-_.!~*\'()a-zA-Z0-9;/?:@&=+$,\[\]]').freeze
254
+ DEFAULT_PARSER = Module.new do
255
+ UNSAFE = Regexp.compile("[^\\-_.!~*'()a-zA-Z0-9;/?:@&=+$,\\[\\]]").freeze
252
256
 
253
- def self.regexp
254
- { UNSAFE: UNSAFE }
255
- end
257
+ def self.regexp
258
+ {UNSAFE: UNSAFE}
259
+ end
256
260
 
257
- def self.escape(s, unsafe = UNSAFE)
258
- CGI.escape(s.to_s)
259
- end
261
+ def self.escape(s, unsafe = UNSAFE)
262
+ CGI.escape(s.to_s)
263
+ end
260
264
 
261
- def self.unescape(s)
262
- CGI.unescape(s.to_s)
263
- end
265
+ def self.unescape(s)
266
+ CGI.unescape(s.to_s)
264
267
  end
268
+ end
265
269
  end
266
270
 
267
271
  # CRuby's URI.decode_www_form_component / encode_www_form_component are used
@@ -330,20 +334,17 @@ module ::URI
330
334
  def self.parse(str)
331
335
  s = str.to_s
332
336
  if s.empty?
333
- return(
334
- Generic.new(
335
- host: nil,
336
- scheme: nil,
337
- port: nil,
338
- path: "",
339
- query: nil,
340
- fragment: nil
341
- )
342
- )
337
+ return (Generic.new(
338
+ host: nil,
339
+ scheme: nil,
340
+ port: nil,
341
+ path: "",
342
+ query: nil,
343
+ fragment: nil
344
+ ))
343
345
  end
344
346
 
345
- js_url =
346
- `
347
+ js_url = `
347
348
  (function() {
348
349
  try { return new URL(#{s}); }
349
350
  catch (e) {
@@ -398,6 +399,7 @@ module ::Kernel
398
399
  return arg if arg.is_a?(::URI::Generic)
399
400
  ::URI.parse(arg.to_s)
400
401
  end
402
+
401
403
  module_function :URI
402
404
  end
403
405
 
@@ -471,9 +473,12 @@ module ::SecureRandom
471
473
  n = 16 if n <= 0
472
474
  hex_string = secure_hex_bytes(n)
473
475
  if hex_string.nil?
474
- raise EntropyError,
475
- "no source of cryptographic entropy available (node:crypto AND Web Crypto both unreachable)"
476
+ raise(
477
+ EntropyError,
478
+ "no source of cryptographic entropy available (node:crypto AND Web Crypto both unreachable)"
479
+ )
476
480
  end
481
+
477
482
  [hex_string].pack("H*")
478
483
  end
479
484
 
@@ -482,9 +487,12 @@ module ::SecureRandom
482
487
  n = 16 if n <= 0
483
488
  out = secure_hex_bytes(n)
484
489
  if out.nil?
485
- raise EntropyError,
486
- "no source of cryptographic entropy available (node:crypto AND Web Crypto both unreachable)"
490
+ raise(
491
+ EntropyError,
492
+ "no source of cryptographic entropy available (node:crypto AND Web Crypto both unreachable)"
493
+ )
487
494
  end
495
+
488
496
  out
489
497
  end
490
498
 
@@ -495,6 +503,7 @@ module ::SecureRandom
495
503
 
496
504
  def self.base64(n = 16)
497
505
  require "base64"
506
+
498
507
  Base64.strict_encode64(random_bytes(n))
499
508
  end
500
509
 
@@ -518,8 +527,7 @@ module ::SecureRandom
518
527
  def self.secure_hex_bytes(n)
519
528
  # Opal does not always auto-return backtick IIFEs; assign first
520
529
  # so the method's last expression is a normal Ruby reference.
521
- result =
522
- `(function(n) {
530
+ result = `(function(n) {
523
531
  try {
524
532
  if (typeof globalThis.__nodeCrypto__ !== 'undefined' && globalThis.__nodeCrypto__) {
525
533
  return globalThis.__nodeCrypto__.randomBytes(n).toString('hex');
@@ -569,19 +577,20 @@ class ::Array
569
577
  end
570
578
 
571
579
  hex = self.first.to_s
572
- nibble_count =
573
- if fmt == "H*"
574
- hex.length
575
- else
576
- [fmt[1..-1].to_i, hex.length].min
577
- end
578
- nibble_count -= 1 if nibble_count.odd? # round down to whole bytes
580
+ nibble_count = if fmt == "H*"
581
+ hex.length
582
+ else
583
+ [fmt[1..-1].to_i, hex.length].min
584
+ end
585
+ # round down to whole bytes
586
+ nibble_count -= 1 if nibble_count.odd?
579
587
  out = ""
580
588
  i = 0
581
589
  while i < nibble_count
582
590
  out = out + hex[i, 2].to_i(16).chr
583
591
  i += 2
584
592
  end
593
+
585
594
  out
586
595
  end
587
596
  end
@@ -608,12 +617,12 @@ class ::String
608
617
  return unpack1_without_homura_hex(format)
609
618
  end
610
619
 
611
- requested_nibbles =
612
- if fmt == "H*"
613
- self.length * 2
614
- else
615
- fmt[1..-1].to_i
616
- end
620
+ requested_nibbles = if fmt == "H*"
621
+ self.length * 2
622
+ else
623
+ fmt[1..-1].to_i
624
+ end
625
+
617
626
  out = ""
618
627
  i = 0
619
628
  n = self.length
@@ -624,6 +633,7 @@ class ::String
624
633
  out = out + h
625
634
  i += 1
626
635
  end
636
+
627
637
  out[0, requested_nibbles]
628
638
  end
629
639
  end
@@ -643,19 +653,21 @@ end
643
653
  begin
644
654
  file_class = ::File
645
655
  unless file_class.respond_to?(:read) &&
646
- !file_class.method(:read).source_location.nil?
656
+ !file_class.method(:read).source_location.nil?
647
657
  def file_class.read(*args)
648
658
  raise ::Errno::ENOENT, args.first.to_s
649
659
  end
660
+
650
661
  def file_class.binread(*args)
651
662
  raise ::Errno::ENOENT, args.first.to_s
652
663
  end
653
664
  end
665
+
654
666
  unless file_class.respond_to?(:fnmatch)
655
667
  def file_class.fnmatch(pattern, path, *)
656
668
  # Very small fnmatch: supports `*` and `?` only, good enough for
657
669
  # Sinatra's template extension matching.
658
- regex = '\A'
670
+ regex = "\\A"
659
671
  i = 0
660
672
  p = pattern.to_s
661
673
  while i < p.length
@@ -670,15 +682,19 @@ begin
670
682
  else
671
683
  regex += c
672
684
  end
685
+
673
686
  i += 1
674
687
  end
675
- regex += '\z'
688
+
689
+ regex += "\\z"
676
690
  !!(path.to_s =~ Regexp.new(regex))
677
691
  end
692
+
678
693
  def file_class.fnmatch?(pattern, path, *)
679
694
  fnmatch(pattern, path)
680
695
  end
681
696
  end
697
+
682
698
  rescue NameError
683
699
  # File not available at this load point — ignore.
684
700
  end
@@ -736,8 +752,9 @@ require "rubygems/version"
736
752
  ISO_8859_15
737
753
  ISO_8859_16
738
754
  MACROMAN
739
- ].each do |name|
740
- unless Encoding.const_defined?(name)
741
- Encoding.const_set(name, Encoding::ASCII_8BIT)
755
+ ]
756
+ .each do |name|
757
+ unless Encoding.const_defined?(name)
758
+ Encoding.const_set(name, Encoding::ASCII_8BIT)
759
+ end
742
760
  end
743
- end