Mercurial Hosting > luan
comparison src/luan/interp/LuaClosure.java @ 31:5cf15507d77e
separate interpreter from interface
git-svn-id: https://luan-java.googlecode.com/svn/trunk@32 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 13 Dec 2012 02:50:04 +0000 |
parents | src/luan/LuaClosure.java@7ee247560db5 |
children | c3eab5a3ce3c |
comparison
equal
deleted
inserted
replaced
30:8d8f4f5caef4 | 31:5cf15507d77e |
---|---|
1 package luan.interp; | |
2 | |
3 import luan.LuaFunction; | |
4 import luan.LuaState; | |
5 import luan.LuaException; | |
6 | |
7 | |
8 final class LuaClosure extends LuaFunction { | |
9 private final Chunk chunk; | |
10 | |
11 LuaClosure(Chunk chunk,LuaStateImpl lua) { | |
12 this.chunk = chunk; | |
13 } | |
14 | |
15 public Object[] call(LuaState luaState,Object... args) throws LuaException { | |
16 LuaStateImpl lua = (LuaStateImpl)luaState; | |
17 Chunk chunk = this.chunk; | |
18 while(true) { | |
19 Object[] varArgs = null; | |
20 if( chunk.isVarArg ) { | |
21 if( args.length > chunk.numArgs ) { | |
22 varArgs = new Object[ args.length - chunk.numArgs ]; | |
23 for( int i=0; i<varArgs.length; i++ ) { | |
24 varArgs[i] = args[chunk.numArgs+i]; | |
25 } | |
26 } else { | |
27 varArgs = LuaFunction.EMPTY_RTN; | |
28 } | |
29 } | |
30 Object[] stack = lua.newStack(chunk.stackSize,varArgs); | |
31 final int n = Math.min(args.length,chunk.numArgs); | |
32 for( int i=0; i<n; i++ ) { | |
33 stack[i] = args[i]; | |
34 } | |
35 Object[] returnValues; | |
36 LuaClosure tailFn; | |
37 try { | |
38 chunk.block.eval(lua); | |
39 } catch(ReturnException e) { | |
40 } finally { | |
41 returnValues = lua.returnValues; | |
42 tailFn = lua.tailFn; | |
43 lua.popStack(); | |
44 } | |
45 if( tailFn == null ) | |
46 return returnValues; | |
47 chunk = tailFn.chunk; | |
48 args = returnValues; | |
49 } | |
50 } | |
51 | |
52 } |