From d45884bab5a881cb1420625ad27c5fccb5b4f6b1 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Mon, 24 Apr 2023 12:17:19 +1200 Subject: [php] Add throws typemaps for string* + const string* --- CHANGES.current | 3 +++ Examples/test-suite/php/li_std_string_runme.php | 26 +++++++++++++++++++++++++ Lib/php/std_string.i | 5 +++++ 3 files changed, 34 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index 15398ef7c..95a8619a7 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,9 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.2.0 (in progress) =========================== +2023-04-24: olly + [PHP] Add throws typemaps for std:string* and const std::string*. + 2023-04-23: olly [Javascript] #2453 The testsuite and examples now select which Javascript engine to test based on what was detected by configure. diff --git a/Examples/test-suite/php/li_std_string_runme.php b/Examples/test-suite/php/li_std_string_runme.php index 169f947a7..390b7e17b 100644 --- a/Examples/test-suite/php/li_std_string_runme.php +++ b/Examples/test-suite/php/li_std_string_runme.php @@ -67,6 +67,32 @@ $s = "byref"; check::equal(li_std_string::test_reference_php($s), null); check::equal($s, "byref.php"); +// Test throwing strings: +try { + test_throw(); + check::fail("test_throw() didn't throw"); +} catch (Exception $s) { + check::equal($s->getMessage(), "test_throw message"); +} +try { + test_const_reference_throw(); + check::fail("test_const_reference_throw() didn't throw"); +} catch (Exception $s) { + check::equal($s->getMessage(), "test_const_reference_throw message"); +} +try { + test_pointer_throw(); + check::fail("test_pointer_throw() didn't throw"); +} catch (Exception $s) { + check::equal($s->getMessage(), "foo"); +} +try { + test_const_pointer_throw(); + check::fail("test_const_pointer_throw() didn't throw"); +} catch (Exception $s) { + check::equal($s->getMessage(), "foo"); +} + // This used to give "Undefined variable: r" li_std_string::test_const_reference_returning_void("foo"); diff --git a/Lib/php/std_string.i b/Lib/php/std_string.i index 0e2e1faff..5abbbbbef 100644 --- a/Lib/php/std_string.i +++ b/Lib/php/std_string.i @@ -56,6 +56,11 @@ namespace std { goto fail; %} + %typemap(throws) string*, const string* %{ + zend_throw_exception(NULL, $1->c_str(), 0); + goto fail; + %} + %typemap(in, phptype="string") const string& ($*1_ltype temp) %{ convert_to_string(&$input); temp.assign(Z_STRVAL($input), Z_STRLEN($input)); -- cgit v1.2.1