您的位置:首页 > 产品设计 > UI/UE

luerl 测试用例

2016-01-04 01:50 405 查看
%%%-------------------------------------------------------------------
%%% @author yangzm
%%% @copyright (C) 2015, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 26. 十二月 2015 下午6:24
%%%-------------------------------------------------------------------
-module(aa).
-export([run/0,run2/0]).

run() ->
  % execute a string, get a result
  {ok,A} = luerl:eval("return 1 + 1"),
  io:format("(5) 1 + 1 = ~p!~n", [A]),

  % execute a file, get a result
  {B,_} = luerl:dofile("/home/yangzm/IdeaProjects/myerlang/src/hello2-2.lua"),
  io:format("(7) 2137 * 42 = ~p?~n", [B]),

  % execute a standard function
  luerl:call_function([print], [<<"(8) Hello, standard print function!">>]),
  {Result1,_} = luerl:call_function([table,pack], [<<"a">>,<<"b">>,42]),
  io:format("(10) ~p?~n", [Result1]),

  % separately parse, then execute (doubles (12) and Chunk2 as assertion)
  St2A = luerl:init(),
  {ok,Chunk2,St2B} = luerl:load("function chunk2() print(\"(12) Hello, Chunk 2!\") end"),
  {Result2,St2C} = luerl:do(Chunk2, St2B),
  luerl:call_function([chunk2], [], St2C),

  %----------------------------------------
  % separately parse, then execute a file, get a result. The file defines confirm(p)
  {ok,Chunk14,St8} = luerl:loadfile("/home/yangzm/IdeaProjects/myerlang/src/hello2-9.lua",luerl:init()),
  {Result14,State14} = luerl:do(Chunk14, St8),

  {Result14A,Nss} = luerl:call_function([confirm], [23,<<"76">>], State14),
  io:format("(36) Well: ~w~n", [Result14A]),

  % 调用第二个函数时要传头一次函数反回值,否则会出错的
  {Result_add,Nss2} = luerl:call_function([add2], [3,5], Nss),
  io:format("(36) Well: ~w~n", [Result_add]),
  %-----------------------------------------

  
  % execute a file, get the decoded result of a table
  {ok,Result15} = luerl:evalfile("/home/yangzm/IdeaProjects/myerlang/src/hello2-10.lua", State14),
  io:format("(37) Decoded table: ~p~n", [Result15]),

  io:format("done~n").

run2() ->
  LuaScript = <<"hello_table = { hello=\"world\" }; return hello_table">>,
  {[_Table], Lua0} = luerl:do(LuaScript),

  {World, Lua1} = luerl:get_table([hello_table, hello], Lua0),
  Lua2 = luerl:set_table([hello_table, hello], there, Lua1),
  {HelloDict,Lua3} = luerl:get_table([hello_table], Lua2),
  {There, Lua4} = luerl:get_table([hello_table, hello], Lua3),
  io:format("(1) hello ~s ~s - ~p~n", [There, World, HelloDict]),

  Lua5 = luerl:set_table1([<<"hello_table">>, <<"goodbye">>], <<"bye">>, Lua4),
  {Bye, Lua6} = luerl:get_table1([<<"hello_table">>, <<"goodbye">>], Lua5),
  {HelloTab, _Lua7} = luerl:get_table1([<<"hello_table">>], Lua6),
  io:format("(2) ~s - ~p~n", [Bye, HelloTab]),
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: