Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed go header code #139

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions Source/buildbindinggo.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,22 @@ func buildGoWrapper(component ComponentDefinition, w LanguageWriter) error {
w.Writeln("/*")
w.Writeln("#include \"%s_dynamic.cc\"", packageName)
w.Writeln("")

if len(component.ImportedComponentDefinitions) > 0 {
w.Writeln("// Injected Components")
for _, subComponent := range(component.ImportedComponentDefinitions) {
subNameSpace := subComponent.NameSpace

w.Writeln("%sHandle inject%sHandle (void * p%sHandle)", subNameSpace, subNameSpace, subNameSpace)
w.Writeln("{");
w.Writeln(" return (%sHandle) p%sHandle;", subNameSpace, subNameSpace);
w.Writeln("}");
w.Writeln("");
}
}


w.Writeln("")
w.Writeln("%sHandle load%sLibrary (const char * pFileName)", component.NameSpace, component.NameSpace)
w.Writeln("{")
w.Writeln(" %sResult nResult;", component.NameSpace)
Expand Down Expand Up @@ -586,6 +602,15 @@ func buildGoWrapper(component ComponentDefinition, w LanguageWriter) error {
w.Writeln(" \"fmt\"")
w.Writeln(" \"unsafe\"")
w.Writeln(" \"runtime\"")

if len(component.ImportedComponentDefinitions) > 0 {
w.Writeln(" // Injected Components")
for _, subComponent := range(component.ImportedComponentDefinitions) {
subNameSpace := subComponent.NameSpace
w.Writeln(" \"../%s\"", subNameSpace)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relative imports does not work when using modules, which is the standard go dependency managment. More info

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, how would we integrate this then? Just leave away the ".."?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the .. don't work, all imports that are not from the standard library needs to be prefixed with the name of the module, even if the package is in the same module. Therefor the name of the module should be known at generation time if you want to import another package.

Another approach would be to define an interface that is fulfilled by the injected component and use it instead of importing it, this way the generated code is decoupled from the location of the injected code. This is possible in Go but not in other languages because a struct doesn't need to specify that it is implementing an interface to be used as so.

Example using the calculator example:

// NumberVariable is infered from Numbers.xml
type NumberVariable interface {
  GetValue() float64
  SetValue(float64)
}

type Calculator struct {
  ...
}
func (c *Calculator) GetEnlistedVariable(index uint32) NumberVariable {
  ...
}

}
}

w.Writeln(")")
w.Writeln("")
w.Writeln("type ref = C.%sHandle", component.NameSpace)
Expand All @@ -608,7 +633,7 @@ func buildGoWrapper(component ComponentDefinition, w LanguageWriter) error {
w.Writeln("// Wrapper represents the number wrapper")
w.Writeln("type Wrapper struct {")
w.Writeln(" _ [0]func() // uncomparable; to make == not compile")
w.Writeln(" LibraryHandle ref")
w.Writeln(" LibraryHandle C.%sHandle", component.NameSpace)
w.Writeln("}")

for _, class := range component.Classes {
Expand Down Expand Up @@ -724,7 +749,7 @@ func getGoType(paramType, namespace, paramClass, paramName string, isPtr bool) (
tp.Type = ptrStr + "string"
tp.CType = ptrStr + "*C.char"
tp.CToGo = ""
tp.GoToC = fmt.Sprintf("(%s)(unsafe.Pointer(&[]byte(%s)[0]))", tp.CType, paramName)
tp.GoToC = fmt.Sprintf("C.CString(%s)", paramName)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C.CString is allocated in the C heap using malloc and it should be freed after used, so this change will leak all string casts.
I believe there is no need to allocate a new c string when a c function requires one and it should be safe to just pass the reference to the memory backing the go string, it won't be relocated nor freed at least until the go wrapper function finishes.
When casting a string from c to go we are just taking a temporary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with the old code was the the string was not nullterminated, causing a lot of memory issues.

Copy link

@qmuntal qmuntal Aug 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I didn't remember that go string are not null-terminated. There are two option then:

  • Append a null termination char to the end of the string: tp.GoToC = fmt.Sprintf("(%s)(unsafe.Pointer(&[]byte(%s+"\x00")[0]))", tp.CType, paramName). I´m not sure that this would work on all cases.
  • Use C.CString(...) and defer C.free(...) in the next line.

tp.Empty = "\"\""
case "pointer":
tp.Type = "uint64"
Expand Down Expand Up @@ -936,7 +961,7 @@ func writeGoMethod(method ComponentDefinitionMethod, w LanguageWriter, NameSpace
implmethodname := "C.CCall_" + packageName + "_"
returnValues = append(returnValues, "nil")
classReturnTypes = append(classReturnTypes, "error")
errorReturn = append(errorReturn, "makeError(uint32(ret))")
errorReturn = append(errorReturn, "makeError(uint32(returnValue))")

var returnString string
if len(classReturnTypes) == 1 {
Expand All @@ -961,25 +986,25 @@ func writeGoMethod(method ComponentDefinitionMethod, w LanguageWriter, NameSpace
retInst := ":"
if requiresInitCall {
if isGlobal {
w.Writeln(" ret := %s(wrapper.LibraryHandle, %s)", implmethodname, strings.Join(initCallParameters, ", "))
w.Writeln(" returnValue := %s(wrapper.LibraryHandle, %s)", implmethodname, strings.Join(initCallParameters, ", "))
} else {
w.Writeln(" ret := %s(inst.wrapperRef.LibraryHandle, %s)", implmethodname, strings.Join(initCallParameters, ", "))
w.Writeln(" returnValue := %s(inst.wrapperRef.LibraryHandle, %s)", implmethodname, strings.Join(initCallParameters, ", "))
}

w.Writeln(" if ret != 0 {")
w.Writeln(" if returnValue != 0 {")
w.Writeln(" return %s", strings.Join(errorReturn, ", "))
w.Writeln(" }")
w.Writelns(" ", initCallLines)
retInst = ""
}

if isGlobal {
w.Writeln(" ret %s= %s(wrapper.LibraryHandle, %s)", retInst, implmethodname, strings.Join(callParameters, ", "))
w.Writeln(" returnValue %s= %s(wrapper.LibraryHandle, %s)", retInst, implmethodname, strings.Join(callParameters, ", "))
} else {
w.Writeln(" ret %s= %s(inst.wrapperRef.LibraryHandle, %s)", retInst, implmethodname, strings.Join(callParameters, ", "))
w.Writeln(" returnValue %s= %s(inst.wrapperRef.LibraryHandle, %s)", retInst, implmethodname, strings.Join(callParameters, ", "))
}

w.Writeln(" if ret != 0 {")
w.Writeln(" if returnValue != 0 {")
w.Writeln(" return %s", strings.Join(errorReturn, ", "))
w.Writeln(" }")
w.Writelns(" ", preOKReturn)
Expand Down