-- run.lua test file
local from4d = true
if not inTxt then
  from4d = false
  inTxt = ""
else
	-- local oldprint = print
	function print(str)
		returnTxt = returnTxt .. "\n" .. str
	end
end
returnTxt = "Hello World from run.lua file: / " .. inTxt .. " / " .. _VERSION .. " / " .. jit.version .. "•ƒ"
print(returnTxt)

package.path = "/Users/manage/svnroot/cpp/MA_Lua/lua4d/?.lua;"..package.path
plg = require "plugin_4d"

local ffi = require "ffi"
local C = ffi.C

local recs1 = C.PA_RecordsInSelection(1)
local txt = "C.PA_RecordsInSelection(1): " .. recs1 
print(txt)
--C.PluginMain(-914) -- works - can call plugin methods if you set params?

local path = plg.us8(C.PA_GetStructureFullPath())
print("C.PA_GetStructureFullPath(): '"..path.."'")

local ret = C.luaCallback(plg.cstr("_err CRY"))
print("C.luaCallback: "..ret) -- returns text length

local str1 = plg.uchar("_err CRY")
local methodId = C.PA_GetMethodID(str1)
print("PA_GetMethodID: (_err CRY): "..methodId)
local param1 = ffi.new("PA_Variable")
C.PA_SetStringVariable(param1, plg.ustr("_err CRY called by: PA_ExecuteMethodByID()") )
C.PA_ExecuteMethodByID(methodId, param1, 1)
-- http://sources.4d.com/trac/4d_4dpluginapi/wiki/CMU84729.HTM

local prs = C.PA_CountActiveProcess()
print("PA_CountActiveProcess: "..prs)

prs = C.PA_CountTotalProcess()
print("PA_CountTotalProcess: "..prs)


-- C.PA_QueryDialog(1)
-- C.PA_Alert(plg.uchar("'Hello from PA_Alert(aaa•ƒöäåÖÄÅ).'"), ffi.cast("void *",0))

-- local str1 = plg.ustr('ALERT("Hello from PA_ExecuteMethod().")')  
-- C.PA_ExecuteMethod(str1)

methodId = C.PA_GetMethodID(plg.uchar("_info Mac"))
local ret = C.PA_ExecuteMethodByID(methodId, nil, 0)
--ret = C.PA_GetLongintVariable(ret) -- this dows not work?
ret = plg.getLong(ret)
print("_info Mac = "..ret)


local max = 0
local ret
local cnt = 1*1000*1000 -- 1 million
for i=1,cnt do
	--local prs = C.PA_CountActiveProcess()
	ret = C.PA_ExecuteMethodByID(methodId, nil, 0)
	C.PA_Yield()
	max = max+1
end
--print(max..". PA_CountActiveProcess: "..prs)
print(max..'. PA_ExecuteMethodByID("_info Mac"): '..plg.getLong(ret))
