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).
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.
27 import java
.io
.InputStream
;
28 import java
.io
.InputStreamReader
;
29 import java
.io
.IOException
;
30 import java
.io
.Reader
;
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
37 final class LuaInternal
extends LuaJavaCallback
39 private InputStream stream
;
40 private Reader reader
;
41 private String chunkname
;
43 LuaInternal(InputStream in
, String chunkname
)
46 this.chunkname
= chunkname
;
49 LuaInternal(Reader in
, String chunkname
)
52 this.chunkname
= chunkname
;
55 public int luaFunction(Lua L
)
61 // In either the stream or the reader case there is a way of
62 // converting the input to the other type.
66 int c
= stream
.read();
69 // Convert to Reader if looks like source code instead of
71 if (c
== Loader
.HEADER
[0])
73 Loader l
= new Loader(stream
, chunkname
);
78 reader
= new InputStreamReader(stream
, "UTF-8");
79 p
= Syntax
.parser(L
, reader
, chunkname
);
84 // Convert to Stream if looks like binary (dumped via
85 // string.dump) instead of source code.
86 if (reader
.markSupported())
89 int c
= reader
.read();
92 if (c
== Loader
.HEADER
[0])
94 stream
= new FromReader(reader
);
95 Loader l
= new Loader(stream
, chunkname
);
100 p
= Syntax
.parser(L
, reader
, chunkname
);
105 p
= Syntax
.parser(L
, reader
, chunkname
);
109 L
.push(new LuaFunction(p
,
114 catch (IOException e
)
116 L
.push("cannot read " + chunkname
+ ": " + e
.toString());
117 L
.dThrow(Lua
.ERRFILE
);