Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Orbiter/examples/trylua.lua
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
136 lines (120 sloc)
4.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- trylua.lua | |
| -- This is an off-line local server version of James Turner's http://trylua.org | |
| local orbiter = require 'orbiter' | |
| local html = require 'orbiter.html' | |
| require 'orbiter.libs.jquery' | |
| local lua = orbiter.new(html) | |
| local load = load | |
| local banner = 'Lua 5.2.1 Copyright (C) 1994-2012 Lua.org, PUC-Rio' | |
| if _VERSION:match '5%.1$' then -- Lua 5.1 compatibility | |
| function load(str,name,mode,env) | |
| local chunk,err = loadstring(str,name) | |
| if chunk then setfenv(chunk,env) end | |
| return chunk,err | |
| end | |
| banner = 'Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio' | |
| end | |
| local style = [[ | |
| body { font-family: 'Lucida Grande'; font-size: 13px; color: #444; width: 960px; } | |
| a { color: #333; } | |
| #console { font-family: 'Lucida Grande'; font-size: 13px; background: #ddd; width: 960px; height: 400px; padding: 5px 5px 15px; } | |
| .jquery-console-welcome { font-style: italic; color: #444; padding-bottom: 2px; } | |
| .jquery-console-inner { height: 100%; background: #efefef; padding: 5px; overflow: auto; } | |
| .jquery-console-prompt-box { color: #444; font-family: monospace; } | |
| .jquery-console-focus .jquery-console-cursor { background: #333; color: #eee; font-weight: bold; } | |
| .jquery-console-message-error { color: red; font-family: sans-serif; font-weight: bold; padding: 2px; } | |
| .jquery-console-message-success { color: green; font-family: monospace; padding: 2px; } | |
| ]] | |
| local script = [[ | |
| $(document).ready(function () { | |
| var lua = $('#console').console({ | |
| autofocus: true, | |
| promptLabel: '> ', | |
| continuedPromptLabel: '>> ', | |
| commandHandle: function (line) { | |
| if (line == 'clear') { | |
| lua.reset(); | |
| return; | |
| } | |
| var ret = ''; | |
| $.ajax({ | |
| data: { code : line }, url: '/request', | |
| success: function (msg) { | |
| if (msg.charAt(0) == '\\') { // server sent us an error | |
| msg = msg.substring(1) | |
| //ret = {msg:msg,className:'jquery-console-message-error' } | |
| ret = msg | |
| } else { | |
| ret = msg; | |
| } | |
| }, | |
| error: function (msg) { ret = 'Epic fail!'; }, | |
| async: false, | |
| timeout: 10000, | |
| }); | |
| if (ret == 'continued') { | |
| lua.continuedPrompt = true; | |
| } else { | |
| lua.continuedPrompt = false; | |
| return ret; | |
| } | |
| }, | |
| promptHistory: true, | |
| autofocus: true, | |
| welcomeMessage: '%s' | |
| }); | |
| }); | |
| ]] | |
| local print_buff,term_print_installed | |
| function term_print(...) | |
| local args,n = {...},select('#',...) | |
| for i = 1,n do | |
| args[i] = tostring(args[i]) | |
| end | |
| table.insert(print_buff,table.concat(args,' ')) | |
| end | |
| local env = { | |
| print = term_print, | |
| } | |
| setmetatable(env,{ __index = _G, __newindex = _G}) | |
| function eval(code) | |
| local status,val,f,err,rcnt | |
| print_buff = {} | |
| code,rcnt = code:gsub('^%s*=','return ') | |
| f,err = load(code,'TMP','t',env) | |
| if f then | |
| status,val = pcall(f) | |
| if not status then err = val | |
| else | |
| if #print_buff > 0 then val = table.concat(print_buff,'\n') end | |
| return tostring(val) | |
| end | |
| end | |
| if err then | |
| err = tostring(err):gsub('^%[string "TMP"%]:1:','') | |
| return '\\'..err | |
| end | |
| end | |
| local span,div = html.tags 'span,div' | |
| function lua:index(web) | |
| return html { | |
| title = 'Try Lua Offline', | |
| scripts = '/resources/javascript/jquery.console.js', | |
| inline_style = style, | |
| inline_script = script:format(banner), | |
| div{id='console',''}, | |
| 'off-line version of James Turner\'s ', | |
| html.link("http://trylua.org","trylua.org"), | |
| ', powered by ', | |
| html.link("http://github.com/chrisdone/jquery-console","jquery-console") | |
| } | |
| end | |
| function lua:request(web) | |
| local code = web.GET.code | |
| local res = eval(code) | |
| return res,'text/plain' | |
| end | |
| lua:dispatch_get(lua.index,'/','/index') | |
| lua:dispatch_get(lua.request,'/request') | |
| lua:dispatch_static('/resources/javascript.*') | |
| lua:run(...) |