Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,12 @@ func (c *Compiler) compileCall(v *ast.Call) {
b.emit(bytecode.OpBinding, 0, 0)
return
}
// __ENCODING__ is the source encoding keyword; rbgo scripts are UTF-8, so it is
// Encoding::UTF_8.
if v.Recv == nil && v.Block == nil && v.Name == "__ENCODING__" && len(v.Args) == 0 {
c.compileNode(&ast.ScopedConst{Recv: &ast.ConstRef{Name: "Encoding"}, Name: "UTF_8"})
return
}
// A bare eval(str) with no explicit binding evaluates against the caller's
// binding, so it can see (and assign) the caller's local variables — exactly
// like eval(str, binding). Rewrite it to that two-argument form using the
Expand Down
17 changes: 17 additions & 0 deletions internal/vm/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,20 @@ func TestEncodingCompatible(t *testing.T) {
}
}
}

// TestEncodingKeyword covers the __ENCODING__ keyword, which evaluates to the
// source encoding (Encoding::UTF_8 for rbgo's UTF-8 scripts) at the top level, in
// a method body and as a sub-expression. Asserted against MRI Ruby 4.0.6.
func TestEncodingKeyword(t *testing.T) {
cases := []struct{ src, want string }{
{`p __ENCODING__`, "#<Encoding:UTF-8>\n"},
{`p __ENCODING__ == Encoding::UTF_8`, "true\n"},
{`p [1, __ENCODING__.name]`, "[1, \"UTF-8\"]\n"},
{`def m; __ENCODING__; end; p m.name`, "\"UTF-8\"\n"},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want)
}
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16260
16270