Skip to content

Commit 940a277

Browse files
committed
coregraphics: Hand-assemble call
1 parent eca1dd8 commit 940a277

File tree

2 files changed

+41
-117
lines changed

2 files changed

+41
-117
lines changed

generate/codegen/gen_function.go

Lines changed: 30 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/progrium/macdriver/internal/set"
9-
"github.com/progrium/macdriver/internal/stringx"
109

1110
"github.com/progrium/macdriver/generate/modules"
1211
"github.com/progrium/macdriver/generate/typing"
@@ -51,6 +50,16 @@ func (f *Function) GoReturn(currentModule *modules.Module) string {
5150
return f.ReturnType.GoName(currentModule, true)
5251
}
5352

53+
// CArgs return go function args
54+
func (f *Function) CArgs(currentModule *modules.Module) string {
55+
// log.Println("rendering function", f.Name)
56+
var args []string
57+
for _, p := range f.Parameters {
58+
args = append(args, fmt.Sprintf("%s %s", p.Type.ObjcName(), p.Name))
59+
}
60+
return strings.Join(args, ", ")
61+
}
62+
5463
// Selector return full Objc function name
5564
func (f *Function) Selector() string {
5665
if f.identifier == "" {
@@ -85,8 +94,8 @@ func (f *Function) NormalizeInstanceTypeFunction(returnType *typing.ClassType) *
8594
}
8695

8796
// WriteGoCallCode generate go function code to call c wrapper code
88-
func (f *Function) WriteGoCallCode(currentModule *modules.Module, typeName string, cw *CodeWriter) {
89-
funcDeclare := f.GoFuncDeclare(currentModule, typeName)
97+
func (f *Function) WriteGoCallCode(currentModule *modules.Module, cw *CodeWriter) {
98+
funcDeclare := f.GoFuncDeclare(currentModule)
9099

91100
if f.Deprecated {
92101
return
@@ -98,10 +107,7 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, typeName strin
98107
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", f.DocURL))
99108
}
100109

101-
var receiver string
102-
receiver = strings.ToLower(typeName[0:1] + "_")
103-
cw.WriteLine("func (" + receiver + " " + typeName + ") " + funcDeclare + " {")
104-
110+
cw.WriteLine("func " + funcDeclare + " {")
105111
cw.Indent()
106112

107113
var returnTypeStr string
@@ -112,7 +118,7 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, typeName strin
112118
default:
113119
returnTypeStr = f.ReturnType.GoName(currentModule, true)
114120
}
115-
callCode := fmt.Sprintf("objc.Call[%s](%s, objc.Sel(\"%s\")", returnTypeStr, receiver, f.Selector())
121+
callCode := fmt.Sprintf("objc.Call[%s](%s, objc.Sel(\"%s\")", returnTypeStr, f.Selector())
116122
var sb strings.Builder
117123
for idx, p := range f.Parameters {
118124
sb.WriteString(", ")
@@ -148,82 +154,32 @@ func (f *Function) WriteGoCallCode(currentModule *modules.Module, typeName strin
148154
cw.WriteLine("}")
149155
}
150156

157+
func (f *Function) WriteCSignature(currentModule *modules.Module, cw *CodeWriter) {
158+
var returnTypeStr string
159+
rt := typing.UnwrapAlias(f.ReturnType)
160+
switch rt.(type) {
161+
case *typing.VoidType:
162+
returnTypeStr = "void *"
163+
default:
164+
returnTypeStr = f.ReturnType.ObjcName()
165+
}
166+
cw.WriteLineF("// %v %v(%v); ", returnTypeStr, f.GoName, f.CArgs(currentModule))
167+
}
168+
151169
// WriteGoInterfaceCode generate go interface function signature code
152170
func (f *Function) WriteGoInterfaceCode(currentModule *modules.Module, classType *typing.ClassType, w *CodeWriter) {
153171
if f.Deprecated {
154172
return
155173
w.WriteLine("// deprecated")
156174
}
157-
funcDeclare := f.GoFuncDeclare(currentModule, classType.GName)
175+
funcDeclare := f.GoFuncDeclare(currentModule)
158176
w.WriteLine(funcDeclare)
159177
}
160178

161179
// GoFuncDeclare generate go function declaration
162-
func (f *Function) GoFuncDeclare(currentModule *modules.Module, goTypeName string) string {
163-
var paramStrs []string
164-
for _, p := range f.Parameters {
165-
paramStrs = append(paramStrs, p.GoDeclare(currentModule, false))
166-
}
167-
168-
var returnType = f.ReturnType.GoName(currentModule, true)
169-
return f.GoFuncName() + "(" + strings.Join(paramStrs, ", ") + ")" + " " + returnType
170-
}
171-
172-
// GoFuncName return go func name
173-
func (f *Function) GoFuncName() string {
174-
if f.goFuncName == "" {
175-
var sb strings.Builder
176-
name := f.GoName
177-
if len(f.Parameters) == 0 {
178-
sb.WriteString(stringx.Capitalize(name))
179-
}
180-
181-
for _, p := range f.Parameters {
182-
sb.WriteString(stringx.Capitalize(p.FieldName))
183-
if p.Object {
184-
sb.WriteString("Object")
185-
}
186-
}
187-
188-
f.goFuncName = sb.String()
189-
}
190-
if f.Suffix || f.goFuncName == "Object" {
191-
return f.goFuncName + "_"
192-
}
193-
return f.goFuncName
194-
}
195-
196-
// ProtocolGoFuncFieldType generate go function declaration for protocol struct impl field
197-
func (f *Function) ProtocolGoFuncFieldType(currentModule *modules.Module) string {
198-
var paramStrs []string
199-
for _, p := range f.Parameters {
200-
paramStrs = append(paramStrs, p.GoDeclare(currentModule, true))
201-
}
202-
203-
return "(" + strings.Join(paramStrs, ", ") + ")" + " " + f.ReturnType.GoName(currentModule, false)
204-
}
205-
206-
// ProtocolGoFuncName return go protocol func name
207-
func (f *Function) ProtocolGoFuncName() string {
208-
if f.goFuncName == "" {
209-
var sb strings.Builder
210-
sb.WriteString(stringx.Capitalize(f.Name))
211-
for idx, p := range f.Parameters {
212-
if idx == 0 {
213-
continue
214-
}
215-
sb.WriteString(stringx.Capitalize(p.FieldName))
216-
if p.Object {
217-
sb.WriteString("Object")
218-
}
219-
}
220-
221-
f.goFuncName = sb.String()
222-
}
223-
if f.Suffix {
224-
return f.goFuncName + "_"
225-
}
226-
return f.goFuncName
180+
func (f *Function) GoFuncDeclare(currentModule *modules.Module) string {
181+
var returnType = f.GoReturn(currentModule)
182+
return f.Type.GoName(currentModule, true) + "(" + f.GoArgs(currentModule) + ") " + returnType
227183
}
228184

229185
// GoImports return all imports for go file
@@ -237,40 +193,3 @@ func (f *Function) GoImports() set.Set[string] {
237193
}
238194
return imports
239195
}
240-
241-
func (f *Function) HasProtocolParam() bool {
242-
for _, p := range f.Parameters {
243-
switch p.Type.(type) {
244-
case *typing.ProtocolType:
245-
return true
246-
}
247-
}
248-
return false
249-
}
250-
251-
func (f *Function) ToProtocolParamAsObjectFunction() *Function {
252-
var newParams = make([]*Param, len(f.Parameters))
253-
for i, p := range f.Parameters {
254-
switch p.Type.(type) {
255-
case *typing.ProtocolType:
256-
newParams[i] = &Param{
257-
Name: p.Name,
258-
Type: typing.Object,
259-
FieldName: p.FieldName,
260-
Object: true,
261-
}
262-
default:
263-
newParams[i] = p
264-
}
265-
}
266-
return &Function{
267-
Name: f.Name,
268-
GoName: f.GoName,
269-
Parameters: newParams,
270-
Suffix: f.Suffix,
271-
ReturnType: f.ReturnType,
272-
Deprecated: f.Deprecated,
273-
Description: f.Description,
274-
DocURL: f.DocURL,
275-
}
276-
}

generate/codegen/modulewriter.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ func (m *ModuleWriter) WriteFunctions() {
155155
cw.WriteLine(AutoGeneratedMark)
156156
cw.WriteLine("package " + m.Module.Package)
157157

158+
cw.WriteLine(`// #import <stdlib.h>
159+
// #import <stdint.h>
160+
// #import <stdbool.h>
161+
// #import <CoreGraphics/CGGeometry.h>
162+
`)
163+
for _, f := range m.Functions {
164+
f.WriteCSignature(&m.Module, cw)
165+
}
166+
cw.WriteLine(`import "C"`)
158167
cw.WriteLine("import (")
159168

160169
// imports
@@ -171,12 +180,8 @@ func (m *ModuleWriter) WriteFunctions() {
171180
cw.UnIndent()
172181
cw.WriteLine(")")
173182

174-
for _, fa := range m.Functions {
175-
if fa.DocURL != "" {
176-
cw.WriteLine(fmt.Sprintf("// %s [Full Topic]", fa.Description))
177-
cw.WriteLine(fmt.Sprintf("//\n// [Full Topic]: %s", fa.DocURL))
178-
}
179-
cw.WriteLineF("// func %s(%s) %s {}", fa.Type.GoName(&m.Module, true), fa.GoArgs(&m.Module), fa.GoReturn(&m.Module))
183+
for _, f := range m.Functions {
184+
f.WriteGoCallCode(&m.Module, cw)
180185
}
181186
}
182187

0 commit comments

Comments
 (0)