Skip to content

Commit

Permalink
Generate builtins from more File methods
Browse files Browse the repository at this point in the history
Enhanced processor to handle methods
- `list`
- `delete`
- `outputStream`
- `inputStream`

One thing that was visible from the hardcoded node classes was that we
had to handle situations where `asGuestValue`/`toHostObject` conversions
were inferred automatically. As more types are marked as builtin(e.g.
`Output_Stream`) those conversions will be very easy to drop.
  • Loading branch information
hubertp committed May 24, 2022
1 parent aec60a5 commit 68c45cc
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 250 deletions.
21 changes: 9 additions & 12 deletions distribution/lib/Standard/Base/0.0.0-dev/src/System/File.enso
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ type File
list name_filter=Nothing recursive=False =
all_files = case recursive of
True -> here.list_descendants this
False -> here.list_immediate_children this
False -> Vector.Vector (this.list_immediate_children)
filtered = case name_filter of
Nothing -> all_files
_ ->
Expand All @@ -691,6 +691,13 @@ type File
relativize : File -> Boolean
relativize child = @Builtin_Method "File.relativize"


## PRIVATE

Utility function that lists immediate children of a directory.
list_immediate_children : File -> Array File
list_immediate_children directory = @Builtin_Method "File.list_immediate_children"

to_text : Text
to_text = this.absolute . path

Expand Down Expand Up @@ -928,16 +935,6 @@ type File_Error
Io_Error file msg -> msg.to_text + " (" + file.path + ")."


## PRIVATE

Utility function that lists immediate children of a directory.
list_immediate_children : File -> Vector.Vector File
list_immediate_children directory =
arr = directory.list_immediate
Vector.Vector arr

list_immediate : File -> Array
list_immediate directory = @Builtin_Method "File.list_immediate"

## PRIVATE

Expand All @@ -951,7 +948,7 @@ list_descendants file =
builder.append file
case file.is_directory of
True ->
children = here.list_immediate_children file
children = Vector.Vector (file.list_immediate_children)
children.each go
False -> Nothing
go file
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.Builtin;
import org.enso.interpreter.node.expression.builtin.error.PolyglotError;
import org.enso.interpreter.runtime.Context;
import org.enso.interpreter.runtime.callable.UnresolvedSymbol;
import org.enso.interpreter.runtime.callable.function.Function;
import org.enso.interpreter.runtime.library.dispatch.MethodDispatchLibrary;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.OpenOption;

Expand Down Expand Up @@ -44,6 +45,21 @@ public TruffleFile resolveUnderlying(EnsoFile subPath) {
return this.truffleFile.resolve(subPath.truffleFile.getPath());
}

@Builtin.Method
@Builtin.WrapException(from = IOException.class, to = PolyglotError.class, propagate = true)
@Builtin.ReturningGuestObject
public OutputStream outputStream(OpenOption[] opts) throws IOException {
return this.withTruffleFile(f -> f.newOutputStream(opts));
}


@Builtin.Method
@Builtin.WrapException(from = IOException.class, to = PolyglotError.class, propagate = true)
@Builtin.ReturningGuestObject
public InputStream inputStream(OpenOption[] opts) throws IOException {
return this.withTruffleFile(f -> f.newInputStream(opts));
}

// TODO
// @Builtin.Method
abstract class Resolve extends Node {
Expand Down Expand Up @@ -95,8 +111,8 @@ public void createDirectories() {
}
}

// TODO
// @Builtin.Method
@Builtin.Method(name = "list_immediate_children")
@Builtin.WrapException(from = IOException.class, to = PolyglotError.class, propagate = true)
public EnsoFile[] list() throws IOException {
return this.truffleFile.list().stream().map(EnsoFile::new).toArray(EnsoFile[]::new);
}
Expand Down Expand Up @@ -126,8 +142,8 @@ public EnsoFile normalize() {
return new EnsoFile(truffleFile.normalize());
}

// TODO
// @Builtin.Method(name = "delete_builtin")
@Builtin.Method(name = "delete_builtin")
@Builtin.WrapException(from = IOException.class, to = PolyglotError.class, propagate = true)
public void delete() throws IOException {
truffleFile.delete();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.runtime.type;

import org.enso.compiler.exception.CompilerError;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.callable.atom.Atom;

Expand Down Expand Up @@ -39,6 +40,8 @@ public static Atom fromTypeSystem(Builtins builtins, String typeName) {
return builtins.dataflowError().newInstance();
case ConstantsGen.FUNCTION:
return builtins.function().newInstance();
case ConstantsGen.FILE:
return builtins.file().newInstance();
case ConstantsGen.INTEGER:
return builtins.number.getInteger().newInstance();
case ConstantsGen.MANAGED_RESOURCE:
Expand All @@ -54,7 +57,7 @@ public static Atom fromTypeSystem(Builtins builtins, String typeName) {
case ConstantsGen.TEXT:
return builtins.text().newInstance();
default:
return null;
throw new CompilerError("Invalid builtin type " + typeName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@
Class<? extends Exception> from();
/** @return Class of Enso's builtin (error) type * */
Class<?> to();

/**
* @return true, if only the original exception should be wrapped. Otherwise we pass provided
* method params.
*/
boolean propagate() default false;
}

/** Container for {@link WrapException} annotations */
@interface WrapExceptions {
WrapException[] value() default {};
}

/** Annotation accepting `env.asGUestValue` translation done on the return object.
* The conversion is generated automatically. The annotation only ensures that it is intentianal.
*/
@interface ReturningGuestObject {}
}
Loading

0 comments on commit 68c45cc

Please sign in to comment.