From 1c584ecbb91e6ed7cda138f1f1b3382cb348af33 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Fri, 12 Sep 2025 13:24:23 -0700 Subject: [PATCH 01/27] Adding better error handling in Go through panic/recovery method with error returns --- PDFTronGo/pdftron.i | 89 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 6 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 4a4da2f4..20f72e9e 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -214,26 +214,103 @@ #undef SetPort %} -%include "exception.i" +// ============== +// ERROR HANDLING +// ============== +// Converts C++ exceptions to Go errors using panic/recovery mechanism. +// All functions now return an error in addition to their return type instead of panicking on exceptions. + +// Ensure necessary imports for error handling code +%insert(go_imports) %{ +import "errors" +import "fmt" +%} + +// Handle exceptions by triggering recoverable panic containing the exception message %exception { try { - $action; - } catch (std::exception &e) { + $action + } catch (const std::exception &e) { _swig_gopanic(e.what()); + } catch (...) { + _swig_gopanic("unknown exception occurred"); } } +// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +%define ERROR_HANDLING_TYPEMAPS(TYPE) +%typemap(gotype) TYPE "$gotype, error" +%typemap(cgoout) TYPE %{ + var swig_r $gotypes + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + swig_r = $cgocall + }() + + return swig_r, swig_err +%} +%enddef + +// Exclude director classes from typemaps +%typemap(gotype) SwigDirector_Callback "" +%typemap(cgoout) SwigDirector_Callback "" +%typemap(gotype) SwigDirector_SignatureHandler "" +%typemap(cgoout) SwigDirector_SignatureHandler "" + +// Apply gotype and cgoout typemaps to functions that return: + +// Value types +ERROR_HANDLING_TYPEMAPS(SWIGTYPE) +// Pointers +ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) +// References +ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) +// Primitives +ERROR_HANDLING_TYPEMAPS(bool) +ERROR_HANDLING_TYPEMAPS(char) +ERROR_HANDLING_TYPEMAPS(double) +ERROR_HANDLING_TYPEMAPS(int) +ERROR_HANDLING_TYPEMAPS(ptrdiff_t) +ERROR_HANDLING_TYPEMAPS(size_t) + +// Generate gotype and cgoout typemaps for void separately +%typemap(gotype) void "error" +%typemap(cgoout) void %{ + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + $cgocall + }() + + return swig_err +%} + +// Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug if ($1).GetMp_obj().Swigcptr() != 0 { - $result = $1 - return $result + return $1, swig_err } - $result = nil + return nil, swig_err %} +// ================== +// END ERROR HANDLING +// ================== + /** * Provides mapping for C++ vectors. * For example, vector will be called as VectorDouble in GoLang. From 32113370fbf3fd6fc375d8dc777c24c7bdc424d1 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 11:26:56 -0700 Subject: [PATCH 02/27] Moved error handling typemaps to after director classes --- PDFTronGo/pdftron.i | 154 ++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 76 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 20f72e9e..71aba210 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -214,9 +214,9 @@ #undef SetPort %} -// ============== -// ERROR HANDLING -// ============== +// =================== +// ERROR HANDLING PT 1 +// =================== // Converts C++ exceptions to Go errors using panic/recovery mechanism. // All functions now return an error in addition to their return type instead of panicking on exceptions. @@ -237,79 +237,9 @@ import "fmt" } } -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAPS(TYPE) -%typemap(gotype) TYPE "$gotype, error" -%typemap(cgoout) TYPE %{ - var swig_r $gotypes - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - swig_r = $cgocall - }() - - return swig_r, swig_err -%} -%enddef - -// Exclude director classes from typemaps -%typemap(gotype) SwigDirector_Callback "" -%typemap(cgoout) SwigDirector_Callback "" -%typemap(gotype) SwigDirector_SignatureHandler "" -%typemap(cgoout) SwigDirector_SignatureHandler "" - -// Apply gotype and cgoout typemaps to functions that return: - -// Value types -ERROR_HANDLING_TYPEMAPS(SWIGTYPE) -// Pointers -ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) -// References -ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) -// Primitives -ERROR_HANDLING_TYPEMAPS(bool) -ERROR_HANDLING_TYPEMAPS(char) -ERROR_HANDLING_TYPEMAPS(double) -ERROR_HANDLING_TYPEMAPS(int) -ERROR_HANDLING_TYPEMAPS(ptrdiff_t) -ERROR_HANDLING_TYPEMAPS(size_t) - -// Generate gotype and cgoout typemaps for void separately -%typemap(gotype) void "error" -%typemap(cgoout) void %{ - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - $cgocall - }() - - return swig_err -%} - -// Handle edge case: SDF::Obj returns nil when internal pointer is invalid -%typemap(goout) pdftron::SDF::Obj -%{ - // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug - if ($1).GetMp_obj().Swigcptr() != 0 { - return $1, swig_err - } - - return nil, swig_err -%} - -// ================== -// END ERROR HANDLING -// ================== +// ======================= +// END ERROR HANDLING PT 1 +// ======================= /** * Provides mapping for C++ vectors. @@ -414,6 +344,78 @@ namespace pdftron { %} %include "PDF/Date.h" +// =================== +// ERROR HANDLING PT 2 +// =================== + +// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +%define ERROR_HANDLING_TYPEMAPS(TYPE) +%typemap(gotype) TYPE "$gotype, error" +%typemap(cgoout) TYPE %{ + var swig_r $gotypes + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + swig_r = $cgocall + }() + + return swig_r, swig_err +%} +%enddef + +// Apply gotype and cgoout typemaps to functions that return: + +// Value types +ERROR_HANDLING_TYPEMAPS(SWIGTYPE) +// Pointers +ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) +// References +ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) +// Primitives +ERROR_HANDLING_TYPEMAPS(bool) +ERROR_HANDLING_TYPEMAPS(char) +ERROR_HANDLING_TYPEMAPS(double) +ERROR_HANDLING_TYPEMAPS(int) +ERROR_HANDLING_TYPEMAPS(ptrdiff_t) +ERROR_HANDLING_TYPEMAPS(size_t) + +// Generate gotype and cgoout typemaps for void separately +%typemap(gotype) void "error" +%typemap(cgoout) void %{ + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + $cgocall + }() + + return swig_err +%} + +// Handle edge case: SDF::Obj returns nil when internal pointer is invalid +%typemap(goout) pdftron::SDF::Obj +%{ + // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug + if ($1).GetMp_obj().Swigcptr() != 0 { + return $1, swig_err + } + + return nil, swig_err +%} + +// ======================= +// END ERROR HANDLING PT 2 +// ======================= + //---------------------------------------------------------------------------------------------- // Fixes overloaded methods From 45398c6b21d295538d3ba9e3583b1c4aae4a8355 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 11:34:57 -0700 Subject: [PATCH 03/27] Added out to typemaps --- PDFTronGo/pdftron.i | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 71aba210..27d264c4 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -350,8 +350,8 @@ namespace pdftron { // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps %define ERROR_HANDLING_TYPEMAPS(TYPE) -%typemap(gotype) TYPE "$gotype, error" -%typemap(cgoout) TYPE %{ +%typemap(gotype, out) TYPE "$gotype, error" +%typemap(cgoout, out) TYPE %{ var swig_r $gotypes var swig_err error @@ -385,8 +385,8 @@ ERROR_HANDLING_TYPEMAPS(ptrdiff_t) ERROR_HANDLING_TYPEMAPS(size_t) // Generate gotype and cgoout typemaps for void separately -%typemap(gotype) void "error" -%typemap(cgoout) void %{ +%typemap(gotype, out) void "error" +%typemap(cgoout, out) void %{ var swig_err error func() { From 941a2fe50bbbb0227067971c8e8f4dec5e865ed0 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 11:45:42 -0700 Subject: [PATCH 04/27] Excluded director classes from error handling typemaps --- PDFTronGo/pdftron.i | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 27d264c4..0a0bf8ae 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -401,6 +401,32 @@ ERROR_HANDLING_TYPEMAPS(size_t) return swig_err %} +// Exclude director classes from error handling typemaps +%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Callback* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" +%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Separation* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Rect* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Date* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Date* %{ + return $cgocall +%} + // Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ From 2b81070aa9477ee25a838c371cb14faffb2c4f73 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 11:54:30 -0700 Subject: [PATCH 05/27] Moved director exclusions to above error handling typemaps --- PDFTronGo/pdftron.i | 52 ++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 0a0bf8ae..ab62c8fe 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -368,6 +368,32 @@ namespace pdftron { %} %enddef +// Exclude director classes from error handling typemaps +%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Callback* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" +%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Separation* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Rect* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Date* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Date* %{ + return $cgocall +%} + // Apply gotype and cgoout typemaps to functions that return: // Value types @@ -401,32 +427,6 @@ ERROR_HANDLING_TYPEMAPS(size_t) return swig_err %} -// Exclude director classes from error handling typemaps -%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Callback* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" -%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Separation* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Rect* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Date* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Date* %{ - return $cgocall -%} - // Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ From 3394e18a2b721eb39b3329d42b14f5378fab1d0b Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 12:00:48 -0700 Subject: [PATCH 06/27] Disabled exception handling on director classes --- PDFTronGo/pdftron.i | 47 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index ab62c8fe..6de2ff4f 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -226,6 +226,13 @@ import "errors" import "fmt" %} +// Exclude exception handling for director classes +%feature("except", "") pdftron::PDF::Callback; +%feature("except", "") pdftron::SDF::SignatureHandler; +%feature("except", "") pdftron::PDF::Separation; +%feature("except", "") pdftron::PDF::Rect; +%feature("except", "") pdftron::PDF::Date; + // Handle exceptions by triggering recoverable panic containing the exception message %exception { try { @@ -348,26 +355,6 @@ namespace pdftron { // ERROR HANDLING PT 2 // =================== -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAPS(TYPE) -%typemap(gotype, out) TYPE "$gotype, error" -%typemap(cgoout, out) TYPE %{ - var swig_r $gotypes - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - swig_r = $cgocall - }() - - return swig_r, swig_err -%} -%enddef - // Exclude director classes from error handling typemaps %typemap(gotype, out) pdftron::PDF::Callback* "$gotype" %typemap(cgoout, out) pdftron::PDF::Callback* %{ @@ -394,6 +381,26 @@ namespace pdftron { return $cgocall %} +// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +%define ERROR_HANDLING_TYPEMAPS(TYPE) +%typemap(gotype, out) TYPE "$gotype, error" +%typemap(cgoout, out) TYPE %{ + var swig_r $gotypes + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + swig_r = $cgocall + }() + + return swig_r, swig_err +%} +%enddef + // Apply gotype and cgoout typemaps to functions that return: // Value types From e91bf8f03d4fd25597fb60294535a8e410897437 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 12:18:17 -0700 Subject: [PATCH 07/27] Consolidated error handling code --- PDFTronGo/pdftron.i | 207 ++++++++++++++++++++------------------------ 1 file changed, 96 insertions(+), 111 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 6de2ff4f..5ed439f4 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -214,9 +214,9 @@ #undef SetPort %} -// =================== -// ERROR HANDLING PT 1 -// =================== +// ============== +// ERROR HANDLING +// ============== // Converts C++ exceptions to Go errors using panic/recovery mechanism. // All functions now return an error in addition to their return type instead of panicking on exceptions. @@ -226,13 +226,6 @@ import "errors" import "fmt" %} -// Exclude exception handling for director classes -%feature("except", "") pdftron::PDF::Callback; -%feature("except", "") pdftron::SDF::SignatureHandler; -%feature("except", "") pdftron::PDF::Separation; -%feature("except", "") pdftron::PDF::Rect; -%feature("except", "") pdftron::PDF::Date; - // Handle exceptions by triggering recoverable panic containing the exception message %exception { try { @@ -244,9 +237,99 @@ import "fmt" } } -// ======================= -// END ERROR HANDLING PT 1 -// ======================= +// Exclude director classes from error handling typemaps +%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Callback* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" +%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Separation* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Rect* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Date* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Date* %{ + return $cgocall +%} + +// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +%define ERROR_HANDLING_TYPEMAPS(TYPE) +%typemap(gotype, out) TYPE "$gotype, error" +%typemap(cgoout, out) TYPE %{ + var swig_r $gotypes + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + swig_r = $cgocall + }() + + return swig_r, swig_err +%} +%enddef + +// Apply gotype and cgoout typemaps to functions that return: + +// Value types +ERROR_HANDLING_TYPEMAPS(SWIGTYPE) +// Pointers +ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) +// References +ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) +// Primitives +ERROR_HANDLING_TYPEMAPS(bool) +ERROR_HANDLING_TYPEMAPS(char) +ERROR_HANDLING_TYPEMAPS(double) +ERROR_HANDLING_TYPEMAPS(int) +ERROR_HANDLING_TYPEMAPS(ptrdiff_t) +ERROR_HANDLING_TYPEMAPS(size_t) + +// Generate gotype and cgoout typemaps for void separately +%typemap(gotype, out) void "error" +%typemap(cgoout, out) void %{ + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + $cgocall + }() + + return swig_err +%} + +// Handle edge case: SDF::Obj returns nil when internal pointer is invalid +%typemap(goout) pdftron::SDF::Obj +%{ + // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug + if ($1).GetMp_obj().Swigcptr() != 0 { + return $1, swig_err + } + + return nil, swig_err +%} + +// ================== +// END ERROR HANDLING +// ================== /** * Provides mapping for C++ vectors. @@ -351,104 +434,6 @@ namespace pdftron { %} %include "PDF/Date.h" -// =================== -// ERROR HANDLING PT 2 -// =================== - -// Exclude director classes from error handling typemaps -%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Callback* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" -%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Separation* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Rect* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Date* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Date* %{ - return $cgocall -%} - -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAPS(TYPE) -%typemap(gotype, out) TYPE "$gotype, error" -%typemap(cgoout, out) TYPE %{ - var swig_r $gotypes - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - swig_r = $cgocall - }() - - return swig_r, swig_err -%} -%enddef - -// Apply gotype and cgoout typemaps to functions that return: - -// Value types -ERROR_HANDLING_TYPEMAPS(SWIGTYPE) -// Pointers -ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) -// References -ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) -// Primitives -ERROR_HANDLING_TYPEMAPS(bool) -ERROR_HANDLING_TYPEMAPS(char) -ERROR_HANDLING_TYPEMAPS(double) -ERROR_HANDLING_TYPEMAPS(int) -ERROR_HANDLING_TYPEMAPS(ptrdiff_t) -ERROR_HANDLING_TYPEMAPS(size_t) - -// Generate gotype and cgoout typemaps for void separately -%typemap(gotype, out) void "error" -%typemap(cgoout, out) void %{ - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - $cgocall - }() - - return swig_err -%} - -// Handle edge case: SDF::Obj returns nil when internal pointer is invalid -%typemap(goout) pdftron::SDF::Obj -%{ - // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug - if ($1).GetMp_obj().Swigcptr() != 0 { - return $1, swig_err - } - - return nil, swig_err -%} - -// ======================= -// END ERROR HANDLING PT 2 -// ======================= - //---------------------------------------------------------------------------------------------- // Fixes overloaded methods From 00b4d298c4d7eb7d9dca0bc84f5ed04e515ae86b Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 12:36:09 -0700 Subject: [PATCH 08/27] Added additional director excludes for SwigDirector_Callback and SwigDirector_SignatureHandler --- PDFTronGo/pdftron.i | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 5ed439f4..3531abe5 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -263,6 +263,16 @@ import "fmt" return $cgocall %} +%typemap(gotype, out) SwigDirector_Callback* "$gotype" +%typemap(cgoout, out) SwigDirector_Callback* %{ + return $cgocall +%} + +%typemap(gotype, out) SwigDirector_SignatureHandler* "$gotype" +%typemap(cgoout, out) SwigDirector_SignatureHandler* %{ + return $cgocall +%} + // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps %define ERROR_HANDLING_TYPEMAPS(TYPE) %typemap(gotype, out) TYPE "$gotype, error" From 4f3484b007456cbd0f03e5683acca0417bc24e9c Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 12:38:07 -0700 Subject: [PATCH 09/27] Excluded SwigDirectors from exception handling --- PDFTronGo/pdftron.i | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 3531abe5..c541b44e 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -226,6 +226,10 @@ import "errors" import "fmt" %} +// Disable exception handling for directors +%feature("except", "0") SwigDirector_Callback; +%feature("except", "0") SwigDirector_SignatureHandler; + // Handle exceptions by triggering recoverable panic containing the exception message %exception { try { From 17c312728104cae3c9cbf75eff16340da5fe4c4d Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 12:42:01 -0700 Subject: [PATCH 10/27] Added more exclusions for directors in a vain hope that it suddenly works --- PDFTronGo/pdftron.i | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index c541b44e..eb9c3edf 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -228,7 +228,9 @@ import "fmt" // Disable exception handling for directors %feature("except", "0") SwigDirector_Callback; +%feature("except", "0") SwigDirector_Callback*; %feature("except", "0") SwigDirector_SignatureHandler; +%feature("except", "0") SwigDirector_SignatureHandler*; // Handle exceptions by triggering recoverable panic containing the exception message %exception { @@ -247,33 +249,43 @@ import "fmt" return $cgocall %} +%typemap(gotype, out) SwigDirector_Callback "$gotype" +%typemap(cgoout, out) SwigDirector_Callback %{ + return $cgocall +%} + +%typemap(gotype, out) SwigDirector_Callback* "$gotype" +%typemap(cgoout, out) SwigDirector_Callback* %{ + return $cgocall +%} + %typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" %typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Separation* %{ +%typemap(gotype, out) SwigDirector_SignatureHandler "$gotype" +%typemap(cgoout, out) SwigDirector_SignatureHandler %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Rect* %{ +%typemap(gotype, out) SwigDirector_SignatureHandler* "$gotype" +%typemap(cgoout, out) SwigDirector_SignatureHandler* %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Date* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Date* %{ +%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Separation* %{ return $cgocall %} -%typemap(gotype, out) SwigDirector_Callback* "$gotype" -%typemap(cgoout, out) SwigDirector_Callback* %{ +%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Rect* %{ return $cgocall %} -%typemap(gotype, out) SwigDirector_SignatureHandler* "$gotype" -%typemap(cgoout, out) SwigDirector_SignatureHandler* %{ +%typemap(gotype, out) pdftron::PDF::Date* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Date* %{ return $cgocall %} From 647c20b42b8ed24de368466982ed9dd1c7f6a51a Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 15:52:08 -0700 Subject: [PATCH 11/27] Fixed syntax errors after being led astray by ChatGPT --- PDFTronGo/pdftron.i | 2 -- 1 file changed, 2 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index eb9c3edf..ecc411a7 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -228,9 +228,7 @@ import "fmt" // Disable exception handling for directors %feature("except", "0") SwigDirector_Callback; -%feature("except", "0") SwigDirector_Callback*; %feature("except", "0") SwigDirector_SignatureHandler; -%feature("except", "0") SwigDirector_SignatureHandler*; // Handle exceptions by triggering recoverable panic containing the exception message %exception { From 59d82ec7d888017d67f02b6885a7d8b3632ab0d0 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 16:00:26 -0700 Subject: [PATCH 12/27] Tried excluding SwigDirector_ classes in a different way --- PDFTronGo/pdftron.i | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index ecc411a7..08fd0b14 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -226,7 +226,7 @@ import "errors" import "fmt" %} -// Disable exception handling for directors +// Disable exception handling for SwigDirector_ classes %feature("except", "0") SwigDirector_Callback; %feature("except", "0") SwigDirector_SignatureHandler; @@ -247,31 +247,11 @@ import "fmt" return $cgocall %} -%typemap(gotype, out) SwigDirector_Callback "$gotype" -%typemap(cgoout, out) SwigDirector_Callback %{ - return $cgocall -%} - -%typemap(gotype, out) SwigDirector_Callback* "$gotype" -%typemap(cgoout, out) SwigDirector_Callback* %{ - return $cgocall -%} - %typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" %typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ return $cgocall %} -%typemap(gotype, out) SwigDirector_SignatureHandler "$gotype" -%typemap(cgoout, out) SwigDirector_SignatureHandler %{ - return $cgocall -%} - -%typemap(gotype, out) SwigDirector_SignatureHandler* "$gotype" -%typemap(cgoout, out) SwigDirector_SignatureHandler* %{ - return $cgocall -%} - %typemap(gotype, out) pdftron::PDF::Separation* "$gotype" %typemap(cgoout, out) pdftron::PDF::Separation* %{ return $cgocall @@ -323,6 +303,12 @@ ERROR_HANDLING_TYPEMAPS(int) ERROR_HANDLING_TYPEMAPS(ptrdiff_t) ERROR_HANDLING_TYPEMAPS(size_t) +// Reset SwigDirector_ classes back to default typemaps +%typemap(gotype, out) SwigDirector_Callback ""; +%typemap(cgoout, out) SwigDirector_Callback ""; +%typemap(gotype, out) SwigDirector_SignatureHandler ""; +%typemap(cgoout, out) SwigDirector_SignatureHandler ""; + // Generate gotype and cgoout typemaps for void separately %typemap(gotype, out) void "error" %typemap(cgoout, out) void %{ From dd0f26dc1d297c4a9e8c59ae683dd11afd8a777f Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Mon, 15 Sep 2025 16:34:15 -0700 Subject: [PATCH 13/27] Idk at this point whats even happening --- PDFTronGo/pdftron.i | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 08fd0b14..4a5b64d0 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -226,10 +226,6 @@ import "errors" import "fmt" %} -// Disable exception handling for SwigDirector_ classes -%feature("except", "0") SwigDirector_Callback; -%feature("except", "0") SwigDirector_SignatureHandler; - // Handle exceptions by triggering recoverable panic containing the exception message %exception { try { @@ -267,6 +263,11 @@ import "fmt" return $cgocall %} +%typemap(gotype, out, match="funcname", pattern="*NewDirectorCallback*") SWIGTYPE * "$gotype" +%typemap(cgoout, out, match="funcname", pattern="*NewDirectorCallback*") SWIGTYPE * "$cgocall" +%typemap(gotype, out, match="funcname", pattern="*NewDirectorSignatureHandler*") SWIGTYPE * "$gotype" +%typemap(cgoout, out, match="funcname", pattern="*NewDirectorSignatureHandler*") SWIGTYPE * "$cgocall" + // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps %define ERROR_HANDLING_TYPEMAPS(TYPE) %typemap(gotype, out) TYPE "$gotype, error" @@ -303,12 +304,6 @@ ERROR_HANDLING_TYPEMAPS(int) ERROR_HANDLING_TYPEMAPS(ptrdiff_t) ERROR_HANDLING_TYPEMAPS(size_t) -// Reset SwigDirector_ classes back to default typemaps -%typemap(gotype, out) SwigDirector_Callback ""; -%typemap(cgoout, out) SwigDirector_Callback ""; -%typemap(gotype, out) SwigDirector_SignatureHandler ""; -%typemap(cgoout, out) SwigDirector_SignatureHandler ""; - // Generate gotype and cgoout typemaps for void separately %typemap(gotype, out) void "error" %typemap(cgoout, out) void %{ From 1f162486120e72cb96c511288be47c8ce7441d67 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 09:55:07 -0700 Subject: [PATCH 14/27] Removed all director exclusions, SWIGTYPE typemaps, and void typemap for testing --- PDFTronGo/pdftron.i | 57 ++++++++------------------------------------- 1 file changed, 10 insertions(+), 47 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 4a5b64d0..ff415dc5 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -237,39 +237,8 @@ import "fmt" } } -// Exclude director classes from error handling typemaps -%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Callback* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" -%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Separation* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Rect* %{ - return $cgocall -%} - -%typemap(gotype, out) pdftron::PDF::Date* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Date* %{ - return $cgocall -%} - -%typemap(gotype, out, match="funcname", pattern="*NewDirectorCallback*") SWIGTYPE * "$gotype" -%typemap(cgoout, out, match="funcname", pattern="*NewDirectorCallback*") SWIGTYPE * "$cgocall" -%typemap(gotype, out, match="funcname", pattern="*NewDirectorSignatureHandler*") SWIGTYPE * "$gotype" -%typemap(cgoout, out, match="funcname", pattern="*NewDirectorSignatureHandler*") SWIGTYPE * "$cgocall" - // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAPS(TYPE) +%define ERROR_HANDLING_TYPEMAP(TYPE) %typemap(gotype, out) TYPE "$gotype, error" %typemap(cgoout, out) TYPE %{ var swig_r $gotypes @@ -290,21 +259,15 @@ import "fmt" // Apply gotype and cgoout typemaps to functions that return: -// Value types -ERROR_HANDLING_TYPEMAPS(SWIGTYPE) -// Pointers -ERROR_HANDLING_TYPEMAPS(SWIGTYPE *) -// References -ERROR_HANDLING_TYPEMAPS(SWIGTYPE &) // Primitives -ERROR_HANDLING_TYPEMAPS(bool) -ERROR_HANDLING_TYPEMAPS(char) -ERROR_HANDLING_TYPEMAPS(double) -ERROR_HANDLING_TYPEMAPS(int) -ERROR_HANDLING_TYPEMAPS(ptrdiff_t) -ERROR_HANDLING_TYPEMAPS(size_t) - -// Generate gotype and cgoout typemaps for void separately +ERROR_HANDLING_TYPEMAP(bool) +ERROR_HANDLING_TYPEMAP(char) +ERROR_HANDLING_TYPEMAP(double) +ERROR_HANDLING_TYPEMAP(int) +ERROR_HANDLING_TYPEMAP(ptrdiff_t) +ERROR_HANDLING_TYPEMAP(size_t) + +/*// Generate gotype and cgoout typemaps for void separately %typemap(gotype, out) void "error" %typemap(cgoout, out) void %{ var swig_err error @@ -319,7 +282,7 @@ ERROR_HANDLING_TYPEMAPS(size_t) }() return swig_err -%} +%}*/ // Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj From db9e880c398837f25050e66c8ab435623a653778 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 10:11:36 -0700 Subject: [PATCH 15/27] Comment out primitive typemaps for testing --- PDFTronGo/pdftron.i | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index ff415dc5..60dc3e6d 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -259,13 +259,13 @@ import "fmt" // Apply gotype and cgoout typemaps to functions that return: -// Primitives +/*// Primitives ERROR_HANDLING_TYPEMAP(bool) ERROR_HANDLING_TYPEMAP(char) ERROR_HANDLING_TYPEMAP(double) ERROR_HANDLING_TYPEMAP(int) ERROR_HANDLING_TYPEMAP(ptrdiff_t) -ERROR_HANDLING_TYPEMAP(size_t) +ERROR_HANDLING_TYPEMAP(size_t)*/ /*// Generate gotype and cgoout typemaps for void separately %typemap(gotype, out) void "error" From d1387482c97c1f53a9822c35f7232d44dcab8a77 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 10:24:45 -0700 Subject: [PATCH 16/27] Reset to before error handling changes for testing --- PDFTronGo/pdftron.i | 77 ++++----------------------------------------- 1 file changed, 6 insertions(+), 71 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 60dc3e6d..4a4da2f4 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -214,91 +214,26 @@ #undef SetPort %} -// ============== -// ERROR HANDLING -// ============== -// Converts C++ exceptions to Go errors using panic/recovery mechanism. -// All functions now return an error in addition to their return type instead of panicking on exceptions. - -// Ensure necessary imports for error handling code -%insert(go_imports) %{ -import "errors" -import "fmt" -%} - -// Handle exceptions by triggering recoverable panic containing the exception message +%include "exception.i" %exception { try { - $action - } catch (const std::exception &e) { + $action; + } catch (std::exception &e) { _swig_gopanic(e.what()); - } catch (...) { - _swig_gopanic("unknown exception occurred"); } } -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAP(TYPE) -%typemap(gotype, out) TYPE "$gotype, error" -%typemap(cgoout, out) TYPE %{ - var swig_r $gotypes - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - swig_r = $cgocall - }() - - return swig_r, swig_err -%} -%enddef - -// Apply gotype and cgoout typemaps to functions that return: - -/*// Primitives -ERROR_HANDLING_TYPEMAP(bool) -ERROR_HANDLING_TYPEMAP(char) -ERROR_HANDLING_TYPEMAP(double) -ERROR_HANDLING_TYPEMAP(int) -ERROR_HANDLING_TYPEMAP(ptrdiff_t) -ERROR_HANDLING_TYPEMAP(size_t)*/ - -/*// Generate gotype and cgoout typemaps for void separately -%typemap(gotype, out) void "error" -%typemap(cgoout, out) void %{ - var swig_err error - - func() { - defer func() { - if r := recover(); r != nil { - swig_err = errors.New(fmt.Sprintf("%v", r)) - } - }() - $cgocall - }() - - return swig_err -%}*/ - -// Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug if ($1).GetMp_obj().Swigcptr() != 0 { - return $1, swig_err + $result = $1 + return $result } - return nil, swig_err + $result = nil %} -// ================== -// END ERROR HANDLING -// ================== - /** * Provides mapping for C++ vectors. * For example, vector will be called as VectorDouble in GoLang. From 11f3d20b7036b9fdcc73d66ecb570b9bed9a0f42 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 10:43:12 -0700 Subject: [PATCH 17/27] Added error handling imports --- PDFTronGo/pdftron.i | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 4a4da2f4..f452ff44 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -214,6 +214,18 @@ #undef SetPort %} +// ============== +// ERROR HANDLING +// ============== +// Converts C++ exceptions to Go errors using panic/recovery mechanism. +// All functions now return an error in addition to their return type instead of panicking on exceptions. + +// Ensure necessary imports for error handling code +%insert(go_imports) %{ +import "errors" +import "fmt" +%} + %include "exception.i" %exception { try { @@ -234,6 +246,10 @@ $result = nil %} +// ================== +// END ERROR HANDLING +// ================== + /** * Provides mapping for C++ vectors. * For example, vector will be called as VectorDouble in GoLang. From a4b3478fe447c95f4c9ab2b169395e6fe8b9a4e5 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 10:45:59 -0700 Subject: [PATCH 18/27] Added macro for generating gotype/cgoout typemaps for exception handling with panic recovery --- PDFTronGo/pdftron.i | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index f452ff44..8c0f261d 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -235,6 +235,26 @@ import "fmt" } } +// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +%define ERROR_HANDLING_TYPEMAPS(TYPE) +%typemap(gotype) TYPE "$gotype, error" +%typemap(cgoout) TYPE %{ + var swig_r $gotypes + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + swig_r = $cgocall + }() + + return swig_r, swig_err +%} +%enddef + %typemap(goout) pdftron::SDF::Obj %{ // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug From 70aa1d85fd8020044b06caf750de777187d77267 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 11:12:08 -0700 Subject: [PATCH 19/27] Applied macro to all functions in our API, and excluded director classes --- PDFTronGo/pdftron.i | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 8c0f261d..4ea23385 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -235,8 +235,34 @@ import "fmt" } } +// Exclude director classes from exception handling typemaps +%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Callback* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" +%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Separation* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Rect* %{ + return $cgocall +%} + +%typemap(gotype, out) pdftron::PDF::Date* "$gotype" +%typemap(cgoout, out) pdftron::PDF::Date* %{ + return $cgocall +%} + // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps -%define ERROR_HANDLING_TYPEMAPS(TYPE) +%define EXCEPTION_HANDLING_TYPEMAP(TYPE) %typemap(gotype) TYPE "$gotype, error" %typemap(cgoout) TYPE %{ var swig_r $gotypes @@ -255,6 +281,23 @@ import "fmt" %} %enddef +// Apply gotype and cgoout typemaps to functions that return: + +// Value types +EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE) +// Pointers +EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE *) +// References +EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE &) +// Primitives +EXCEPTION_HANDLING_TYPEMAP(bool) +EXCEPTION_HANDLING_TYPEMAP(char) +EXCEPTION_HANDLING_TYPEMAP(double) +EXCEPTION_HANDLING_TYPEMAP(int) +EXCEPTION_HANDLING_TYPEMAP(ptrdiff_t) +EXCEPTION_HANDLING_TYPEMAP(size_t) + +// Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ // Without the brackets, swig attempts to turn $1 into a c++ dereference.. seems like a bug From fb5e5522a389fd89c9d513ebfd176dbd12a5be2b Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 11:16:29 -0700 Subject: [PATCH 20/27] Added void typemap and added out to macro --- PDFTronGo/pdftron.i | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 4ea23385..cc8634e1 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -263,8 +263,8 @@ import "fmt" // Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps %define EXCEPTION_HANDLING_TYPEMAP(TYPE) -%typemap(gotype) TYPE "$gotype, error" -%typemap(cgoout) TYPE %{ +%typemap(gotype, out) TYPE "$gotype, error" +%typemap(cgoout, out) TYPE %{ var swig_r $gotypes var swig_err error @@ -297,6 +297,23 @@ EXCEPTION_HANDLING_TYPEMAP(int) EXCEPTION_HANDLING_TYPEMAP(ptrdiff_t) EXCEPTION_HANDLING_TYPEMAP(size_t) +// Generate gotype and cgoout typemaps for void separately +%typemap(gotype, out) void "error" +%typemap(cgoout, out) void %{ + var swig_err error + + func() { + defer func() { + if r := recover(); r != nil { + swig_err = errors.New(fmt.Sprintf("%v", r)) + } + }() + $cgocall + }() + + return swig_err +%} + // Handle edge case: SDF::Obj returns nil when internal pointer is invalid %typemap(goout) pdftron::SDF::Obj %{ From 8bb0f44f777af6b7d96d470b7de7c04739dc08d2 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 11:43:31 -0700 Subject: [PATCH 21/27] removed newline from import to try and fix bug --- PDFTronGo/pdftron.i | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index cc8634e1..618b28f7 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -221,11 +221,12 @@ // All functions now return an error in addition to their return type instead of panicking on exceptions. // Ensure necessary imports for error handling code -%insert(go_imports) %{ -import "errors" +%insert(go_imports) +%{import "errors" import "fmt" %} +// Handle exceptions by triggering recoverable panic containing the exception message %include "exception.i" %exception { try { From fcffb1451cc7b48d15c92d3070067cd42f2c820f Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 11:48:08 -0700 Subject: [PATCH 22/27] Changed sytnax on go_import to try and remove blank import line --- PDFTronGo/pdftron.i | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 618b28f7..dd25f42c 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -221,10 +221,7 @@ // All functions now return an error in addition to their return type instead of panicking on exceptions. // Ensure necessary imports for error handling code -%insert(go_imports) -%{import "errors" -import "fmt" -%} +%go_import("error", "fmt") // Handle exceptions by triggering recoverable panic containing the exception message %include "exception.i" From 3eec07d9b15c1a9de484deef805152a0d12c8014 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 11:59:02 -0700 Subject: [PATCH 23/27] Fix typo in go_imports --- PDFTronGo/pdftron.i | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index dd25f42c..0016b093 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -221,7 +221,7 @@ // All functions now return an error in addition to their return type instead of panicking on exceptions. // Ensure necessary imports for error handling code -%go_import("error", "fmt") +%go_import("errors", "fmt") // Handle exceptions by triggering recoverable panic containing the exception message %include "exception.i" From 32aa20c02db9d1f299f116a1360a5a4e96dc9d01 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 15:11:44 -0700 Subject: [PATCH 24/27] Change cgoout typemaps to goout, as cgoout does not exist --- PDFTronGo/pdftron.i | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 0016b093..4a92d948 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -234,35 +234,35 @@ } // Exclude director classes from exception handling typemaps -%typemap(gotype, out) pdftron::PDF::Callback* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Callback* %{ +%typemap(gotype) pdftron::PDF::Callback* "$gotype" +%typemap(goout) pdftron::PDF::Callback* %{ return $cgocall %} -%typemap(gotype, out) pdftron::SDF::SignatureHandler* "$gotype" -%typemap(cgoout, out) pdftron::SDF::SignatureHandler* %{ +%typemap(gotype) pdftron::SDF::SignatureHandler* "$gotype" +%typemap(goout) pdftron::SDF::SignatureHandler* %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Separation* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Separation* %{ +%typemap(gotype) pdftron::PDF::Separation* "$gotype" +%typemap(goout) pdftron::PDF::Separation* %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Rect* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Rect* %{ +%typemap(gotype) pdftron::PDF::Rect* "$gotype" +%typemap(goout) pdftron::PDF::Rect* %{ return $cgocall %} -%typemap(gotype, out) pdftron::PDF::Date* "$gotype" -%typemap(cgoout, out) pdftron::PDF::Date* %{ +%typemap(gotype) pdftron::PDF::Date* "$gotype" +%typemap(goout) pdftron::PDF::Date* %{ return $cgocall %} -// Macro for generating gotype (adding error to return) and cgoout (adding panic recovery to return errors) typemaps +// Macro for generating gotype (adding error to return) and goout (adding panic recovery to return errors) typemaps %define EXCEPTION_HANDLING_TYPEMAP(TYPE) -%typemap(gotype, out) TYPE "$gotype, error" -%typemap(cgoout, out) TYPE %{ +%typemap(gotype) TYPE "$gotype, error" +%typemap(goout) TYPE %{ var swig_r $gotypes var swig_err error @@ -279,7 +279,7 @@ %} %enddef -// Apply gotype and cgoout typemaps to functions that return: +// Apply gotype and goout typemaps to functions that return: // Value types EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE) @@ -295,9 +295,9 @@ EXCEPTION_HANDLING_TYPEMAP(int) EXCEPTION_HANDLING_TYPEMAP(ptrdiff_t) EXCEPTION_HANDLING_TYPEMAP(size_t) -// Generate gotype and cgoout typemaps for void separately -%typemap(gotype, out) void "error" -%typemap(cgoout, out) void %{ +// Generate gotype and goout typemaps for void separately +%typemap(gotype) void "error" +%typemap(goout) void %{ var swig_err error func() { From 5e4acb639fded61d917e5ec2fe8101a6a5268084 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 15:59:44 -0700 Subject: [PATCH 25/27] Removed pointers and references from error handling typemap macros to see if that fixes the pointer conversion error --- PDFTronGo/pdftron.i | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index 4a92d948..c2ba75c8 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -281,12 +281,8 @@ // Apply gotype and goout typemaps to functions that return: -// Value types +// Non-primitives EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE) -// Pointers -EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE *) -// References -EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE &) // Primitives EXCEPTION_HANDLING_TYPEMAP(bool) EXCEPTION_HANDLING_TYPEMAP(char) From f7e4a0846dd04af5f222f52abdfc09c06835c647 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Tue, 16 Sep 2025 16:03:05 -0700 Subject: [PATCH 26/27] Removed non-primitives from error handling typemap macros to see if that fixes the pointer conversion error --- PDFTronGo/pdftron.i | 2 -- 1 file changed, 2 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index c2ba75c8..c835c363 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -281,8 +281,6 @@ // Apply gotype and goout typemaps to functions that return: -// Non-primitives -EXCEPTION_HANDLING_TYPEMAP(SWIGTYPE) // Primitives EXCEPTION_HANDLING_TYPEMAP(bool) EXCEPTION_HANDLING_TYPEMAP(char) From 82f3bc5a77d1de4b5ab277fe696398da4b2e29d2 Mon Sep 17 00:00:00 2001 From: andrewrubenmoreno Date: Wed, 17 Sep 2025 09:52:08 -0700 Subject: [PATCH 27/27] Removed director typemap exclusions as they aren't needed anymore --- PDFTronGo/pdftron.i | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/PDFTronGo/pdftron.i b/PDFTronGo/pdftron.i index c835c363..6a82e74a 100644 --- a/PDFTronGo/pdftron.i +++ b/PDFTronGo/pdftron.i @@ -233,32 +233,6 @@ } } -// Exclude director classes from exception handling typemaps -%typemap(gotype) pdftron::PDF::Callback* "$gotype" -%typemap(goout) pdftron::PDF::Callback* %{ - return $cgocall -%} - -%typemap(gotype) pdftron::SDF::SignatureHandler* "$gotype" -%typemap(goout) pdftron::SDF::SignatureHandler* %{ - return $cgocall -%} - -%typemap(gotype) pdftron::PDF::Separation* "$gotype" -%typemap(goout) pdftron::PDF::Separation* %{ - return $cgocall -%} - -%typemap(gotype) pdftron::PDF::Rect* "$gotype" -%typemap(goout) pdftron::PDF::Rect* %{ - return $cgocall -%} - -%typemap(gotype) pdftron::PDF::Date* "$gotype" -%typemap(goout) pdftron::PDF::Date* %{ - return $cgocall -%} - // Macro for generating gotype (adding error to return) and goout (adding panic recovery to return errors) typemaps %define EXCEPTION_HANDLING_TYPEMAP(TYPE) %typemap(gotype) TYPE "$gotype, error"