Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit 41f5760

Browse files
committed
document the insulation magic that happens with Object.create, Map, etc.
1 parent d748a51 commit 41f5760

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

shells/chrome/Readme.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,36 @@ To hack on the plugin:
1111

1212
Generally, changes to the UI will auto-propagate if you have `webpack --watch`
1313
on (close devtools and re-open them). If you change the background script or
14-
injector, you'll have to reload the extension from the extensions page.
14+
injector, you might have to reload the extension from the extensions page.
15+
16+
## Insulating the environment
17+
18+
React Devtools has part of the code (the backend + agent) running in the same
19+
javascript context as the inspected page, which makes the code vulnerable to
20+
environmental inconsistencies. For example, the backend uses the es6 `Map`
21+
class and normally expects it to be available in the global scope. If a user
22+
script has overridden this, the backend breaks.
23+
24+
To prevent this, the content script [`src/GlobalHook.js`](src/GlobalHook.js),
25+
which runs before any user js, saves the native values we depend on to the
26+
`__REACT_DEVTOOLS_GLOBAL_HOOK__` global. These are:
27+
28+
- Set
29+
- Map
30+
- WeakMap
31+
- Object.create
32+
33+
Then in `webpack.backend.js`, these saved values are substituted for the
34+
globally referenced name (e.g. `Map` gets replaced with
35+
`window.__REACT_DEVTOOLS_GLOBAL_HOOK__.nativeMap`).
36+
37+
## Fixing document.create
38+
39+
React Native sets `document.createElement` to `null` in order to convince js
40+
libs that they are not running in a browser environment while `debug in
41+
chrome` is enabled.
42+
43+
To deal with this, [`src/inject.js`](src/inject.js) calls
44+
`document.constructor.prototype.createElement` when it needs to create a
45+
`<script>` tag.
1546

test/example/sink.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<body>
88
<script src="build/sink.js"></script>
99
<script>
10+
// Test to make sure the backend doesn't depend on Object.create
11+
// or ES6 globals
1012
Object.create = null;
1113
Map = null;
1214
Set = null;

0 commit comments

Comments
 (0)