Skip to content

Commit

Permalink
#859 Testcase for yavi Validation / Invoke Dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkosertic committed Apr 5, 2023
1 parent 88dcff3 commit 19ba37e
Show file tree
Hide file tree
Showing 16 changed files with 709 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,10 @@ public static byte getByteArrayEntry(final byte[] arr, final int index) {
public static void setByteArrayEntry(final byte[] arr, final int index, final byte value) {
arr[index] = value;
}

@Export("getIntArrayEntry")
public static int getIntArrayEntry(final int[] arr, final int index) {
return arr[index];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,34 @@ public static boolean nullsafeEquals(final Object a, final Object b) {
}
return false;
}

@Export("toByte")
public static Byte toByte(final byte v) {
return Byte.valueOf(v);
}

@Export("toShort")
public static Short toShort(final short v) {
return Short.valueOf(v);
}

@Export("toInteger")
public static Integer toInteger(final int v) {
return Integer.valueOf(v);
}

@Export("toLong")
public static Long toLong(final long v) {
return Long.valueOf(v);
}

@Export("toFloat")
public static Float toFloat(final float v) {
return Float.valueOf(v);
}

@Export("toDouble")
public static Double toDouble(final float v) {
return Double.valueOf(v);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@

import de.mirkosertic.bytecoder.api.SubstitutesInClass;

import java.util.Locale;

@SubstitutesInClass(completeReplace = true)
public class TString implements CharSequence, Comparable<String> {

public TString() {
}

public TString(String value) {
public TString(final String value) {
this();
initializeWith(value);
}
Expand Down Expand Up @@ -53,12 +55,19 @@ public TString(final char[] data) {
initializeWith(data, 0, data.length);
}

public TString(final int[] data, final int offset, final int count) {
this();
initializeWith(data, offset, count);
}

native void initializeWith(byte[] data, int offset, int count, byte coder);

native void initializeWith(final String value);

native void initializeWith(char[] data, int offset, int count);

native void initializeWith(int[] data, int offset, int count);

public String toString() {
return (String) (Object) this;
}
Expand Down Expand Up @@ -158,6 +167,14 @@ public int compareTo(final String anotherString) {

public native boolean startsWith(final String value);

public native boolean endsWith(final String value);

public native String replaceAll(final String regex, final String replacement);

public int codePointCount(final int beginIndex, final int endIndex) {
return endIndex - beginIndex;
}

public native String trim();

public native int length();
Expand All @@ -184,5 +201,9 @@ public int hashCode() {

public native String toUpperCase();

public native String toUpperCase(final Locale locale);

public native String toLowerCase();

public native String toLowerCase(final Locale locale);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023 Mirko Sertic
*
* 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 de.mirkosertic.bytecoder.classlib.java.text;

import de.mirkosertic.bytecoder.api.SubstitutesInClass;

import java.text.Normalizer;

@SubstitutesInClass(completeReplace = true)
public class TNormalizer {

public static String normalize(final CharSequence charSequence, final Normalizer.Form form) {
// TODO: Check how this can be implemented
return charSequence.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2023 Mirko Sertic
*
* 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 de.mirkosertic.bytecoder.classlib.java.util.regex;

import de.mirkosertic.bytecoder.api.SubstitutesInClass;

import java.util.regex.Pattern;

@SubstitutesInClass(completeReplace = true)
public class TPattern {

private int flags;

private String pattern;

public static Pattern compile(final String regex) {
return compile(regex, 0);
}
public static Pattern compile(final String regex, final int flags) {
return (Pattern) (Object) new TPattern(flags, regex);
}

public TPattern(final int flags, final String pattern) {
this.flags = flags;
this.pattern = pattern;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ public JSCompileResult generateCodeFor(final CompileUnit compileUnit, final Comp
}
pw.println(" {");

generateFieldsFor(pw, compileUnit, cl);

generateClassInitFor(pw, compileUnit, cl);
generateInterfaceFieldsFor(pw, compileUnit, cl);

generateLambdaLogicFor(pw, compileUnit, cl);

Expand All @@ -136,8 +134,12 @@ public JSCompileResult generateCodeFor(final CompileUnit compileUnit, final Comp
pw.print(cl.classNode.access);
pw.println(";");

generateInterfaceClassInitFor(className, pw, compileUnit, cl);

generateStaticMethodsFor(className, pw, compileUnit, cl, compileOptions, generatedMethodsRegistry);

generateStaticInterfaceFieldsFor(className, pw, compileUnit, cl);

pw.println();
} else {
pw.print("class ");
Expand Down Expand Up @@ -262,7 +264,6 @@ private void generateLambdaLogicFor(final PrintWriter pw, final CompileUnit comp
}

private void generateClassInitFor(final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl) {

pw.println();
pw.println(" static #rt = undefined;");
pw.println(" static get $rt() {");
Expand Down Expand Up @@ -309,6 +310,60 @@ private void generateClassInitFor(final PrintWriter pw, final CompileUnit compil
}
}

private void generateInterfaceClassInitFor(final String prefix, final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl) {
pw.print(prefix);
pw.println("._rt = null;");
pw.print("Object.defineProperty(");
pw.print(prefix);
pw.println(",'$rt', {");
pw.println(" get() {");
pw.println(" if (!this._rt) {");
pw.print(" this._rt = bytecoder.newRuntimeClassFor(");
pw.print(generateClassName(cl.type));
pw.print(",[");
boolean f = true;
for (final ResolvedClass type : cl.allTypesOf()) {
if (f) {
f = false;
} else {
pw.print(",");
}
pw.print(generateClassName(type.type));
}
pw.println("]);");
pw.println(" }");
pw.println(" return this._rt;");
pw.println(" }");
pw.println("});");

if (cl.requiresClassInitializer()) {
pw.println();
pw.print(prefix);
pw.println("._iguard = false;");
pw.print("Object.defineProperty(");
pw.print(prefix);
pw.println(",'$i', {");
pw.println(" get() {");
pw.println(" if (!this._iguard) {");
pw.println(" this._iguard = true;");
if (cl.superClass != null && cl.superClass.requiresClassInitializer()) {
pw.print(" ");
pw.print(generateClassName(cl.superClass.type));
pw.println(".$i;");
}

if (cl.classInitializer != null) {
pw.print(" this.");
pw.print(generateMethodName("<clinit>", cl.classInitializer.methodType));
pw.println("();");
}
pw.println(" }");
pw.println(" return this;");
pw.println(" }");
pw.println("});");
}
}

private void generateFieldsFor(final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl) {

pw.println(" nativeObject = null;");
Expand Down Expand Up @@ -345,6 +400,73 @@ private void generateFieldsFor(final PrintWriter pw, final CompileUnit compileUn
}
}

private void generateInterfaceFieldsFor(final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl) {

pw.println(" nativeObject = null;");

if (!cl.resolvedFields.isEmpty()) {
pw.println();
for (final ResolvedField f : cl.resolvedFields) {
if (!Modifier.isStatic(f.access)) {
pw.print(" ");
pw.print(generateFieldName(f.name));
switch (f.type.getSort()) {
case Type.FLOAT:
case Type.DOUBLE:
pw.print(" = 0.0");
break;
case Type.BOOLEAN:
pw.print(" = false");
break;
case Type.ARRAY:
case Type.OBJECT:
case Type.METHOD:
pw.print(" = null");
break;
default:
pw.print(" = 0");
break;
}
pw.println(";");
}
}

pw.println();
}
}

private void generateStaticInterfaceFieldsFor(final String prefix, final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl) {
if (!cl.resolvedFields.isEmpty()) {
for (final ResolvedField f : cl.resolvedFields) {
if (!Modifier.isStatic(f.access)) {
pw.print(prefix);
pw.print(".");
pw.print(generateFieldName(f.name));
switch (f.type.getSort()) {
case Type.FLOAT:
case Type.DOUBLE:
pw.print(" = 0.0");
break;
case Type.BOOLEAN:
pw.print(" = false");
break;
case Type.ARRAY:
case Type.OBJECT:
case Type.METHOD:
pw.print(" = null");
break;
default:
pw.print(" = 0");
break;
}
pw.println(";");
}
}

pw.println();
}
}

public void generateMethodsFor(final PrintWriter pw, final CompileUnit compileUnit, final ResolvedClass cl, final CompileOptions compileOptions, final GeneratedMethodsRegistry generatedMethodsRegistry) {

for (final ResolvedMethod m : cl.resolvedMethods) {
Expand Down
Loading

0 comments on commit 19ba37e

Please sign in to comment.