Skip to content

Commit

Permalink
hilda_client: add %objc magic function
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Mar 6, 2024
1 parent 0555e7c commit 5d7d9dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 24 additions & 2 deletions hilda/hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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')


Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 5d7d9dc

Please sign in to comment.