Merge pull request #448 from xushiwei/q
patch fmt: printArg; py.Str; py.FromCStr/FromCStrAndLen/FromGoString
This commit is contained in:
29
README.md
29
README.md
@@ -73,24 +73,33 @@ And you can import any Python library into `llgo` through a program called `llpy
|
|||||||
|
|
||||||
Note: For third-party libraries (such as pandas and pytorch), you still need to install the library files.
|
Note: For third-party libraries (such as pandas and pytorch), you still need to install the library files.
|
||||||
|
|
||||||
Here is an example using the Python `math` library:
|
Here is an example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/c"
|
|
||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
"github.com/goplus/llgo/py/math"
|
"github.com/goplus/llgo/py/math"
|
||||||
|
"github.com/goplus/llgo/py/std"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
x := math.Sqrt(py.Float(2))
|
x := math.Sqrt(py.Float(2)) // x = sqrt(2)
|
||||||
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
|
std.Print(py.Str("sqrt(2) ="), x) // print("sqrt(2) =", x)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Here, We call `py.Float(2)` to create a Python number 2, and pass it to Python’s `math.sqrt` to get `x`. Then use `x.Float64()` to convert x to Go's `float64` type, and print the value through the C `printf` function.
|
It is equivalent to the following Python code:
|
||||||
|
|
||||||
|
```py
|
||||||
|
import math
|
||||||
|
|
||||||
|
x = math.sqrt(2)
|
||||||
|
print("sqrt =", x)
|
||||||
|
```
|
||||||
|
|
||||||
|
Here, We call `py.Float(2)` to create a Python number 2, and pass it to Python’s `math.sqrt` to get `x`. Then we call `std.Print` to print the result.
|
||||||
|
|
||||||
Let's look at a slightly more complex example. For example, we use `numpy` to calculate:
|
Let's look at a slightly more complex example. For example, we use `numpy` to calculate:
|
||||||
|
|
||||||
@@ -98,9 +107,9 @@ Let's look at a slightly more complex example. For example, we use `numpy` to ca
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/c"
|
|
||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
"github.com/goplus/llgo/py/numpy"
|
"github.com/goplus/llgo/py/numpy"
|
||||||
|
"github.com/goplus/llgo/py/std"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -115,7 +124,7 @@ func main() {
|
|||||||
py.List(3.0, 2.0, 1.0),
|
py.List(3.0, 2.0, 1.0),
|
||||||
)
|
)
|
||||||
x := numpy.Add(a, b)
|
x := numpy.Add(a, b)
|
||||||
c.Printf(c.Str("a+b = %s\n"), x.Str().CStr())
|
std.Print(py.Str("a+b ="), x)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -246,9 +255,9 @@ Here are the Go packages that can be imported correctly:
|
|||||||
- [Clang 17](https://clang.llvm.org)
|
- [Clang 17](https://clang.llvm.org)
|
||||||
- [pkg-config 0.29+](https://www.freedesktop.org/wiki/Software/pkg-config/)
|
- [pkg-config 0.29+](https://www.freedesktop.org/wiki/Software/pkg-config/)
|
||||||
- [bdwgc/libgc 8.0+](https://www.hboehm.info/gc/)
|
- [bdwgc/libgc 8.0+](https://www.hboehm.info/gc/)
|
||||||
- [cJSON 1.7+](https://github.com/DaveGamble/cJSON) (optional, for [`github.com/goplus/llgo/c/cjson`](https://pkg.go.dev/github.com/goplus/llgo/c/cjson))
|
- [cJSON 1.7+](https://github.com/DaveGamble/cJSON) (optional, for [github.com/goplus/llgo/c/cjson](https://pkg.go.dev/github.com/goplus/llgo/c/cjson))
|
||||||
- [SQLite 3](https://www.sqlite.org) (optional, for [`github.com/goplus/llgo/c/sqlite`](https://pkg.go.dev/github.com/goplus/llgo/c/sqlite))
|
- [SQLite 3](https://www.sqlite.org) (optional, for [github.com/goplus/llgo/c/sqlite](https://pkg.go.dev/github.com/goplus/llgo/c/sqlite))
|
||||||
- [Python 3.11+](https://www.python.org) (optional, for [`github.com/goplus/llgo/py`](https://pkg.go.dev/github.com/goplus/llgo/py))
|
- [Python 3.11+](https://www.python.org) (optional, for [github.com/goplus/llgo/py](https://pkg.go.dev/github.com/goplus/llgo/py))
|
||||||
|
|
||||||
## How to install
|
## How to install
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Hello, world")
|
fmt.Println("Hello, world")
|
||||||
fmt.Printf("%f\n", 3.14)
|
fmt.Printf("%f\n", 3.14)
|
||||||
|
fmt.Printf("%v\n", errors.New("error message"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/c"
|
|
||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
"github.com/goplus/llgo/py/math"
|
"github.com/goplus/llgo/py/math"
|
||||||
|
"github.com/goplus/llgo/py/std"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
x := math.Sqrt(py.Float(2))
|
x := math.Sqrt(py.Float(2)) // x = sqrt(2)
|
||||||
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
|
std.Print(py.Str("sqrt(2) ="), x) // print("sqrt(2) =", x)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/c"
|
|
||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
"github.com/goplus/llgo/py/numpy"
|
"github.com/goplus/llgo/py/numpy"
|
||||||
|
"github.com/goplus/llgo/py/std"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -18,5 +18,5 @@ func main() {
|
|||||||
py.List(3.0, 2.0, 1.0),
|
py.List(3.0, 2.0, 1.0),
|
||||||
)
|
)
|
||||||
x := numpy.Add(a, b)
|
x := numpy.Add(a, b)
|
||||||
c.Printf(c.Str("a+b = %s\n"), x.Str().CStr())
|
std.Print(py.Str("a+b ="), x)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"github.com/goplus/llgo/py"
|
"github.com/goplus/llgo/py"
|
||||||
"github.com/goplus/llgo/py/math"
|
"github.com/goplus/llgo/py/math"
|
||||||
"github.com/goplus/llgo/py/os"
|
"github.com/goplus/llgo/py/os"
|
||||||
|
"github.com/goplus/llgo/py/std"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
x := math.Sqrt(py.Float(2))
|
x := math.Sqrt(py.Float(2))
|
||||||
wd := os.Getcwd()
|
wd := os.Getcwd()
|
||||||
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
|
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
|
||||||
c.Printf(c.Str("cwd = %s\n"), wd.CStr())
|
std.Print(py.Str("cwd ="), wd)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ source_filename = "main"
|
|||||||
@__llgo_py.math.sqrt = linkonce global ptr null, align 8
|
@__llgo_py.math.sqrt = linkonce global ptr null, align 8
|
||||||
@__llgo_py.os.getcwd = linkonce global ptr null, align 8
|
@__llgo_py.os.getcwd = linkonce global ptr null, align 8
|
||||||
@0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1
|
@0 = private unnamed_addr constant [14 x i8] c"sqrt(2) = %f\0A\00", align 1
|
||||||
@1 = private unnamed_addr constant [10 x i8] c"cwd = %s\0A\00", align 1
|
@1 = private unnamed_addr constant [6 x i8] c"cwd =\00", align 1
|
||||||
|
@__llgo_py.builtins.print = linkonce global ptr null, align 8
|
||||||
|
@__llgo_py.builtins = external global ptr, align 8
|
||||||
|
@2 = private unnamed_addr constant [6 x i8] c"print\00", align 1
|
||||||
@__llgo_py.math = external global ptr, align 8
|
@__llgo_py.math = external global ptr, align 8
|
||||||
@2 = private unnamed_addr constant [5 x i8] c"sqrt\00", align 1
|
@3 = private unnamed_addr constant [5 x i8] c"sqrt\00", align 1
|
||||||
@__llgo_py.os = external global ptr, align 8
|
@__llgo_py.os = external global ptr, align 8
|
||||||
@3 = private unnamed_addr constant [7 x i8] c"getcwd\00", align 1
|
@4 = private unnamed_addr constant [7 x i8] c"getcwd\00", align 1
|
||||||
|
|
||||||
define void @main.init() {
|
define void @main.init() {
|
||||||
_llgo_0:
|
_llgo_0:
|
||||||
@@ -22,10 +25,13 @@ _llgo_1: ; preds = %_llgo_0
|
|||||||
store i1 true, ptr @"main.init$guard", align 1
|
store i1 true, ptr @"main.init$guard", align 1
|
||||||
call void @"github.com/goplus/llgo/py/math.init"()
|
call void @"github.com/goplus/llgo/py/math.init"()
|
||||||
call void @"github.com/goplus/llgo/py/os.init"()
|
call void @"github.com/goplus/llgo/py/os.init"()
|
||||||
%1 = load ptr, ptr @__llgo_py.math, align 8
|
call void @"github.com/goplus/llgo/py/std.init"()
|
||||||
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @2, ptr @__llgo_py.math.sqrt, ptr null)
|
%1 = load ptr, ptr @__llgo_py.builtins, align 8
|
||||||
%2 = load ptr, ptr @__llgo_py.os, align 8
|
call void (ptr, ...) @llgoLoadPyModSyms(ptr %1, ptr @2, ptr @__llgo_py.builtins.print, ptr null)
|
||||||
call void (ptr, ...) @llgoLoadPyModSyms(ptr %2, ptr @3, ptr @__llgo_py.os.getcwd, ptr null)
|
%2 = load ptr, ptr @__llgo_py.math, align 8
|
||||||
|
call void (ptr, ...) @llgoLoadPyModSyms(ptr %2, ptr @3, ptr @__llgo_py.math.sqrt, ptr null)
|
||||||
|
%3 = load ptr, ptr @__llgo_py.os, align 8
|
||||||
|
call void (ptr, ...) @llgoLoadPyModSyms(ptr %3, ptr @4, ptr @__llgo_py.os.getcwd, ptr null)
|
||||||
br label %_llgo_2
|
br label %_llgo_2
|
||||||
|
|
||||||
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
_llgo_2: ; preds = %_llgo_1, %_llgo_0
|
||||||
@@ -46,8 +52,9 @@ _llgo_0:
|
|||||||
%6 = call ptr @PyObject_CallNoArgs(ptr %5)
|
%6 = call ptr @PyObject_CallNoArgs(ptr %5)
|
||||||
%7 = call double @PyFloat_AsDouble(ptr %4)
|
%7 = call double @PyFloat_AsDouble(ptr %4)
|
||||||
%8 = call i32 (ptr, ...) @printf(ptr @0, double %7)
|
%8 = call i32 (ptr, ...) @printf(ptr @0, double %7)
|
||||||
%9 = call ptr @PyUnicode_AsUTF8(ptr %6)
|
%9 = call ptr @PyUnicode_FromString(ptr @1)
|
||||||
%10 = call i32 (ptr, ...) @printf(ptr @1, ptr %9)
|
%10 = load ptr, ptr @__llgo_py.builtins.print, align 8
|
||||||
|
%11 = call ptr (ptr, ...) @PyObject_CallFunctionObjArgs(ptr %10, ptr %9, ptr %6, ptr null)
|
||||||
ret i32 0
|
ret i32 0
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,6 +62,8 @@ declare void @"github.com/goplus/llgo/py/math.init"()
|
|||||||
|
|
||||||
declare void @"github.com/goplus/llgo/py/os.init"()
|
declare void @"github.com/goplus/llgo/py/os.init"()
|
||||||
|
|
||||||
|
declare void @"github.com/goplus/llgo/py/std.init"()
|
||||||
|
|
||||||
declare void @"github.com/goplus/llgo/internal/runtime.init"()
|
declare void @"github.com/goplus/llgo/internal/runtime.init"()
|
||||||
|
|
||||||
declare ptr @PyFloat_FromDouble(double)
|
declare ptr @PyFloat_FromDouble(double)
|
||||||
@@ -67,7 +76,9 @@ declare double @PyFloat_AsDouble(ptr)
|
|||||||
|
|
||||||
declare i32 @printf(ptr, ...)
|
declare i32 @printf(ptr, ...)
|
||||||
|
|
||||||
declare ptr @PyUnicode_AsUTF8(ptr)
|
declare ptr @PyUnicode_FromString(ptr)
|
||||||
|
|
||||||
|
declare ptr @PyObject_CallFunctionObjArgs(ptr, ...)
|
||||||
|
|
||||||
declare void @llgoLoadPyModSyms(ptr, ...)
|
declare void @llgoLoadPyModSyms(ptr, ...)
|
||||||
|
|
||||||
|
|||||||
@@ -144,6 +144,8 @@ func TestErrBuiltin(t *testing.T) {
|
|||||||
test("siglongjmp", func(ctx *context) { ctx.siglongjmp(nil, nil) })
|
test("siglongjmp", func(ctx *context) { ctx.siglongjmp(nil, nil) })
|
||||||
test("cstr(NoArgs)", func(ctx *context) { cstr(nil, nil) })
|
test("cstr(NoArgs)", func(ctx *context) { cstr(nil, nil) })
|
||||||
test("cstr(Nonconst)", func(ctx *context) { cstr(nil, []ssa.Value{&ssa.Parameter{}}) })
|
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("atomic", func(ctx *context) { ctx.atomic(nil, 0, nil) })
|
||||||
test("atomicLoad", func(ctx *context) { ctx.atomicLoad(nil, nil) })
|
test("atomicLoad", func(ctx *context) { ctx.atomicLoad(nil, nil) })
|
||||||
test("atomicStore", func(ctx *context) { ctx.atomicStore(nil, nil) })
|
test("atomicStore", func(ctx *context) { ctx.atomicStore(nil, nil) })
|
||||||
|
|||||||
@@ -391,6 +391,7 @@ const (
|
|||||||
llgoSiglongjmp = llgoInstrBase + 0xc
|
llgoSiglongjmp = llgoInstrBase + 0xc
|
||||||
|
|
||||||
llgoPyList = llgoInstrBase + 0x10
|
llgoPyList = llgoInstrBase + 0x10
|
||||||
|
llgoPyStr = llgoInstrBase + 0x11
|
||||||
|
|
||||||
llgoAtomicLoad = llgoInstrBase + 0x1d
|
llgoAtomicLoad = llgoInstrBase + 0x1d
|
||||||
llgoAtomicStore = llgoInstrBase + 0x1e
|
llgoAtomicStore = llgoInstrBase + 0x1e
|
||||||
|
|||||||
16
cl/instr.go
16
cl/instr.go
@@ -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(string) *int8
|
||||||
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
func cstr(b llssa.Builder, args []ssa.Value) (ret llssa.Expr) {
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
@@ -175,6 +188,7 @@ var llgoInstrs = map[string]int{
|
|||||||
"string": llgoString,
|
"string": llgoString,
|
||||||
"stringData": llgoStringData,
|
"stringData": llgoStringData,
|
||||||
"funcAddr": llgoFuncAddr,
|
"funcAddr": llgoFuncAddr,
|
||||||
|
"pystr": llgoPyStr,
|
||||||
"pyList": llgoPyList,
|
"pyList": llgoPyList,
|
||||||
"sigjmpbuf": llgoSigjmpbuf,
|
"sigjmpbuf": llgoSigjmpbuf,
|
||||||
"sigsetjmp": llgoSigsetjmp,
|
"sigsetjmp": llgoSigsetjmp,
|
||||||
@@ -314,6 +328,8 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
|
|||||||
case llgoPyList:
|
case llgoPyList:
|
||||||
args := p.compileValues(b, args, fnHasVArg)
|
args := p.compileValues(b, args, fnHasVArg)
|
||||||
ret = b.PyList(args...)
|
ret = b.PyList(args...)
|
||||||
|
case llgoPyStr:
|
||||||
|
ret = pystr(b, args)
|
||||||
case llgoCstr:
|
case llgoCstr:
|
||||||
ret = cstr(b, args)
|
ret = cstr(b, args)
|
||||||
case llgoAdvance:
|
case llgoAdvance:
|
||||||
|
|||||||
@@ -750,7 +750,6 @@ func (p *pp) printArg(arg any, verb rune) {
|
|||||||
p.fmtInteger(uint64(f), unsigned, verb)
|
p.fmtInteger(uint64(f), unsigned, verb)
|
||||||
case string:
|
case string:
|
||||||
p.fmtString(f, verb)
|
p.fmtString(f, verb)
|
||||||
/* TODO(xsw):
|
|
||||||
case []byte:
|
case []byte:
|
||||||
p.fmtBytes(f, verb, "[]byte")
|
p.fmtBytes(f, verb, "[]byte")
|
||||||
case reflect.Value:
|
case reflect.Value:
|
||||||
@@ -763,17 +762,13 @@ func (p *pp) printArg(arg any, verb rune) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.printValue(f, verb, 0)
|
p.printValue(f, verb, 0)
|
||||||
*/
|
|
||||||
default:
|
default:
|
||||||
/*
|
|
||||||
// If the type is not simple, it might have methods.
|
// If the type is not simple, it might have methods.
|
||||||
if !p.handleMethods(verb) {
|
if !p.handleMethods(verb) {
|
||||||
// Need to use reflection, since the type had no
|
// Need to use reflection, since the type had no
|
||||||
// interface methods that could be used for formatting.
|
// interface methods that could be used for formatting.
|
||||||
p.printValue(reflect.ValueOf(f), verb, 0)
|
p.printValue(reflect.ValueOf(f), verb, 0)
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
panic("todo: fmt.(*pp).printArg")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ func NewFuncWithQualName(code, globals, qualname *Object) *Object
|
|||||||
// Return true if o is a function object (has type PyFunction_Type). The
|
// Return true if o is a function object (has type PyFunction_Type). The
|
||||||
// parameter must not be nil. This function always succeeds.
|
// parameter must not be nil. This function always succeeds.
|
||||||
//
|
//
|
||||||
// llgo:link (*Object).FuncCheck C.PyFunction_Check
|
//- llgo:link (*Object).FuncCheck C.PyFunction_Check
|
||||||
func (o *Object) FuncCheck() c.Int { return 0 }
|
func (o *Object) FuncCheck() c.Int { return 0 }
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -49,5 +49,5 @@ func (t *Object) TypeFlags() uint32 { return 0 }
|
|||||||
// llgo:link (*Object).TypeModule C.PyType_GetModule
|
// llgo:link (*Object).TypeModule C.PyType_GetModule
|
||||||
func (t *Object) TypeModule() *Object { return nil }
|
func (t *Object) TypeModule() *Object { return nil }
|
||||||
|
|
||||||
// llgo:link (*Object).TypeModuleByDef C.PyType_GetModuleByDef
|
// -llgo:link (*Object).TypeModuleByDef C.PyType_GetModuleByDef
|
||||||
// func (t *Object) TypeModuleByDef(def *ModuleDef) *Object { return nil }
|
// func (t *Object) TypeModuleByDef(def *ModuleDef) *Object { return nil }
|
||||||
|
|||||||
@@ -24,6 +24,20 @@ import (
|
|||||||
|
|
||||||
// https://docs.python.org/3/c-api/unicode.html
|
// https://docs.python.org/3/c-api/unicode.html
|
||||||
|
|
||||||
|
//go:linkname Str llgo.pystr
|
||||||
|
func Str(s string) *Object
|
||||||
|
|
||||||
|
//go:linkname FromCStr C.PyUnicode_FromString
|
||||||
|
func FromCStr(str *c.Char) *Object
|
||||||
|
|
||||||
|
//go:linkname FromCStrAndLen C.PyUnicode_FromStringAndSize
|
||||||
|
func FromCStrAndLen(str *c.Char, n int) *Object
|
||||||
|
|
||||||
|
// FromGoString returns a new Unicode object from a Go string.
|
||||||
|
func FromGoString(s string) *Object {
|
||||||
|
return FromCStrAndLen(c.GoStringData(s), len(s))
|
||||||
|
}
|
||||||
|
|
||||||
// Return a pointer to the UTF-8 encoding of the Unicode object, and store the
|
// Return a pointer to the UTF-8 encoding of the Unicode object, and store the
|
||||||
// size of the encoded representation (in bytes) in size. The size argument can
|
// size of the encoded representation (in bytes) in size. The size argument can
|
||||||
// be nil; in this case no size will be stored. The returned buffer always has
|
// be nil; in this case no size will be stored. The returned buffer always has
|
||||||
|
|||||||
@@ -174,6 +174,7 @@ type aProgram struct {
|
|||||||
callFOArgs *types.Signature
|
callFOArgs *types.Signature
|
||||||
loadPyModS *types.Signature
|
loadPyModS *types.Signature
|
||||||
getAttrStr *types.Signature
|
getAttrStr *types.Signature
|
||||||
|
pyUniStr *types.Signature
|
||||||
|
|
||||||
mallocTy *types.Signature
|
mallocTy *types.Signature
|
||||||
freeTy *types.Signature
|
freeTy *types.Signature
|
||||||
|
|||||||
@@ -184,6 +184,17 @@ func (p Program) tyLoadPyModSyms() *types.Signature {
|
|||||||
return p.loadPyModS
|
return p.loadPyModS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func(*char) *Object
|
||||||
|
func (p Program) tyPyUnicodeFromString() *types.Signature {
|
||||||
|
if p.pyUniStr == nil {
|
||||||
|
charPtr := types.NewPointer(types.Typ[types.Int8])
|
||||||
|
params := types.NewTuple(types.NewParam(token.NoPos, nil, "", charPtr))
|
||||||
|
results := types.NewTuple(p.paramObjPtr())
|
||||||
|
p.pyUniStr = types.NewSignatureType(nil, nil, nil, params, results, false)
|
||||||
|
}
|
||||||
|
return p.pyUniStr
|
||||||
|
}
|
||||||
|
|
||||||
// func(*Objecg, *char) *Object
|
// func(*Objecg, *char) *Object
|
||||||
func (p Program) tyGetAttrString() *types.Signature {
|
func (p Program) tyGetAttrString() *types.Signature {
|
||||||
if p.getAttrStr == nil {
|
if p.getAttrStr == nil {
|
||||||
@@ -332,6 +343,12 @@ func (b Builder) callPyInit() (ret Expr) {
|
|||||||
return b.Call(fn)
|
return b.Call(fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PyStr returns a py-style string constant expression.
|
||||||
|
func (b Builder) PyStr(v string) Expr {
|
||||||
|
fn := b.Pkg.pyFunc("PyUnicode_FromString", b.Prog.tyPyUnicodeFromString())
|
||||||
|
return b.Call(fn, b.CStr(v))
|
||||||
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
type aPyGlobal struct {
|
type aPyGlobal struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user