binary safety

This commit is contained in:
Luxferre
2026-07-04 12:59:58 +03:00
parent c1c66f9152
commit 147a05d8e0
5 changed files with 102 additions and 23 deletions
+50 -5
View File
@@ -351,7 +351,7 @@ func NewClyx(libfname string, libCode string) *Clyx {
var l []any
if s, ok := qVal.(string); ok {
l = make([]any, len(s))
for i, r := range s { l[i] = int64(r) }
for i := 0; i < len(s); i++ { l[i] = int64(s[i]) }
} else {
l = qVal.([]any)
}
@@ -415,8 +415,46 @@ func NewClyx(libfname string, libCode string) *Clyx {
c.stack = append(c.stack, &ClientSocket{conn: conn, r: bufio.NewReader(conn)})
}
}
c.words["write"] = func() { data, fd := c.pop(), c.pop(); n, err := fd.(ClyxFile).Write(data.(string)); if err != nil { panic(err) }; c.stack = append(c.stack, int64(n)) }
c.words["read"] = func() { fd := c.pop().(ClyxFile); if srv, ok := fd.(*ServerSocket); ok { client, err := srv.Accept(); if err != nil { panic(err) }; c.stack = append(c.stack, client) } else { res, err := fd.Read(); if err != nil { panic(err) }; c.stack = append(c.stack, res) } }
c.words["write"] = func() {
data, fd := c.pop(), c.pop()
var toWrite string
if listVal, ok := data.([]any); ok {
var sb strings.Builder
for _, val := range listVal {
sb.WriteByte(byte(toInt(val)))
}
toWrite = sb.String()
} else if strVal, ok := data.(string); ok {
toWrite = strVal
} else {
panic("write expects string or list")
}
n, err := fd.(ClyxFile).Write(toWrite)
if err != nil {
panic(err)
}
c.stack = append(c.stack, int64(n))
}
c.words["read"] = func() {
fd := c.pop().(ClyxFile)
if srv, ok := fd.(*ServerSocket); ok {
client, err := srv.Accept()
if err != nil {
panic(err)
}
c.stack = append(c.stack, client)
} else {
res, err := fd.Read()
if err != nil {
panic(err)
}
var list []any
for i := 0; i < len(res); i++ {
list = append(list, int64(res[i]))
}
c.stack = append(c.stack, list)
}
}
c.words["close"] = func() { if fd, ok := c.pop().(ClyxFile); ok { fd.Close() } }
c.words["delf"] = func() { if err := os.Remove(c.pop().(string)); err != nil { panic(err) } }
c.words["maked"] = func() { if err := os.Mkdir(c.pop().(string), 0755); err != nil { panic(err) } }
@@ -428,9 +466,16 @@ func NewClyx(libfname string, libCode string) *Clyx {
c.words["loop"] = func() { body, cond := c.pop().([]any), c.pop().([]any); for { c.execQuot(cond); if !isTruthy(c.pop()) { break }; c.execQuot(body) } }
c.words["hsargs"] = func() { c.stack = append(c.stack, toAnySlice(os.Args)) }
c.words["hsexit"] = func() { os.Exit(int(toInt(c.pop()))) }
c.words["l2s"] = func() {
l := c.pop().([]any)
var sb strings.Builder
for _, val := range l {
sb.WriteByte(byte(toInt(val)))
}
c.stack = append(c.stack, sb.String())
}
c.words["hseval"] = func() { fmt.Fprintln(os.Stderr, "Error: unsupported word"); os.Exit(1) }
c.words["chr"] = func() { c.stack = append(c.stack, string(rune(toInt(c.pop())))) }
c.words["asc"] = func() { s := c.pop().(string); r, _ := utf8.DecodeRuneInString(s); c.stack = append(c.stack, int64(r)) }
regMath := func(name string, f func(float64) float64) {
c.words[name] = func() { c.stack = append(c.stack, f(toFloat(c.pop()))) }
@@ -441,7 +486,7 @@ func NewClyx(libfname string, libCode string) *Clyx {
regMath("exp", math.Exp)
regMath("log", math.Log)
c.Run(`"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf i ]`)
c.Run(`"::" [ next defw ] defw :: readf [ "r" fopen dup read swap close ] :: src [ readf l2s i ]`)
if libfname == "" {
c.Run(libCode)