Improve __concat
This commit is contained in:
@@ -63,8 +63,7 @@ public class LuaNumber extends LuaValue {
|
||||
}
|
||||
|
||||
public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); }
|
||||
public Buffer concat(Buffer rhs) { return rhs.prepend(this.strvalue()); }
|
||||
public LuaValue concatTo(LuaValue lhs) { return lhs.concaterror(); }
|
||||
public Buffer concat(Buffer rhs) { return concat(rhs.value()).buffer(); }
|
||||
public LuaValue concatTo(LuaNumber lhs) { return strvalue().concatTo(lhs.strvalue()); }
|
||||
public LuaValue concatTo(LuaString lhs) { return strvalue().concatTo(lhs); }
|
||||
|
||||
|
||||
@@ -157,7 +157,6 @@ public class LuaString extends LuaValue {
|
||||
// concatenation
|
||||
public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); }
|
||||
public Buffer concat(Buffer rhs) { return rhs.prepend(this); }
|
||||
public LuaValue concatTo(LuaValue lhs) { return lhs.concaterror(); }
|
||||
public LuaValue concatTo(LuaNumber lhs) { return concatTo(lhs.strvalue()); }
|
||||
public LuaValue concatTo(LuaString lhs) {
|
||||
byte[] b = new byte[lhs.m_length+this.m_length];
|
||||
|
||||
@@ -347,15 +347,14 @@ public class LuaValue extends Varargs {
|
||||
// concatenation
|
||||
public LuaValue concat(LuaValue rhs) { return rhs.concatTo(this); }
|
||||
public LuaValue concatTo(LuaValue lhs) { return lhs.concatmt(this); }
|
||||
public LuaValue concatTo(LuaNumber lhs) { return concaterror(); }
|
||||
public LuaValue concatTo(LuaString lhs) { return concaterror(); }
|
||||
public LuaValue concatTo(LuaNumber lhs) { return lhs.concatmt(this); }
|
||||
public LuaValue concatTo(LuaString lhs) { return lhs.concatmt(this); }
|
||||
public Buffer buffer() { return new Buffer(this); }
|
||||
public Buffer concat(Buffer rhs) { return rhs.setvalue(concat(rhs.value())); }
|
||||
public LuaValue concatmt(LuaValue rhs) {
|
||||
LuaValue h=metatag(CONCAT);
|
||||
LuaValue v=this;
|
||||
if ( h.isnil() && (h=(v=rhs).metatag(CONCAT)).isnil())
|
||||
return v.concaterror();
|
||||
if ( h.isnil() && (h=rhs.metatag(CONCAT)).isnil())
|
||||
return (isstring()? rhs: this).concaterror();
|
||||
return h.call(this,rhs);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,26 @@ public class CompatibiltyTest extends TestSuite {
|
||||
private static final String dir = "test/lua";
|
||||
|
||||
abstract protected static class CompatibiltyTestSuite extends ScriptDrivenTest {
|
||||
LuaValue savedStringMetatable;
|
||||
protected CompatibiltyTestSuite(PlatformType platform) {
|
||||
super(platform,dir);
|
||||
}
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
savedStringMetatable = LuaString.s_metatable;
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
LuaNil.s_metatable = null;
|
||||
LuaBoolean.s_metatable = null;
|
||||
LuaNumber.s_metatable = null;
|
||||
LuaFunction.s_metatable = null;
|
||||
LuaThread.s_metatable = null;
|
||||
LuaString.s_metatable = savedStringMetatable;
|
||||
}
|
||||
|
||||
public void testBaseLib() { runTest("baselib"); }
|
||||
public void testCoroutineLib() { runTest("coroutinelib"); }
|
||||
public void testDebugLib() { runTest("debuglib"); }
|
||||
|
||||
@@ -665,16 +665,18 @@ public class UnaryBinaryOperatorsTest extends TestCase {
|
||||
} } );
|
||||
assertEquals( tru, tru.concat(tbl) );
|
||||
assertEquals( tbl, tbl.concat(tru) );
|
||||
assertEquals( tru, tru.concat(new Buffer(tbl)).value() );
|
||||
assertEquals( tbl, tbl.concat(new Buffer(tru)).value() );
|
||||
assertEquals( fal, fal.concat(tbl.concat(new Buffer(tru))).value() );
|
||||
assertEquals( uda, uda.concat(tru.concat(new Buffer(tbl))).value() );
|
||||
try { tru.concat(def); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tru); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { tru.concat(new Buffer(def)).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(new Buffer(tru)).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { fal.concat(def.concat(new Buffer(tru))).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { ghi.concat(tru.concat(new Buffer(def))).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
assertEquals( tru, tru.concat(tbl) );
|
||||
assertEquals( tbl, tbl.concat(tru) );
|
||||
assertEquals( tru, tru.concat(tbl.buffer()).value() );
|
||||
assertEquals( tbl, tbl.concat(tru.buffer()).value() );
|
||||
assertEquals( fal, fal.concat(tbl.concat(tru.buffer())).value() );
|
||||
assertEquals( uda, uda.concat(tru.concat(tbl.buffer())).value() );
|
||||
try { tbl.concat(def); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tbl); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { tbl.concat(def.buffer()).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tbl.buffer()).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { uda.concat(def.concat(tbl.buffer())).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { ghi.concat(tbl.concat(def.buffer())).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
|
||||
// always use right argument
|
||||
LuaBoolean.s_metatable = LuaValue.tableOf( new LuaValue[] {
|
||||
@@ -685,16 +687,16 @@ public class UnaryBinaryOperatorsTest extends TestCase {
|
||||
} } );
|
||||
assertEquals( tbl, tru.concat(tbl) );
|
||||
assertEquals( tru, tbl.concat(tru) );
|
||||
assertEquals( tbl, tru.concat(new Buffer(tbl)).value() );
|
||||
assertEquals( tru, tbl.concat(new Buffer(tru)).value() );
|
||||
assertEquals( tru, uda.concat(tbl.concat(new Buffer(tru))).value() );
|
||||
assertEquals( tbl, fal.concat(tru.concat(new Buffer(tbl))).value() );
|
||||
try { tru.concat(def); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tru); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { tru.concat(new Buffer(def)).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(new Buffer(tru)).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { uda.concat(def.concat(new Buffer(tru))).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { fal.concat(tru.concat(new Buffer(def))).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
assertEquals( tbl, tru.concat(tbl.buffer()).value() );
|
||||
assertEquals( tru, tbl.concat(tru.buffer()).value() );
|
||||
assertEquals( tru, uda.concat(tbl.concat(tru.buffer())).value() );
|
||||
assertEquals( tbl, fal.concat(tru.concat(tbl.buffer())).value() );
|
||||
try { tbl.concat(def); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tbl); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { tbl.concat(def.buffer()).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { def.concat(tbl.buffer()).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { uda.concat(def.concat(tbl.buffer())).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
try { uda.concat(tbl.concat(def.buffer())).value(); fail("did not throw error"); } catch ( LuaError le ) { };
|
||||
|
||||
} finally {
|
||||
LuaBoolean.s_metatable = null;
|
||||
|
||||
Reference in New Issue
Block a user