Skip to content

Commit

Permalink
Added UTF8 support for Python and Lua.
Browse files Browse the repository at this point in the history
  • Loading branch information
max99x committed Sep 7, 2011
1 parent bc107cb commit 9aeb3d8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
6 changes: 3 additions & 3 deletions langs/lua/jsrepl_lua.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ class self.JSREPLEngine
@error_buffer = []
@Lua = sandbox.Module.Lua
@Lua.initialize(null,
(chr) -> output String.fromCharCode chr
makeUtf8Print output,
(chr) => @error_buffer.push String.fromCharCode chr)
ready()

Eval: (command) ->
@error_buffer = []
try
result = @Lua.eval command
result = @Lua.eval encodeUtf8 command
if @error_buffer.length
@error @error_buffer.join ''
else
Expand All @@ -21,7 +21,7 @@ class self.JSREPLEngine

EvalSync: (command) ->
@error_buffer = []
result = @Lua.eval command
result = @Lua.eval encodeUtf8 command
if @error_buffer.length
throw @error_buffer.join ''
return result
Expand Down
18 changes: 8 additions & 10 deletions langs/python/jsrepl_python.coffee
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
class self.JSREPLEngine
constructor: (unused_input, output, @result, @error, sandbox, ready) ->
constructor: (unused_input, @output, @result, @error, sandbox, ready) ->
@Python = sandbox.Python
sandbox.print = (->)
@error_buffer = []
@Python = sandbox.Python
callback = () =>
@Python.initialize(null,
(chr) -> if chr? then output String.fromCharCode chr
(chr) => @error_buffer.push String.fromCharCode chr)
ready()
setTimeout callback, 100
@Python.initialize(null,
makeUtf8Print output,
(chr) => @error_buffer.push String.fromCharCode chr)
ready()

Eval: (command) ->
@error_buffer = []
try
result = @Python.eval command
result = @Python.eval encodeUtf8 command
if @error_buffer.length
@error @error_buffer.join ''
else
Expand All @@ -23,7 +21,7 @@ class self.JSREPLEngine

EvalSync: (command) ->
@error_buffer = []
result = @Python.eval command
result = @Python.eval encodeUtf8 command
if @error_buffer.length
throw @error_buffer.join ''
return result
Expand Down
4 changes: 3 additions & 1 deletion languages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
['{', '}']
]
scripts: [
'util/utf8.coffee'
'extern/lua/lua.closure.js'
]
engine: 'langs/lua/jsrepl_lua.coffee'
Expand All @@ -341,6 +342,7 @@
['{', '}']
]
scripts: [
'util/utf8.coffee'
'extern/python/python.js'
]
engine: 'langs/python/jsrepl_python.coffee'
Expand All @@ -364,4 +366,4 @@
]
engine: 'langs/smalltalk/jsrepl_smalltalk.coffee'
libs: []
worker_friendly: true
worker_friendly: true
42 changes: 42 additions & 0 deletions util/utf8.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# A simple filter for output that decodes UTF8. Useful for Emscripted code.
@makeUtf8Print = (print) ->
buffer = []
return (chr) ->
if not chr? then return
if chr < 0 then chr += 256
switch buffer.length
when 0
if chr < 128
print String.fromCharCode chr
else
buffer.push chr
when 1
if buffer[0] > 191 and buffer[0] < 224
uni = ((buffer[0] & 31) << 6) | (chr & 63)
print String.fromCharCode uni
buffer = []
else
buffer.push chr
when 2
uni = (((buffer[0] & 15) << 12) |
((buffer[1] & 63) << 6) |
(chr & 63))
print String.fromCharCode uni
buffer = []

@encodeUtf8 = (str) ->
utftext = []

for c in str.split ''
c = c.charCodeAt 0
if c < 128
utftext.push String.fromCharCode c
else if c > 127 and c < 2048
utftext.push String.fromCharCode (c >> 6) | 192
utftext.push String.fromCharCode (c & 63) | 128
else
utftext.push String.fromCharCode (c >> 12) | 224
utftext.push String.fromCharCode ((c >> 6) & 63) | 128
utftext.push String.fromCharCode (c & 63) | 128

return utftext.join ''

0 comments on commit 9aeb3d8

Please sign in to comment.