Move FS support to teavm-core module

This commit is contained in:
Alexey Andreev 2022-11-07 09:53:46 +01:00
parent bb087b7630
commit 3eb4b742ea
23 changed files with 127 additions and 64 deletions

View File

@ -23,10 +23,10 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Objects;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileSystem;
import org.teavm.classlib.fs.VirtualFileSystemProvider;
import org.teavm.classlib.java.util.TRandom;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileSystem;
import org.teavm.runtime.fs.VirtualFileSystemProvider;
public class TFile implements Serializable, Comparable<TFile> {
private String path;

View File

@ -19,8 +19,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class TFileInputStream extends InputStream {
private static final byte[] ONE_BYTE_BUFFER = new byte[1];

View File

@ -20,8 +20,8 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Objects;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class TFileOutputStream extends OutputStream {
private static final byte[] ONE_BYTE_BUFER = new byte[1];

View File

@ -23,10 +23,10 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UTFDataFormatException;
import java.util.Objects;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.classlib.java.lang.TIndexOutOfBoundsException;
import org.teavm.classlib.java.lang.TNullPointerException;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class TRandomAccessFile implements DataInput, DataOutput, Closeable {
private static final byte[] ONE_BYTE_BUFFER = new byte[1];

View File

@ -18,12 +18,11 @@ package org.teavm.classlib.java.lang;
import java.util.Enumeration;
import java.util.Properties;
import org.teavm.backend.c.intrinsic.RuntimeInclude;
import org.teavm.backend.c.runtime.Memory;
import org.teavm.backend.c.runtime.fs.CFileSystem;
import org.teavm.backend.javascript.spi.GeneratedBy;
import org.teavm.backend.wasm.runtime.WasmSupport;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.fs.VirtualFileSystemProvider;
import org.teavm.classlib.fs.c.CFileSystem;
import org.teavm.classlib.impl.c.Memory;
import org.teavm.classlib.impl.console.StderrOutputStream;
import org.teavm.classlib.impl.console.StdoutOutputStream;
import org.teavm.classlib.java.io.TConsole;
@ -41,6 +40,7 @@ import org.teavm.runtime.Allocator;
import org.teavm.runtime.GC;
import org.teavm.runtime.RuntimeArray;
import org.teavm.runtime.RuntimeClass;
import org.teavm.runtime.fs.VirtualFileSystemProvider;
public final class TSystem extends TObject {
private static TPrintStream outCache;

View File

@ -75,6 +75,7 @@ import org.teavm.backend.c.intrinsic.RuntimeClassIntrinsic;
import org.teavm.backend.c.intrinsic.ShadowStackIntrinsic;
import org.teavm.backend.c.intrinsic.StringsIntrinsic;
import org.teavm.backend.c.intrinsic.StructureIntrinsic;
import org.teavm.backend.c.transform.CFileSystemTransformer;
import org.teavm.backend.lowlevel.analyze.LowLevelInliningFilterFactory;
import org.teavm.backend.lowlevel.dependency.ExceptionHandlingDependencyListener;
import org.teavm.backend.lowlevel.dependency.StringsDependencyListener;
@ -227,6 +228,7 @@ public class CTarget implements TeaVMTarget, TeaVMCHost {
transformers.add(new ClassPatch());
transformers.add(new CDependencyListener());
transformers.add(new WeakReferenceTransformation());
transformers.add(new CFileSystemTransformer());
return transformers;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.c;
package org.teavm.backend.c.runtime;
import org.teavm.interop.Structure;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.c;
package org.teavm.backend.c.runtime;
import org.teavm.interop.Address;
import org.teavm.interop.Import;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.impl.c;
package org.teavm.backend.c.runtime;
import org.teavm.interop.Structure;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,20 +13,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.c;
package org.teavm.backend.c.runtime.fs;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import org.teavm.backend.c.intrinsic.RuntimeInclude;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileSystem;
import org.teavm.classlib.impl.c.Memory;
import org.teavm.classlib.impl.c.StringList;
import org.teavm.backend.c.runtime.Memory;
import org.teavm.backend.c.runtime.StringList;
import org.teavm.interop.Address;
import org.teavm.interop.Import;
import org.teavm.interop.Unmanaged;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileSystem;
public class CFileSystem implements VirtualFileSystem {
private Map<String, Entry> cache = new HashMap<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,17 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.c;
package org.teavm.backend.c.runtime.fs;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.classlib.impl.c.Memory;
import org.teavm.classlib.impl.c.StringList;
import org.teavm.backend.c.runtime.Memory;
import org.teavm.backend.c.runtime.StringList;
import org.teavm.interop.Address;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class CVirtualFile implements VirtualFile {
CFileSystem fileSystem;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2019 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.c;
package org.teavm.backend.c.runtime.fs;
import java.io.IOException;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class CVirtualFileAccessor implements VirtualFileAccessor {
private long file;

View File

@ -0,0 +1,30 @@
/*
* Copyright 2022 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.backend.c.transform;
import org.teavm.backend.c.runtime.fs.CFileSystem;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.MethodHolder;
import org.teavm.model.emit.ProgramEmitter;
import org.teavm.runtime.fs.VirtualFileSystemProviderTransformer;
public class CFileSystemTransformer extends VirtualFileSystemProviderTransformer {
@Override
protected void transformCreateMethod(MethodHolder method, ClassHolderTransformerContext context) {
ProgramEmitter pe = ProgramEmitter.create(method, context.getHierarchy());
pe.construct(CFileSystem.class).returnValue();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs;
package org.teavm.runtime.fs;
import java.io.IOException;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs;
package org.teavm.runtime.fs;
import java.io.IOException;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs;
package org.teavm.runtime.fs;
public interface VirtualFileSystem {
String getUserDir();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2017 Alexey Andreev.
* Copyright 2022 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -13,30 +13,27 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs;
package org.teavm.runtime.fs;
import org.teavm.classlib.PlatformDetector;
import org.teavm.classlib.fs.c.CFileSystem;
import org.teavm.classlib.fs.memory.InMemoryVirtualFileSystem;
import org.teavm.runtime.fs.memory.InMemoryVirtualFileSystem;
public final class VirtualFileSystemProvider {
private static VirtualFileSystem instance;
static {
if (PlatformDetector.isC()) {
instance = new CFileSystem();
} else {
instance = new InMemoryVirtualFileSystem();
}
}
private VirtualFileSystemProvider() {
}
public static VirtualFileSystem getInstance() {
if (instance == null) {
instance = create();
}
return instance;
}
private static VirtualFileSystem create() {
return new InMemoryVirtualFileSystem();
}
public static void setInstance(VirtualFileSystem instance) {
VirtualFileSystemProvider.instance = instance;
}

View File

@ -0,0 +1,34 @@
/*
* Copyright 2022 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.runtime.fs;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.MethodDescriptor;
import org.teavm.model.MethodHolder;
public abstract class VirtualFileSystemProviderTransformer implements ClassHolderTransformer {
@Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
if (cls.getName().equals(VirtualFileSystemProvider.class.getName())) {
MethodHolder method = cls.getMethod(new MethodDescriptor("create", VirtualFileSystem.class));
transformCreateMethod(method, context);
}
}
protected abstract void transformCreateMethod(MethodHolder method, ClassHolderTransformerContext context);
}

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.memory;
package org.teavm.runtime.fs.memory;
import java.io.IOException;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFileAccessor;
public abstract class AbstractInMemoryVirtualFile {
String name;

View File

@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.memory;
package org.teavm.runtime.fs.memory;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class InMemoryVirtualDirectory extends AbstractInMemoryVirtualFile {
final Map<String, AbstractInMemoryVirtualFile> children = new LinkedHashMap<>();

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.memory;
package org.teavm.runtime.fs.memory;
import java.io.IOException;
import java.util.Arrays;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class InMemoryVirtualFile extends AbstractInMemoryVirtualFile {
byte[] data = new byte[0];

View File

@ -13,10 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.memory;
package org.teavm.runtime.fs.memory;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileSystem;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileSystem;
public class InMemoryVirtualFileSystem implements VirtualFileSystem {
InMemoryVirtualDirectory root = new InMemoryVirtualDirectory("");

View File

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.fs.memory;
package org.teavm.runtime.fs.memory;
import java.io.IOException;
import org.teavm.classlib.fs.VirtualFile;
import org.teavm.classlib.fs.VirtualFileAccessor;
import org.teavm.runtime.fs.VirtualFile;
import org.teavm.runtime.fs.VirtualFileAccessor;
public class VirtualFileImpl implements VirtualFile {
private InMemoryVirtualFileSystem fs;