/*******************************************************************************
 * Copyright (c) 2010 Luaj.org. All rights reserved.
 *
 * 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 org.luaj.vm2.luajc;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Hashtable;

import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.LocVars;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.compiler.LuaC;

/**
 * Implementation of {@link org.luaj.vm2.Globals.Compiler} which does direct
 * lua-to-java-bytecode compiling.
 * <p>
 * By default, when using {@link org.luaj.vm2.libs.jse.JsePlatform} or
 * {@link org.luaj.vm2.libs.jme.JmePlatform}
 * to construct globals, the plain compiler {@link LuaC} is installed and lua code
 * will only be compiled into lua bytecode and execute as {@link LuaClosure}.
 * <p>
 * To override the default compiling behavior with {@link LuaJC}
 * lua-to-java bytecode compiler, install it before undumping code,
 * for example:
 * <pre> {@code
 * LuaValue globals = JsePlatform.standardGlobals();
 * LuaJC.install(globals);
 * LuaValue chunk = globals.load( "print('hello, world'), "main.lua");
 * System.out.println(chunk.isclosure());  // Will be false when LuaJC is working.
 * chunk.call();
 * } </pre>
 * <p>
 * This requires the bcel library to be on the class path to work as expected.
 * If the library is not found, the default {@link LuaC} lua-to-lua-bytecode
 * compiler will be used.
 *
 * @see Globals#compiler
 * @see #install(Globals)
 * @see org.luaj.vm2.compiler.LuaC
 * @see LuaValue
 */
public class LuaJC implements Globals.Loader {

	public static final LuaJC instance = new LuaJC();

	/**
	 * Install the compiler as the main Globals.Loader to use in a set of globals.
	 * Prototypes that require interpreter-only semantics are emitted as
	 * generated delegating wrappers that execute the dumped prototype through
	 * {@link org.luaj.vm2.LuaClosure}.
	 */
	public static final void install(Globals G) {
		G.loader = instance;
	}

	protected LuaJC() {}

	public Hashtable compileAll(InputStream script, String chunkname, String filename, Globals globals, boolean genmain) throws IOException {
		final String classname = toStandardJavaClassName( chunkname );
		final Prototype p = globals.loadPrototype(script, classname, "bt");
		return compileProtoAndSubProtos(p, classname, filename, genmain);
	}

	public Hashtable compileAll(Reader script, String chunkname, String filename, Globals globals, boolean genmain) throws IOException {
		final String classname = toStandardJavaClassName( chunkname );
		final Prototype p = globals.compilePrototype(script, classname);
		return compileProtoAndSubProtos(p, classname, filename, genmain);
	}

	private Hashtable compileProtoAndSubProtos(Prototype p, String classname, String filename, boolean genmain) throws IOException {
		if (requiresInterpreterDelegate(p)) {
			return compileDelegatingPrototype(p, classname, filename, genmain);
		}
		final String luaname = toStandardLuaFileName( filename );
		final Hashtable h = new Hashtable();
		final JavaGen gen = new JavaGen(p, classname, luaname, genmain);
		insert( h, gen );
		return h;
	}

	private Hashtable compileDelegatingPrototype(Prototype p, String classname, String filename, boolean genmain) throws IOException {
		final Hashtable h = new Hashtable();
		final DelegateJavaGen gen = new DelegateJavaGen(classname, toStandardLuaFileName(filename), genmain,
			LuaJCDelegateSupport.dumpPrototypeHex(p));
		h.put(gen.classname, gen.bytecode);
		return h;
	}

	private void insert(Hashtable h, JavaGen gen) {
		h.put(gen.classname, gen.bytecode);
		for ( int i=0, n=gen.inners!=null? gen.inners.length: 0; i<n; i++ )
			insert(h, gen.inners[i]);
	}

	public LuaFunction load(Prototype p, String name, LuaValue globals) throws IOException {
		String luaname = toStandardLuaFileName( name );
		String classname = toStandardJavaClassName( luaname );
		JavaLoader loader = new JavaLoader();
		if (requiresInterpreterDelegate(p)) {
			DelegateJavaGen gen = new DelegateJavaGen(classname, luaname, false, LuaJCDelegateSupport.dumpPrototypeHex(p));
			return loader.load(gen, globals);
		}
		return loader.load(p, classname, luaname, globals);
	}

	private boolean requiresInterpreterDelegate(Prototype p) {
		return hasToCloseLocals(p) || hasNamedVarargTables(p) || usesInterpreterOnlyRuntimeSemantics(p);
	}

	private boolean hasToCloseLocals(Prototype p) {
		if (p.locvars != null) {
			for (int i = 0; i < p.locvars.length; i++) {
				LocVars local = p.locvars[i];
				if (local != null && local.toclose)
					return true;
			}
		}
		if (p.p != null) {
			for (int i = 0; i < p.p.length; i++) {
				if (p.p[i] != null && hasToCloseLocals(p.p[i]))
					return true;
			}
		}
		return false;
	}

	private boolean hasNamedVarargTables(Prototype p) {
		if (p.varargname != null) {
			return true;
		}
		if (p.p != null) {
			for (int i = 0; i < p.p.length; i++) {
				if (p.p[i] != null && hasNamedVarargTables(p.p[i])) {
					return true;
				}
			}
		}
		return false;
	}

	private boolean usesInterpreterOnlyRuntimeSemantics(Prototype p) {
		if (prototypeReferences(p, "xpcall") || prototypeReferences(p, "debug")) {
			return true;
		}
		if (p.p != null) {
			for (int i = 0; i < p.p.length; i++) {
				if (p.p[i] != null && usesInterpreterOnlyRuntimeSemantics(p.p[i])) {
					return true;
				}
			}
		}
		return false;
	}

	private boolean prototypeReferences(Prototype p, String name) {
		if (p.k == null) {
			return false;
		}
		LuaString target = LuaString.valueOf(name);
		for (int i = 0; i < p.k.length; i++) {
			LuaValue constant = p.k[i];
			if (constant != null && constant.isstring() && constant.strvalue().raweq(target)) {
				return true;
			}
		}
		return false;
	}

	private static String toStandardJavaClassName( String luachunkname ) {
		String stub = toStub( luachunkname );
		StringBuffer classname = new StringBuffer();
		for (int i = 0, n = stub.length(); i < n; ++i) {
			final char c = stub.charAt(i);
			switch(i) {
				case 0:
					if(Character.isJavaIdentifierStart(c)) {
						classname.append(c);
					} else {
						classname.append('_');
					}
					break;
				default:
					if(c == '/') {
						classname.append(c);
					} else if(Character.isJavaIdentifierPart(c)) {
						classname.append(c);
					} else {
						classname.append('_');
					}
					break;
			}
		}
		return classname.toString();
	}

	private static String toStandardLuaFileName( String luachunkname ) {
		String stub = toStub( luachunkname );
		String filename = stub.replace('.','/')+".lua";
		return filename.startsWith("@")? filename.substring(1): filename;
	}

	private static String toStub( String s ) {
		String stub = s.endsWith(".lua")? s.substring(0,s.length()-4): s;
		return stub;
	}
}
