lexer_kit 0.6.1 → 0.7.0

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.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/Cargo.lock +265 -0
  3. data/Cargo.toml +37 -0
  4. data/README.md +7 -3
  5. data/crates/lexer_kit_core/Cargo.toml +17 -0
  6. data/crates/lexer_kit_core/src/dfa.rs +437 -0
  7. data/crates/lexer_kit_core/src/format.rs +599 -0
  8. data/crates/lexer_kit_core/src/lib.rs +18 -0
  9. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/opcodes.rs +375 -1
  10. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/safety_test.rs +21 -16
  11. data/crates/lexer_kit_core/src/token_buffer.rs +130 -0
  12. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/trie.rs +37 -1
  13. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/types.rs +1 -1
  14. data/{ext/lexer_kit_rust → crates/lexer_kit_core}/src/vm.rs +165 -2
  15. data/ext/lexer_kit_rust/Cargo.toml +7 -3
  16. data/ext/lexer_kit_rust/src/fast_stream.rs +101 -438
  17. data/ext/lexer_kit_rust/src/lib.rs +200 -215
  18. data/lib/lexer_kit/builder/compiler.rb +50 -15
  19. data/lib/lexer_kit/builder/conflict_detector.rb +18 -20
  20. data/lib/lexer_kit/builder/validator.rb +45 -0
  21. data/lib/lexer_kit/builder.rb +34 -7
  22. data/lib/lexer_kit/cli/commands.rb +17 -7
  23. data/lib/lexer_kit/core/source.rb +3 -2
  24. data/lib/lexer_kit/debug/disassembler.rb +0 -2
  25. data/lib/lexer_kit/dfa/dfa_builder.rb +1 -1
  26. data/lib/lexer_kit/dfa/dfa_minimizer.rb +2 -2
  27. data/lib/lexer_kit/dfa/regex_ast.rb +4 -0
  28. data/lib/lexer_kit/dfa/regex_parser.rb +41 -8
  29. data/lib/lexer_kit/errors.rb +2 -2
  30. data/lib/lexer_kit/format/lkb1.rb +3 -3
  31. data/lib/lexer_kit/format/lkt1.rb +3 -1
  32. data/lib/lexer_kit/format.rb +4 -4
  33. data/lib/lexer_kit/ir/compiled_program.rb +39 -26
  34. data/lib/lexer_kit/ir/constant_pool.rb +33 -11
  35. data/lib/lexer_kit/ir/dfa_table.rb +9 -19
  36. data/lib/lexer_kit/ir/instruction.rb +3 -0
  37. data/lib/lexer_kit/ir/jump_table.rb +3 -13
  38. data/lib/lexer_kit/ir/keyword_table.rb +0 -9
  39. data/lib/lexer_kit/ir/limits.rb +48 -0
  40. data/lib/lexer_kit/ir/serializer.rb +69 -30
  41. data/lib/lexer_kit/ir.rb +1 -0
  42. data/lib/lexer_kit/version.rb +1 -1
  43. data/lib/lexer_kit.rb +15 -2
  44. data/vendor/nupur/Cargo.toml +24 -0
  45. data/vendor/nupur/LICENSE +176 -0
  46. data/vendor/nupur/NOTICE +64 -0
  47. data/vendor/nupur/README.ja.md +150 -0
  48. data/vendor/nupur/README.md +257 -0
  49. data/vendor/nupur/src/abi.rs +143 -0
  50. data/vendor/nupur/src/args.rs +318 -0
  51. data/vendor/nupur/src/build.rs +101 -0
  52. data/vendor/nupur/src/caught.rs +171 -0
  53. data/vendor/nupur/src/cname.rs +37 -0
  54. data/vendor/nupur/src/describe.rs +175 -0
  55. data/vendor/nupur/src/encoding.rs +240 -0
  56. data/vendor/nupur/src/gvl.rs +181 -0
  57. data/vendor/nupur/src/hash.rs +52 -0
  58. data/vendor/nupur/src/inject.rs +20 -0
  59. data/vendor/nupur/src/lib.rs +280 -0
  60. data/vendor/nupur/src/lint_probe.rs +142 -0
  61. data/vendor/nupur/src/msg.rs +193 -0
  62. data/vendor/nupur/src/no_unwind.rs +39 -0
  63. data/vendor/nupur/src/raw/check.rs +71 -0
  64. data/vendor/nupur/src/raw/data.rs +28 -0
  65. data/vendor/nupur/src/raw/define.rs +215 -0
  66. data/vendor/nupur/src/raw/exception.rs +69 -0
  67. data/vendor/nupur/src/raw/iterate.rs +115 -0
  68. data/vendor/nupur/src/raw/mod.rs +105 -0
  69. data/vendor/nupur/src/raw/protect.rs +144 -0
  70. data/vendor/nupur/src/raw/string.rs +148 -0
  71. data/vendor/nupur/src/raw/symbol.rs +38 -0
  72. data/vendor/nupur/src/raw/values.rs +293 -0
  73. data/vendor/nupur/src/rb/call.rs +118 -0
  74. data/vendor/nupur/src/rb/class.rs +170 -0
  75. data/vendor/nupur/src/rb/convert.rs +134 -0
  76. data/vendor/nupur/src/rb/error.rs +355 -0
  77. data/vendor/nupur/src/rb/global.rs +141 -0
  78. data/vendor/nupur/src/rb/kept.rs +55 -0
  79. data/vendor/nupur/src/rb/method/frame.rs +81 -0
  80. data/vendor/nupur/src/rb/method.rs +468 -0
  81. data/vendor/nupur/src/rb/mod.rs +40 -0
  82. data/vendor/nupur/src/rb/object.rs +66 -0
  83. data/vendor/nupur/src/rb/ruby.rs +107 -0
  84. data/vendor/nupur/src/rb/value/header.rs +65 -0
  85. data/vendor/nupur/src/rb/value/id.rs +61 -0
  86. data/vendor/nupur/src/rb/value/integer.rs +132 -0
  87. data/vendor/nupur/src/rb/value.rs +454 -0
  88. data/vendor/nupur/src/stack.rs +32 -0
  89. data/vendor/nupur/src/string/make.rs +234 -0
  90. data/vendor/nupur/src/string/mod.rs +24 -0
  91. data/vendor/nupur/src/string/read.rs +411 -0
  92. data/vendor/nupur/src/table.rs +58 -0
  93. data/vendor/nupur/src/typed/callbacks.rs +125 -0
  94. data/vendor/nupur/src/typed/data_type.rs +132 -0
  95. data/vendor/nupur/src/typed/embedded.rs +103 -0
  96. data/vendor/nupur/src/typed/external.rs +76 -0
  97. data/vendor/nupur/src/typed/hidden.rs +95 -0
  98. data/vendor/nupur/src/typed/mod.rs +373 -0
  99. data/vendor/nupur/src/utf8_copy.rs +262 -0
  100. data/vendor/nupur/sys/Cargo.toml +29 -0
  101. data/vendor/nupur/sys/src/data_type.rs +213 -0
  102. data/vendor/nupur/sys/src/int.rs +248 -0
  103. data/vendor/nupur/sys/src/lib.rs +61 -0
  104. data/vendor/nupur/sys/src/lint_probe.rs +93 -0
  105. data/vendor/nupur/sys/src/no_unwind.rs +99 -0
  106. data/vendor/nupur/sys/src/non_raising.rs +585 -0
  107. data/vendor/nupur/sys/src/protect.rs +349 -0
  108. data/vendor/nupur/sys/src/raising.rs +257 -0
  109. data/vendor/nupur/sys/src/value.rs +279 -0
  110. data/vendor/nupur.sha256 +68 -0
  111. metadata +92 -12
  112. data/ext/lexer_kit_rust/src/deserializer.rs +0 -213
  113. data/ext/lexer_kit_rust/src/dfa.rs +0 -217
@@ -0,0 +1,24 @@
1
+ [package]
2
+ name = "nupur"
3
+ version = "0.0.1"
4
+ edition = "2021"
5
+ rust-version = "1.95"
6
+ publish = false
7
+ description = "A Ruby binding layer for extensions in Rust: values, exceptions, method trampolines, TypedData and the GVL, over rb-sys (written for Makiri)"
8
+
9
+ [lib]
10
+ path = "src/lib.rs"
11
+
12
+ [lints]
13
+ workspace = true
14
+
15
+ [features]
16
+ # The hooks of `rake oom`: the extension installs both (`inject::install`) -
17
+ # one makes the armed allocating call raise, the other fails a checked
18
+ # copy's heap buffer. Off, the checks are gone. The extension's own `alloc-inject` turns this on.
19
+ alloc-inject = ["nupur_sys/alloc-inject"]
20
+
21
+ [dependencies]
22
+ # Ruby's C API, through the one crate that depends on rb-sys (`sys/`): Nupur
23
+ # names `nupur_sys::`, never rb-sys.
24
+ nupur_sys = { path = "sys", version = "0.0.1" }
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for describing the origin of the Work and
141
+ reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Support. While redistributing the Work or
166
+ Derivative Works thereof, You may choose to offer, and charge a
167
+ fee for, acceptance of support, warranty, indemnity, or other
168
+ liability obligations and/or rights consistent with this License.
169
+ However, in accepting such obligations, You may act only on Your
170
+ own behalf and on Your sole responsibility, not on behalf of any
171
+ other Contributor, and only if You agree to indemnify, defend,
172
+ and hold each Contributor harmless for any liability incurred by,
173
+ or claims asserted against, such Contributor by reason of your
174
+ accepting any such warranty or support.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,64 @@
1
+ Makiri
2
+ Copyright 2026 Makiri contributors
3
+
4
+ This product includes software developed by:
5
+
6
+ * Lexbor (vendored as a git submodule under vendor/lexbor)
7
+ Copyright (C) Alexander Borisov
8
+ Licensed under the Apache License, Version 2.0
9
+ https://lexbor.com / https://github.com/lexbor/lexbor
10
+
11
+ The HTML tokenizer and tree builder under ext/makiri/rust/src/html/ and the
12
+ CSS syntax, selector and stylesheet parser under
13
+ ext/makiri/rust/src/selectors/ are Rust ports of Lexbor's, as are the HTML
14
+ CSS matcher under ext/makiri/rust/src/css_match/ and the tag and
15
+ attribute tables generated from Lexbor's. The extension links no Lexbor
16
+ code; Lexbor's LICENSE and NOTICE are kept under vendor/lexbor/ in the gem.
17
+
18
+ * encoding_rs (linked into the native extension)
19
+ Copyright Mozilla Foundation
20
+ Licensed under the Apache License, Version 2.0 (at the licensee's option,
21
+ also the MIT license)
22
+ https://github.com/hsivonen/encoding_rs
23
+
24
+ encoding_rs includes data derived from the data files supplied with the
25
+ WHATWG Encoding Standard, under this license:
26
+
27
+ Copyright © WHATWG (Apple, Google, Mozilla, Microsoft).
28
+
29
+ Redistribution and use in source and binary forms, with or without
30
+ modification, are permitted provided that the following conditions are
31
+ met:
32
+
33
+ 1. Redistributions of source code must retain the above copyright
34
+ notice, this list of conditions and the following disclaimer.
35
+
36
+ 2. Redistributions in binary form must reproduce the above copyright
37
+ notice, this list of conditions and the following disclaimer in the
38
+ documentation and/or other materials provided with the distribution.
39
+
40
+ 3. Neither the name of the copyright holder nor the names of its
41
+ contributors may be used to endorse or promote products derived from
42
+ this software without specific prior written permission.
43
+
44
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
45
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
46
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
48
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
49
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
50
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
51
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
52
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
53
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
54
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55
+
56
+ * chardetng (linked into the native extension)
57
+ Copyright Mozilla Foundation
58
+ Licensed under the Apache License, Version 2.0 (at the licensee's option,
59
+ also the MIT license)
60
+ https://github.com/hsivonen/chardetng
61
+
62
+ The XPath 1.0 engine under ext/makiri/rust/src/xpath/ is an original
63
+ implementation written for this project. No code is derived from libxml2 or
64
+ libxslt.
@@ -0,0 +1,150 @@
1
+ # Nupur(ヌプㇽ)
2
+
3
+ Nupur は Makiri の拡張(extension)における Ruby バインディングレイヤーです。
4
+ これにより、拡張ライブラリは Ruby の C APIに対応する安全な API を利用できます。これには Ruby の値とそのクラスチェック済みハンドル、例外、メソッドトランポリン、TypedData、GVL、および例外を発生する可能性のあるすべての C 呼び出しに対するラッパーが含まれます。
5
+ Ruby とやりとりするすべての `unsafe` コードは、ただ一つの例外を除いて Nupur またはその配下のクレートに実装されています。その唯一の例外は、拡張ライブラリの `Init_` であり、定義を `run_init` に渡します。
6
+
7
+ レイヤー構成(上から順に):
8
+
9
+ ```
10
+ makiri_rs 拡張ライブラリ:Makiri の Ruby メソッド
11
+ nupur nupur_sys 上に構築された安全な API
12
+ nupur_sys (sys/) Ruby の C API。各呼び出しは証明(proof)を受け取る。rb-sys に依存する唯一のクレート
13
+ rb-sys Ruby の C API(生成されたバインディング)
14
+ ```
15
+
16
+ Nupurは拡張ライブラリ自体に依存しておらず、その存在すら認識していません。Nupurが拡張から必要とするもの(破損した内部不変条件に対する例外クラスや、`rake oom` フック)は、拡張によって `Init_` に登録されます。この拡張は`crate::nupur` としてNupurに公開され、その `bridge` は `rb`、`typed`、`gvl`、`build`、`hash`、および `args` を再エクスポートします。
17
+
18
+ ## 保証しているもの
19
+
20
+ Nupurのルールの大部分は、その型とクレートの依存関係であり、これらを破るコードはコンパイルできません。`src/lib.rs`にそれぞれ詳細が記載されています。
21
+
22
+ - **raiseがRustの後始末を飛び越えない** raiseは`longjmp`であり、またいだRustフレームのデストラクタを実行しません。raiseを引き起こし得るすべてのC呼び出しは、`&Protected`を受け取る`nupur_sys::raising`のラッパーとなっています。`&Protected`を作るのは`rb_protect`のサンクだけで、サンクはそれを`raw::call`が実行するクロージャに貸し出すため、raiseは`Err`として返されます。NupurにはC関数を呼び出す他の手段はありません。rb-sysに依存しておらず、`nupur_sys`もC関数を再エクスポートしていません。保護の外でraiseが起こるのは次の2か所で、どちらも解放すべきものを持たないフレームしかまたぎません。
23
+ - メソッドのトランポリンが`Err`をraiseするとき(または`break`や`throw`を再開するとき)と、`Init_`が`fatal`をraiseするとき。いずれもメソッドのすべてのRustフレームが復帰した後です。
24
+ - ラップしたオブジェクトのマークコールバック内の`rb_gc_mark`(`nupur_sys::non_raising::gc_mark_in_dmark`)。Rubyのmark stackを伸ばせないとき、`NoMemoryError`をraiseし得ます。コールバックは後始末の要るものを何も持たず、このリスクはどのC拡張のマーク関数にも共通です。
25
+ - **Cに到達するpanicを禁止する** トランポリンはpanicを内部エラーに変換します。Cが呼び出すコールバックがpanicを捕捉し(`caught::PanicLatch`)、その呼び出し元が再開できるようにします。巻き戻り(unwind)してはならないコードは`no_unwind`サイトであり、`--cfg nupur_no_unwind`を指定したリリースビルドによって、それが不可能であることが証明されます(`rake no_unwind`)。
26
+ - **GVL は型になる**:GVL を必要とする Ruby の C APIを呼び出すには、GVL を保持していることを型レベルで保証する値を要求します。その保証する値には、対象となる Ruby の値(Nupur の `Value` が内部的に保持する `nupur_sys::value::Value`)か、対象となる値が存在しない場合にはトークン `Ruby` を使用します。どちらも `Send` を実装していないため、GVL を解放した状態で実行される処理の内部に持ち込むことはできません。値を生成できるのは `nupur_sys` の内部だけで、トークンを生成できるのは `rb::method` の非公開関数 `entry_token` のみです。この関数は Ruby から呼び出されるエントリポイントでのみ使用されます。
27
+ - **GCはラップされたオブジェクトが保持するすべてのRuby値を認識する** GCはラップされたオブジェクトのRustデータ内部を参照できないため、オブジェクトが生存させている値(MakiriノードはDocumentを保持します)は、そのマークコールバックによって報告される必要があります。Nupurはこのような値をスロット(`nupur_sys::value::Root`)に保持し、決してRustデータには保持せず、自らマークを行います。拡張機能はマーク関数を記述しないため、マークを忘れることはありません。
28
+ - **ラップされたオブジェクトはそのデータの借用期間よりも長く生存する** データには`typed::Typed` ハンドルを通じてのみアクセスでき、このハンドルが最後の借用が終了するまでオブジェクトを生存させ続けます。
29
+ - **Stringの読み取りはコピーで行う** 文字列のスライスが渡されることはありません。テキストはstdのバリデータによってUTF-8であることが確認されたコピーであり、Stringのキャッシュされたコードレンジから信頼されることはありません。
30
+ - **Cの整数は1箇所で変換する** (`nupur_sys::int`)。Cの `long` はWindowsでは32ビットであるため、変換処理はどちらのビット幅にも対応するよう1ヶ所に記述され、両方でテスト・検証されています (`verus/int.rs`)。
31
+ - **2つのRactorが同時にこのレイヤーで実行できる**。拡張ライブラリが自身をRactorセーフであると宣言した場合 (`rb::method::declare_ractor_safe`)、2つのRactorが同時に実行できます。Nupurの要素で、任意のRactorが読み取り可能なもの以外は、両者間で共有されません。ラップしたオブジェクトは、型を共有可能と宣言しない限り(`TypedType::shareable`)、作ったRactorから出ません。共有可能な型はペイロードが `Sync` であることを要し、オブジェクトは作られた時点で凍結され、共有可能になります。
32
+
33
+ ### 型で保持できないこと
34
+
35
+ 一部のルールは型として表現できません。Makiri ではこれらをコードのチェック(`lints/nupur_lints` にある `rake dylint` の lint、`rake no_unwind`、`rake unsafe:boundaries`)によって保持しているため、他で Nupur のコピーを使用する場合は独自チェックが必要です。
36
+
37
+ - **トークンは Ruby から呼び出される場所でのみ作成されること**:Ruby が GVL を保持した状態でコードを呼び出したという事実は呼び出し元の状態であり、型で表現することはできません。`gvl_token_outside_entry` は トランポリン や `run_init` を Ruby が呼び出す場所に限定し、`nupur_sys` のトークン作成処理(`Ruby::assume_gvl`)を `entry_token` に制限します。
38
+ - **Ruby の値は GC のスタックスキャンに見える場所に留まること**:値は `Copy` であり、スキャンから内容が見えない `Vec` や `Box` への格納を型の仕組みだけで防ぐことはできません。`ruby_value_on_heap` がバインディング、フィールド、static、式の型を読み取って検出します。
39
+ - **`nupur_sys` 自体のコードがこのバインディング層のルールを守ること**:`nupur_sys` は rb-sys を参照できる唯一のクレートであるため、その内部では raise、GVL、整数のルールは lint として管理されます(`raising_call_outside_protect`、`ffi_without_gvl_proof`、`c_integer_outside_int`)。どの C 関数が raise し得るかは、Ruby の C API の調査に基づくテーブルで管理されています。
40
+
41
+ 拡張ライブラリ自体もいくつかのルールを守る必要があります。GVL を解放して実行されるボディは Ruby を実行しないこと、呼び出し元の Ruby を勝手に実行しないこと、そして Ractor セーフな拡張ライブラリの独自状態は Ractor 間で安全であることです。`src/lib.rs` にこれらと各チェック方法が一覧化されています。使用前に読んでください。
42
+
43
+ ## 拡張ライブラリの記述方法
44
+
45
+ メソッドはRustの関数で、GVL のトークン(`&Ruby`)を受け取り、その値または `Result` を返すものです。`method!` / `function!` マクロがこれを トランポリン にラップします。`Init_` は `run_init` に 2 つの関数を渡します。1 つは実行中 Ruby のチェック(拒否された場合は `LoadError` になる)、もう 1 つは各種定義です。以下は `test/ext/src/lib.rs` にあるテスト拡張ライブラリの簡略版コピーです。
46
+
47
+ ```rust
48
+ use nupur::rb::{prelude::*, Error, ExceptionClass, RbConst, Ruby, Value};
49
+ use nupur::function;
50
+
51
+ static INTERNAL: RbConst<ExceptionClass> = RbConst::new();
52
+
53
+ fn echo(_: &Ruby, v: Value) -> Value {
54
+ v
55
+ }
56
+
57
+ fn check(ruby: &Ruby) -> Result<(), Error> {
58
+ nupur::abi::check_running_api("nupur_test", "rake nupur:spec")?;
59
+ nupur::abi::check_raise_state(ruby)
60
+ }
61
+
62
+ fn init(ruby: &Ruby) -> Result<(), Error> {
63
+ let m = ruby.define_module("NupurTest")?;
64
+ let internal = m.define_error("InternalError", ruby.exception_exception())?;
65
+ INTERNAL.set(internal)?;
66
+ nupur::rb::error::INTERNAL_ERROR.set(internal)?;
67
+ m.define_module_function("echo", function!(echo, 1))?;
68
+ Ok(())
69
+ }
70
+
71
+ #[no_mangle]
72
+ #[allow(non_snake_case)]
73
+ pub extern "C" fn Init_nupur_test() {
74
+ // 安全性: Ruby は自身のスレッド上で GVL を保持したまま `Init_nupur_test` を呼び出します。
75
+ unsafe { nupur::rb::method::run_init(check, init) };
76
+ }
77
+ ```
78
+
79
+ テスト用拡張ライブラリでは、スロットを持つオブジェクトのラップ(`TypedType`, `Birth`)、引数とキーワードの読み取り(`args`)、Hash のトラバース、String の読み取り、GVL 解放状態での処理の実行などもカバーしています。
80
+
81
+ 拡張ライブラリ側のビルドで知っておくべきことが三つあります。
82
+
83
+ - `rb::Error` はメッセージをインラインで持つ(256 バイト)ので、生成時にメモリ割り当てが起きません。その代わり `Result<_, Error>` はどれも clippy の `result_large_err` の既定値を超えます。Makiri と同じく、拡張ライブラリの `clippy.toml` に `large-error-threshold = 288` を書いてください。
84
+ - プロファイルは Nupur のテスト用拡張ライブラリと同じく `panic = "unwind"` にします。`abort` だと、メソッド内の panic が内部エラーとして Ruby に届く前にホストプロセスを殺してしまいます。依存先の crate の profile は効かないので Nupur からは設定できず、`unwind` でなければ Nupur はコンパイルを拒否します。
85
+ - Nupur の lint を `allow` するときは `cfg_attr(dylint_lib = "nupur_lints", ...)` の下に書くので、拡張ライブラリの `Cargo.toml` に cfg を登録してください。登録しないと、その `allow` のたびに警告が出ます。書き方は `[lints.rust] unexpected_cfgs = { level = "warn", check-cfg = ['cfg(dylint_lib, values(any()))'] }` です。lint は拡張ライブラリのワークスペースで `cargo dylint --path <makiri>/ext/makiri/rust/lints --pattern nupur_lints --no-deps` と実行すると掛かります。ほかの crate も一緒に lint するときの拡張ライブラリの crate 名、Ruby に依存しない crate、独自の巻き戻り禁止ガードは、ワークスペースの `dylint.toml` の `[nupur_lints]` に書きます(`lints/nupur_lints/README.md`)。
86
+
87
+ 渡されたオブジェクトのメソッドを呼ぶ拡張ライブラリ(`to_json` を呼ぶ JSON の生成器など)は、`rb::call` を通して呼びます。安全に呼ぶための規則はそのモジュールの文書にあり、要点は一つです。呼び出された側が無効にしうるものを、呼び出しをまたいで持たないことです。
88
+
89
+ ## Magnus との比較
90
+
91
+ Nupur は Makiri において Magnus を置き換えたものであり、多くの名称(`Value`、`RString`、`Error`、`method!`)を継承しています。ここでの比較対象は、2026 年 10 月の置き換えまで Makiri で使用されていた Magnus 0.8.2 です。
92
+
93
+ Magnus は Ruby の C API の広範囲をカバーする汎用バインディングです。これに対して Nupur は、Makiri が使用する C 関数と型のみをカバーします。Magnus を使う場合、Makiri が必要とする 30 ほどの機能のために、Magnus の内部構造とすべての `unsafe` を信用することになります。Nupur を使う場合は、信頼するのは Makiri が選定し契約を記述した C 関数のみとなります。
94
+
95
+ 両者の安全性に関する違い:
96
+
97
+ | | Magnus | Nupur |
98
+ | --- | --- | --- |
99
+ | Raise し得る C 呼び出し | 多くの呼び出しが `protect` 下で動作。どれが raise するかは実装者の判断でありユーザーは検証不可 | それぞれが保護のトークンを受け取るラッパーであり、`rb_protect` の外では呼び出し不可。どの関数が raise するかは Ruby C API の調査に基づくテーブルで管理 |
100
+ | メソッド内でのパニック | `fatal` として raise される(Magnus #118)。発生場所での rescue は不可 | 拡張ライブラリの内部エラー(ホスト側が名前で rescue 可能な `Exception`)として raise される |
101
+ | 引数 | `to_str` / `to_int` で自動変換される(呼び出し側で再定義可能な Ruby コード) | 自動変換なし:メソッドは `Value` を受け取って型をチェック。`RString` や `i64` を直接受け取るメソッドはコンパイル不可 |
102
+ | ラップされたオブジェクト内の Ruby 値 | ユーザーがマーク関数を記述。`Vec` やフィールド内に値を置くことが可能(ヒープ用には `BoxValue` が存在) | 固定スロット。一度だけ書き込まれ Nupur がマーク。ペイロードは Ruby 値を保持せず、ヒープ上の保持は lint で拒否 |
103
+ | ラップされたオブジェクトデータの参照 | 借用した `&T` が、最適化によって破棄されたその `Value` より長生きし、解放済みメモリを指す恐れがあった(Magnus #131, #101) | 最後の参照が終わるまで GC からオブジェクトが見える状態を保つ `Typed` ハンドル経由で借用 |
104
+ | String のバイト列 | `RString::as_slice` で貸し出し可能 | コピーによる取り出しのみ(貸し出し不可) |
105
+ | GVL | `Ruby::get()` で実行時にチェック | Ruby の値、または値がない場合はトークンで保持を証明。トークンは Ruby からの呼び出し口でのみ作成され、どちらも `Send` ではないため GVL 解放処理には進入不可 |
106
+ | 生の `VALUE` | `rb-sys` 機能(`AsRawValue`, `FromRawValue`)で使用可能 | Nupur からはアクセス不可:`nupur_sys` は `VALUE` を再エクスポートしない |
107
+
108
+ Magnus のほうが記述は容易です。引数や返り値を自動変換し、ハンドルは `Copy` であり、Ruby API の大部分をサポートする公開クレートです。Nupur はこれらを意図的に放棄しています。エラーを引き起こしやすい書き方は「不可能であるか、最も冗長な書き方になる」ように設計されています。Nupur は公開されておらず、Makiri のワークスペース内でのみ使用されます。
109
+
110
+ ## ディレクトリ/モジュール構造
111
+
112
+ | Nupur | 含まれる内容 |
113
+ | --- | --- |
114
+ | `rb` | `Value` とクラスチェック付きハンドル、`Error`、`method!` / `function!` トランポリン、クラス・モジュール定義、任意のオブジェクトの public メソッドを呼ぶ `rb::call` |
115
+ | `typed` | TypedData: Rust のペイロードを Ruby オブジェクトとしてラップ、そのスロットと GC コールバック、GC へ報告するメモリ |
116
+ | `gvl` | GVL を解放した本体の実行(`without_gvl`)と、Ruby がスレッドを返すよう求めたら止まる本体の実行(`without_gvl_cancellable`。本体は `Cancel` に問い合わせる) |
117
+ | `build` | Array、Hash、数値、Symbol、UTF-8 String の構築 |
118
+ | `string`, `encoding`, `utf8_copy` | コピーによる String の読み取り(検査済み UTF-8 として `read_utf8`、バイト列として `read_bytes`)、UTF-8 の String の生成(`&str` を受け取り有効と印を付ける `utf8_str`、呼び出し側が UTF-8 と保証するバイト列から作る `utf8_str_trusting`)と ASCII-8BIT の String の生成(`binary_str`)、エンコーディング |
119
+ | `args`, `hash` | メソッドの引数の読み取り、受け付けるキーワードを宣言したうえでの読み取り(`Kwargs::only`。それ以外は拒む)、Hash の走査 |
120
+ | `describe`, `msg` | 呼び出しやメモリ割り当てを行わずに例外メッセージへ Ruby 値を書き込む処理 |
121
+ | `abi` | バイナリが実行中 Ruby に対して前提とする内容と、ロード時のチェック |
122
+ | `caught`, `no_unwind` | C 境界でのパニックキャッチ、巻き戻り禁止ボディ |
123
+ | `stack`, `table`, `inject` | ネイティブスタックチェック、内部用整数キーテーブル、`rake oom` フック |
124
+
125
+ `raw`(保護された呼び出し)、`int`(`nupur_sys::int` の再エクスポート)、`cname` はクレートのプライベートモジュールです。
126
+
127
+ | `nupur_sys` | 含まれる内容 |
128
+ | --- | --- |
129
+ | `protect` | `rb_protect` の thunk、`Protected` トークン、`$!`、保留中の非ローカルエグジット |
130
+ | `raising` | Raise する可能性のある C 呼び出し(各呼び出しは `&Protected` を受け取る) |
131
+ | `non_raising` | Raise しない C 呼び出し(値またはトークンを受け取る。読み取りのほか、書き込みや GVL の解放を行うものもある) |
132
+ | `value` | `Value`、トークン `Ruby`、スロットの `Root` |
133
+ | `data_type` | TypedData 型の `rb_data_type_t` とそのフラグ(`DataFlags`) |
134
+ | `int` | C の整数型と Rust のビット幅間の変換 |
135
+ | `no_unwind` | 巻き戻り禁止ボディのガード(Nupur が再エクスポート) |
136
+
137
+ ## 機能(Features)と依存関係
138
+
139
+ Nupur は `nupur_sys` にのみ依存し、`nupur_sys` は rb-sys にのみ依存します。唯一の feature である `alloc-inject` は `rake oom` フックをコンパイルします。拡張ライブラリ自体の `alloc-inject` がこれを有効化し、さらに `nupur_sys` の同機能を有効化します。拡張ライブラリは既に `rb_*` シンボルを提供する Ruby プロセス内にロードされるため、rb-sys は `link-ruby` なしで使用されます。
140
+
141
+ ## テスト
142
+
143
+ ```bash
144
+ cargo test -p nupur -p nupur_sys
145
+ bundle exec rake nupur:spec
146
+ ```
147
+
148
+ `rake nupur:spec` はテスト用拡張ライブラリ(`test/ext`。Makiri のコードを含まず Nupur の上にいくつかのメソッドを持つのみ)をビルドし、その横にあるスペック(`test/spec`)を実行します。テスト内容には、メソッド内・GVL 解放下・Hash トラバース中のパニック、ブロックのエグジット、GC とコンパクション下でのスロット動作、検証付き String 読み取りなどが含まれます。CI(Linux / macOS)で実行されています。
149
+
150
+ `nupur_sys` の compile-fail doctest は、トークンや値が外部で作成できないことを実証します。各テストが指定するエラー内容を検証できるのは nightly の rustdoc のみであるため、`rake rust:compile_fail` で実行されます。