-
Notifications
You must be signed in to change notification settings - Fork 409
feat(extgen): make the generator idempotent and avoid touching the original source #2011
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
base: main
Are you sure you want to change the base?
Conversation
2caf932 to
0672f92
Compare
| start := 0 | ||
| funcBytes := []byte(goFunction) | ||
| if idx := bytes.Index(funcBytes, []byte("func ")); idx != -1 { | ||
| start = idx + len("func ") | ||
| } else { | ||
| return "" | ||
| } |
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.
| start := 0 | |
| funcBytes := []byte(goFunction) | |
| if idx := bytes.Index(funcBytes, []byte("func ")); idx != -1 { | |
| start = idx + len("func ") | |
| } else { | |
| return "" | |
| } | |
| idx := strings.Index(goFunction, "func ") | |
| if idx == -1 { | |
| return "" | |
| } | |
| start := idx + len("func ") |
| return "" | ||
| } | ||
|
|
||
| return string(bytes.TrimSpace(funcBytes[start:end])) |
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.
| return string(bytes.TrimSpace(funcBytes[start:end])) | |
| return strings.TrimSpace(goFunction[start:end])) |
|
|
||
| // extractGoFunctionSignatureParams extracts the parameters from a Go function signature. | ||
| func extractGoFunctionSignatureParams(goFunction string) string { | ||
| start := bytes.IndexByte([]byte(goFunction), '(') |
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.
| start := bytes.IndexByte([]byte(goFunction), '(') | |
| start := strings.IndexByte(goFunction, '(') |
| return "" | ||
| } | ||
|
|
||
| return string(bytes.TrimSpace([]byte(goFunction[start:end]))) |
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.
| return string(bytes.TrimSpace([]byte(goFunction[start:end]))) | |
| return strings.TrimSpace(goFunction[start:end])) |
|
|
||
| // extractGoFunctionSignatureReturn extracts the return type from a Go function signature. | ||
| func extractGoFunctionSignatureReturn(goFunction string) string { | ||
| start := bytes.IndexByte([]byte(goFunction), '(') |
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.
| start := bytes.IndexByte([]byte(goFunction), '(') | |
| start := strings.IndexByte(goFunction, '(') |
| } | ||
|
|
||
| var names []string | ||
| parts := bytes.Split([]byte(params), []byte(",")) |
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.
| parts := bytes.Split([]byte(params), []byte(",")) | |
| parts := strings.Split(params, ",") |
| var names []string | ||
| parts := bytes.Split([]byte(params), []byte(",")) | ||
| for _, part := range parts { | ||
| part = bytes.TrimSpace(part) |
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.
| part = bytes.TrimSpace(part) | |
| part = strings.TrimSpace(part) |
| continue | ||
| } | ||
|
|
||
| words := bytes.Fields(part) |
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.
| words := bytes.Fields(part) | |
| words := strings.Fields(part) |
|
|
||
| words := bytes.Fields(part) | ||
| if len(words) > 0 { | ||
| names = append(names, string(words[0])) |
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.
| names = append(names, string(words[0])) | |
| names = append(names, words[0]) |
| var result []byte | ||
| for i, name := range names { | ||
| if i > 0 { | ||
| result = append(result, []byte(", ")...) | ||
| } | ||
| result = append(result, []byte(name)...) | ||
| } | ||
|
|
||
| return string(result) |
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.
| var result []byte | |
| for i, name := range names { | |
| if i > 0 { | |
| result = append(result, []byte(", ")...) | |
| } | |
| result = append(result, []byte(name)...) | |
| } | |
| return string(result) | |
| var result strings.Builder | |
| for i, name := range names { | |
| if i > 0 { | |
| result.WriteString(", ") | |
| } | |
| result.WriteString(name) | |
| } | |
| return result.String() |
The DX is currently not optimal as we need to touch and edit the original source file. It is still in WIP, but this PR aims to improve a lot the DX of the extension generator by making it idempotent.
The proposed approach is to generate a wrapper to the original source file that will call it and then make the bindings to C. The original Go source will remain untouched and the generator can be run multiple times easily.
This also adds a header comment to all generated files (C file, C headers et al.):