summaryrefslogtreecommitdiff
path: root/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
diff options
context:
space:
mode:
authorMed Ismail Bennani <ismail@bennani.ma>2023-07-21 16:43:24 -0700
committerMed Ismail Bennani <ismail@bennani.ma>2023-07-21 18:47:46 -0700
commit57bd882343f8e4cca598b6ad47da93476cffb987 (patch)
treed95844a2c7104133e153a9b8ecb9663aea706e58 /lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
parentaa34b34e93d444efec6cd894e5e55927d0e413e6 (diff)
[lldb] Convert script native types to StructuredData counterpart
This patch adds the ability to pass native types from the script interpreter to methods that use a {SB,}StructuredData argument. To do so, this patch changes the `ScriptedObject` struture that holds the pointer to the script object as well as the originating script interpreter language. It also exposes that to the SB API via a new class called `SBScriptObject`. This structure allows the debugger to parse the script object and convert it to a StructuredData object. If the type is not compatible with the StructuredData types, we will store its pointer in a `StructuredData::Generic` object. This patch also adds some SWIG typemaps that checks the input argument to ensure it's either an SBStructuredData object, in which case it just passes it throught, or a python object that is NOT another SB type, to provide some guardrails for the user. rdar://111467140 Differential Revision: https://reviews.llvm.org/D155161 Signed-off-by: Med Ismail Bennani <ismail@bennani.ma>
Diffstat (limited to 'lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py')
-rw-r--r--lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
index 38951a69be0d..b3db3bc61e4d 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -76,6 +76,40 @@ class TestStructuredDataAPI(TestBase):
# Tests for array data type
self.array_struct_test(dict_struct)
+ s.Clear()
+ self.assertSuccess(example.GetAsJSON(s))
+ py_obj = json.loads(s.GetData())
+ self.assertTrue(py_obj)
+ self.assertIn("key_dict", py_obj)
+
+ py_dict = py_obj["key_dict"]
+ self.assertEqual(py_dict["key_string"], "STRING")
+ self.assertEqual(py_dict["key_uint"], 0xFFFFFFFF00000000)
+ self.assertEqual(py_dict["key_sint"], -42)
+ self.assertEqual(py_dict["key_float"], 2.99)
+ self.assertEqual(py_dict["key_bool"], True)
+ self.assertEqual(py_dict["key_array"], ["23", "arr"])
+
+ class MyRandomClass:
+ payload = "foo"
+
+ py_dict["key_generic"] = MyRandomClass()
+
+ stp = lldb.SBScriptObject(py_dict, lldb.eScriptLanguagePython)
+ self.assertEqual(stp.ptr, py_dict)
+
+ sd = lldb.SBStructuredData(stp, self.dbg)
+ self.assertTrue(sd.IsValid())
+ self.assertEqual(sd.GetSize(), len(py_dict))
+
+ generic_sd = sd.GetValueForKey("key_generic")
+ self.assertTrue(generic_sd.IsValid())
+ self.assertEqual(generic_sd.GetType(), lldb.eStructuredDataTypeGeneric)
+
+ my_random_class = generic_sd.GetGenericValue()
+ self.assertTrue(my_random_class)
+ self.assertEqual(my_random_class.payload, MyRandomClass.payload)
+
def invalid_struct_test(self, example):
invalid_struct = lldb.SBStructuredData()
invalid_struct = example.GetValueForKey("invalid_key")