facebookarchive / stetho

Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.
http://facebook.github.io/stetho/
MIT License
12.66k stars 1.13k forks source link

Added static method for removing objects previously added by .mapObject() #614

Closed AlexTrotsenko closed 5 years ago

AlexTrotsenko commented 5 years ago

I am developing debugger for J2V8, which uses Stetho for communication over Chrome DevTools protocol. Besides implementing Debugger domain I need to add local v8 variables to Runtime by static .mapObject() in order them to be visible in the Chrome Debugger UI.

The problem, that @ChromeDevtoolsMethod .releaseObject() method is not called by Chrome DevTools and I want to remove objects stored in the session manually in order to avoid memory leaks and possible OOM errors. That's why I would like to have static method to remove them from session. Some work-around are possible already now, but it makes sense for me to have it because static counter-part for adding object is exists - .mapObject() .

jasta commented 5 years ago

I'm fine to merge this and I think I agree with your motivation, but one thing to call out: this will be fragile and fairly hard to maintain as a real API into the future. I'm sympathetic to your use case and want to support you, but I'm not necessarily confident that directly reaching into the Runtime module is the best contract for us to maintain. Is your integration open sourced somewhere I can see so at least Stetho can track this as a major customer to support and make sure we don't accidentally break?

AlexTrotsenko commented 5 years ago

@jasta sorry for delay with the answer - I have missed your reply somehow. The project is available on github: https://github.com/AlexTrotsenko/j2v8-debugger

The exact place where V8 variable is added to Runtime is following (in Kotlin).

Concerning you notes about regarding fragility of such API:

Then my client code might looks like following:

private class V8ToChromeDevToolsBreakHandler(private val currentPeerProvider: () -> JsonRpcPeer?) : BreakHandler {
   val subSession = SubSession()
   ...
    override fun onBreak(event: DebugHandler.DebugEvent?, state: ExecutionState?, eventData: EventData?, data: V8Object?) {
                        ... 
                       val storedVariablesId = Runtime.mapObject(currentPeerProvider(), knowVariables, subSession)
                        .... 
    }
    ....
    /**
     * Resumes V8 execution. Called from Stetho thread.
     */
    fun resume() {
        subSession.onSubSessionEnded()
        resumeWith(null)
    }
}

P.S. Thanks taking time and merging all my pull requests - I will integrate the latest version of Stetho into j2v8-debugger project.