|
23 | 23 | - toLua, serialize |
24 | 24 | Only during processing: |
25 | 25 | - getCurrentPathIn, getCurrentPathOut |
26 | | - - outputValue, outputLua |
| 26 | + - outputValue, outputLua, outputLuaTemplate |
27 | 27 | Search this file for 'EnvironmentTable' for more info. |
28 | 28 |
|
29 | 29 | Exported stuff from the library: |
@@ -270,6 +270,7 @@ function printTraceback(message, level) |
270 | 270 | end |
271 | 271 |
|
272 | 272 | function error(err, level) |
| 273 | + -- @Check: Should we prepend the path? And, in all or just some cases? |
273 | 274 | level = 1+(level or 1) |
274 | 275 | printTraceback(tryToFormatError(err), level) |
275 | 276 | currentErrorHandler(err, level) |
|
281 | 282 | function errorOnLine(path, ln, agent, s, ...) |
282 | 283 | s = s:format(...) |
283 | 284 | if agent then |
284 | | - printf("Error @ %s:%d: [%s] %s", path, ln, agent, s) |
| 285 | + printf("Error @ %s:%d: [%s] %s\n", path, ln, agent, s) |
285 | 286 | else |
286 | | - printf("Error @ %s:%d: %s", path, ln, s) |
| 287 | + printf("Error @ %s:%d: %s\n", path, ln, s) |
287 | 288 | end |
288 | 289 | currentErrorHandler(s, 2) |
289 | 290 | return s |
@@ -1074,6 +1075,35 @@ function metaFuncs.outputLua(...) |
1074 | 1075 | end |
1075 | 1076 | end |
1076 | 1077 |
|
| 1078 | +-- outputLuaTemplate() |
| 1079 | +-- Use a string as a template for outputting Lua code with values. |
| 1080 | +-- Question marks (?) are replaced with the values. |
| 1081 | +-- outputLuaTemplate( luaStringTemplate, value1, ... ) |
| 1082 | +-- Examples: |
| 1083 | +-- outputLuaTemplate("local name, age = ?, ?", "Harry", 48) |
| 1084 | +-- outputLuaTemplate("dogs[?] = ?", "greyhound", {italian=false, count=5}) |
| 1085 | +function metaFuncs.outputLuaTemplate(lua, ...) |
| 1086 | + errorIfNotRunningMeta(2) |
| 1087 | + assertarg(1, lua, "string") |
| 1088 | + |
| 1089 | + local args = {...} |
| 1090 | + local n = 0 |
| 1091 | + local v, err |
| 1092 | + |
| 1093 | + lua = lua:gsub("%?", function() |
| 1094 | + n = n+1 |
| 1095 | + v, err = toLua(args[n]) |
| 1096 | + |
| 1097 | + if not v then |
| 1098 | + error(F("Bad argument %d: %s", 1+n, err), 3) |
| 1099 | + end |
| 1100 | + |
| 1101 | + return assert(v) |
| 1102 | + end) |
| 1103 | + |
| 1104 | + table.insert(outputFromMeta, lua) |
| 1105 | +end |
| 1106 | + |
1077 | 1107 | -- getCurrentPathIn() |
1078 | 1108 | -- Get what file is currently being processed, if any. |
1079 | 1109 | -- path = getCurrentPathIn( ) |
@@ -1841,19 +1871,25 @@ local function _processFileOrString(params, isFile) |
1841 | 1871 | end |
1842 | 1872 | end |
1843 | 1873 |
|
| 1874 | +local ERROR_REDIRECTION = setmetatable({}, {__tostring=function()return"Internal redirection of error."end}) |
| 1875 | + |
1844 | 1876 | local function processFileOrString(params, isFile) |
1845 | 1877 | local returnValues = nil |
1846 | 1878 | local errorToReturn = nil |
1847 | 1879 |
|
1848 | 1880 | isDebug = params.debug |
1849 | 1881 | pushErrorHandler(function(err, levelFromOurError) |
| 1882 | + if err == ERROR_REDIRECTION then return end |
| 1883 | + |
1850 | 1884 | errorToReturn = err |
1851 | 1885 |
|
1852 | 1886 | if not levelFromOurError then |
1853 | 1887 | printTraceback(tryToFormatError(err), 2) |
1854 | 1888 | end |
1855 | 1889 |
|
1856 | 1890 | if params.onError then params.onError(errorToReturn) end |
| 1891 | + |
| 1892 | + if levelFromOurError then _error(ERROR_REDIRECTION) end |
1857 | 1893 | end) |
1858 | 1894 |
|
1859 | 1895 | xpcall( |
|
0 commit comments