-
Notifications
You must be signed in to change notification settings - Fork 24
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
base: develop
Are you sure you want to change the base?
Fixed go header code #139
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
} | ||
} | ||
|
||
w.Writeln(")") | ||
w.Writeln("") | ||
w.Writeln("type ref = C.%sHandle", component.NameSpace) | ||
|
@@ -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 { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
|
||
tp.Empty = "\"\"" | ||
case "pointer": | ||
tp.Type = "uint64" | ||
|
@@ -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 { | ||
|
@@ -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) | ||
|
There was a problem hiding this comment.
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 infoThere was a problem hiding this comment.
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 ".."?
There was a problem hiding this comment.
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: