当前位置: 首页 > 工具软件 > Luerl > 使用案例 >

luerl 测试用例

璩正志
2023-12-01
%%%-------------------------------------------------------------------
%%% @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]),
 类似资料: