|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2012 Luaj.org. All rights reserved. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + ******************************************************************************/ |
| 22 | +package org.luaj.vm2.lib; |
| 23 | + |
| 24 | +import org.luaj.vm2.LuaInteger; |
| 25 | +import org.luaj.vm2.LuaTable; |
| 26 | +import org.luaj.vm2.LuaValue; |
| 27 | +import org.luaj.vm2.Varargs; |
| 28 | + |
| 29 | +/** |
| 30 | + * Subclass of LibFunction that implements the Lua standard {@code bit32} library. |
| 31 | + */ |
| 32 | +public class Bit32Lib extends ZeroArgFunction |
| 33 | +{ |
| 34 | + public LuaValue call( ) |
| 35 | + { |
| 36 | + LuaTable t = new LuaTable(); |
| 37 | + bind( t, Bit32LibV.class, new String[] { |
| 38 | + "band", "bnot", "bor", "btest", "bxor", "extract", "replace" |
| 39 | + } ); |
| 40 | + bind( t, Bit32Lib2.class, new String[] { |
| 41 | + "arshift", "lrotate", "lshift", "rrotate", "rshift" |
| 42 | + } ); |
| 43 | + env.set( "bit32", t ); |
| 44 | + return t; |
| 45 | + } |
| 46 | + |
| 47 | + public static final class Bit32LibV extends VarArgFunction |
| 48 | + { |
| 49 | + public Varargs invoke( Varargs args ) |
| 50 | + { |
| 51 | + switch( opcode ) |
| 52 | + { |
| 53 | + case 0: // band |
| 54 | + { |
| 55 | + int result = -1; |
| 56 | + for( int i = 1; i <= args.narg(); i++ ) |
| 57 | + { |
| 58 | + result &= args.checkint( i ); |
| 59 | + } |
| 60 | + return bitsToValue( result ); |
| 61 | + } |
| 62 | + case 1: // bnot |
| 63 | + return bitsToValue( ~args.checkint( 1 ) ); |
| 64 | + case 2: // bot |
| 65 | + { |
| 66 | + int result = 0; |
| 67 | + for( int i = 1; i <= args.narg(); i++ ) |
| 68 | + { |
| 69 | + result |= args.checkint( i ); |
| 70 | + } |
| 71 | + return bitsToValue( result ); |
| 72 | + } |
| 73 | + case 3: // btest |
| 74 | + { |
| 75 | + int bits = -1; |
| 76 | + for( int i = 1; i <= args.narg(); i++ ) |
| 77 | + { |
| 78 | + bits &= args.checkint( i ); |
| 79 | + } |
| 80 | + return valueOf( bits != 0 ); |
| 81 | + } |
| 82 | + case 4: // bxor |
| 83 | + { |
| 84 | + int result = 0; |
| 85 | + for( int i = 1; i <= args.narg(); i++ ) |
| 86 | + { |
| 87 | + result ^= args.checkint( i ); |
| 88 | + } |
| 89 | + return bitsToValue( result ); |
| 90 | + } |
| 91 | + case 5: // extract |
| 92 | + { |
| 93 | + int field = args.checkint( 2 ); |
| 94 | + int width = args.optint( 3, 1 ); |
| 95 | + |
| 96 | + if( field < 0 ) argerror( 2, "field cannot be negative" ); |
| 97 | + if( width <= 0 ) argerror( 3, "width must be postive" ); |
| 98 | + if( field + width > 32 ) error( "trying to access non-existent bits" ); |
| 99 | + |
| 100 | + return bitsToValue( (args.checkint( 1 ) >>> field) & (-1 >>> (32 - width)) ); |
| 101 | + } |
| 102 | + case 6: // replace |
| 103 | + { |
| 104 | + int n = args.checkint( 1 ); |
| 105 | + int v = args.checkint( 2 ); |
| 106 | + int field = args.checkint( 3 ); |
| 107 | + int width = args.optint( 4, 1 ); |
| 108 | + |
| 109 | + if( field < 0 ) argerror( 3, "field cannot be negative" ); |
| 110 | + if( width <= 0 ) argerror( 4, "width must be postive" ); |
| 111 | + if( field + width > 32 ) error( "trying to access non-existent bits" ); |
| 112 | + |
| 113 | + int mask = (-1 >>> (32 - width)) << field; |
| 114 | + n = (n & ~mask) | ((v << field) & mask); |
| 115 | + return bitsToValue( n ); |
| 116 | + } |
| 117 | + } |
| 118 | + return NIL; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public static final class Bit32Lib2 extends TwoArgFunction |
| 123 | + { |
| 124 | + public LuaValue call( LuaValue arg1, LuaValue arg2 ) |
| 125 | + { |
| 126 | + switch( opcode ) |
| 127 | + { |
| 128 | + case 0: // arshift |
| 129 | + { |
| 130 | + int x = arg1.checkint(); |
| 131 | + int disp = arg2.checkint(); |
| 132 | + return disp >= 0 ? bitsToValue( x >> disp ) : bitsToValue( x << -disp ); |
| 133 | + } |
| 134 | + case 1: // lrotate |
| 135 | + return rotate( arg1.checkint(), arg2.checkint() ); |
| 136 | + case 2: // lshift |
| 137 | + return shift( arg1.checkint(), arg2.checkint() ); |
| 138 | + case 3: // rrotate |
| 139 | + return rotate( arg1.checkint(), -arg2.checkint() ); |
| 140 | + case 4: // rshift |
| 141 | + return shift( arg1.checkint(), -arg2.checkint() ); |
| 142 | + } |
| 143 | + return NIL; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + static LuaValue rotate( int x, int disp ) |
| 148 | + { |
| 149 | + if( disp < 0 ) |
| 150 | + { |
| 151 | + disp = -disp & 31; |
| 152 | + return bitsToValue( (x >>> disp) | (x << (32 - disp)) ); |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + disp = disp & 31; |
| 157 | + return bitsToValue( (x << disp) | (x >>> (32 - disp)) ); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + static LuaValue shift( int x, int disp ) |
| 162 | + { |
| 163 | + if( disp >= 32 || disp <= -32 ) |
| 164 | + { |
| 165 | + return ZERO; |
| 166 | + } |
| 167 | + else if( disp >= 0 ) |
| 168 | + { |
| 169 | + return bitsToValue( x << disp ); |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + return bitsToValue( x >>> -disp ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private static LuaValue bitsToValue( int x ) |
| 178 | + { |
| 179 | + return x < 0 ? LuaValue.valueOf( (long) x & 0xFFFFFFFFL ) : LuaInteger.valueOf( x ); |
| 180 | + } |
| 181 | +} |
0 commit comments