mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
C: support passing strings as char16_t*, fix bugs under msvc
This commit is contained in:
parent
d01ba897c0
commit
9642eeec0a
|
@ -24,7 +24,11 @@ import org.teavm.model.MethodReference;
|
||||||
public class MathNativeGenerator implements Generator {
|
public class MathNativeGenerator implements Generator {
|
||||||
@Override
|
@Override
|
||||||
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
|
public void generate(GeneratorContext context, SourceWriter writer, MethodReference methodRef) throws IOException {
|
||||||
function(context, writer, "Math." + methodRef.getName(), methodRef.parameterCount());
|
String name = methodRef.getName();
|
||||||
|
if (name.equals("randomImpl")) {
|
||||||
|
name = "random";
|
||||||
|
}
|
||||||
|
function(context, writer, "Math." + name, methodRef.parameterCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void function(GeneratorContext context, SourceWriter writer, String name, int paramCount)
|
private void function(GeneratorContext context, SourceWriter writer, String name, int paramCount)
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
package org.teavm.classlib.java.lang;
|
package org.teavm.classlib.java.lang;
|
||||||
|
|
||||||
import org.teavm.backend.javascript.spi.GeneratedBy;
|
import org.teavm.backend.javascript.spi.GeneratedBy;
|
||||||
|
import org.teavm.classlib.PlatformDetector;
|
||||||
import org.teavm.interop.Import;
|
import org.teavm.interop.Import;
|
||||||
import org.teavm.interop.NoSideEffects;
|
import org.teavm.interop.NoSideEffects;
|
||||||
import org.teavm.interop.Unmanaged;
|
import org.teavm.interop.Unmanaged;
|
||||||
|
@ -126,10 +127,17 @@ public final class TMath extends TObject {
|
||||||
return (long) (a + signum(a) * 0.5);
|
return (long) (a + signum(a) * 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Unmanaged
|
||||||
|
public static double random() {
|
||||||
|
return PlatformDetector.isC() ? randomC() : randomImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Import(name = "teavm_rand")
|
||||||
|
private static native double randomC();
|
||||||
|
|
||||||
@GeneratedBy(MathNativeGenerator.class)
|
@GeneratedBy(MathNativeGenerator.class)
|
||||||
@Import(module = "teavmMath", name = "random")
|
@Import(module = "teavmMath", name = "random")
|
||||||
@Unmanaged
|
private static native double randomImpl();
|
||||||
public static native double random();
|
|
||||||
|
|
||||||
public static int min(int a, int b) {
|
public static int min(int a, int b) {
|
||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
|
|
|
@ -244,7 +244,7 @@ public class ClassGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void generateStringPool(ValueType type) {
|
private void generateStringPool(ValueType type) {
|
||||||
if (!context.isIncremental()) {
|
if (!context.isIncremental() || context.getStringPool().getStrings().isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,7 +70,9 @@ import org.teavm.backend.c.intrinsic.IntrinsicContext;
|
||||||
import org.teavm.backend.c.util.InteropUtil;
|
import org.teavm.backend.c.util.InteropUtil;
|
||||||
import org.teavm.diagnostics.Diagnostics;
|
import org.teavm.diagnostics.Diagnostics;
|
||||||
import org.teavm.interop.Address;
|
import org.teavm.interop.Address;
|
||||||
|
import org.teavm.interop.c.Char16;
|
||||||
import org.teavm.interop.c.Variable;
|
import org.teavm.interop.c.Variable;
|
||||||
|
import org.teavm.model.AnnotationContainerReader;
|
||||||
import org.teavm.model.CallLocation;
|
import org.teavm.model.CallLocation;
|
||||||
import org.teavm.model.ClassReader;
|
import org.teavm.model.ClassReader;
|
||||||
import org.teavm.model.ClassReaderSource;
|
import org.teavm.model.ClassReaderSource;
|
||||||
|
@ -558,8 +560,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
temporaries.add(allocTemporaryVariable(parameterTypeForCall(method, i)));
|
temporaries.add(allocTemporaryVariable(parameterTypeForCall(method, i)));
|
||||||
}
|
}
|
||||||
boolean stringResult = method.getResultType().isObject(String.class);
|
boolean stringResult = method.getResultType().isObject(String.class);
|
||||||
|
boolean string16Result = method.getAnnotations().get(Char16.class.getName()) != null;
|
||||||
|
|
||||||
writer.print("(");
|
writer.print("(");
|
||||||
|
AnnotationContainerReader[] parameterAnnotations = method.getParameterAnnotations();
|
||||||
|
|
||||||
for (int i = 0; i < arguments.size(); ++i) {
|
for (int i = 0; i < arguments.size(); ++i) {
|
||||||
String tmp = temporaries.get(i);
|
String tmp = temporaries.get(i);
|
||||||
writer.print(tmp + " = ");
|
writer.print(tmp + " = ");
|
||||||
|
@ -567,7 +572,11 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
? method.parameterType(i)
|
? method.parameterType(i)
|
||||||
: i == 0 ? ValueType.object(method.getOwnerName()) : method.parameterType(i - 1);
|
: i == 0 ? ValueType.object(method.getOwnerName()) : method.parameterType(i - 1);
|
||||||
if (type.isObject(String.class)) {
|
if (type.isObject(String.class)) {
|
||||||
writer.print("teavm_stringToC(");
|
int annotIndex = method.hasModifier(ElementModifier.STATIC) ? i : i - 1;
|
||||||
|
boolean is16Char = annotIndex >= 0
|
||||||
|
&& parameterAnnotations[annotIndex].get(Char16.class.getName()) != null;
|
||||||
|
String functionName = is16Char ? "teavm_stringToC16" : "teavm_stringToC";
|
||||||
|
writer.print(functionName).print("(");
|
||||||
arguments.get(i).acceptVisitor(this);
|
arguments.get(i).acceptVisitor(this);
|
||||||
writer.print(")");
|
writer.print(")");
|
||||||
stringTemporaries.add(tmp);
|
stringTemporaries.add(tmp);
|
||||||
|
@ -616,7 +625,8 @@ public class CodeGenerationVisitor implements ExprVisitor, StatementVisitor {
|
||||||
if (resultTmp != null) {
|
if (resultTmp != null) {
|
||||||
writer.print(", ");
|
writer.print(", ");
|
||||||
if (stringResult) {
|
if (stringResult) {
|
||||||
writer.print("teavm_cToString(");
|
String functionName = string16Result ? "teavm_c16ToString" : "teavm_cToString";
|
||||||
|
writer.print(functionName).print("(");
|
||||||
}
|
}
|
||||||
writer.print(resultTmp);
|
writer.print(resultTmp);
|
||||||
if (stringResult) {
|
if (stringResult) {
|
||||||
|
|
|
@ -70,8 +70,10 @@ public final class CodeGeneratorUtil {
|
||||||
writer.print("INFINITY");
|
writer.print("INFINITY");
|
||||||
} else if (Float.isNaN(f)) {
|
} else if (Float.isNaN(f)) {
|
||||||
writer.print("NAN");
|
writer.print("NAN");
|
||||||
} else {
|
} else if ((int) f == f) {
|
||||||
writer.print(f + "f");
|
writer.print(f + "f");
|
||||||
|
} else {
|
||||||
|
writer.print(Float.toHexString(f) + "f");
|
||||||
}
|
}
|
||||||
} else if (value instanceof Double) {
|
} else if (value instanceof Double) {
|
||||||
double d = (Double) value;
|
double d = (Double) value;
|
||||||
|
@ -82,8 +84,10 @@ public final class CodeGeneratorUtil {
|
||||||
writer.print("INFINITY");
|
writer.print("INFINITY");
|
||||||
} else if (Double.isNaN(d)) {
|
} else if (Double.isNaN(d)) {
|
||||||
writer.print("NAN");
|
writer.print("NAN");
|
||||||
} else {
|
} else if ((int) d == d) {
|
||||||
writer.print(value.toString());
|
writer.print(value.toString());
|
||||||
|
} else {
|
||||||
|
writer.print(Double.toHexString(d));
|
||||||
}
|
}
|
||||||
} else if (value instanceof Boolean) {
|
} else if (value instanceof Boolean) {
|
||||||
writer.print((Boolean) value ? "1" : "0");
|
writer.print((Boolean) value ? "1" : "0");
|
||||||
|
|
|
@ -41,6 +41,18 @@ public class StringsIntrinsic implements Intrinsic {
|
||||||
context.emit(invocation.getArguments().get(0));
|
context.emit(invocation.getArguments().get(0));
|
||||||
context.writer().print(")");
|
context.writer().print(")");
|
||||||
break;
|
break;
|
||||||
|
case "toC16": {
|
||||||
|
Expr arg = invocation.getArguments().get(0);
|
||||||
|
context.writer().print("teavm_stringToC16(");
|
||||||
|
context.emit(arg);
|
||||||
|
context.writer().print(")");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "fromC16":
|
||||||
|
context.writer().print("teavm_c16ToString(");
|
||||||
|
context.emit(invocation.getArguments().get(0));
|
||||||
|
context.writer().print(")");
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -192,6 +192,14 @@ int64_t teavm_currentTimeMillis() {
|
||||||
uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32);
|
uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32);
|
||||||
return (int64_t) ((current - teavm_unixTimeOffset) / 10000);
|
return (int64_t) ((current - teavm_unixTimeOffset) / 10000);
|
||||||
}
|
}
|
||||||
|
int64_t teavm_currentTimeNano() {
|
||||||
|
SYSTEMTIME time;
|
||||||
|
FILETIME fileTime;
|
||||||
|
GetSystemTime(&time);
|
||||||
|
SystemTimeToFileTime(&time, &fileTime);
|
||||||
|
uint64_t current = fileTime.dwLowDateTime | ((uint64_t) fileTime.dwHighDateTime << 32);
|
||||||
|
return (int64_t) ((current - teavm_unixTimeOffset) / 10);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int32_t teavm_timeZoneOffset() {
|
int32_t teavm_timeZoneOffset() {
|
||||||
|
@ -353,6 +361,21 @@ char* teavm_stringToC(void* obj) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char16_t* teavm_stringToC16(void* obj) {
|
||||||
|
if (obj == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
TeaVM_String* javaString = (TeaVM_String*) obj;
|
||||||
|
TeaVM_Array* charArray = javaString->characters;
|
||||||
|
char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t);
|
||||||
|
size_t sz = charArray->size;
|
||||||
|
char16_t* result = malloc((sz + 1) * sizeof(char16_t));
|
||||||
|
memcpy(result, javaChars, sz * sizeof(char16_t));
|
||||||
|
result[sz] = 0;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
TeaVM_String* teavm_cToString(char* cstring) {
|
TeaVM_String* teavm_cToString(char* cstring) {
|
||||||
if (cstring == NULL) {
|
if (cstring == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -375,6 +398,21 @@ TeaVM_String* teavm_cToString(char* cstring) {
|
||||||
return teavm_createString(charArray);
|
return teavm_createString(charArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TeaVM_String* teavm_c16ToString(char16_t* cstring) {
|
||||||
|
if (cstring == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t size = 0;
|
||||||
|
while (cstring[size] == 0) {
|
||||||
|
++size;
|
||||||
|
}
|
||||||
|
TeaVM_Array* charArray = teavm_allocateCharArray(size);
|
||||||
|
char16_t* javaChars = TEAVM_ARRAY_DATA(charArray, char16_t);
|
||||||
|
memcpy(javaChars, cstring, size * sizeof(char16_t));
|
||||||
|
return teavm_createString(charArray);
|
||||||
|
}
|
||||||
|
|
||||||
char16_t* teavm_mbToChar16(char* cstring, int32_t* length) {
|
char16_t* teavm_mbToChar16(char* cstring, int32_t* length) {
|
||||||
size_t clen = strlen(cstring);
|
size_t clen = strlen(cstring);
|
||||||
int32_t size = teavm_c16Size(cstring, clen);
|
int32_t size = teavm_c16Size(cstring, clen);
|
||||||
|
|
|
@ -207,6 +207,8 @@ extern int32_t teavm_timeZoneOffset();
|
||||||
|
|
||||||
extern char* teavm_stringToC(void*);
|
extern char* teavm_stringToC(void*);
|
||||||
extern TeaVM_String* teavm_cToString(char*);
|
extern TeaVM_String* teavm_cToString(char*);
|
||||||
|
extern char16_t* teavm_stringToC16(void*);
|
||||||
|
extern TeaVM_String* teavm_c16ToString(char16_t*);
|
||||||
extern char16_t* teavm_mbToChar16(char*, int32_t*);
|
extern char16_t* teavm_mbToChar16(char*, int32_t*);
|
||||||
extern char* teavm_char16ToMb(char16_t*, int32_t);
|
extern char* teavm_char16ToMb(char16_t*, int32_t);
|
||||||
static inline void teavm_free(void* s) {
|
static inline void teavm_free(void* s) {
|
||||||
|
|
|
@ -22,4 +22,8 @@ public final class Strings {
|
||||||
public static native Address toC(String javaString);
|
public static native Address toC(String javaString);
|
||||||
|
|
||||||
public static native String fromC(Address cString);
|
public static native String fromC(Address cString);
|
||||||
|
|
||||||
|
public static native Address toC16(String javaString);
|
||||||
|
|
||||||
|
public static native String fromC16(Address cString);
|
||||||
}
|
}
|
||||||
|
|
26
interop/core/src/main/java/org/teavm/interop/c/Char16.java
Normal file
26
interop/core/src/main/java/org/teavm/interop/c/Char16.java
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2019 konsoletyper.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.teavm.interop.c;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
||||||
|
public @interface Char16 {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user