py.Str; py.FromCStr/FromCStrAndLen/FromGoString

This commit is contained in:
xushiwei
2024-07-04 18:34:00 +08:00
parent 23da63767c
commit e55e90db1a
11 changed files with 77 additions and 17 deletions

View File

@@ -144,6 +144,8 @@ func TestErrBuiltin(t *testing.T) {
test("siglongjmp", func(ctx *context) { ctx.siglongjmp(nil, nil) })
test("cstr(NoArgs)", func(ctx *context) { cstr(nil, nil) })
test("cstr(Nonconst)", func(ctx *context) { cstr(nil, []ssa.Value{&ssa.Parameter{}}) })
test("pystr(NoArgs)", func(ctx *context) { pystr(nil, nil) })
test("pystr(Nonconst)", func(ctx *context) { pystr(nil, []ssa.Value{&ssa.Parameter{}}) })
test("atomic", func(ctx *context) { ctx.atomic(nil, 0, nil) })
test("atomicLoad", func(ctx *context) { ctx.atomicLoad(nil, nil) })
test("atomicStore", func(ctx *context) { ctx.atomicStore(nil, nil) })

View File

@@ -391,6 +391,7 @@ const (
llgoSiglongjmp = llgoInstrBase + 0xc
llgoPyList = llgoInstrBase + 0x10
llgoPyStr = llgoInstrBase + 0x11
llgoAtomicLoad = llgoInstrBase + 0x1d
llgoAtomicStore = llgoInstrBase + 0x1e

View File

@@ -28,6 +28,19 @@ import (
// -----------------------------------------------------------------------------
// func pystr(string) *py.Object
func pystr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
if c, ok := args[0].(*ssa.Const); ok {
if v := c.Value; v.Kind() == constant.String {
sv := constant.StringVal(v)
return b.PyStr(sv)
}
}
}
panic("pystr(<string-literal>): invalid arguments")
}
// func cstr(string) *int8
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
if len(args) == 1 {
@@ -175,6 +188,7 @@ var llgoInstrs = map[string]int{
"string": llgoString,
"stringData": llgoStringData,
"funcAddr": llgoFuncAddr,
"pystr": llgoPyStr,
"pyList": llgoPyList,
"sigjmpbuf": llgoSigjmpbuf,
"sigsetjmp": llgoSigsetjmp,
@@ -314,6 +328,8 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
case llgoPyList:
args := p.compileValues(b, args, fnHasVArg)
ret = b.PyList(args...)
case llgoPyStr:
ret = pystr(b, args)
case llgoCstr:
ret = cstr(b, args)
case llgoAdvance: