Files
five/hbrtl/procinfo.go
CharlesKWON f4ed42556b checkpoint: season-wide bug fix campaign + infra
Cumulative season's silent-bug hunting (~62 fixes) across the FiveSql2
SQL engine, the Five compiler/runtime, and the hbrdd RDD layer. Saved
as a single checkpoint before refactoring the parser to delegate xBase
command translation to the preprocessor.

Highlights:

FiveSql2 engine (_FiveSql2/src/)
- prefix-glob index attach -> explicit convention (<table>_pk.ntx,
  <table>_uq.ntx, <table>.cdx) — fixes silent multi-row INSERT row-drop
- DROP/CREATE TABLE FErase chain extended (.cdx, .fsc, .fsv, .dbt, .fpt)
- COUNT(DISTINCT col) parsed + aggregated via hSeen hash
- UNION column-count mismatch returns SQL_ERR_GRAMMAR (was silent)
- DISTINCT + ORDER BY hidden-col leak fixed (trim before DISTINCT)
- Derived table FROM (SELECT...) + JOIN right-side derived
- Self-FK CASCADE depth 2+ via SqlGetSingleColPK pre-collect
- LAG/LEAD default arg uses SqlEvalRowExpr (handles -N const exprs)
- DATE literal round-trip validation (Feb 29 non-leap rejected)
- CREATE OR REPLACE VIEW; CREATE VIEW errors on already-exists
- AlterTable type dispatcher comma-wrapped (1-char type "A" no longer
  matches CHARACTER)

Compiler / runtime
- gengo: HB_ -> FV_ prefix on emitted Go function names (Five identity)
- gengo split: emit_block.go, emit_stmt.go, folding.go extracted
- parser/stmtreg.go nudges
- hbrt: debug TUI/CLI restructure (debugcmd, debugkey, termios_*),
  windows debug stubs collapsed
- thread/vm/value/class/pcinterp tightening from panic traces

RDD layer (hbrdd/)
- dbf: null bitmap support (null.go + null_test.go), mmap split
  (mmap_posix.go / mmap_windows.go), byte-level numeric parse
- ntx/cdx: windows mmap parity
- workarea + mem RDD: cross-area state-bleed fixes

RTL (hbrtl/)
- errorlog rewrite with platform-specific FD (errorlog_fd_unix /
  errorlog_fd_other)
- sqlscan, sqlhelpers, indexrtl, datetime extensions

Gates green at checkpoint:
- go test ./...        : PASS
- FiveSql2 SQL:1999    : 43/43
- Harbour compat       : 56/56

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-30 09:26:25 +09:00

296 lines
6.4 KiB
Go

// Copyright (c) 2026 Charles KWON OhJun (charleskwonohjun@gmail.com)
// All rights reserved.
// Stack introspection: PROCNAME, PROCLINE, PROCFILE, ERRORLEVEL
package hbrtl
import (
"five/hbrt"
"os"
"strconv"
"strings"
"time"
)
// Silence unused import warning
var _ = strconv.Itoa
// PROCNAME([nLevel]) → cFunctionName
func ProcName(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
level := 0
if nParams >= 1 && !t.Local(1).IsNil() {
level = t.Local(1).AsInt()
}
stack := t.DebugCallStack()
if level >= 0 && level < len(stack) {
t.RetString(stack[level].Function)
} else {
t.RetString("")
}
}
// PROCLINE([nLevel]) → nLineNumber
func ProcLine(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
level := 0
if nParams >= 1 && !t.Local(1).IsNil() {
level = t.Local(1).AsInt()
}
stack := t.DebugCallStack()
if level >= 0 && level < len(stack) {
t.RetInt(int64(stack[level].Line))
} else {
t.RetInt(0)
}
}
// PROCFILE([nLevel]) → cSourceFileName
func ProcFile(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
level := 0
if nParams >= 1 && !t.Local(1).IsNil() {
level = t.Local(1).AsInt()
}
stack := t.DebugCallStack()
if level >= 0 && level < len(stack) {
t.RetString(stack[level].Module)
} else {
t.RetString("")
}
}
var exitLevel int
// ERRORLEVEL([nNewLevel]) → nOldLevel
func ErrorLevel(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
old := exitLevel
if nParams >= 1 && !t.Local(1).IsNil() {
exitLevel = t.Local(1).AsInt()
}
t.RetInt(int64(old))
}
// TONE(nFrequency [, nDuration]) → NIL
func Tone(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
// Terminal bell
os.Stdout.Write([]byte{7})
t.RetNil()
}
// CENTER(cString, nWidth [, cFill]) → cCentered (alias: PADC)
func Center(t *hbrt.Thread) {
PadC(t)
}
// STRZERO already exists, HB_NTOS already exists
// HB_NTOS(nValue) → cString (no leading spaces) — already in missing.go
// FIELDPOS(cFieldName) → nPos
func FieldPos(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProcFast()
fname := strings.ToUpper(t.Local(1).AsString())
wam := getWA(t)
if wam == nil {
t.RetInt(0)
return
}
area := wam.Current()
if area == nil {
t.RetInt(0)
return
}
// Try DBFArea's built-in field position cache (O(1) hash lookup).
// Falls back to linear scan for non-DBF areas (mem RDD, etc.).
type fieldPosCacher interface {
FieldPosCache(name string) int
}
if fpc, ok := area.(fieldPosCacher); ok {
pos := fpc.FieldPosCache(fname)
t.RetInt(int64(pos))
return
}
// Fallback: linear scan
for i := 0; i < area.FieldCount(); i++ {
fi := area.GetFieldInfo(i)
if strings.EqualFold(fi.Name, fname) {
t.RetInt(int64(i + 1))
return
}
}
t.RetInt(0)
}
func eqFold(a, b string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
ca, cb := a[i], b[i]
if ca >= 'a' && ca <= 'z' {
ca -= 32
}
if cb >= 'a' && cb <= 'z' {
cb -= 32
}
if ca != cb {
return false
}
}
return true
}
// FIELDBLOCK(nField) → bBlock — create block that reads field n
func FieldBlockFunc(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProc()
nField := t.Local(1).AsInt()
// Create a block that calls FieldGet(nField) on the current area
blk := hbrt.MakeBlock(func(t2 *hbrt.Thread) {
t2.Frame(0, 0)
defer t2.EndProc()
wam := getWA(t2)
if wam != nil {
if area := wam.Current(); area != nil {
val, _ := area.GetValue(nField - 1) // 1-based to 0-based
t2.PushValue(val)
t2.RetValue()
return
}
}
t2.RetNil()
}, 0)
t.RetVal(blk)
}
// FIELDNAME(nField) → cName
func FieldNameFunc(t *hbrt.Thread) {
t.Frame(1, 0)
defer t.EndProc()
nField := t.Local(1).AsInt()
wam := getWA(t)
if wam != nil {
if area := wam.Current(); area != nil {
if nField >= 1 && nField <= area.FieldCount() {
fi := area.GetFieldInfo(nField - 1)
t.RetString(fi.Name)
return
}
}
}
t.RetString("")
}
// AFIELDS(@aNames [, @aTypes, @aWidths, @aDecs]) → nFieldCount
func AFields(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
wam := getWA(t)
if wam == nil {
t.RetInt(0)
return
}
area := wam.Current()
if area == nil {
t.RetInt(0)
return
}
nFields := area.FieldCount()
t.RetInt(int64(nFields))
}
// DBSTRUCT() → aStruct (array of {name, type, len, dec})
func DbStruct(t *hbrt.Thread) {
t.Frame(0, 0)
defer t.EndProc()
wam := getWA(t)
if wam == nil {
t.RetVal(hbrt.MakeArray(0))
return
}
area := wam.Current()
if area == nil {
t.RetVal(hbrt.MakeArray(0))
return
}
nFields := area.FieldCount()
items := make([]hbrt.Value, nFields)
for i := 0; i < nFields; i++ {
fi := area.GetFieldInfo(i)
// 5-element row: name / type / len / dec / flags. Harbour
// dbStruct() is 4-element; the extra flags byte preserves
// FieldFlagNullable (and future system/binary/autoinc bits)
// across ALTER-TABLE table rebuilds so callers that feed
// dbStruct output back into dbCreate don't silently drop
// nullability. Four-element callers still index [1..4] as
// before.
row := []hbrt.Value{
hbrt.MakeString(fi.Name),
hbrt.MakeString(string(fi.Type)),
hbrt.MakeInt(int(fi.Len)),
hbrt.MakeInt(int(fi.Dec)),
hbrt.MakeInt(int(fi.Flags)),
}
items[i] = hbrt.MakeArrayFrom(row)
}
t.RetVal(hbrt.MakeArrayFrom(items))
}
// HB_DATETIME([nYear, nMonth, nDay [, nHour [, nMin [, nSec [, nMsec]]]]]) → tTimestamp
func HbDatetime(t *hbrt.Thread) {
nParams := t.ParamCount()
t.Frame(nParams, 0)
defer t.EndProc()
if nParams == 0 {
// No args: current date+time
now := time.Now()
y, m, d := now.Date()
julian := dateToJulian(y, int(m), d)
ms := int32(now.Hour()*3600000 + now.Minute()*60000 + now.Second()*1000 + now.Nanosecond()/1000000)
t.RetVal(hbrt.MakeTimestamp(julian, ms))
return
}
// With args: construct from components
y := 0; m := 1; d := 1; hh := 0; mm := 0; ss := 0; ms := 0
if nParams >= 1 { y = t.Local(1).AsInt() }
if nParams >= 2 { m = t.Local(2).AsInt() }
if nParams >= 3 { d = t.Local(3).AsInt() }
if nParams >= 4 { hh = t.Local(4).AsInt() }
if nParams >= 5 { mm = t.Local(5).AsInt() }
if nParams >= 6 { ss = t.Local(6).AsInt() }
if nParams >= 7 { ms = t.Local(7).AsInt() }
julian := dateToJulian(y, m, d)
timeMs := int32(hh*3600000 + mm*60000 + ss*1000 + ms)
t.RetVal(hbrt.MakeTimestamp(julian, timeMs))
}