@@ -33,6 +33,7 @@ func (m *ModuleWriter) WriteCode() {
3333 m .WriteTypeAliases ()
3434 m .WriteStructs ()
3535 m .WriteFunctions ()
36+ m .WriteFunctionWrappers ()
3637 if m .Module .Package == "coreimage" {
3738 // filter protocols maybe arent "real" protocols?
3839 // get "cannot find protocol declaration" with protocol imports
@@ -125,19 +126,20 @@ func (m *ModuleWriter) WriteStructs() {
125126 cw .WriteLine (")" )
126127
127128 for _ , s := range m .Structs {
128- if s .DocURL != "" {
129- cw .WriteLine (fmt .Sprintf ("// %s [Full Topic]" , s .Description ))
130- cw .WriteLine (fmt .Sprintf ("//\n // [Full Topic]: %s" , s .DocURL ))
131- }
132-
133129 // if Ref type, allias to unsafe.Pointer
134130 if strings .HasSuffix (s .Name , "Ref" ) {
131+ if s .DocURL != "" {
132+ cw .WriteLine (fmt .Sprintf ("// %s [Full Topic]" , s .Description ))
133+ cw .WriteLine (fmt .Sprintf ("//\n // [Full Topic]: %s" , s .DocURL ))
134+ }
135+
135136 cw .WriteLineF ("type %s unsafe.Pointer" , s .GoName )
136137 continue
137138 }
138139 }
139140}
140141
142+ // WriteFunctions writes the go code to call exposed functions.
141143func (m * ModuleWriter ) WriteFunctions () {
142144 if len (m .Functions ) == 0 {
143145 return
@@ -156,10 +158,10 @@ func (m *ModuleWriter) WriteFunctions() {
156158 cw .WriteLine ("package " + m .Module .Package )
157159
158160 //TODO: determine imports from functions
159- cw .WriteLine (`// #import <stdlib.h>
160- // #import <stdint.h>
161- // #import <stdbool.h>
162- // #import <CoreGraphics/CGGeometry.h>` )
161+ cw .WriteLineF (`// #import <stdlib.h>
162+ // #import <stdint.h>
163+ // #import <stdbool.h>
164+ // #import "%s"` , m . Module . Header )
163165 for _ , f := range m .Functions {
164166 f .WriteCSignature (& m .Module , cw )
165167 }
@@ -185,6 +187,32 @@ func (m *ModuleWriter) WriteFunctions() {
185187 }
186188}
187189
190+ // WriteFunctionWrappers writes the objc code to wrap exposed functions.
191+ // The cgo type system is unaware of objective c types so these wrappers must exist to allow
192+ // us to call the functions and return appropritely.
193+ func (m * ModuleWriter ) WriteFunctionWrappers () {
194+ if len (m .Functions ) == 0 {
195+ return
196+ }
197+
198+ filePath := filepath .Join (m .PlatformDir , m .Module .Package , "functions.gen.m" )
199+ os .MkdirAll (filepath .Dir (filePath ), 0755 )
200+ f , err := os .Create (filePath )
201+ if err != nil {
202+ panic (err )
203+ }
204+ defer f .Close ()
205+
206+ cw := & CodeWriter {Writer : f , IndentStr : "\t " }
207+ cw .WriteLine (AutoGeneratedMark )
208+
209+ //TODO: determine appropriate imports
210+ cw .WriteLineF ("#import \" %s\" " , m .Module .Header )
211+ for _ , f := range m .Functions {
212+ f .WriteObjcWrapper (& m .Module , cw )
213+ }
214+ }
215+
188216func (m * ModuleWriter ) WriteEnumAliases () {
189217 enums := make ([]* AliasInfo , len (m .EnumAliases ))
190218 copy (enums , m .EnumAliases )
0 commit comments