From 5d7d9dc5af2cc4cf00d286aac25af6a02beda5c0 Mon Sep 17 00:00:00 2001 From: doronz88 Date: Wed, 6 Mar 2024 14:14:08 +0200 Subject: [PATCH] hilda_client: add `%objc` magic function --- README.md | 3 +++ hilda/hilda_client.py | 26 ++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d4476e1..71d18ad 100644 --- a/README.md +++ b/README.md @@ -505,6 +505,9 @@ d = NSDictionary.new() # call its `new` selector # which is equivalent to: NSDictionary = p.objc_get_class('NSDictionary') d = NSDictionary.new() + +# Or you can use the IPython magic function +%objc NSDictionary ``` This is possible only since `NSDictionary` is exported. In case it is not, you must call `objc_get_class()` explicitly. diff --git a/hilda/hilda_client.py b/hilda/hilda_client.py index cfa27b0..589da66 100644 --- a/hilda/hilda_client.py +++ b/hilda/hilda_client.py @@ -22,6 +22,7 @@ import lldb from humanfriendly import prompts from humanfriendly.terminal.html import html_to_ansi +from IPython.core.magic import register_line_magic # noqa: F401 from pygments import highlight from pygments.formatters import TerminalTrueColorFormatter from pygments.lexers import XmlLexer @@ -59,6 +60,26 @@ Have a nice flight ✈️! Starting an IPython shell... """ +MAGIC_FUNCTIONS = """ +from IPython.core.magic import register_line_magic, needs_local_scope + +@register_line_magic +@needs_local_scope +def objc(line, local_ns=None): + p = local_ns['p'] + className = line.strip() + if not className: + print("Error: className is required.") + return + try: + # Assuming `p.objc_get_class` is a method you have defined or imported + # Replace `p` with the correct reference to where `objc_get_class` is defined + local_ns[className] = p.objc_get_class(className) + p.log_info(f"{className} class loaded successfully.") + except Exception: + p.log_error(f"Error loading class {className}") +""" + SerializableSymbol = namedtuple('SerializableSymbol', 'address type_ filename') @@ -997,8 +1018,9 @@ def interact(self, additional_namespace: typing.Mapping = None) -> None: config = Config() config.IPCompleter.use_jedi = True config.InteractiveShellApp.exec_lines = [ - '''disable_logs()''', - '''IPython.get_ipython().events.register('pre_run_cell', self._ipython_run_cell_hook)''' + """disable_logs()""", + """IPython.get_ipython().events.register('pre_run_cell', self._ipython_run_cell_hook)""", + MAGIC_FUNCTIONS, ] namespace = globals() namespace.update(locals())