ruby_wasm_ic_minimal 0.1.3
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +79 -0
- data/lib/ruby_wasm_ic_minimal/guard.rb +41 -0
- data/lib/ruby_wasm_ic_minimal/standins/date.rb +22 -0
- data/lib/ruby_wasm_ic_minimal/standins/encoding_constants.rb +16 -0
- data/lib/ruby_wasm_ic_minimal/standins/stringio.rb +270 -0
- data/lib/ruby_wasm_ic_minimal/standins/strscan.rb +197 -0
- data/lib/ruby_wasm_ic_minimal/version.rb +5 -0
- data/lib/ruby_wasm_ic_minimal.rb +23 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6664c0bc3baf20513dd6d0ed532aa5c76ea4abcb9b9bfede49339ba39d640229
|
|
4
|
+
data.tar.gz: abd4ef6d17e0156aee54f49c7a238ac8c1086f75eabbb3e555b47482e6f38461
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b409889a4046742e044c659be7706b938f22f3e346f4ff26d046d0e01511f576889c90868936465ad66fca974b33e120745f5954b95dbed9559dcd4f0bca9034
|
|
7
|
+
data.tar.gz: 5025f7dfc9334411badfa20a81f74e4db920e85f9ea95d30acabf2e9e14b9a2fb2278bdae46567a623158390252d2891c08da5b190e7905cd2beccad341eb3b7
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the ruby_wasm_builder authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# ruby_wasm_ic_minimal
|
|
2
|
+
|
|
3
|
+
Pure-Ruby stand-ins for four things a **`minimal`** Ruby canister lacks, for
|
|
4
|
+
canisters packed by [ruby_wasm_builder](https://gitlab.com/phrug/ruby_wasm_builder)
|
|
5
|
+
on its `minimal` Build. That Build links no C extension and defines no
|
|
6
|
+
`Encoding` constant, and Rack and Roda need all four:
|
|
7
|
+
|
|
8
|
+
| file (in `lib/ruby_wasm_ic_minimal/standins/`) | stands in for | why Rack needs it |
|
|
9
|
+
| --- | --- | --- |
|
|
10
|
+
| `encoding_constants.rb` | `Encoding::UTF_8`, `US_ASCII`, `ASCII_8BIT` (and `BINARY`, `ASCII`) | it decodes every query parameter as UTF-8 |
|
|
11
|
+
| `stringio.rb` | `StringIO` (a C extension in CRuby) | `rack.input`, `Rack::MockRequest` |
|
|
12
|
+
| `strscan.rb` | `StringScanner` (a C extension) | the multipart parser, loaded by `Rack::Request` |
|
|
13
|
+
| `date.rb` | `date` (its work is the C extension `date_core`) | only because `time` requires it; **a stub** |
|
|
14
|
+
|
|
15
|
+
`stringio.rb` and `strscan.rb` are checked against CRuby's C extensions on
|
|
16
|
+
MRI, operation by operation, and `StringScanner` keeps Ruby 3.3's meaning (a
|
|
17
|
+
`String` in `scan_until` is a `TypeError`). `date.rb` is a stub: Rack only
|
|
18
|
+
formats and reads HTTP dates, which need nothing from `Date`; `Time.parse`,
|
|
19
|
+
`Time.strptime` and `Date` itself raise `NotImplementedError` when called.
|
|
20
|
+
|
|
21
|
+
## Only in a `minimal` canister
|
|
22
|
+
|
|
23
|
+
**Do not put this gem in a Gemfile.** Each file shadows a real library by its
|
|
24
|
+
name, and `date.rb` is a stub. So every file refuses to load anywhere but a
|
|
25
|
+
`minimal` canister, raising `LoadError`. A `minimal` canister is one where the
|
|
26
|
+
builder's wrapper gem has defined `IC::DATA_DIR` and the core registered no
|
|
27
|
+
alias for any encoding (`Encoding.name_list.size == Encoding.list.size`).
|
|
28
|
+
Observed on PocketIC with the 0.6.0 Base Module (28 October 2026): twelve
|
|
29
|
+
encodings, the core's built-in table, and no alias, which is why
|
|
30
|
+
`# encoding: binary` traps there. MRI and the `full` Build register aliases,
|
|
31
|
+
so it refuses there too: carry none of it on `full`.
|
|
32
|
+
|
|
33
|
+
## Carrying it
|
|
34
|
+
|
|
35
|
+
Copy its `lib/` into your Application, and require it first, before Rack:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
gem fetch ruby_wasm_ic_minimal && gem unpack ruby_wasm_ic_minimal-*.gem
|
|
39
|
+
cp -r ruby_wasm_ic_minimal-*/lib my_app/vendor/ruby_wasm_ic_minimal
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
# app.rb
|
|
44
|
+
here = __dir__
|
|
45
|
+
$LOAD_PATH.unshift(File.join(here, "vendor/ruby_wasm_ic_minimal"), File.join(here, "vendor/rack"))
|
|
46
|
+
require "ruby_wasm_ic_minimal" # puts the stand-ins first on $LOAD_PATH and loads all four
|
|
47
|
+
require "rack"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The stand-ins live in `lib/ruby_wasm_ic_minimal/standins/`, not in `lib/`
|
|
51
|
+
itself, so having the gem installed, or in a Gemfile, shadows nothing on MRI.
|
|
52
|
+
`require "ruby_wasm_ic_minimal"` is what puts them on the load path, and only
|
|
53
|
+
in a `minimal` canister. After it, Rack's own `require "strscan"` and `time`'s
|
|
54
|
+
`require "date"` find them by name.
|
|
55
|
+
|
|
56
|
+
## Development
|
|
57
|
+
|
|
58
|
+
This repository is the gem's source. The
|
|
59
|
+
[ruby_wasm_builder](https://gitlab.com/phrug/ruby_wasm_builder) suite tests the
|
|
60
|
+
published gem (its `spec/gems/`): the guard on MRI, the stand-ins traced
|
|
61
|
+
against CRuby's C extensions, and the 0.7.0 `roda_on_minimal` Script played as
|
|
62
|
+
`minimal` with this gem in place of the Challenge's own `carried/` files. To
|
|
63
|
+
release: bump `lib/ruby_wasm_ic_minimal/version.rb`, then `gem build
|
|
64
|
+
ruby_wasm_ic_minimal.gemspec` and check `gem spec ruby_wasm_ic_minimal-*.gem
|
|
65
|
+
files` lists the lib files before `gem push`.
|
|
66
|
+
|
|
67
|
+
## Changes
|
|
68
|
+
|
|
69
|
+
- **0.1.3:** the stand-ins moved under `lib/ruby_wasm_ic_minimal/standins/`,
|
|
70
|
+
put on `$LOAD_PATH` by `require "ruby_wasm_ic_minimal"`, so a Gemfile that
|
|
71
|
+
lists the gem shadows nothing; the guard's rule is "no aliases". **Use
|
|
72
|
+
0.1.3 or later in a Gemfile:** with 0.1.2 or earlier listed, any
|
|
73
|
+
`require "stringio"` on MRI finds the stand-in and raises.
|
|
74
|
+
- **0.1.2:** the guard asks for `ISO-8859-1` by name, and loads on a
|
|
75
|
+
`minimal` canister. The stand-ins are still at the top of `lib/`.
|
|
76
|
+
- **0.1.1:** the first release with files.
|
|
77
|
+
- **0.1.0:** published empty (no `spec.files`).
|
|
78
|
+
|
|
79
|
+
MIT licensed.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# The guard the entry file and every stand-in in ruby_wasm_ic_minimal require
|
|
2
|
+
# first. A stand-in shadows a real library by its name (`date`, `stringio`,
|
|
3
|
+
# `strscan`), and `date.rb` here is a stub, so loading one anywhere but a
|
|
4
|
+
# `minimal` canister would quietly take the real library away. The guard
|
|
5
|
+
# refuses that instead, with LoadError. Since 0.1.3 the stand-ins also sit
|
|
6
|
+
# outside lib/ proper (ruby_wasm_ic_minimal/standins/), so they shadow nothing
|
|
7
|
+
# unless that directory is put on $LOAD_PATH; the guard is the second line.
|
|
8
|
+
#
|
|
9
|
+
# A `minimal` canister is one where the wrapper gem has defined IC::DATA_DIR
|
|
10
|
+
# (every canister packed by ruby_wasm_builder) and the core registered no
|
|
11
|
+
# alias for any encoding: `Encoding.name_list` is exactly `Encoding.list`'s
|
|
12
|
+
# names. Observed on PocketIC 16.0.0 with the 0.6.0 Base Module (28 October
|
|
13
|
+
# 2026): twelve encodings, the core's built-in table, and twelve names, no
|
|
14
|
+
# alias among them, which is why `# encoding: binary` traps there. MRI, and a
|
|
15
|
+
# Build that links the encoding database (`full`), register aliases (BINARY,
|
|
16
|
+
# ASCII, CP65001, ...) and the pseudo-names `locale`, `external` and
|
|
17
|
+
# `filesystem`, so their name list is longer. 0.1.1 counted `Encoding.list`
|
|
18
|
+
# against 3 and refused on every canister; 0.1.2 asked for `ISO-8859-1` by
|
|
19
|
+
# name, which held on a canister too.
|
|
20
|
+
#
|
|
21
|
+
# It counts names rather than looking for Encoding::UTF_8, which this gem's
|
|
22
|
+
# own encoding_constants.rb defines, and defining a constant adds no name.
|
|
23
|
+
# Decided once, at the first stand-in loaded, and kept in
|
|
24
|
+
# RubyWasmIcMinimal::ON_MINIMAL; a test harness may set that constant first
|
|
25
|
+
# (ruby_wasm_builder's MRI player does, for `minimal: true`).
|
|
26
|
+
module RubyWasmIcMinimal
|
|
27
|
+
unless const_defined?(:ON_MINIMAL, false)
|
|
28
|
+
ON_MINIMAL = !defined?(IC::DATA_DIR).nil? && Encoding.name_list.size == Encoding.list.size
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# `feature` is the library a stand-in stands in for, or nil for the entry
|
|
32
|
+
# file, ruby_wasm_ic_minimal.rb.
|
|
33
|
+
def self.guard!(feature = nil)
|
|
34
|
+
return if ON_MINIMAL
|
|
35
|
+
|
|
36
|
+
raise LoadError, "ruby_wasm_ic_minimal is only for a minimal canister, and this is not one" unless feature
|
|
37
|
+
|
|
38
|
+
raise LoadError, "ruby_wasm_ic_minimal's #{feature}.rb stands in for #{feature} only in a minimal canister, " \
|
|
39
|
+
"and this is not one; take its standins directory off $LOAD_PATH so the real #{feature} loads"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# date.rb (ruby_wasm_ic_minimal): a stand-in for CRuby's `date`, whose work is done by the
|
|
2
|
+
# C extension date_core, which the `minimal` Build does not link. `time`
|
|
3
|
+
# requires `date` at load, and Rack requires `time`, so without this file
|
|
4
|
+
# `require "rack"` fails. Rack only formats and reads HTTP dates
|
|
5
|
+
# (Time#httpdate, Time.httpdate), and those need nothing from Date.
|
|
6
|
+
#
|
|
7
|
+
# What needs a real Date fails here, loudly and only when called:
|
|
8
|
+
# Time.parse and Time.strptime (through Date._parse and Date._strptime), and
|
|
9
|
+
# Date itself. Under `full` this file is not carried.
|
|
10
|
+
require_relative "../guard"
|
|
11
|
+
RubyWasmIcMinimal.guard!("date")
|
|
12
|
+
class Date
|
|
13
|
+
def self.unavailable(what)
|
|
14
|
+
raise NotImplementedError, "#{what} needs the date C extension, which the minimal Build lacks"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self._parse(*) = unavailable("Date._parse (Time.parse)")
|
|
18
|
+
def self._strptime(*) = unavailable("Date._strptime (Time.strptime)")
|
|
19
|
+
def self.new(*) = unavailable("Date.new")
|
|
20
|
+
def self.today(*) = unavailable("Date.today")
|
|
21
|
+
def self.parse(*) = unavailable("Date.parse")
|
|
22
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# encoding_constants.rb (ruby_wasm_ic_minimal): the Encoding constants a canister lacks.
|
|
2
|
+
# The `minimal` Build has three encodings, UTF-8, US-ASCII and ASCII-8BIT,
|
|
3
|
+
# and no constant naming any of them (ADR-0060). Rack names
|
|
4
|
+
# Encoding::UTF_8 to decode every query parameter, and `cgi/util` names it as
|
|
5
|
+
# it loads. Each is found here from a literal, without naming a constant:
|
|
6
|
+
# a "\u" escape is always UTF-8, a Symbol's text US-ASCII, String#b binary.
|
|
7
|
+
# Only these three: there is no transcoding to any other (ADR-0053).
|
|
8
|
+
require_relative "../guard"
|
|
9
|
+
RubyWasmIcMinimal.guard!("encoding_constants")
|
|
10
|
+
class Encoding
|
|
11
|
+
UTF_8 = "\u0000".encoding unless const_defined?(:UTF_8, false)
|
|
12
|
+
US_ASCII = :a.to_s.encoding unless const_defined?(:US_ASCII, false)
|
|
13
|
+
ASCII_8BIT = "".b.encoding unless const_defined?(:ASCII_8BIT, false)
|
|
14
|
+
BINARY = ASCII_8BIT unless const_defined?(:BINARY, false)
|
|
15
|
+
ASCII = US_ASCII unless const_defined?(:ASCII, false)
|
|
16
|
+
end
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
# stringio.rb (ruby_wasm_ic_minimal): a pure-Ruby StringIO for the `minimal` Build, where
|
|
2
|
+
# CRuby's is a C extension it does not link. Rack names it for request bodies
|
|
3
|
+
# (rack.input) and in Rack::MockRequest; an Application may use it as any
|
|
4
|
+
# in-memory IO.
|
|
5
|
+
#
|
|
6
|
+
# The common reading and writing API, by bytes, with CRuby's meaning. Not
|
|
7
|
+
# here: the encoding conversions (set_encoding only tags), fcntl, and the
|
|
8
|
+
# *_nonblock methods.
|
|
9
|
+
require_relative "../guard"
|
|
10
|
+
RubyWasmIcMinimal.guard!("stringio")
|
|
11
|
+
class StringIO
|
|
12
|
+
include Enumerable
|
|
13
|
+
|
|
14
|
+
attr_reader :string, :lineno
|
|
15
|
+
attr_writer :lineno
|
|
16
|
+
|
|
17
|
+
def self.open(*args)
|
|
18
|
+
io = new(*args)
|
|
19
|
+
return io unless block_given?
|
|
20
|
+
|
|
21
|
+
begin
|
|
22
|
+
yield io
|
|
23
|
+
ensure
|
|
24
|
+
io.close
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def initialize(string = +"", mode = nil)
|
|
29
|
+
@string = string
|
|
30
|
+
@pos = 0
|
|
31
|
+
@lineno = 0
|
|
32
|
+
@closed_read = @closed_write = false
|
|
33
|
+
@readonly = string.frozen? || mode.to_s.start_with?("r") && !mode.to_s.include?("+")
|
|
34
|
+
@string.replace("") if mode.to_s.start_with?("w") && !@string.frozen?
|
|
35
|
+
@pos = @string.bytesize if mode.to_s.start_with?("a")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def string=(s)
|
|
39
|
+
@string = s
|
|
40
|
+
@pos = 0
|
|
41
|
+
@lineno = 0
|
|
42
|
+
s
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def pos = @pos
|
|
46
|
+
alias tell pos
|
|
47
|
+
|
|
48
|
+
def pos=(n)
|
|
49
|
+
raise Errno::EINVAL, "negative position" if n.negative?
|
|
50
|
+
|
|
51
|
+
@pos = n
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def seek(offset, whence = IO::SEEK_SET)
|
|
55
|
+
base = case whence
|
|
56
|
+
when IO::SEEK_SET, :SET then 0
|
|
57
|
+
when IO::SEEK_CUR, :CUR then @pos
|
|
58
|
+
when IO::SEEK_END, :END then @string.bytesize
|
|
59
|
+
else raise Errno::EINVAL, "invalid whence"
|
|
60
|
+
end
|
|
61
|
+
self.pos = base + offset
|
|
62
|
+
0
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def rewind
|
|
66
|
+
@pos = 0
|
|
67
|
+
@lineno = 0
|
|
68
|
+
0
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def size = @string.bytesize
|
|
72
|
+
alias length size
|
|
73
|
+
def eof? = @pos >= @string.bytesize
|
|
74
|
+
alias eof eof?
|
|
75
|
+
|
|
76
|
+
def read(length = nil, buffer = nil)
|
|
77
|
+
readable!
|
|
78
|
+
if length.nil?
|
|
79
|
+
out = (@string.byteslice(@pos, @string.bytesize - @pos) || "").dup
|
|
80
|
+
@pos = [@pos, @string.bytesize].max
|
|
81
|
+
out.force_encoding(@string.encoding)
|
|
82
|
+
else
|
|
83
|
+
raise ArgumentError, "negative length #{length} given" if length.negative?
|
|
84
|
+
|
|
85
|
+
out = (@string.byteslice(@pos, length) || "").b
|
|
86
|
+
@pos += out.bytesize
|
|
87
|
+
return (buffer&.replace("") && nil) if length.positive? && out.empty?
|
|
88
|
+
end
|
|
89
|
+
buffer ? buffer.replace(out) : out
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def read_nonblock(length, buffer = nil, exception: true)
|
|
93
|
+
out = read(length, buffer)
|
|
94
|
+
return out unless out.nil?
|
|
95
|
+
raise EOFError, "end of file reached" if exception
|
|
96
|
+
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def readpartial(length, buffer = nil)
|
|
101
|
+
out = read(length, buffer)
|
|
102
|
+
raise EOFError, "end of file reached" if out.nil?
|
|
103
|
+
|
|
104
|
+
out
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def getc
|
|
108
|
+
readable!
|
|
109
|
+
return nil if eof?
|
|
110
|
+
|
|
111
|
+
ch = (@string.byteslice(@pos, @string.bytesize - @pos) || "").force_encoding(@string.encoding)[0]
|
|
112
|
+
@pos += ch.bytesize
|
|
113
|
+
ch
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def getbyte
|
|
117
|
+
readable!
|
|
118
|
+
return nil if eof?
|
|
119
|
+
|
|
120
|
+
b = @string.getbyte(@pos)
|
|
121
|
+
@pos += 1
|
|
122
|
+
b
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def each_byte
|
|
126
|
+
return enum_for(:each_byte) unless block_given?
|
|
127
|
+
|
|
128
|
+
while (b = getbyte)
|
|
129
|
+
yield b
|
|
130
|
+
end
|
|
131
|
+
self
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def ungetc(s)
|
|
135
|
+
readable!
|
|
136
|
+
return nil if s.nil?
|
|
137
|
+
|
|
138
|
+
s = s.chr if s.is_a?(Integer)
|
|
139
|
+
if @pos.zero?
|
|
140
|
+
@string.prepend(s)
|
|
141
|
+
else
|
|
142
|
+
@pos -= s.bytesize
|
|
143
|
+
@string.bytesplice(@pos, s.bytesize, s)
|
|
144
|
+
end
|
|
145
|
+
nil
|
|
146
|
+
end
|
|
147
|
+
alias ungetbyte ungetc
|
|
148
|
+
|
|
149
|
+
def gets(sep = $/, limit = nil, chomp: false)
|
|
150
|
+
readable!
|
|
151
|
+
sep, limit = nil, sep if sep.is_a?(Integer)
|
|
152
|
+
return nil if eof?
|
|
153
|
+
|
|
154
|
+
rest = (@string.byteslice(@pos, @string.bytesize - @pos) || "").force_encoding(@string.encoding)
|
|
155
|
+
line =
|
|
156
|
+
if sep.nil?
|
|
157
|
+
rest
|
|
158
|
+
elsif sep.empty?
|
|
159
|
+
rest.sub!(/\A\n+/) { |nl| @pos += nl.bytesize; "" }
|
|
160
|
+
(i = rest.index(/\n\n+/)) ? rest[0, i] + rest[i..][/\A\n+/] : rest
|
|
161
|
+
else
|
|
162
|
+
(i = rest.index(sep)) ? rest[0, i + sep.length] : rest
|
|
163
|
+
end
|
|
164
|
+
line = line.byteslice(0, limit) if limit && limit >= 0 && line.bytesize > limit
|
|
165
|
+
@pos += line.bytesize
|
|
166
|
+
@lineno += 1
|
|
167
|
+
$_ = chomp && sep ? line.chomp(sep.empty? ? "\n" : sep) : line
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def readline(*args, **kw) = gets(*args, **kw) || raise(EOFError, "end of file reached")
|
|
171
|
+
|
|
172
|
+
def each_line(sep = $/, limit = nil, chomp: false)
|
|
173
|
+
return enum_for(:each_line, sep, limit, chomp: chomp) unless block_given?
|
|
174
|
+
|
|
175
|
+
while (line = gets(sep, limit, chomp: chomp))
|
|
176
|
+
yield line
|
|
177
|
+
end
|
|
178
|
+
self
|
|
179
|
+
end
|
|
180
|
+
alias each each_line
|
|
181
|
+
|
|
182
|
+
def readlines(*args, **kw) = each_line(*args, **kw).to_a
|
|
183
|
+
|
|
184
|
+
def write(*objs)
|
|
185
|
+
writable!
|
|
186
|
+
objs.sum do |obj|
|
|
187
|
+
s = obj.to_s
|
|
188
|
+
if @pos > @string.bytesize
|
|
189
|
+
@string << ("\0" * (@pos - @string.bytesize))
|
|
190
|
+
end
|
|
191
|
+
@string.bytesplice(@pos, [s.bytesize, @string.bytesize - @pos].min, s.b.force_encoding(@string.encoding))
|
|
192
|
+
@pos += s.bytesize
|
|
193
|
+
s.bytesize
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
alias syswrite write
|
|
197
|
+
alias write_nonblock write
|
|
198
|
+
|
|
199
|
+
def <<(obj)
|
|
200
|
+
write(obj)
|
|
201
|
+
self
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def print(*objs)
|
|
205
|
+
objs = [$_] if objs.empty?
|
|
206
|
+
objs.each { write(_1) }
|
|
207
|
+
write($\) if $\
|
|
208
|
+
nil
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def puts(*objs)
|
|
212
|
+
return write("\n") && nil if objs.empty?
|
|
213
|
+
|
|
214
|
+
objs.flatten.each do |o|
|
|
215
|
+
s = o.nil? ? "" : o.to_s
|
|
216
|
+
write(s.end_with?("\n") ? s : "#{s}\n")
|
|
217
|
+
end
|
|
218
|
+
nil
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def printf(fmt, *args) = write(format(fmt, *args)) && nil
|
|
222
|
+
def putc(c) = (write(c.is_a?(Integer) ? (c & 0xff).chr : c.to_s[0]) && c)
|
|
223
|
+
|
|
224
|
+
def truncate(n)
|
|
225
|
+
writable!
|
|
226
|
+
@string.replace(n < @string.bytesize ? @string.byteslice(0, n) : @string + ("\0" * (n - @string.bytesize)))
|
|
227
|
+
0
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def close
|
|
231
|
+
@closed_read = @closed_write = true
|
|
232
|
+
nil
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def close_read = (@closed_read = true) && nil
|
|
236
|
+
def close_write = (@closed_write = true) && nil
|
|
237
|
+
def closed? = @closed_read && @closed_write
|
|
238
|
+
def closed_read? = @closed_read
|
|
239
|
+
def closed_write? = @closed_write
|
|
240
|
+
|
|
241
|
+
def binmode
|
|
242
|
+
@string.force_encoding("".b.encoding) unless @string.frozen?
|
|
243
|
+
self
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
def set_encoding(*) = self
|
|
247
|
+
def external_encoding = @string.encoding
|
|
248
|
+
def internal_encoding = nil
|
|
249
|
+
def sync = true
|
|
250
|
+
def sync=(value)
|
|
251
|
+
value
|
|
252
|
+
end
|
|
253
|
+
def flush = self
|
|
254
|
+
def fsync = 0
|
|
255
|
+
def fileno = nil
|
|
256
|
+
def isatty = false
|
|
257
|
+
alias tty? isatty
|
|
258
|
+
def pid = nil
|
|
259
|
+
def fcntl(*) = raise(NotImplementedError, "fcntl")
|
|
260
|
+
|
|
261
|
+
private
|
|
262
|
+
|
|
263
|
+
def readable!
|
|
264
|
+
raise IOError, "not opened for reading" if @closed_read
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def writable!
|
|
268
|
+
raise IOError, "not opened for writing" if @closed_write || @readonly
|
|
269
|
+
end
|
|
270
|
+
end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# strscan.rb (ruby_wasm_ic_minimal): a pure-Ruby StringScanner for the `minimal` Build,
|
|
2
|
+
# where CRuby's is a C extension it does not link. Rack's multipart parser
|
|
3
|
+
# requires it at load, so Rack::Request cannot load without it, and it is what
|
|
4
|
+
# reads a multipart body.
|
|
5
|
+
#
|
|
6
|
+
# The common API, with CRuby's meaning: positions are byte offsets, a pattern
|
|
7
|
+
# is matched only at the scan pointer (scan, skip, match?, check; a Regexp
|
|
8
|
+
# or a String) or anywhere after it (*_until, exist?; a Regexp only, as in
|
|
9
|
+
# Ruby 3.3). As in CRuby's default mode, the text begins at the pointer: \A
|
|
10
|
+
# and ^ match there, which Rack's boundary pattern depends on. Not here: the
|
|
11
|
+
# fixed_anchor: mode, named_captures, scan_byte and peek_byte. Checked
|
|
12
|
+
# against CRuby's on MRI, operation by operation.
|
|
13
|
+
require_relative "../guard"
|
|
14
|
+
RubyWasmIcMinimal.guard!("strscan")
|
|
15
|
+
class StringScanner
|
|
16
|
+
class Error < StandardError; end
|
|
17
|
+
|
|
18
|
+
attr_reader :string
|
|
19
|
+
|
|
20
|
+
def initialize(string, _dup = false, fixed_anchor: false)
|
|
21
|
+
raise ArgumentError, "fixed_anchor: true is not supported here" if fixed_anchor
|
|
22
|
+
|
|
23
|
+
@string = string
|
|
24
|
+
@pos = 0
|
|
25
|
+
clear
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def string=(s)
|
|
29
|
+
@string = s
|
|
30
|
+
@pos = 0
|
|
31
|
+
clear
|
|
32
|
+
s
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def concat(s)
|
|
36
|
+
@string << s
|
|
37
|
+
self
|
|
38
|
+
end
|
|
39
|
+
alias << concat
|
|
40
|
+
|
|
41
|
+
def pos = @pos
|
|
42
|
+
|
|
43
|
+
def pos=(n)
|
|
44
|
+
n += @string.bytesize if n.negative?
|
|
45
|
+
raise RangeError, "index out of range" unless n.between?(0, @string.bytesize)
|
|
46
|
+
|
|
47
|
+
@pos = n
|
|
48
|
+
end
|
|
49
|
+
alias pointer pos
|
|
50
|
+
alias pointer= pos=
|
|
51
|
+
|
|
52
|
+
def charpos = @string.byteslice(0, @pos).length
|
|
53
|
+
|
|
54
|
+
def reset
|
|
55
|
+
@pos = 0
|
|
56
|
+
clear
|
|
57
|
+
self
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def terminate
|
|
61
|
+
@pos = @string.bytesize
|
|
62
|
+
clear
|
|
63
|
+
self
|
|
64
|
+
end
|
|
65
|
+
alias clear_terminate terminate
|
|
66
|
+
|
|
67
|
+
def eos? = @pos >= @string.bytesize
|
|
68
|
+
def rest = @string.byteslice(@pos, @string.bytesize - @pos) || ""
|
|
69
|
+
def rest? = !eos?
|
|
70
|
+
def rest_size = @string.bytesize - @pos
|
|
71
|
+
def beginning_of_line? = @pos.zero? || @string.getbyte(@pos - 1) == 10
|
|
72
|
+
alias bol? beginning_of_line?
|
|
73
|
+
|
|
74
|
+
# At the pointer: advance, return the match.
|
|
75
|
+
def scan(pattern) = go(pattern, anchored: true, advance: true, string: true)
|
|
76
|
+
# At the pointer: advance, return its length.
|
|
77
|
+
def skip(pattern) = go(pattern, anchored: true, advance: true, string: false)
|
|
78
|
+
# At the pointer: do not advance, return its length.
|
|
79
|
+
def match?(pattern) = go(pattern, anchored: true, advance: false, string: false)
|
|
80
|
+
# At the pointer: do not advance, return the match.
|
|
81
|
+
def check(pattern) = go(pattern, anchored: true, advance: false, string: true)
|
|
82
|
+
# Anywhere after it: advance past the match, return what was passed over.
|
|
83
|
+
def scan_until(pattern) = go(pattern, anchored: false, advance: true, string: true)
|
|
84
|
+
def skip_until(pattern) = go(pattern, anchored: false, advance: true, string: false)
|
|
85
|
+
def check_until(pattern) = go(pattern, anchored: false, advance: false, string: true)
|
|
86
|
+
def exist?(pattern) = go(pattern, anchored: false, advance: false, string: false)
|
|
87
|
+
|
|
88
|
+
def search_full(pattern, advance, return_string)
|
|
89
|
+
go(pattern, anchored: false, advance: advance, string: return_string)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def scan_full(pattern, advance, return_string)
|
|
93
|
+
go(pattern, anchored: true, advance: advance, string: return_string)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def getch
|
|
97
|
+
return nil if eos?
|
|
98
|
+
|
|
99
|
+
ch = rest[0]
|
|
100
|
+
set_match(@pos, @pos + ch.bytesize, nil)
|
|
101
|
+
@pos += ch.bytesize
|
|
102
|
+
ch
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def get_byte
|
|
106
|
+
return nil if eos?
|
|
107
|
+
|
|
108
|
+
b = @string.byteslice(@pos, 1)
|
|
109
|
+
set_match(@pos, @pos + 1, nil)
|
|
110
|
+
@pos += 1
|
|
111
|
+
b
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def peek(n) = @string.byteslice(@pos, n) || ""
|
|
115
|
+
|
|
116
|
+
def unscan
|
|
117
|
+
raise Error, "unscan error: nothing to unscan" unless @last
|
|
118
|
+
|
|
119
|
+
@pos = @last
|
|
120
|
+
clear
|
|
121
|
+
self
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def matched? = !@m_end.nil?
|
|
125
|
+
def matched = matched? ? @string.byteslice(@m_start, @m_end - @m_start) : nil
|
|
126
|
+
def matched_size = matched? ? @m_end - @m_start : nil
|
|
127
|
+
def pre_match = matched? ? @string.byteslice(0, @m_start) : nil
|
|
128
|
+
def post_match = matched? ? @string.byteslice(@m_end, @string.bytesize - @m_end) : nil
|
|
129
|
+
|
|
130
|
+
def [](i)
|
|
131
|
+
return nil unless matched?
|
|
132
|
+
return (i.zero? ? matched : nil) unless @md
|
|
133
|
+
|
|
134
|
+
@md[i]
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def size = matched? ? (@md ? @md.size : 1) : nil
|
|
138
|
+
def captures = @md ? @md.captures : (matched? ? [] : nil)
|
|
139
|
+
def values_at(*is) = matched? ? is.map { self[_1] } : nil
|
|
140
|
+
|
|
141
|
+
def inspect
|
|
142
|
+
return "#<StringScanner fin>" if eos?
|
|
143
|
+
|
|
144
|
+
"#<StringScanner #{@pos}/#{@string.bytesize} @ #{peek(5).inspect}>"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
private
|
|
148
|
+
|
|
149
|
+
def clear
|
|
150
|
+
@m_start = @m_end = @md = @last = nil
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def set_match(from, to, md)
|
|
154
|
+
@last = @pos
|
|
155
|
+
@m_start = from
|
|
156
|
+
@m_end = to
|
|
157
|
+
@md = md
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def go(pattern, anchored:, advance:, string:)
|
|
161
|
+
start = @pos
|
|
162
|
+
if pattern.is_a?(String)
|
|
163
|
+
# As Ruby 3.3's strscan: a String only where the match is at the pointer.
|
|
164
|
+
raise TypeError, "wrong argument type String (expected Regexp)" unless anchored
|
|
165
|
+
return fail_match unless @string.byteslice(start, pattern.bytesize) == pattern
|
|
166
|
+
|
|
167
|
+
from = start
|
|
168
|
+
to = from + pattern.bytesize
|
|
169
|
+
md = nil
|
|
170
|
+
else
|
|
171
|
+
# As CRuby's (fixed_anchor: false): the text begins at the pointer, so
|
|
172
|
+
# \A and ^ match there and nothing before it is seen.
|
|
173
|
+
md = regexp(pattern, anchored).match(rest)
|
|
174
|
+
return fail_match unless md
|
|
175
|
+
|
|
176
|
+
from = start + md.byteoffset(0)[0]
|
|
177
|
+
to = start + md.byteoffset(0)[1]
|
|
178
|
+
end
|
|
179
|
+
set_match(from, to, md)
|
|
180
|
+
@pos = to if advance
|
|
181
|
+
length = to - start
|
|
182
|
+
return length unless string
|
|
183
|
+
|
|
184
|
+
anchored ? @string.byteslice(from, to - from) : @string.byteslice(start, length)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
def fail_match
|
|
188
|
+
clear
|
|
189
|
+
nil
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def regexp(re, anchored)
|
|
193
|
+
return re unless anchored
|
|
194
|
+
|
|
195
|
+
(@anchored ||= {})[re] ||= Regexp.new("\\A(?:#{re.source})", re.options)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ruby_wasm_ic_minimal: pure-Ruby stand-ins for what Rack and Roda need and a
|
|
2
|
+
# `minimal` canister lacks, for canisters packed by ruby_wasm_builder on its
|
|
3
|
+
# `minimal` Build only.
|
|
4
|
+
#
|
|
5
|
+
# Requiring this file, first and before Rack, puts the stand-ins' directory
|
|
6
|
+
# first on $LOAD_PATH and loads all four, so Rack's own `require "strscan"`
|
|
7
|
+
# and `time`'s `require "date"` find them by their libraries' names. They live
|
|
8
|
+
# in a directory of their own, not in lib/, so a Gemfile or `gem install` that
|
|
9
|
+
# puts lib/ on $LOAD_PATH shadows nothing (0.1.3). Anywhere but a `minimal`
|
|
10
|
+
# canister this file, and each stand-in, raises LoadError (see
|
|
11
|
+
# ruby_wasm_ic_minimal/guard.rb).
|
|
12
|
+
require_relative "ruby_wasm_ic_minimal/version"
|
|
13
|
+
require_relative "ruby_wasm_ic_minimal/guard"
|
|
14
|
+
RubyWasmIcMinimal.guard!
|
|
15
|
+
|
|
16
|
+
module RubyWasmIcMinimal
|
|
17
|
+
STANDINS = File.join(__dir__, "ruby_wasm_ic_minimal", "standins")
|
|
18
|
+
end
|
|
19
|
+
$LOAD_PATH.unshift(RubyWasmIcMinimal::STANDINS) unless $LOAD_PATH.include?(RubyWasmIcMinimal::STANDINS)
|
|
20
|
+
require "encoding_constants"
|
|
21
|
+
require "stringio"
|
|
22
|
+
require "strscan"
|
|
23
|
+
require "date"
|
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ruby_wasm_ic_minimal
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.3
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- graial
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-10-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: This gem contains the four carried files required for running rack and
|
|
14
|
+
roda on the minimal ruby.wasm build.
|
|
15
|
+
email:
|
|
16
|
+
- graial@paglalayag.net
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/ruby_wasm_ic_minimal.rb
|
|
24
|
+
- lib/ruby_wasm_ic_minimal/guard.rb
|
|
25
|
+
- lib/ruby_wasm_ic_minimal/standins/date.rb
|
|
26
|
+
- lib/ruby_wasm_ic_minimal/standins/encoding_constants.rb
|
|
27
|
+
- lib/ruby_wasm_ic_minimal/standins/stringio.rb
|
|
28
|
+
- lib/ruby_wasm_ic_minimal/standins/strscan.rb
|
|
29
|
+
- lib/ruby_wasm_ic_minimal/version.rb
|
|
30
|
+
homepage: https://github.com/graial/ruby_wasm_ic_minimal
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata: {}
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: 3.3.3
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubygems_version: 3.5.11
|
|
50
|
+
signing_key:
|
|
51
|
+
specification_version: 4
|
|
52
|
+
summary: Four carried files required for running rack and roda on the minimal ruby.wasm
|
|
53
|
+
build
|
|
54
|
+
test_files: []
|