Skip to content

Commit

Permalink
refactor IResource
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvergnaud committed Mar 19, 2022
1 parent e974dca commit 62344a0
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import prompto.utils.CodeWriter;
import prompto.utils.IdentifierList;
import prompto.value.IInstance;
import prompto.value.NativeInstance;
import prompto.value.NativeCategory;

public class NativeCategoryDeclaration extends ConcreteCategoryDeclaration {

Expand All @@ -43,7 +43,7 @@ public void register(Context context) {

@Override
protected void setDbId(Context context, IInstance instance, PromptoDbId dbId) {
((NativeInstance)instance).setDbId(dbId);
((NativeCategory)instance).setDbId(dbId);
}

@Override
Expand Down Expand Up @@ -118,7 +118,7 @@ protected void categoryTypeToMDialect(CodeWriter writer) {

@Override
public IInstance newInstance(Context context) throws PromptoError {
return new NativeInstance(context, this);
return new NativeCategory(context, this);
}

public String getBoundClassName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import prompto.error.PromptoError;
import prompto.error.ReadWriteError;
import prompto.error.SyntaxError;
import prompto.intrinsic.IResource;
import prompto.parser.CodeSection;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.IType;
import prompto.type.ResourceType;
import prompto.type.TextType;
import prompto.utils.CodeWriter;
import prompto.value.IResource;
import prompto.value.IValue;
import prompto.value.TextValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import prompto.error.PromptoError;
import prompto.error.ReadWriteError;
import prompto.error.SyntaxError;
import prompto.intrinsic.IResource;
import prompto.intrinsic.PromptoBinary;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
Expand All @@ -20,7 +21,6 @@
import prompto.type.ResourceType;
import prompto.utils.CodeWriter;
import prompto.value.BlobValue;
import prompto.value.IResource;
import prompto.value.IValue;

public class ReadBlobExpression implements IExpression {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
import prompto.error.PromptoError;
import prompto.error.ReadWriteError;
import prompto.error.SyntaxError;
import prompto.intrinsic.IResource;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.IType;
import prompto.type.ResourceType;
import prompto.type.TextType;
import prompto.utils.CodeWriter;
import prompto.value.IResource;
import prompto.value.IValue;
import prompto.value.NullValue;
import prompto.value.TextValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package prompto.value;
package prompto.intrinsic;

import java.io.IOException;
import java.io.Reader;
import java.util.function.Consumer;

import prompto.intrinsic.PromptoBinary;

public interface IResource {

boolean isReadable();
boolean isWritable();
Reader asReader() throws IOException;
String readLine() throws IOException;
void writeLine(String data) throws IOException;
PromptoBinary readBlob() throws IOException;
String readFully() throws IOException;
void readFully(Consumer<String> thenWith) throws IOException;
PromptoBinary readBlob() throws IOException;
boolean isWritable();
void writeLine(String data) throws IOException;
void writeFully(String data) throws IOException;
void writeFully(String text, Consumer<String> thenWith) throws IOException;
void close();
Expand Down
6 changes: 3 additions & 3 deletions Core/src/main/java/prompto/java/JavaClassType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import prompto.value.IValue;
import prompto.value.IteratorValue;
import prompto.value.ListValue;
import prompto.value.NativeInstance;
import prompto.value.NativeCategory;
import prompto.value.SetValue;
import prompto.value.TextValue;

Expand Down Expand Up @@ -153,7 +153,7 @@ public static IValue convertJavaValueToPromptoValue(Context context, Object valu
if(val!=null)
return val;
if(returnType==AnyType.instance())
return new NativeInstance(AnyNativeCategoryDeclaration.getInstance(), value);
return new NativeCategory(AnyNativeCategoryDeclaration.getInstance(), value);
else
throw new InternalError("Unable to convert:" + value.getClass().getSimpleName());
}
Expand All @@ -172,7 +172,7 @@ private static IValue convertCategory(Context context, Object value, Type type,
// ensure the underlying declaration is loaded
context.getRegisteredDeclaration(IDeclaration.class, returnType.getTypeNameId());
NativeCategoryDeclaration decl = context.getNativeBinding(type);
return decl!=null ? new NativeInstance(decl, value) : null;
return decl!=null ? new NativeCategory(decl, value) : null;
}

private static IValue convertDocument(Context context, Object value, Type type, IType returnType) {
Expand Down
6 changes: 3 additions & 3 deletions Core/src/main/java/prompto/java/JavaMethodExpression.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import prompto.type.VoidType;
import prompto.utils.CodeWriter;
import prompto.value.IValue;
import prompto.value.NativeInstance;
import prompto.value.NativeCategory;


public class JavaMethodExpression extends JavaSelectorExpression {
Expand Down Expand Up @@ -107,8 +107,8 @@ public Object interpret(Context context) throws PromptoError {
Object instance = parent.interpret(context);
if(instance==null)
throw new SyntaxError("Could not locate: " + parent.toString());
if(instance instanceof NativeInstance)
instance = ((NativeInstance)instance).getInstance();
if(instance instanceof NativeCategory)
instance = ((NativeCategory)instance).getInstance();
try {
Method method = findMethod(context, instance);
if(method==null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import prompto.compiler.ResultInfo;
import prompto.compiler.StackLocal;
import prompto.error.PromptoError;
import prompto.intrinsic.IResource;
import prompto.runtime.Context;
import prompto.transpiler.Transpiler;
import prompto.type.IType;
import prompto.utils.CodeWriter;
import prompto.value.IResource;
import prompto.value.IValue;

public class WithResourceStatement extends BaseStatement {
Expand Down
2 changes: 1 addition & 1 deletion Core/src/main/java/prompto/statement/WriteStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import prompto.error.ReadWriteError;
import prompto.expression.IExpression;
import prompto.grammar.ThenWith;
import prompto.intrinsic.IResource;
import prompto.runtime.Context;
import prompto.runtime.Context.ResourceContext;
import prompto.runtime.Variable;
Expand All @@ -32,7 +33,6 @@
import prompto.type.TextType;
import prompto.type.VoidType;
import prompto.utils.CodeWriter;
import prompto.value.IResource;
import prompto.value.IValue;
import prompto.value.TextValue;

Expand Down
2 changes: 1 addition & 1 deletion Core/src/main/java/prompto/value/ConcreteInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ protected IValue getAttributeMember(Context context, Identifier id, boolean auto

protected IValue getCategory(Context context) {
NativeCategoryDeclaration decl = context.getRegisteredDeclaration(NativeCategoryDeclaration.class, new Identifier("Category"));
return new NativeInstance(decl, declaration);
return new NativeCategory(decl, declaration);
}

protected IValue getMemberAllowGetter(Context context, Identifier id, boolean allowGetter) throws PromptoError {
Expand Down
16 changes: 16 additions & 0 deletions Core/src/main/java/prompto/value/NativeCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package prompto.value;

import prompto.declaration.NativeCategoryDeclaration;
import prompto.runtime.Context;

public class NativeCategory extends NativeInstance<Object> {

public NativeCategory(Context context, NativeCategoryDeclaration declaration) {
super(context, declaration);
}

public NativeCategory(NativeCategoryDeclaration declaration, Object instance) {
super(declaration, instance);
}

}
17 changes: 9 additions & 8 deletions Core/src/main/java/prompto/value/NativeInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
import prompto.type.CategoryType;
import prompto.type.NativeCategoryType;

public class NativeInstance extends BaseValue implements IInstance {
public abstract class NativeInstance<T extends Object> extends BaseValue implements IInstance {

NativeCategoryDeclaration declaration;
Object instance = null;
T instance = null;
IStorable storable = null;
boolean mutable = false;

public NativeInstance(Context context, NativeCategoryDeclaration declaration) {
protected NativeInstance(Context context, NativeCategoryDeclaration declaration) {
super(new NativeCategoryType(declaration));
this.declaration = declaration;
this.instance = makeInstance(context);
Expand All @@ -51,7 +51,7 @@ public NativeInstance(Context context, NativeCategoryDeclaration declaration) {
}
}

public NativeInstance(NativeCategoryDeclaration declaration, Object instance) {
protected NativeInstance(NativeCategoryDeclaration declaration, T instance) {
super(new NativeCategoryType(declaration));
this.declaration = declaration;
this.instance = instance;
Expand Down Expand Up @@ -122,9 +122,10 @@ public Object getInstance() {
return instance;
}

private Object makeInstance(Context context) {
@SuppressWarnings("unchecked")
private T makeInstance(Context context) {
try {
Class<?> mapped = declaration.getBoundClass(true);
Class<T> mapped = (Class<T>) declaration.getBoundClass(true);
return mapped.getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -170,7 +171,7 @@ public IValue getMember(Context context, Identifier attrName, boolean autoCreate

private IValue getCategory(Context context) {
NativeCategoryDeclaration decl = context.getRegisteredDeclaration(NativeCategoryDeclaration.class, new Identifier("Category"));
return new NativeInstance(decl, declaration);
return new NativeCategory(decl, declaration);
}

public IValue getMemberAllowGetter(Context context, Identifier attrName, boolean allowGetter) throws PromptoError {
Expand Down Expand Up @@ -321,7 +322,7 @@ public void toJsonStream(Context context, JsonGenerator generator, boolean withT
}

@Override
public NativeInstance toMutable() {
public IInstance toMutable() {
throw new UnsupportedOperationException();
}
}
16 changes: 15 additions & 1 deletion Core/src/main/java/prompto/value/NativeResource.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package prompto.value;

import java.io.IOException;
import java.io.Reader;
import java.util.function.Consumer;

import prompto.declaration.NativeResourceDeclaration;
import prompto.intrinsic.IResource;
import prompto.intrinsic.PromptoBinary;
import prompto.runtime.Context;

public class NativeResource extends NativeInstance implements IResource {
public class NativeResource extends NativeInstance<IResource> implements IResource {

public NativeResource(Context context, NativeResourceDeclaration declaration) {
super(context, declaration);
Expand Down Expand Up @@ -60,5 +62,17 @@ public void close() {
((IResource)instance).close();
}

@Override
public Reader asReader() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public void readFully(Consumer<String> thenWith) throws IOException {
// TODO Auto-generated method stub

}


}
18 changes: 15 additions & 3 deletions Core/src/test/java/prompto/runtime/utils/MyResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

import prompto.intrinsic.IResource;
import prompto.intrinsic.PromptoBinary;
import prompto.value.IResource;

public class MyResource implements IResource {

Expand Down Expand Up @@ -77,13 +77,20 @@ public void writeFully(String data, Consumer<String> thenWith) {
}

@Override
public String readLine() throws IOException {
public BufferedReader asReader() throws IOException {
if(reader==null) {
if(getContent()==null)
return null;
reader = new BufferedReader(new StringReader(getContent()));
}
return reader.readLine();
return reader;
}


@SuppressWarnings("resource")
@Override
public String readLine() throws IOException {
return asReader().readLine();
}

@Override
Expand All @@ -94,4 +101,9 @@ public void writeLine(String data) throws IOException {
setContent(content + data);
}

@Override
public void readFully(Consumer<String> thenWith) throws IOException {
thenWith.accept(getContent());
}

}
17 changes: 14 additions & 3 deletions Runtime/src/main/java/prompto/internet/Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.List;
import java.util.function.Consumer;

import prompto.intrinsic.IResource;
import prompto.intrinsic.PromptoBinary;
import prompto.value.IResource;

public class Url implements IResource {

Expand Down Expand Up @@ -103,6 +103,11 @@ public String readFully() throws IOException {
}
}

@Override
public void readFully(Consumer<String> thenWith) throws IOException {
throw new UnsupportedOperationException();
}

private String readStringFully(InputStream input) throws IOException {
ByteArrayOutputStream data = readBytesFully(input);
return data.toString(encoding);
Expand Down Expand Up @@ -131,10 +136,16 @@ public void writeFully(String text, Consumer<String> thenWith) throws IOExceptio
}

@Override
public String readLine() throws IOException {
public BufferedReader asReader() throws IOException {
if(reader==null)
reader = new BufferedReader(new InputStreamReader(url.openStream()));
return reader.readLine();
return reader;
}

@SuppressWarnings("resource")
@Override
public String readLine() throws IOException {
return asReader().readLine();
}

@Override
Expand Down
Loading

0 comments on commit 62344a0

Please sign in to comment.