luerl 测试用例

%%%-------------------------------------------------------------------
%%% @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]),
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值