NPLPackages/main

NPL meta compiler in function

LiXizhi opened this issue · 2 comments

--[[
Title: 
Author(s): LiXizhi
Date: 2017/2/2
Desc: 
use the lib:
------------------------------------------------------------
NPL.load("script/ide/System/Compiler/tests/test_DSL_NPL.npl")
local TestDSLNPL = commonlib.gettable("Tests.TestDSLNPL");
TestDSLNPL.TestRPC()
------------------------------------------------------------
]]

local TestDSLNPL = commonlib.gettable("Tests.TestDSLNPL");

-- defined in global scope so that it is auto loaded when activated in another thread
def_rpc("Test.testRPC"){
	LOG.std(nil, "info", "category", msg);
	msg.output = true; 
	ParaEngine.Sleep(1);
	return msg; 
}
Test.testRPC:MakePublic();

function TestDSLNPL.TestRPC()
	-- now we can invoke it anywhere in any thread or remote address.
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		assert(msg.output == true and msg[1] == "input")
		echo(msg);
	end);
	
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		echo("succeed: not timed out");
	end, 2000);
	
	-- time out in 500ms
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		assert(err == "timeout" and msg==nil)
		echo("succeed: timed out");
	end, 500);
end

run following code

NPL.load("script/ide/System/Compiler/tests/test_DSL_NPL.npl")
local TestDSLNPL = commonlib.gettable("Tests.TestDSLNPL");
TestDSLNPL.TestRPC()

generate error

[string "script/ide/System/Compiler/nplgen.lua"]:254: attempt to index a nil value <Runtime error>

pretty strange

The following code works, when you comment out the function TestDSLNPL.TestRPC() line

local TestDSLNPL = commonlib.gettable("Tests.TestDSLNPL");

-- defined in global scope so that it is auto loaded when activated in another thread
def_rpc("Test.testRPC"){
	LOG.std(nil, "info", "category", msg);
	msg.output = true; 
	ParaEngine.Sleep(1);
	return msg; 
}
Test.testRPC:MakePublic();

--function TestDSLNPL.TestRPC()
	-- now we can invoke it anywhere in any thread or remote address.
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		assert(msg.output == true and msg[1] == "input")
		echo(msg);
	end);
	
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		echo("succeed: not timed out");
	end, 2000);
	
	-- time out in 500ms
	Test.testRPC("(worker1)", {"input"}, function(err, msg) 
		assert(err == "timeout" and msg==nil)
		echo("succeed: timed out");
	end, 500);
--end

It's a bug when generating code from ast when function name is like a.b.c(). Fixed.