This repository has been archived by the owner on Jun 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 100
/
repl.coffee
367 lines (314 loc) · 11.3 KB
/
repl.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Get this loader script_element element.
script_element = document.getElementById 'jsrepl-script'
if script_element?
# The path that would prefix all files to load.
BASE_PATH = script_element.src.split('/')[...-1].join '/'
SANDBOX_SRC = "#{BASE_PATH}/sandbox.html"
else
throw new Error 'JSREPL script element cannot be found. Make sure you have the ID "jsrepl-script" on it.'
class Loader
constructor: ->
# A function to get the body and head tag.
# Won't be available if this was called before window load.
loadfn = =>
@head = document.getElementsByTagName('head')[0]
@body = document.getElementsByTagName('body')[0]
# If this file was loaded in an iframe its probably after window load so
# lets try and get the head & body tags anyways.
loadfn()
# If we are still before window load then we should create a function queue
# to delay till after window load.
@loadfns = [loadfn]
# When the window actually loads, execute all queued functions.
window.onload = =>
fn() for fn in @loadfns
@iframe = null
# Appends an element to the head or body tag, or queues call till window load
# if the element in question is not available.
_appendChild: (tag, elem) ->
fn = => @[tag].appendChild elem
if @[tag]?
# The tag actually exists just append the elem.
fn()
else
# We are before window load; delay the append till window load.
@loadfns.push fn
# Creates a new sandbox (iframe window), and injects self into it. Calls
# callback with the new sandbox.
createSandbox: (callback) ->
if @iframe? then @body.removeChild @iframe
@iframe = document.createElement 'iframe'
@iframe.src = SANDBOX_SRC
@iframe.style.display = 'none'
@iframe.onload = =>
callback @iframe.contentWindow
@_appendChild 'body', @iframe
class EventEmitter
constructor: ->
@listeners = {}
makeArray: (obj) ->
if Object::toString.call(obj) != '[object Array]'
obj = [obj]
obj
on: (types, fn) ->
return if typeof fn != 'function'
types = @makeArray types
for type in types
if not @listeners[type]?
@listeners[type] = [fn]
else
@listeners[type].push fn
off: (types, fn) ->
types = @makeArray types
for type in types
listeners = @listeners[type]
continue if not listeners?
if fn?
i = listeners.indexOf fn
if i > -1
listeners.splice(i, 1)
else
@listeners[type] = []
fire: (type, args) ->
args = @makeArray args
listeners = @listeners[type]
return if not listeners?
args.push type
# We clone the array of listeners before we fire.
# Just incase one listener is a "once" listener
# Which will screw up the iteration.
fn.apply @, args for fn in (f for f in listeners)
once: (types, fn) ->
types = @makeArray types
cb = (args...) =>
@off type, cb for type in types
fn args...
@on type, cb for type in types
workerSupported = 'Worker' of window
class Sandbox extends EventEmitter
# baseScripts: The scripts that loads every time a new worker is created.
constructor: (baseScripts, @input_server, listeners = {}) ->
@baseScripts = (BASE_PATH + '/' + path for path in baseScripts)
@loader = new Loader
for type, fn of listeners
listeners[type] = [fn] if typeof fn is 'function'
@listeners = listeners
# onmessage handler for worker.
onmsg: (event) =>
# IE fires extra events that we want to ignore.
try
msg = JSON.parse event.data
# Execute listeners.
@fire msg.type, [msg.data]
catch e
# Ignore.
# Loads a new instance of a worker with the basescripts + the new scripts.
load: (moreScripts, workerFriendly=true) ->
allScripts = @baseScripts.concat moreScripts
base = allScripts.shift()
# Kill existing worker if we have one.
if @worker? then @kill()
# Called after creating the worker.
postCreate = =>
@post type: 'importScripts', data: allScripts
if @input_server?
@post type: 'set_input_server', data: @input_server
# Remove previous onmsg handler on the window if exists.
window.removeEventListener 'message', @onmsg, false
if not workerSupported or not workerFriendly
# Worker not supported; create a new iframe sandbox replacing the old one.
@loader.createSandbox (sandbox) =>
@worker = sandbox
@workerIsIframe = true
window.addEventListener 'message', @onmsg, false
postCreate()
else
# Workers are supported! \o/
@worker = new Worker base
@workerIsIframe = false
@worker.addEventListener 'message', @onmsg, false
postCreate()
post: (msgObj) ->
msgStr = JSON.stringify msgObj
if not @workerIsIframe
@worker.postMessage msgStr
else
# Worker is an iframe; additional origin argument required.
@worker.postMessage msgStr, '*'
# Terminate worker or delete iframe.
kill: ->
@worker.terminate?()
if @loader.body? and @loader.iframe
@loader.body.removeChild @loader.iframe
delete @loader['iframe']
# The Cakefile defines this to be true, to prevent JSREPL from loading files
# that have already been merged in.
# window.__BAKED_JSREPL_BUILD__ = false
# Basic user agent detection.
UA = do ->
UA_REGEXS =
firefox_3: /firefox\/3/i
opera: /opera/i
chrome: /chrome/i
for ua, ua_regex of UA_REGEXS
if ua_regex.test window.navigator.userAgent
return ua
class JSREPL extends EventEmitter
constructor: ({ result, error, input, output, progress, @timeout, input_server } = {}) ->
super()
if window.openDatabase?
db = openDatabase 'replit_input', '1.0', 'Emscripted input', 1024
db.transaction (tx) ->
tx.executeSql 'DROP TABLE IF EXISTS input'
tx.executeSql 'CREATE TABLE input (text)'
input_server ?= {}
input_server.input_id = Math.floor(Math.random() * 9007199254740992) + 1
# The definition of the current language.
@lang = null
# There are two input event types. Abstract that for users.
@on 'input', input
# Create initial worker.
baseScripts = ['sandbox.js']
if not window.__BAKED_JSREPL_BUILD__
baseScripts = baseScripts.concat ['util/polyfills.js', 'util/mtwister.js']
@sandbox = new Sandbox baseScripts, input_server,
output: output
input: =>
@fire 'input', (data) =>
@sandbox.post
type: 'input.write'
data: data
error: error
result: result
progress: progress
db_input: =>
@fire 'input', (data) =>
@sandbox.fire 'recieved_input', [data]
db.transaction (tx) ->
tx.executeSql "INSERT INTO input (text) VALUES ('#{data}')", []
server_input: =>
@fire 'input', (data) =>
@sandbox.fire 'recieved_input', [data]
# Note: we increment the input_id after each request to avoid race
# conditions on the server. Keep this code in sync with sandbox.js
url = (input_server.url || '/emscripten/input/') + input_server.input_id++
if input_server.cors
xhr = new XMLHttpRequest()
if 'withCredentials' of xhr
xhr.open 'POST', url, true
else if XDomainRequest?
xhr = new XDomainRequest()
xhr.open 'POST', url
else
throw new Error('CORS not supported on your browser')
else
xhr = new XMLHttpRequest()
xhr.open 'POST', url, true
xhr.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded; charset=UTF-8'
)
xhr.send "input=#{data}"
# Only listen to input events to abstract all input types.
# Proxy other events to the sandbox.
on: (types, fn) =>
types = @makeArray types
for type in types
if type is 'input'
super 'input', fn
else
@sandbox.on type, fn
off: (types, fn) =>
types = @makeArray types
for type in types
if type is 'input'
super 'input', fn
else
@sandbox.off type, fn
# Loads the specified language.
# @arg lang_name: The name of the language to load, a member of
# JSREPL::Languages as defined in languages.coffee.
# @arg callback: The function to call after loading finishes.
# @arg worker_friendly: Whether we should load the language in a worker.
loadLanguage: (lang_name, loadInWorker, callback) =>
if typeof loadInWorker is 'function'
[callback, loadInWorker] = [loadInWorker, undefined]
if not JSREPL::Languages::[lang_name]?
throw new Error "Language #{lang_name} not supported."
@current_lang_name = lang_name
# Switch the language.
@lang = JSREPL::Languages::[lang_name]
# One time ready callback.
if callback?
@sandbox.once 'ready', callback
# Load worker with language specific scripts.
lang_scripts = for script in @lang.scripts
if typeof script == 'object'
script[UA] || script['default']
else
script
@sandbox.load lang_scripts.concat([@lang.engine]), loadInWorker
# Checks whether the REPL should continue to the next line rather than run
# the evaluator. Forces evaluation if the last line is empty. Otherwise
# delegates to the language engine's command completion checker.
# @arg command: A string containing the command entered so far.
checkLineEnd: (command, callback) =>
if /\n\s*$/.test command
callback false
else
@sandbox.once 'indent', callback
@sandbox.post
type: 'getNextLineIndent'
data: command
# Evaluates a command in the current engine.
# @arg command: A string containing the code to execute.
eval: (command, callback) =>
if not @sandbox.workerIsIframe and @timeout? and @timeout.time and @timeout.callback
t = null
cb = =>
@sandbox.fire 'timeout'
a = @timeout.callback()
if not a
t = setTimeout cb, @timeout.time
else
unbind()
t = setTimeout cb, @timeout.time
listener = (args..., type) =>
clearTimeout t
if type is 'input'
@once 'recieved_input', =>
t = setTimeout cb, @timeout.time
bind()
bind = =>
@once ['result', 'error', 'input'], listener
unbind = =>
@off ['result', 'error', 'input'], listener
bind()
if typeof callback is 'function'
@once ['result', 'error'], (args..., type) =>
if type is 'error'
callback args[0], null
else
callback null, args[0]
@sandbox.post
type: 'engine.Eval'
data: command
# Evaluates a command in the current engine and return a raw result.
# @arg command: A string containing the code to execute.
rawEval: (command) =>
@sandbox.post
type: 'engine.RawEval'
data: command
# Gets the language config object.
# @arg lang_name: The language name to return the config for
getLangConfig: (lang_name) =>
JSREPL::Languages::[lang_name or @current_lang_name] or null
# The languages and engines modules.
class JSREPL::Languages
# Export for testing.
class JSREPL::__test__
JSREPL::__test__::Loader = Loader
JSREPL::__test__::EventEmitter = EventEmitter
JSREPL::__test__::Sandbox = Sandbox
# Export JSREPL to the world.
@JSREPL = JSREPL