Skip to content

Commit 060fb21

Browse files
committed
Merge pull request dan200#298 from SquidDev-CC/ComputerCraft/feature/luaj-bit32
Replace BitAPI with a LuaJ implementation of bit32
2 parents ef00870 + 6fca136 commit 060fb21

File tree

6 files changed

+195
-110
lines changed

6 files changed

+195
-110
lines changed

libs/luaj-jse-2.0.3.jar

2.4 KB
Binary file not shown.
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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+
}

src/main/java/dan200/computercraft/core/apis/BitAPI.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/main/java/dan200/computercraft/core/computer/Computer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,6 @@ private void createAPIs()
611611
m_apis.add( new FSAPI( m_apiEnvironment ) );
612612
m_apis.add( new PeripheralAPI( m_apiEnvironment ) );
613613
m_apis.add( new OSAPI( m_apiEnvironment ) );
614-
m_apis.add( new BitAPI( m_apiEnvironment ) );
615614
//m_apis.add( new BufferAPI( m_apiEnvironment ) );
616615
if( ComputerCraft.http_enable )
617616
{

src/main/java/dan200/computercraft/core/lua/LuaJLuaMachine.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import dan200.computercraft.core.computer.MainThread;
1818

1919
import org.luaj.vm2.*;
20+
import org.luaj.vm2.lib.Bit32Lib;
2021
import org.luaj.vm2.lib.OneArgFunction;
2122
import org.luaj.vm2.lib.VarArgFunction;
2223
import org.luaj.vm2.lib.ZeroArgFunction;
@@ -54,6 +55,7 @@ public LuaJLuaMachine( Computer computer )
5455

5556
// Create an environment to run in
5657
m_globals = JsePlatform.debugGlobals();
58+
m_globals.load( new Bit32Lib() );
5759
m_loadString = m_globals.get("loadstring");
5860
m_assert = m_globals.get("assert");
5961

src/main/resources/assets/computercraft/lua/bios.lua

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,6 @@ if _VERSION == "Lua 5.1" then
5555
table.unpack = unpack
5656
table.pack = function( ... ) return { n = select( "#", ... ), ... } end
5757

58-
-- Install the bit32 api
59-
local nativebit = bit
60-
bit32 = {}
61-
bit32.arshift = nativebit.brshift
62-
bit32.band = nativebit.band
63-
bit32.bnot = nativebit.bnot
64-
bit32.bor = nativebit.bor
65-
bit32.btest = function( a, b ) return nativebit.band(a,b) ~= 0 end
66-
bit32.bxor = nativebit.bxor
67-
bit32.lshift = nativebit.blshift
68-
bit32.rshift = nativebit.blogic_rshift
69-
7058
if _CC_DISABLE_LUA51_FEATURES then
7159
-- Remove the Lua 5.1 features that will be removed when we update to Lua 5.2, for compatibility testing.
7260
-- See "disable_lua51_functions" in ComputerCraft.cfg
@@ -76,11 +64,21 @@ if _VERSION == "Lua 5.1" then
7664
unpack = nil
7765
math.log10 = nil
7866
table.maxn = nil
79-
bit = nil
67+
else
68+
-- Inject a stub for the old bit library
69+
_G.bit = {
70+
bnot = bit32.bnot,
71+
band = bit32.band,
72+
bor = bit32.bor,
73+
bxor = bit32.bxor,
74+
brshift = bit32.arshift,
75+
blshift = bit32.lshift,
76+
blogic_rshift = bit32.rshift
77+
}
8078
end
8179
end
8280

83-
if _VERSION == "Lua 5.3" then
81+
if _VERSION == "Lua 5.3" and not bit32 then
8482
-- If we're on Lua 5.3, install the bit32 api from Lua 5.2
8583
-- (Loaded from a string so this file will still parse on <5.3 lua)
8684
load( [[

0 commit comments

Comments
 (0)