Repository navigation
cgi/escape is ruby's C extension, linked in; a C-only feature is its extension in $LOADED_FEATURES - #286
Conversation
…ed once ext/cgi/escape/escape.c (4.0.10, unchanged) is compiled in its own unit (capi/static_cgi_escape.c) and is the shipped cgi/escape.so, which ruby's lib/cgi/escape.rb -- now 4.0.10's byte for byte -- requires. A feature that is a C extension alone in ruby's build (etc, zlib, stringio, erb/escape, ...), or a shipped one asked for with .so, is <rubyarchdir>/<name>.bundle and that alone in $LOADED_FEATURES. CLASS_OF of a class is its metaclass, through which escape.c reads @@accept_charset. corpus 918, 919.
…by .so A shipped library of the bare name lists <name>.bundle when it starts, so bigdecimal.rb's require 'bigdecimal.so' read as loaded and ran nothing (corpus 403).
…ath, source_location is nil Refinement#import_methods refused Zlib's methods by their .rb file (corpus 579); the file is the extension now.
284km
left a comment
There was a problem hiding this comment.
Review of 534a430 (the code is 81c42f5 plus the loop record).
Summary
The C API change and the $LOADED_FEATURES change work as described on macOS, on both engines. They match ruby 4.0.10 in everything I probed that this PR claims. One Major finding blocks the merge: CI's build-linux job fails at the header check, so the Linux corpus never runs. The rest are Suggestions. Their causes were there before this PR.
Findings
Major: build-linux fails at capi/tools/headers_check.py.
- The job says:
capi/vendor/cgi_escape.c is not the tree's file. - I reproduced it on this head against a clean ruby 4.0.10 tree.
- The file itself is fine: it is byte for byte ruby 4.0.10's
ext/cgi/escape/escape.c(checked withdiff). - The check compares each vendored file with the tree file of the same path,
<tree>/cgi_escape.c. A renamed file needs an entry inVENDOR_PATHS, aserb_escape.chas. The fix is one line:'cgi_escape.c': 'ext/cgi/escape/escape.c'. - Effect: the job stops at "Check Ruby's C API...". Every later step is skipped, including the Linux corpus on the evaluator and on the VM. So nothing on Linux has checked the
.soside of the new$LOADED_FEATUREScode. By reading,dlx9comes fromsys_os (),cext_lib_fileandmeth_source_locationaccept.sotoo, and I found no hard-coded.bundle. - The posted local results list capi_check, capi_spec_check, capi_mkmf_check and capi_gem_check. They do not list headers_check, which CI runs first in the same step.
Suggestion: the internal name (sng:CGI) can show in an error message.
- With
CGI::Escape.remove_class_variable(:@@accept_charset),CGI.unescape("%41")raisesuninitialized class variable @@accept_charset in (sng:CGI). Ruby saysin #<Class:CGI>. - This was already there: on main,
Plain.singleton_class.class_variable_get(:@@nope)prints(sng:Plain)too. This PR only makes C reach it, throughCLASS_OF. It is rare, but the message comes fromclass_variable_geton a singleton class, so it is easy to fix.
Suggestion: the metaclass is not yet a full CRuby metaclass to C.
FL_TEST(CLASS_OF(klass), FL_SINGLETON)is false; CRuby says true.capi_describe_xnever sets bit 0x40, whichmirror.creads. So this was already true of every singleton class, including an object's. One effect:rb_class_new(CLASS_OF(klass))does not raise "can't make subclass of singleton class".OBJ_FROZEN(CLASS_OF(k))is false afterk.freeze; CRuby says true. From Ruby,k.singleton_class.frozen?is also false, on main too.CLASS_OFof a module is stillModule, and of a singleton class stillClass. CRuby answers the module's or class's own singleton class. The PR says this; it is follow-up work.
Suggestion: list more of require "<name>.so" under "Left".
- The description says a C-only feature has one entry "also when it is required as
name.so". That holds for etc, stringio, pty and io/console. On this head (and on main)require "X.so"still raises LoadError for io/nonblock, date_core, fcntl, continuation, digest/md5, digest/sha1, digest/rmd160, json/ext/parser, json/ext/generator, rbconfig/sizeof and erb/escape. Ruby returns true. Only io/wait.so is in "Left". require "zlib.so"thenrequire "zlib"returns true twice and lists the extension only on the second call. Ruby returns true, then false.require "date"does not listdate_core.bundle; ruby lists it. This is the same on main.
Suggestion (cosmetic): backtraces now point into the extension file.
StringIO.new("").read(-1)now gives a first frame of<rubyarchdir>/stringio.bundle:260:in 'StringIO#__sio_len'. Before it wasstringio.rb:260. Ruby shows the caller's line (-e:1:in 'StringIO#read'). Neither is ruby's answer. A file with a line number inside a.bundlemay confuse a reader.
Not from this PR, noticed while probing. Both are the same on main:
- A C singleton method named
ancestorson a module, which callsrb_funcall(k, rb_intern("ancestors"), 0), gets the defining module's ancestors, notk's. A Cconst_getcallingrb_const_getdid something similar. An instance methodinspectthat callsinspecton another object is fine. rb_method_boundp(CLASS_OF(Plain), rb_intern("new"), 0)is false; ruby says true.
What I checked
All of this was done by running, on the author's build (the worktree at this head, clean, binary newer than every .mere and capi change). I compared with ruby 4.0.10 and with a main build that does not have this PR.
CLASS_OFon a class. I wrote a small C extension and built it twice, as capi_check does: once against this PR'scapi/includeand once against ruby's headers. I probed 30 C API paths on 13 receivers: classes, a subclass,Class,Object,BasicObject,String, an anonymous class, a Struct class, modules, and a singleton class.- These match ruby for every class:
CLASS_OFisrb_singleton_classand is the same asRBASIC_CLASS;rb_obj_class;rb_class_name,rb_class2name,rb_obj_classnameandrb_class_realof it (stillClass); its superclass and ancestors;rb_obj_is_kind_ofandrb_obj_is_instance_of. - These also match:
rb_funcallon the class and on its metaclass;rb_method_boundpandrb_respond_tofor a singleton method;rb_cvar_get,rb_cvar_setandrb_cvar_definedthrough the metaclass (own, inherited, and from an included module);rb_const_getandrb_ivar_geton the metaclass. - These now match too:
rb_define_singleton_method;rb_extend_object;rb_define_method(CLASS_OF(klass), ...);rb_include_module(CLASS_OF(klass), ...). - Main was wrong on many of these. For example,
rb_define_method(CLASS_OF(Plain), ...)defined the method onClassfor every class.rb_include_module(CLASS_OF(Plain2), M)madeObject.mwork.rb_cvar_setput the variable onClass. So I found no caller that depended on the old answer. - The metaclass is still lazy as seen from Ruby. After C has taken
CLASS_OF(Fresh),Fresh.singleton_methodsis[], andFresh.singleton_class?is false.Fresh.singleton_classis the same object C got.Class.subclassesandObject.subclassescontain no singleton class.
- These match ruby for every class:
$LOADED_FEATURES. Each case ran in its own process, with paths normalized. I triedcgi/escape,erb/escape,erb,cgi,erbthencgi,cgi/escapethenerb,cgi/escapetwice and thencgi/escape.so,cgi/escape.sothencgi/escape,erb/escapewith and without.so,etc/etc.so,stringio.so/stringio,zlib,io/console,date,strscan,json, and thegrep(/escape/)view.erbandcgineed ruby'slibon-I, as the unittest runner gives it.- cgi/escape and erb/escape now give the same entries in the same order as ruby. The differences left are the ones in "Left" (set.rb, strscan, io/console's stringio) and the ones above.
require_relativeof a localcgi/escape.rb, arequire_relative'd file that requirescgi/escape, and-I dirwith a localcgi/escape.rbthat shadows the library all match ruby.
- CGI. I tried
@@accept_charsetset to EUC-JP, Shift_JIS, ISO-8859-1, US-ASCII, ASCII-8BIT and UTF-16LE, to a String, a Symbol and nil, and removed. Receivers wereCGI, an instance of a class that includesCGI::Escape, and an object extended with it. I also tried explicit and bad encoding arguments, invalid bytes in all six methods, numeric references (out of range, NUL, surrogate, huge), EUC-JP/Shift_JIS/Latin-1 input, UTF-16 input, frozen receivers (identity and frozen state of the result), a String subclass (changed and unchanged), non-String arguments, and thehandescape_htmlaliases.- The evaluator and the VM print the same. Both match ruby except for the
(sng:CGI)message above and a surrogate'sinspect. The surrogate's bytes are the same; String#inspect of invalid UTF-8 differs, which is not from this PR. - Main differed from ruby on 13 lines here: owner and
source_location, String-subclass results, TypeError messages and others.
- The evaluator and the VM print the same. Both match ruby except for the
- capi_check.py, run from a scratch copy of this head against a clean ruby 4.0.10 tree: the contracts agree (api 376, loader 48/49), they hold under GC stress, and each of the 6 negative controls fails.
headers_check.pyfails as in CI (above). I did not run capi_spec_check, capi_mkmf_check or capi_gem_check. - Small runs. test/cgi/test_cgi_escape.rb gives 435 tests, 526 assertions, 0 skips on ruby, the evaluator and the VM (main: 522 assertions, 2 skips). Corpus 918, 919, 403 and 579 match ruby on both engines. I did not run the full corpus: a long test run was in progress on this machine.
git merge-tree --write-tree origin/main origin/pr286is clean.
Local results posted by the author
These are present, on 81c42f5 with main ecd6507: run_corpus.sh --both (gates, then 948/948 with the hash index on and off), MERE_RUBY_VM=1 948/948, the four capi checks, and cgi/erb/encoding/m17n on both engines (665 tests, MATCH 664, RED 0). headers_check is not among them.
CI
At the time of review, build-linux has failed (see Major). The unittest harness/plan, the Linux gem suites, runtime-load-path (Linux), linux-build-deps and NKF Wasm pass. build-macos, unittest-reference and the macOS suites are still pending.
| @@ -0,0 +1,13 @@ | |||
| /* cgi/escape: ruby's ext/cgi/escape/escape.c (CGI::EscapeExt: escapeHTML, | |||
| * unescapeHTML, escape, unescape, escapeURIComponent, unescapeURIComponent), | |||
| * CRuby 4.0.10's, unchanged, in capi/vendor/cgi_escape.c, linked into this | |||
There was a problem hiding this comment.
Major: this file is unchanged, but CI's capi/tools/headers_check.py cannot tell. It compares each vendored file with the tree's file of the same path, and <tree>/cgi_escape.c does not exist. So build-linux fails with capi/vendor/cgi_escape.c is not the tree's file, and the Linux corpus steps are skipped. Add 'cgi_escape.c': 'ext/cgi/escape/escape.c' to VENDOR_PATHS in capi/tools/headers_check.py, next to erb_escape.c.
…pe.c (review of #286) The vendored file is compared with its path in the tree, as erb_escape.c is; CI's build-linux stopped here. Left: the review's four suggestions.
284km
left a comment
There was a problem hiding this comment.
Re-reviewed fc68eb3. The Major (build-linux failing in headers_check on capi/vendor/cgi_escape.c) is resolved: VENDOR_PATHS now maps cgi_escape.c to ext/cgi/escape/escape.c, like erb_escape.c. I ran this head's capi/tools/headers_check.py against a clean ruby 4.0.10 tree: 194 headers, 10 of them patched (headers.patch); 22 vendored sources unchanged.
Because CI's Linux corpus had been skipped, the author also built on ubuntu:24.04 (aarch64) and checked corpus 403, 579, 914, 918 and 919 on the evaluator and the VM against ruby: all identical, and $LOADED_FEATURES shows aarch64-linux/cgi/escape.so plus cgi/escape.rb, and a single stringio.so line. The four Suggestions are now listed under Left in the description and the loop note.
No other code changed since the last review (only the checker line and the record). The earlier results stand: corpus 948/948 on both engines, the 4 capi checks, cgi/erb/encoding/m17n 665 tests with RED 0, test_cgi_escape matching ruby with no omits. git merge-tree against current main: clean. No mere-pr-review-watch inline comments.
Merging.
This finishes what #253 left:
$LOADED_FEATURES.What changes
cgi/escape.sois ruby's C extension.capi/vendor/cgi_escape.cis ruby 4.0.10'sext/cgi/escape/escape.c, unchanged.capi/static_cgi_escape.c, because erb's escape.c has static functions of the same names.cgi/escapeinmr_static_extsand shipped as the featurecgi/escape.so.lib/cgi_escape_ext.rbis now 4.0.10'slib/cgi/escape.rbbyte for byte, below its header. Its ownrequire 'cgi/escape.so'loads the C. A build with no C side rescues the LoadError, as ruby's miniruby does.CLASS_OFof a class is its metaclass, as in CRuby.Class, so escape.c'srb_cvar_get(CLASS_OF(CGI), :@@accept_charset)raised NameError.$LOADED_FEATURES.cext_c_only, each checked withresolve_feature_pathon ruby 4.0.10.<rubyarchdir>/<name>.bundle, and that is its only entry, also when it is required asname.so.source_locationis nil, andRefinement#import_methodsrefuses them, as ruby does.CLASS_OFof a class.Two regressions in the first version, both fixed:
require 'bigdecimal.so'inside bigdecimal.rb was treated as already loaded. Only C-only features andcgi/escape.soare now their extension by.so.import_methodsimported Zlib's methods.cext_lib_filenow recognises the extension path.Checked
All on 81c42f5 (this branch with main ecd6507, including #284), mere 9dec2ff6, ruby 4.0.10.
run_corpus.sh --both: the gates, then 948/948 with the hash index on and off.MERE_RUBY_VM=1 run_corpus.sh --both: 948/948.unittest/run.shon cgi/test_cgi_escape.rb, erb, ruby/test_encoding.rb and ruby/test_m17n.rb, on the evaluator and on the VM:capi/tools/headers_check.py(review of cgi/escape is ruby's C extension, linked in; a C-only feature is its extension in $LOADED_FEATURES #286: CI's build-linux stopped there):cgi_escape.cwas missing fromVENDOR_PATHS; it is now added (fc68eb3).tools/build.sh --cc-only --fast).$LOADED_FEATURESlists.../aarch64-linux/cgi/escape.soandcgi/escape.rb, andstringio.soonce.requireadds, compared with ruby 4.0.10 and with main's build:Left
From the review (all on main too):
(sng:CGI)appears in error messages where ruby says#<Class:CGI>..soraise LoadError.require "zlib"afterrequire "zlib.so"returns true a second time.require "date"does not listdate_core.bundle.stringio.bundle:260.From before:
These are not from this change and remain:
require "erb/escape"and"cgi/escape"also addset.rb.version.rbandloader.rbare not listed.require "io/console"also lists stringio.require "io/wait.so"raises LoadError where ruby returns false.require "zlib.so"afterrequire "zlib"returns true.strscan/strscan.rbthat its extension requires.Record:
loop/2026-11-25-capi-cgi-escape.md.🤖 Generated with Claude Code