Skip to content

Commit cdfde54

Browse files
committed
Merge remote-tracking branch 'origin/master' into java-6
2 parents 6536189 + 3e9f52c commit cdfde54

34 files changed

+419
-243
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Cobalt
2-
Cobalt is an Lua implementation for Java, designed for use in the Minecraft mod
3-
[CC: Tweaked]. Is is based on [LuaJ 2.0][LuaJ], though has diverged
2+
Cobalt is a Lua implementation for Java, designed for use in the Minecraft mod
3+
[CC: Tweaked]. It is based on [LuaJ 2.0][LuaJ], though has diverged
44
significantly over the years.
55

66
## Features
7-
Cobalt implements a (mostly) compliant Lua 5.1 implementation, with several
7+
Cobalt is a (mostly) compliant implementation of Lua 5.2, with several
88
interesting additional features:
99

10-
- Backports much of the Lua 5.2-5.4 standard library.
10+
- Backwards compatibility with Lua 5.1 (`getfenv`/`setfenv`, `goto` as an
11+
identifier).
12+
- Backports much of the Lua 5.3 and 5.4 standard library.
1113
- Allows yielding _anywhere_ within a Lua program, including debug hooks and
12-
any inside any native function.
14+
inside any native function.
1315
- Support for interrupting and resuming the VM at arbitrary points.
1416
- Efficient concatenation of strings using ropes.
1517

build-tools/src/main/kotlin/cc/tweaked/cobalt/build/ClassEmitter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ private val subclassRelations = mapOf(
6868
UnorderedPair("org/squiddev/cobalt/LuaValue", "org/squiddev/cobalt/LuaString") to "org/squiddev/cobalt/LuaValue",
6969
UnorderedPair("org/squiddev/cobalt/Varargs", "org/squiddev/cobalt/LuaValue") to "org/squiddev/cobalt/Varargs",
7070
UnorderedPair("org/squiddev/cobalt/LuaError", "org/squiddev/cobalt/compiler/CompileException") to "java/lang/Exception",
71+
UnorderedPair("org/squiddev/cobalt/LuaError", "java/lang/Exception") to "java/lang/Exception",
72+
UnorderedPair("org/squiddev/cobalt/compiler/CompileException", "java/lang/Exception") to "java/lang/Exception"
7173
)
7274

7375
fun getCommonSuperclass(type1: String, type2: String): String? = subclassRelations[UnorderedPair(type1, type2)]

build-tools/src/main/kotlin/cc/tweaked/cobalt/build/coroutine/DefinitionData.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ interface DefinitionData {
6363
private val builtinMethods = run {
6464
val opHelper = "org/squiddev/cobalt/OperationHelper"
6565
val dispatch = "org/squiddev/cobalt/function/Dispatch"
66+
val nop = YieldType.Direct { }
6667
val first = YieldType.Direct { mw ->
6768
mw.visitMethodInsn(INVOKEVIRTUAL, VARARGS.internalName, "first", "()${LUA_VALUE.descriptor}", false)
6869
}
@@ -75,11 +76,14 @@ private val builtinMethods = run {
7576
mapOf(
7677
Desc("org/squiddev/cobalt/compiler/InputReader", "read", "()I") to YieldType.Resume,
7778
Desc("org/squiddev/cobalt/unwind/SuspendedFunction", "call", "(Lorg/squiddev/cobalt/LuaState;)Ljava/lang/Object;") to YieldType.Resume,
79+
Desc(dispatch, "call", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE)) to first,
7880
Desc(dispatch, "call", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE, LUA_VALUE)) to first,
7981
Desc(dispatch, "call", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE, LUA_VALUE, LUA_VALUE)) to first,
82+
Desc(dispatch, "invoke", Type.getMethodDescriptor(VARARGS, LUA_STATE, LUA_VALUE, VARARGS)) to nop,
8083
Desc(opHelper, "eq", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, LUA_STATE, LUA_VALUE, LUA_VALUE)) to firstBool,
8184
Desc(opHelper, "lt", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, LUA_STATE, LUA_VALUE, LUA_VALUE)) to firstBool,
8285
Desc(opHelper, "length", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE)) to first,
86+
Desc(opHelper, "toString", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE)) to first,
8387
Desc(opHelper, "getTable", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE, Type.INT_TYPE)) to first,
8488
Desc(opHelper, "getTable", Type.getMethodDescriptor(LUA_VALUE, LUA_STATE, LUA_VALUE, LUA_VALUE)) to first,
8589
Desc(opHelper, "setTable", Type.getMethodDescriptor(Type.VOID_TYPE, LUA_STATE, LUA_VALUE, Type.INT_TYPE, LUA_VALUE)) to drop,

build.gradle.kts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "cc.tweaked"
9-
version = "0.9.1"
9+
version = "0.9.5"
1010

1111
java {
1212
toolchain {
@@ -149,29 +149,29 @@ publishing {
149149
pom {
150150
name = "Cobalt"
151151
description = "A reentrant fork of LuaJ for Lua 5.2"
152-
url = "https://github.com/SquidDev/Cobalt"
152+
url = "https://github.com/cc-tweaked/Cobalt"
153153

154154
scm {
155-
url = "https://github.com/SquidDev/Cobalt.git"
155+
url = "https://github.com/cc-tweaked/Cobalt.git"
156156
}
157157

158158
issueManagement {
159159
system = "github"
160-
url = "https://github.com/SquidDev/Cobalt/issues"
160+
url = "https://github.com/cc-tweaked/Cobalt/issues"
161161
}
162162

163163
licenses {
164164
license {
165165
name = "MIT"
166-
url = "https://github.com/SquidDev/Cobalt/blob/master/LICENSE"
166+
url = "https://github.com/cc-tweaked/Cobalt/blob/master/LICENSE"
167167
}
168168
}
169169
}
170170
}
171171
}
172172

173173
repositories {
174-
maven("https://squiddev.cc/maven") {
174+
maven("https://maven.squiddev.cc") {
175175
name = "SquidDev"
176176
credentials(PasswordCredentials::class)
177177
}

src/main/java/cc/tweaked/cobalt/internal/doubles/CharBuffer.java

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

src/main/java/cc/tweaked/cobalt/internal/doubles/DoubleToStringConverter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
package cc.tweaked.cobalt.internal.doubles;
3333

3434
import org.checkerframework.checker.signedness.qual.Unsigned;
35+
import org.squiddev.cobalt.Buffer;
3536

3637
import static cc.tweaked.cobalt.internal.doubles.Assert.requireArg;
3738

@@ -120,7 +121,7 @@ private DoubleToStringConverter() {
120121
* If either of them is NULL or the value is not special then the
121122
* function returns false.
122123
*/
123-
private static void handleSpecialValues(double value, FormatOptions fo, CharBuffer resultBuilder) {
124+
private static void handleSpecialValues(double value, FormatOptions fo, Buffer resultBuilder) {
124125
boolean sign = value < 0.0;
125126

126127
int effectiveWidth = fo.width();
@@ -164,7 +165,7 @@ private static void createExponentialRepresentation(
164165
int length,
165166
int exponent,
166167
FormatOptions fo,
167-
CharBuffer resultBuilder
168+
Buffer resultBuilder
168169
) {
169170
requireArg(decimalDigits.length() != 0, "decimalDigits must not be empty");
170171
requireArg(length <= decimalDigits.length(), "length must be smaller than decimalDigits");
@@ -246,7 +247,7 @@ private static void createDecimalRepresentation(
246247
double value,
247248
int digitsAfterPoint,
248249
FormatOptions fo,
249-
CharBuffer resultBuilder
250+
Buffer resultBuilder
250251
) {
251252
int decimalPoint = decimalDigits.getPointPosition();
252253
int digitsLength = decimalDigits.length();
@@ -395,7 +396,7 @@ private static int calculateExpWidth(
395396
* MAX_FIXED_DIGITS_AFTER_POINT</code><br/>
396397
* characters (one additional character for the sign, and one for the decimal point).
397398
*/
398-
public static void toFixed(double value, int requestedDigits, FormatOptions formatOptions, CharBuffer resultBuilder) {
399+
public static void toFixed(double value, int requestedDigits, FormatOptions formatOptions, Buffer resultBuilder) {
399400
// DOUBLE_CONVERSION_ASSERT(MAX_FIXED_DIGITS_BEFORE_POINT == 60);
400401

401402
if (Doubles.isSpecial(value)) {
@@ -442,7 +443,7 @@ public static void toFixed(double value, int requestedDigits, FormatOptions form
442443
* @param formatOptions
443444
* @throws IllegalArgumentException if <code>requestedDigits > MAX_EXPONENTIAL_DIGITS</code>
444445
*/
445-
public static void toExponential(double value, int requestedDigits, FormatOptions formatOptions, CharBuffer resultBuilder) {
446+
public static void toExponential(double value, int requestedDigits, FormatOptions formatOptions, Buffer resultBuilder) {
446447
if (Doubles.isSpecial(value)) {
447448
handleSpecialValues(value, formatOptions, resultBuilder);
448449
return;
@@ -512,7 +513,7 @@ public static void toExponential(double value, int requestedDigits, FormatOption
512513
* @throws IllegalArgumentException when <code>precision < MIN_PRECISION_DIGITS</code> or
513514
* <code>precision > MAX_PRECISION_DIGITS</code>
514515
*/
515-
public static void toPrecision(double value, int precision, FormatOptions formatOptions, CharBuffer resultBuilder) {
516+
public static void toPrecision(double value, int precision, FormatOptions formatOptions, Buffer resultBuilder) {
516517
if (Doubles.isSpecial(value)) {
517518
handleSpecialValues(value, formatOptions, resultBuilder);
518519
return;
@@ -562,7 +563,7 @@ public static void toPrecision(double value, int precision, FormatOptions format
562563
* @param formatOptions Additional options for this number's formatting.
563564
* @param resultBuilder The buffer to output to.
564565
*/
565-
public static void toHex(double value, int requestedDigits, FormatOptions formatOptions, CharBuffer resultBuilder) {
566+
public static void toHex(double value, int requestedDigits, FormatOptions formatOptions, Buffer resultBuilder) {
566567
if (Doubles.isSpecial(value)) {
567568
handleSpecialValues(value, formatOptions, resultBuilder);
568569
return;
@@ -651,7 +652,7 @@ private static boolean shouldEmitMinus(double value) {
651652
return (Double.doubleToRawLongBits(value) & Doubles.SIGN_MASK) != 0 && value != 0.0;
652653
}
653654

654-
private static void appendSign(double value, FormatOptions formatOptions, CharBuffer resultBuilder) {
655+
private static void appendSign(double value, FormatOptions formatOptions, Buffer resultBuilder) {
655656
if (shouldEmitMinus(value)) {
656657
resultBuilder.append('-');
657658
} else if (formatOptions.spaceWhenPositive()) {
@@ -771,7 +772,7 @@ static void doubleToAscii(double v, DtoaMode mode, int requestedDigits, DecimalR
771772
* Add character padding to the builder. If count is non-positive,
772773
* nothing is added to the builder.
773774
*/
774-
private static void addPadding(CharBuffer sb, char character, int count) {
775+
private static void addPadding(Buffer sb, char character, int count) {
775776
for (int i = count; i > 0; i--) sb.append(character);
776777
}
777778

src/main/java/cc/tweaked/cobalt/internal/string/NumberParser.java

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,37 @@ public final class NumberParser {
1111
private NumberParser() {
1212
}
1313

14-
public static double parse(byte[] bytes, int offset, int length, int base) {
15-
int index = offset, end = offset + length;
16-
while (index < end && StringLib.isWhitespace(bytes[index])) index++;
17-
while (index < end && StringLib.isWhitespace(bytes[end - 1])) end--;
14+
public static double parse(byte[] bytes, int start, int length, int base) {
15+
int end = start + length;
16+
while (start < end && StringLib.isWhitespace(bytes[start])) start++;
17+
while (start < end && StringLib.isWhitespace(bytes[end - 1])) end--;
1818

19+
var originalStart = start;
1920
boolean isNeg = false;
20-
if (index < end) {
21-
switch (bytes[index]) {
22-
case '+' -> index++;
21+
if (start < end) {
22+
switch (bytes[start]) {
23+
case '+' -> start++;
2324
case '-' -> {
24-
index++;
25+
start++;
2526
isNeg = true;
2627
}
2728
}
2829
}
2930

30-
if (index >= end) return Double.NaN;
31+
if (start >= end) return Double.NaN;
3132

32-
if ((base == 10 || base == 16) && (bytes[index] == '0' && index + 1 < end && (bytes[index + 1] == 'x' || bytes[index + 1] == 'X'))) {
33+
if ((base == 10 || base == 16) && (bytes[start] == '0' && start + 1 < end && (bytes[start + 1] == 'x' || bytes[start + 1] == 'X'))) {
3334
base = 16;
34-
index += 2;
35-
36-
if (index >= end) return Double.NaN;
37-
}
35+
start += 2;
3836

39-
double value = scanLong(base, bytes, index, end);
40-
if (Double.isNaN(value)) {
41-
value = switch (base) {
42-
case 10 -> scanDouble(bytes, index, end);
43-
case 16 -> scanHexDouble(bytes, index, end);
44-
default -> Double.NaN;
45-
};
37+
if (start >= end) return Double.NaN;
4638
}
4739

48-
return isNeg ? -value : value;
40+
return switch (base) {
41+
case 10 -> scanDouble(bytes, originalStart, end);
42+
case 16 -> scanHexDouble(bytes, start, end, isNeg);
43+
default -> scanLong(base, bytes, start, end, isNeg);
44+
};
4945
}
5046

5147
/**
@@ -54,11 +50,12 @@ public static double parse(byte[] bytes, int offset, int length, int base) {
5450
* @param base the base to use, such as 10
5551
* @param start the index to start searching from
5652
* @param end the first index beyond the search range
53+
* @param isNeg Whether this is a negative value.
5754
* @return double value if conversion is valid,
5855
* or Double.NaN if not
5956
*/
60-
private static double scanLong(int base, byte[] bytes, int start, int end) {
61-
long x = 0;
57+
private static double scanLong(int base, byte[] bytes, int start, int end, boolean isNeg) {
58+
double x = 0;
6259
for (int i = start; i < end; i++) {
6360
var chr = bytes[i];
6461
int digit;
@@ -73,7 +70,7 @@ private static double scanLong(int base, byte[] bytes, int start, int end) {
7370
if (digit >= base) return Double.NaN;
7471
x = x * base + digit;
7572
}
76-
return x;
73+
return isNeg ? -x : x;
7774
}
7875

7976
/**
@@ -85,31 +82,11 @@ private static double scanLong(int base, byte[] bytes, int start, int end) {
8582
* or Double.NaN if not
8683
*/
8784
private static double scanDouble(byte[] bytes, int start, int end) {
88-
for (int i = start; i < end; i++) {
89-
switch (bytes[i]) {
90-
case '-':
91-
case '+':
92-
case '.':
93-
case 'e':
94-
case 'E':
95-
case '0':
96-
case '1':
97-
case '2':
98-
case '3':
99-
case '4':
100-
case '5':
101-
case '6':
102-
case '7':
103-
case '8':
104-
case '9':
105-
break;
106-
default:
107-
return Double.NaN;
108-
}
109-
}
11085
char[] c = new char[end - start];
11186
for (int i = start; i < end; i++) {
112-
c[i - start] = (char) bytes[i];
87+
var b = bytes[i];
88+
if (!isValidDoubleCharacter(b)) return Double.NaN;
89+
c[i - start] = (char) b;
11390
}
11491
try {
11592
return Double.parseDouble(String.valueOf(c));
@@ -118,8 +95,12 @@ private static double scanDouble(byte[] bytes, int start, int end) {
11895
}
11996
}
12097

121-
private static double scanHexDouble(byte[] bytes, int index, int end) {
122-
long result = 0; // The mantissa
98+
private static boolean isValidDoubleCharacter(byte b) {
99+
return (b >= '0' && b <= '9') || b == '+' || b == '-' || b == '.' || b == 'E' || b == 'e';
100+
}
101+
102+
private static double scanHexDouble(byte[] bytes, int index, int end, boolean isNeg) {
103+
double result = 0; // The mantissa
123104
int exponent = 0;
124105

125106
int sigDigits = 0, nonSigDigits = 0; // Number of significant digits and non-significant digits (leading 0s).
@@ -187,6 +168,7 @@ private static double scanHexDouble(byte[] bytes, int index, int end) {
187168
exponent += givenExponent;
188169
}
189170

190-
return Math.scalb((double) result, exponent);
171+
if (isNeg) result = -result;
172+
return Math.scalb(result, exponent);
191173
}
192174
}

src/main/java/org/squiddev/cobalt/Buffer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
*/
2525
package org.squiddev.cobalt;
2626

27-
import cc.tweaked.cobalt.internal.doubles.CharBuffer;
28-
2927
/**
3028
* String buffer for use in string library methods, optimized for producing {@link LuaString} instances.
3129
*
3230
* @see LuaValue
3331
* @see LuaString
3432
*/
35-
public final class Buffer implements CharBuffer {
33+
public final class Buffer {
3634
/**
3735
* Default capacity for a buffer: 64
3836
*/
@@ -181,7 +179,6 @@ public Buffer append(LuaString str, int start, int srcLength) {
181179
* @return {@code this}, for chaining.
182180
* @see LuaString#encode(String, byte[], int)
183181
*/
184-
@Override
185182
public Buffer append(String str) {
186183
final int n = str.length();
187184
ensure(n);

0 commit comments

Comments
 (0)