Closed Bug 737743 Opened 8 years ago Closed 6 months ago

On-the-fly JavaScript editing

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set

Tracking

()

RESOLVED DUPLICATE of bug 771339

People

(Reporter: ide, Assigned: arai)

References

(Blocks 1 open bug)

Details

It is currently not possible to dynamically edit JS code, effectively "hot swapping" it into the runtime. This feature is useful to web developers in the form of JS debugging tools in Firefox, for example.

For reference, WebKit's Web Inspector's behavior works well when debugging. Specifically, if a function is edited, all references to that function are updated and execute the new code when invoked. Web Inspector also restricts editing to when the script is paused at a breakpoint.
Status: UNCONFIRMED → NEW
Ever confirmed: true
(In reply to James Ide from comment #0)
> For reference, WebKit's Web Inspector's behavior works well when debugging.
> Specifically, if a function is edited, all references to that function are
> updated and execute the new code when invoked. Web Inspector also restricts
> editing to when the script is paused at a breakpoint.

In Chrome, at least, you can also edit function bodies while the browser is just waiting for events. For example, you can change the function body of a click handler (but not apparently code that is in an inline <script> tag in the page, and certainly not the closure of the function that was set up when the function was first created).
Assignee: general → nobody
Component: JavaScript Engine → XUL
Assignee: nobody → general
Component: XUL → JavaScript Engine
This kind of thing should be a priority. It's really what people want.

This stuff gives me the willies, though. For example, how does it interact with optimizations that lose information? Suppose we start with:

function scheduleAlert(text1, text2) {
  setTimeout(function () { alert(text1); }, 100);
}

and then call:

scheduleAlert("a", "b");

and then, before that function runs, change the body to:

  setTimeout(function () { alert(text2); }, 100);

Now, the closure that was passed to setTimeout may not have bothered to retain the value of text2 at all; it's gone.

Do we show an error? Warn, patch future revisions, and let the old version of the code run?

This question could be re-phrased in terms of event handlers, or XHR callbacks, or any number of other cases; don't be distracted by setTimeout-specific issues.

If it is as likely as I'd expect that this would have bugs in corner cases, we should not make the ability to replace function bodies available to content; it should be a tools-only API, so that web content can't use it in attacks.
(In reply to Jim Blandy :jimb from comment #2)
> Do we show an error? Warn, patch future revisions, and let the old version
> of the code run?

FWIW, Web Inspector runs the old version of the code and alerts "a" in your example. I think that's reasonable behavior since a different function is created each time scheduleAlert is run. On the other hand this works as expected:

function scheduleAlert(text1, text2) {
  setTimeout(doAlert.bind(null, text1, text2), 5000);
}
function doAlert(text1, text2) {
  alert(text1);  // changing to text2 after scheduling will alert text2
}
> it should be a tools-only API

I believe the tools use caes are the primary ones, yes.
Assignee: general → nobody
Maybe reconsider this thing?

This feature is a popular thing among developers I think. Especially as devs might try to get switch from Chrome/ium to Firefox and really miss that feature. (I've seen many to say Firefox Quantum is great, but the devtools lack behind Chrome)

So I consider this to be a mayor feature, which Chrome has, but Firefox is still missing.
Assignee: nobody → arai.unmht
Status: NEW → ASSIGNED
Apparently, Google Chrome updates the function's reference when the function doesn't capture local variables or arguments.
If there's no reference, or only to global variable, it updates in realtime.
(In reply to Tooru Fujisawa [:arai] from comment #6)
> If there's no reference, or only to global variable, it updates in realtime.

I meant "no capture"
See Also: → 771339, 894997
Type: defect → enhancement

How far does this actually differ from bug 771339? And it also somewhat overlaps with bug 894997. So which one is meant to cover what?

Sebastian

Flags: needinfo?(hkirschner)
Keywords: dev-doc-needed

Makes sense, lets prioritize them together when we come around to JS editing (probably under the new meta anyways)

Status: ASSIGNED → RESOLVED
Closed: 6 months ago
Flags: needinfo?(hkirschner)
Resolution: --- → DUPLICATE
Duplicate of bug: 771339
You need to log in before you can comment on or make changes to this bug.