NHMLFixup v10
[jpcrr.git] / mnj / lua / LuaInternal.java
bloba17bc663931d9ff5d7617ddd1883f7a3dbf66088
1 /* $Header: //info.ravenbrook.com/project/jili/version/1.1/code/mnj/lua/LuaInternal.java#1 $
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
3 * All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject
11 * to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
20 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
21 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 package mnj.lua;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.io.IOException;
30 import java.io.Reader;
32 /**
33 * Class used to implement internal callbacks. Currently there is only
34 * one callback used, one that parses or loads a Lua chunk into binary
35 * form.
37 final class LuaInternal extends LuaJavaCallback
39 private InputStream stream;
40 private Reader reader;
41 private String chunkname;
43 LuaInternal(InputStream in, String chunkname)
45 this.stream = in;
46 this.chunkname = chunkname;
49 LuaInternal(Reader in, String chunkname)
51 this.reader = in;
52 this.chunkname = chunkname;
55 public int luaFunction(Lua L)
57 try
59 Proto p = null;
61 // In either the stream or the reader case there is a way of
62 // converting the input to the other type.
63 if (stream != null)
65 stream.mark(1);
66 int c = stream.read();
67 stream.reset();
69 // Convert to Reader if looks like source code instead of
70 // binary.
71 if (c == Loader.HEADER[0])
73 Loader l = new Loader(stream, chunkname);
74 p = l.undump();
76 else
78 reader = new InputStreamReader(stream, "UTF-8");
79 p = Syntax.parser(L, reader, chunkname);
82 else
84 // Convert to Stream if looks like binary (dumped via
85 // string.dump) instead of source code.
86 if (reader.markSupported())
88 reader.mark(1);
89 int c = reader.read();
90 reader.reset();
92 if (c == Loader.HEADER[0])
94 stream = new FromReader(reader);
95 Loader l = new Loader(stream, chunkname);
96 p = l.undump();
98 else
100 p = Syntax.parser(L, reader, chunkname);
103 else
105 p = Syntax.parser(L, reader, chunkname);
109 L.push(new LuaFunction(p,
110 new UpVal[0],
111 L.getGlobals()));
112 return 1;
114 catch (IOException e)
116 L.push("cannot read " + chunkname + ": " + e.toString());
117 L.dThrow(Lua.ERRFILE);
118 return 0;