Improve resource loading contributed by Jaroslav:

* Implement base64 decoder instead of atob, which is not supported 
  by IE9
* Avoid direct usage of window identifier to prevent global namespace
  from spoiling
* Make customizable approach to supply list of resources to write
  into JavaScript
This commit is contained in:
Alexey Andreev 2015-12-26 13:26:34 +03:00
parent de7f0910e9
commit d718177fe0
22 changed files with 401 additions and 20 deletions

View File

@ -0,0 +1,11 @@
package org.teavm.classlib;
import org.teavm.model.ListableClassReaderSource;
/**
*
* @author Alexey Andreev
*/
public interface ResourceSupplier {
String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource);
}

View File

@ -0,0 +1,87 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.classlib.impl;
import java.util.Arrays;
/**
*
* @author Alexey Andreev
*/
public class Base64 {
private static char[] alphabet = new char[64];
private static int[] reverse = new int[256];
static {
int i = 0;
for (char c = 'A'; c <= 'Z'; ++c) {
alphabet[i++] = c;
}
for (char c = 'a'; c <= 'z'; ++c) {
alphabet[i++] = c;
}
for (char c = '0'; c <= '9'; ++c) {
alphabet[i++] = c;
}
alphabet[i++] = '+';
alphabet[i++] = '/';
Arrays.fill(reverse, -1);
for (i = 0; i < alphabet.length; ++i) {
reverse[alphabet[i]] = i;
}
}
public static byte[] decode(String text) {
int outputSize = ((text.length() - 1) / 4 + 1) * 3;
int i, j;
for (i = text.length() - 1; i >= 0 && text.charAt(i) == '='; --i) {
--outputSize;
}
byte[] output = new byte[outputSize];
int triples = (outputSize / 3) * 3;
i = 0;
for (j = 0; i < triples;) {
int a = decode(text.charAt(i++));
int b = decode(text.charAt(i++));
int c = decode(text.charAt(i++));
int d = decode(text.charAt(i++));
int out = (a << 18) | (b << 12) | (c << 6) | d;
output[j++] = (byte) (out >>> 16);
output[j++] = (byte) (out >>> 8);
output[j++] = (byte) (out);
}
int rem = output.length - j;
if (rem == 1) {
int a = decode(text.charAt(i));
int b = decode(text.charAt(i + 1));
output[j] = (byte) ((a << 2) | (b >>> 4));
} else if (rem == 2) {
int a = decode(text.charAt(i));
int b = decode(text.charAt(i + 1));
int c = decode(text.charAt(i + 2));
output[j] = (byte) ((a << 2) | (b >>> 4));
output[j + 1] = (byte) ((b << 4) | (c >>> 2));
}
return output;
}
private static int decode(char c) {
return c < 256 ? reverse[c] : -1;
}
}

View File

@ -0,0 +1,68 @@
package org.teavm.classlib.java.lang;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.teavm.classlib.ResourceSupplier;
import org.teavm.codegen.SourceWriter;
import org.teavm.javascript.Renderer;
import org.teavm.javascript.spi.Injector;
import org.teavm.javascript.spi.InjectorContext;
import org.teavm.model.MethodReference;
/**
*
* @author Alexey Andreev
*/
public class ClassLoaderNativeGenerator implements Injector {
@Override
public void generate(InjectorContext context, MethodReference methodRef) throws IOException {
switch (methodRef.getName()) {
case "supplyResources":
generateSupplyResources(context);
break;
}
}
private void generateSupplyResources(InjectorContext context) throws IOException {
SourceWriter writer = context.getWriter();
writer.append("{").indent();
ClassLoader classLoader = context.getClassLoader();
Set<String> resourceSet = new HashSet<>();
for (ResourceSupplier supplier : ServiceLoader.load(ResourceSupplier.class, classLoader)) {
String[] resources = supplier.supplyResources(classLoader, context.getClassSource());
if (resources != null) {
resourceSet.addAll(Arrays.asList(resources));
}
}
boolean first = true;
for (String resource : resourceSet) {
try (InputStream input = classLoader.getResourceAsStream(resource)) {
if (input == null) {
continue;
}
if (!first) {
writer.append(',');
}
first = false;
writer.newLine();
String data = Base64.getEncoder().encodeToString(IOUtils.toByteArray(input));
writer.append("\"").append(Renderer.escapeString(resource)).append("\"");
writer.ws().append(':').ws();
writer.append("\"").append(data).append("\"");
}
}
if (!first) {
writer.newLine();
}
writer.outdent().append('}');
}
}

View File

@ -15,15 +15,12 @@
*/
package org.teavm.classlib.java.lang;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.teavm.classlib.impl.DeclaringClassMetadataGenerator;
import org.teavm.classlib.java.lang.annotation.TAnnotation;
import org.teavm.classlib.java.lang.reflect.TAnnotatedElement;
import org.teavm.jso.JSBody;
import org.teavm.platform.Platform;
import org.teavm.platform.PlatformClass;
import org.teavm.platform.metadata.ClassResource;
@ -270,23 +267,21 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
}
}
@JSBody(params = "res", script =
"if (!window.teaVMResources) return null;\n"
+ "var data = window.teaVMResources[res];\n"
+ "return data ? window.atob(data) : null;\n"
)
private static native String readResource(String message);
public InputStream getResourceAsStream(String name) {
TString clazzName = getName();
int lastDot = clazzName.lastIndexOf('.');
String resName;
if (lastDot == -1) {
resName = name;
} else {
resName = clazzName.substring(0, lastDot).replace('.', '/') + "/" + name;
if (name.startsWith("/")) {
return getClassLoader().getResourceAsStream(name.substring(1));
}
String data = readResource(resName);
return data == null ? null : new ByteArrayInputStream(data.getBytes());
TClass<?> cls = this;
while (cls.isArray()) {
cls = cls.getComponentType();
}
String prefix = cls.getName().toString();
int index = prefix.lastIndexOf('.');
if (index >= 0) {
name = prefix.substring(0, index + 1).replace('.', '/') + name;
}
return getClassLoader().getResourceAsStream(name);
}
}

View File

@ -15,6 +15,13 @@
*/
package org.teavm.classlib.java.lang;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.teavm.classlib.impl.Base64;
import org.teavm.javascript.spi.InjectedBy;
import org.teavm.jso.JSIndexer;
import org.teavm.jso.JSObject;
/**
*
* @author Alexey Andreev
@ -22,6 +29,7 @@ package org.teavm.classlib.java.lang;
public abstract class TClassLoader extends TObject {
private TClassLoader parent;
private static TSystemClassLoader systemClassLoader = new TSystemClassLoader();
private static ResourceContainer resources;
protected TClassLoader() {
this(null);
@ -38,4 +46,20 @@ public abstract class TClassLoader extends TObject {
public static TClassLoader getSystemClassLoader() {
return systemClassLoader;
}
public InputStream getResourceAsStream(String name) {
if (resources == null) {
resources = supplyResources();
}
String data = resources.getResource(name);
return data == null ? null : new ByteArrayInputStream(Base64.decode(data));
}
@InjectedBy(ClassLoaderNativeGenerator.class)
private static native ResourceContainer supplyResources();
static interface ResourceContainer extends JSObject {
@JSIndexer
String getResource(String name);
}
}

View File

@ -0,0 +1,45 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.classlib.impl;
import static org.junit.Assert.*;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
/**
*
* @author Alexey Andreev
*/
public class Base64Test {
@Test
public void decoderWorks() {
assertEquals("q", decode("cQ=="));
assertEquals("qw", decode("cXc="));
assertEquals("qwe", decode("cXdl"));
assertEquals("qwer", decode("cXdlcg=="));
assertEquals("qwert", decode("cXdlcnQ="));
assertEquals("qwerty", decode("cXdlcnR5"));
assertEquals("qwertyu", decode("cXdlcnR5dQ=="));
}
private String decode(String text) {
try {
return new String(Base64.decode(text), "UTF-8");
} catch (UnsupportedEncodingException e) {
return "";
}
}
}

View File

@ -2377,6 +2377,16 @@ public class Renderer implements ExprVisitor, StatementVisitor, RenderingContext
public Precedence getPrecedence() {
return precedence;
}
@Override
public ClassLoader getClassLoader() {
return classLoader;
}
@Override
public ListableClassReaderSource getClassSource() {
return classSource;
}
}
@Override

View File

@ -21,6 +21,7 @@ import org.teavm.codegen.SourceWriter;
import org.teavm.common.ServiceRepository;
import org.teavm.javascript.Precedence;
import org.teavm.javascript.ast.Expr;
import org.teavm.model.ListableClassReaderSource;
import org.teavm.model.ValueType;
/**
@ -47,4 +48,8 @@ public interface InjectorContext extends ServiceRepository {
void writeExpr(Expr expr, Precedence precedence) throws IOException;
Precedence getPrecedence();
ClassLoader getClassLoader();
ListableClassReaderSource getClassSource();
}

View File

@ -29,6 +29,5 @@ public class HTML4JPlugin implements TeaVMPlugin {
host.add(new JavaScriptBodyTransformer());
host.add(new JCLHacks());
host.add(new JavaScriptResourceInterceptor());
host.add(new ResourcesInterceptor());
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.html4j;
import java.util.HashSet;
import java.util.Set;
import org.teavm.classlib.ResourceSupplier;
import org.teavm.model.ListableClassReaderSource;
/**
*
* @author Alexey Andreev
*/
public class HTML4jResourceSupplier implements ResourceSupplier {
@Override
public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) {
Set<String> resources = new HashSet<>();
for (String className : classSource.getClassNames()) {
final int lastDot = className.lastIndexOf('.');
if (lastDot == -1) {
continue;
}
String packageName = className.substring(0, lastDot);
String resourceName = packageName.replace('.', '/') + "/" + "jvm.txt";
resources.add(resourceName);
}
return resources.toArray(new String[0]);
}
}

View File

@ -0,0 +1 @@
org.teavm.html4j.HTML4jResourceSupplier

View File

@ -0,0 +1,50 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.classlib.java.lang;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.junit.Test;
/**
*
* @author Alexey Andreev
*/
public class ClassLoaderTest {
@Test
public void loadsResources() {
assertEquals("q", loadResource("1"));
assertEquals("qw", loadResource("2"));
assertEquals("qwe", loadResource("3"));
assertEquals("qwer", loadResource("4"));
assertEquals("qwert", loadResource("5"));
assertEquals("qwerty", loadResource("6"));
assertEquals("qwertyu", loadResource("7"));
assertEquals("qwertyui", loadResource("8"));
}
private static String loadResource(String name) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream(
"resources-for-test/" + name), "UTF-8"))) {
return reader.readLine();
} catch (IOException e) {
return "";
}
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2015 Alexey Andreev.
*
* 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.classlib.java.lang;
import org.teavm.classlib.ResourceSupplier;
import org.teavm.model.ListableClassReaderSource;
/**
*
* @author Alexey Andreev
*/
public class TestResourcesSupplier implements ResourceSupplier {
@Override
public String[] supplyResources(ClassLoader classLoader, ListableClassReaderSource classSource) {
String[] result = { "1", "2", "3", "4", "5", "6", "7", "8" };
for (int i = 0; i < result.length; ++i) {
result[i] = "resources-for-test/" + result[i];
}
return result;
}
}

View File

@ -0,0 +1 @@
org.teavm.classlib.java.lang.TestResourcesSupplier

View File

@ -0,0 +1 @@
q

View File

@ -0,0 +1 @@
qw

View File

@ -0,0 +1 @@
qwe

View File

@ -0,0 +1 @@
qwer

View File

@ -0,0 +1 @@
qwert

View File

@ -0,0 +1 @@
qwerty

View File

@ -0,0 +1 @@
qwertyu

View File

@ -0,0 +1 @@
qwertyui