MCPcopy Create free account
hub / github.com/Distributive-Network/PythonMonkey / enable

Function enable

python/pythonmonkey/lib/pmdb.py:20–180  ·  view source on GitHub ↗
(debuggerGlobalObject=pm.eval("debuggerGlobal"))

Source from the content-addressed store, hash-verified

18
19
20def enable(debuggerGlobalObject=pm.eval("debuggerGlobal")):
21 if debuggerGlobalObject._pmdbEnabled:
22 return # already enabled, skipping
23
24 debuggerGlobalObject._pmdbEnabled = True
25 pm.eval("debuggerGlobal.eval")("""(mainGlobal, debuggerInput, _pythonPrint, _pythonExit) => {
26 const dbg = new Debugger()
27 const mainDebuggee = dbg.addDebuggee(mainGlobal)
28 dbg.uncaughtExceptionHook = (e) => {
29 _pythonPrint(e)
30 }
31
32 function makeDebuggeeValue (val) {
33 if (val instanceof Debugger.Object) {
34 return dbg.adoptDebuggeeValue(val)
35 } else {
36 // See https://firefox-source-docs.mozilla.org/js/Debugger/Debugger.Object.html#makedebuggeevalue-value
37 return mainDebuggee.makeDebuggeeValue(val)
38 }
39 }
40
41 function print (...args) {
42 const logger = makeDebuggeeValue(mainGlobal.console.log)
43 logger.apply(logger, args.map(makeDebuggeeValue))
44 }
45
46 function printErr (...args) {
47 const logger = makeDebuggeeValue(mainGlobal.console.error)
48 logger.apply(logger, args.map(makeDebuggeeValue))
49 }
50
51 function printSource (frame, location) {
52 const src = frame.script.source.text
53 const line = src.split('\\n').slice(location.lineNumber-1, location.lineNumber).join('\\n')
54 print(line)
55 print(" ".repeat(location.columnNumber) + "^") // indicate column position
56 }
57
58 function getCommandInputs () {
59 const input = debuggerInput("(pmdb) > ") // blocking
60 const [_, command, rest] = input.match(/\\s*(\\w+)?(?:\\s+(.*))?/)
61 return { command, rest }
62 }
63
64 function enterDebuggerLoop (frame, checkIsBreakpoint = false) {
65 const metadata = frame.script.getOffsetMetadata(frame.offset)
66 if (checkIsBreakpoint && !metadata.isBreakpoint) {
67 // This bytecode offset does not qualify as a breakpoint, skipping
68 return
69 }
70
71 blockingLoop: while (true) {
72 const { command, rest } = getCommandInputs() // blocking
73 switch (command) {
74 case "b":
75 case "break": {
76 // Set breakpoint on specific line number
77 const lineNum = Number(rest)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected