MCPcopy Create free account
hub / github.com/marijnh/Eloquent-JavaScript / setupEnv

Method setupEnv

src/client/sandbox.mjs:164–219  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

162 }
163
164 setupEnv() {
165 let win = this.win
166 win.__sandbox = this
167
168 win.onerror = (e, _file, line) => {
169 if (!this.output) return
170 this.output.out("error", [e + (line != null ? " (line " + line + ")" : "")])
171 return true
172 }
173 win.console = {
174 log: (...args) => this.out("log", args),
175 error: (...args) => this.out("error", args),
176 warn: (...args) => this.out("warn", args),
177 info: (...args) => this.out("log", args)
178 }
179 win.setInterval(() => {
180 this.startedAt = null
181 }, 1000)
182
183 win.__setTimeout = win.setTimeout
184 win.__setInterval = win.setInterval
185 win.setTimeout = (code, time, ...args) => {
186 if (args.length && typeof code != "string") {
187 let f = code
188 code = () => f(...args)
189 }
190 let val = win.__setTimeout(() => this.run(code), time)
191 this.timeouts.push(val)
192 return val
193 }
194 win.setInterval = (code, time, ...args) => {
195 if (args.length && typeof code != "string") {
196 let f = code
197 code = () => f(...args)
198 }
199 let val = win.__setInterval(() => this.run(code), time)
200 this.intervals.push(val)
201 return val
202 }
203 let reqAnimFrame = win.requestAnimationFrame
204 win.requestAnimationFrame = f => {
205 let val = reqAnimFrame.call(win, f)
206 if (this.frames.length > 50)
207 this.frames[this.framePos++ % 50] = val
208 else
209 this.frames.push(val)
210 return val
211 }
212
213 win.addEventListener("unhandledrejection", e => this.error(e.reason))
214
215 win.require = name => this.require(name)
216 win.require.preload = (name, code) => resolved.store(name, {name, code})
217 win.module = {exports: {}}
218 win.exports = win.module.exports
219 }
220
221 resizeFrame() {

Callers 2

constructorMethod · 0.95
setHTMLMethod · 0.95

Calls 6

outMethod · 0.95
runMethod · 0.95
errorMethod · 0.95
requireMethod · 0.95
storeMethod · 0.80
outMethod · 0.45

Tested by

no test coverage detected