Skip to content

Commit

Permalink
bind: add Go constructor for bound classes
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Lagergren <[email protected]>
  • Loading branch information
elagergren-spideroak committed Nov 28, 2022
1 parent aaac322 commit a1109b0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions FORK
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changes
#
# 1. Panic when a Java class cannot be found.
#
# 2. Add Go constructor for generated Java wrappers of Go code to facilitate
# Java calling back into Go.
2 changes: 1 addition & 1 deletion bind/genclasses.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func (g *ClassGen) genGo(cls *java.Class) {
g.Printf("C.free(unsafe.Pointer(cls))\n")
// Before Go 1.11 clazz was a pointer value, an uintptr after.
g.Printf("if uintptr(clazz) == 0 {\n")
g.Printf(" return\n")
g.Printf("\tpanic(`unable to find class %q`)\n", strings.Replace(cls.FindName, ".", "/", -1))
g.Printf("}\n")
g.Printf("class_%s = clazz\n", cls.JNIName)
for _, fs := range cls.Funcs {
Expand Down
27 changes: 27 additions & 0 deletions internal/importers/java/java.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,33 @@ func (j *Importer) Import(refs *importers.References) ([]*Class, error) {
JNIName: JNIMangle(n),
PkgName: emb.Name,
HasNoArgCon: true,
Funcs: []*FuncSet{
{
Name: n,
GoName: "New",
Funcs: []*Func{{
FuncSig: FuncSig{
Name: "new", // or emb.Name
Desc: "()V",
},
JNIName: JNIMangle(emb.Name),
Public: true,
Final: true,
Constructor: true,
Ret: &Type{
Kind: Object,
Class: n,
},
}},
CommonSig: CommonSig{
HasRet: true,
Ret: &Type{
Kind: Object,
Class: n,
},
},
},
},
}
for _, ref := range emb.Refs {
jpkg := strings.Replace(ref.Pkg, "/", ".", -1)
Expand Down

0 comments on commit a1109b0

Please sign in to comment.