Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implemented loadfile

  • Loading branch information...
commit 487f7d2ff78a50f85a6598348263e177efcc1bc2 1 parent 11d6bae
@spkrka spkrka authored
View
4 .idea/misc.xml
@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
+ <component name="ASMPluginConfiguration">
+ <asm skipDebug="false" skipFrames="false" skipCode="false" expandFrames="false" />
+ <groovy codeStyle="LEGACY" />
+ </component>
<component name="DependencyValidationManager">
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</component>
View
15 core/src/se/krka/kahlua/luaj/compiler/LuaCompiler.java
@@ -78,13 +78,20 @@ public int call(LuaCallFrame callFrame, int nArguments) {
return 0;
}
- private int loadstream(LuaCallFrame callFrame, int nArguments) {
+ public static int loadstream(LuaCallFrame callFrame, int nArguments) {
try {
KahluaUtil.luaAssert(nArguments >= 2, "not enough arguments");
- InputStream is = (InputStream) callFrame.get(0);
- KahluaUtil.luaAssert(is != null, "No inputstream given");
+ Object input = callFrame.get(0);
+ KahluaUtil.luaAssert(input != null, "No input given");
String name = (String) callFrame.get(1);
- return callFrame.push(loadis(is, name, null, callFrame.getEnvironment()));
+ if (input instanceof Reader) {
+ return callFrame.push(loadis((Reader) input, name, null, callFrame.getEnvironment()));
+ }
+ if (input instanceof InputStream) {
+ return callFrame.push(loadis((InputStream) input, name, null, callFrame.getEnvironment()));
+ }
+ KahluaUtil.fail("Invalid type to loadstream: " + input.getClass());
+ return 0;
} catch (RuntimeException e) {
return callFrame.push(null, e.getMessage());
} catch (IOException e) {
View
32 j2se/src/se/krka/kahlua/require/Loadfile.java
@@ -0,0 +1,32 @@
+package se.krka.kahlua.require;
+
+import se.krka.kahlua.luaj.compiler.LuaCompiler;
+import se.krka.kahlua.vm.*;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.Map;
+
+public class Loadfile implements JavaFunction {
+ public void install(KahluaTable environment) {
+ environment.rawset("loadfile", this);
+ }
+
+ private final LuaSourceProvider luaSourceProvider;
+
+ public Loadfile(LuaSourceProvider luaSourceProvider) {
+ this.luaSourceProvider = luaSourceProvider;
+ }
+
+ public int call(LuaCallFrame callFrame, int nArguments) {
+ String path = KahluaUtil.getStringArg(callFrame, 1, "loadfile");
+ Reader source = luaSourceProvider.getLuaSource(path);
+ if (source == null) {
+ KahluaUtil.fail("Does not exist: " + path);
+ }
+ callFrame.setTop(2);
+ callFrame.set(0, source);
+ callFrame.set(1, path);
+ return LuaCompiler.loadstream(callFrame, 2);
+ }
+}
View
63 j2se/test/se/krka/kahlua/require/LoadfileTest.java
@@ -0,0 +1,63 @@
+/*
+Copyright (c) 2011 Kristofer Karlsson <kristofer.karlsson@gmail.com>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+package se.krka.kahlua.require;
+
+import org.junit.Test;
+import se.krka.kahlua.j2se.J2SEPlatform;
+import se.krka.kahlua.luaj.compiler.LuaCompiler;
+import se.krka.kahlua.vm.KahluaTable;
+import se.krka.kahlua.vm.KahluaThread;
+import se.krka.kahlua.vm.LuaClosure;
+import se.krka.kahlua.vm.Platform;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+
+public class LoadfileTest {
+
+ @Test
+ public void load() throws IOException {
+ MockProvider provider = new MockProvider();
+ provider.addSource("/a", "t = {}");
+
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
+ PrintStream printStream = new PrintStream(byteArrayOutputStream);
+ Platform platform = new J2SEPlatform();
+ KahluaTable env = platform.newEnvironment();
+ KahluaThread state = new KahluaThread(printStream, platform, env);
+ new Loadfile(provider).install(env);
+
+ LuaClosure luaClosure = LuaCompiler.loadstring(
+ "loadfile('/a')() if(t == nil) then print('a') else print('b') end", "foo", state.getEnvironment());
+ Object[] objects = state.pcall(luaClosure);
+ assertEquals(Arrays.toString(objects), Boolean.TRUE, objects[0]);
+ String outputString = RequireTest.setNewLines(new String(byteArrayOutputStream.toByteArray()));
+ assertEquals("b\n", outputString);
+ }
+
+}
+
View
2  j2se/test/se/krka/kahlua/require/RequireTest.java
@@ -37,7 +37,7 @@ public void testMultipleRequire() throws IOException {
assertEquals("Great success\n", outputString);
}
- private String setNewLines(String s) {
+ public static String setNewLines(String s) {
return s.replace("\r\n", "\n");
}
Please sign in to comment.
Something went wrong with that request. Please try again.