From 83222078cd1f2f9d2c12279653c48dfd70390f1c Mon Sep 17 00:00:00 2001 From: Context Git Mirror Bot Date: Mon, 12 May 2014 08:14:15 +0200 Subject: 2014-05-08 11:19:00 --- tex/context/base/mlib-lua.lua | 77 +++++++++++++++++++++++++++++-------------- 1 file changed, 52 insertions(+), 25 deletions(-) (limited to 'tex/context/base/mlib-lua.lua') diff --git a/tex/context/base/mlib-lua.lua b/tex/context/base/mlib-lua.lua index 9c7a2e43a..e3a3ba5d8 100644 --- a/tex/context/base/mlib-lua.lua +++ b/tex/context/base/mlib-lua.lua @@ -8,6 +8,8 @@ if not modules then modules = { } end modules ['mlib-pdf'] = { -- This is very preliminary code! +-- maybe we need mplib.model, but how with instances + local type, tostring, select, loadstring = type, tostring, select, loadstring local formatters = string.formatters local find, gsub = string.find, string.gsub @@ -51,14 +53,25 @@ function mp._f_() end end -local f_pair = formatters["(%s,%s)"] -local f_triplet = formatters["(%s,%s,%s)"] -local f_quadruple = formatters["(%s,%s,%s,%s)"] +local f_numeric = formatters["%.16f"] +local f_pair = formatters["(%.16f,%.16f)"] +local f_triplet = formatters["(%.16f,%.16f,%.16f)"] +local f_quadruple = formatters["(%.16f,%.16f,%.16f,%.16f)"] function mp.print(...) for i=1,select("#",...) do - n = n + 1 - buffer[n] = tostring((select(i,...))) + local value = select(i,...) + if value then + n = n + 1 + local t = type(value) + if t == "number" then + buffer[n] = f_numeric(value) + elseif t == "string" then + buffer[n] = value + else + buffer[n] = tostring(value) + end + end end end @@ -113,14 +126,23 @@ end local f_code = formatters["%s return mp._f_()"] -function metapost.runscript(code) - local f = loadstring(f_code(code)) - if f then - return tostring(f()) - else - return "" - end -end +-- function metapost.runscript(code) +-- local f = loadstring(f_code(code)) +-- if f then +-- local result = f() +-- if result then +-- local t = type(result) +-- if t == "number" then +-- return f_numeric(result) +-- elseif t == "string" then +-- return result +-- else +-- return tostring(result) +-- end +-- end +-- end +-- return "" +-- end local cache, n = { }, 0 -- todo: when > n then reset cache or make weak @@ -128,29 +150,34 @@ function metapost.runscript(code) if trace_enabled and trace_luarun then report_luarun("code: %s",code) end + local f if n > 100 then cache = nil -- forget about caching - local f = loadstring(f_code(code)) - if f then - return tostring(f()) - else - return "" - end + f = loadstring(f_code(code)) else - local f = cache[code] - if f then - return tostring(f()) - else + f = cache[code] + if not f then f = loadstring(f_code(code)) if f then n = n + 1 cache[code] = f - return tostring(f()) + end + end + end + if f then + local result = f() + if result then + local t = type(result) + if t == "number" then + return f_numeric(result) + elseif t == "string" then + return result else - return "" + return tostring(result) end end end + return "" end -- function metapost.initializescriptrunner(mpx) -- cgit v1.2.3