Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ final class StandardRuntime
{
IonSystem ionSystem =
IonSystemBuilder.standard()
.withCatalog(builder.getDefaultIonCatalog())
.build();
.withCatalog(builder.getDefaultIonCatalog())
Comment thread
toddjonker marked this conversation as resolved.
.build();
myRegistry = new ModuleRegistry();

myDefaultLanguage = builder.getDefaultLanguage();
Expand Down Expand Up @@ -196,28 +196,27 @@ public ModuleBuilder makeModuleBuilder(String absoluteModulePath)
}


//========================================================================



@Override
public IonValue ionize(Object fusionValue, ValueFactory factory)
throws FusionException
{
return FusionValue.copyToIonValue(myTopLevel.getEvaluator(), fusionValue, factory);
return myTopLevel.withEvaluator(eval ->
FusionValue.copyToIonValue(eval, fusionValue, factory));
}


@Override
public IonValue ionizeMaybe(Object fusionValue, ValueFactory factory)
throws FusionException
{
return FusionValue.copyToIonValueMaybe(myTopLevel.getEvaluator(), fusionValue, factory);
return myTopLevel.withEvaluator(eval ->
FusionValue.copyToIonValueMaybe(eval, fusionValue, factory));
}


//========================================================================


private class SandboxBuilderImpl
implements SandboxBuilder
{
Expand Down
169 changes: 63 additions & 106 deletions runtime/src/main/java/dev/ionfusion/fusion/StandardTopLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ ModuleRegistry getRegistry()
return myNamespace.getRegistry();
}

@FunctionalInterface
interface EvalTask<T>
{
T run(Evaluator eval) throws FusionException;
}

<T> T withEvaluator(EvalTask<T> task)
Comment thread
toddjonker marked this conversation as resolved.
throws FusionException
{
try
{
return task.run(myEvaluator);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
}


//========================================================================

@Override
public Object eval(String source, SourceName name)
throws FusionInterruptedException, FusionException
Expand Down Expand Up @@ -106,26 +128,21 @@ public Object eval(String source)
public Object eval(IonReader source, SourceName name)
throws FusionInterruptedException, FusionException
{
try
{
Object result = voidValue(myEvaluator);
return withEvaluator(eval -> {
Object result = voidValue(eval);

if (source.getType() == null) source.next();
while (source.getType() != null)
{
SyntaxValue sourceExpr = readSyntax(myEvaluator, source, name);
SyntaxValue sourceExpr = readSyntax(eval, source, name);

// This method parameterizes current_namespace for us:
result = FusionEval.eval(myEvaluator, sourceExpr, myNamespace);
result = FusionEval.eval(eval, sourceExpr, myNamespace);
source.next();
}

return result;
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
});
}


Expand All @@ -141,19 +158,12 @@ public Object eval(IonReader source)
public Object load(File source)
throws FusionInterruptedException, FusionException
{
try
{
LoadHandler load = myEvaluator.getGlobalState().myLoadHandler;
return withEvaluator(eval -> {
LoadHandler load = eval.getGlobalState().myLoadHandler;

// This method parameterizes current_namespace for us:
return load.loadTopLevel(myEvaluator,
myNamespace,
source.toString());
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
return load.loadTopLevel(eval, myNamespace, source.toString());
});
}


Expand All @@ -170,38 +180,28 @@ public void loadModule(String absoluteModulePath,
throw new IllegalArgumentException(message);
}

try
{
withEvaluator(eval -> {
// Make sure we use the registry on our namespace.
Evaluator eval =
myEvaluator.parameterizeCurrentNamespace(myNamespace);
Evaluator parameterized =
eval.parameterizeCurrentNamespace(myNamespace);

ModuleNameResolver resolver =
myEvaluator.getGlobalState().myModuleNameResolver;
eval.getGlobalState().myModuleNameResolver;
ModuleIdentity id =
ModuleIdentity.forAbsolutePath(absoluteModulePath);
ModuleLocation loc =
ModuleLocation.forIonReader(source, name);

resolver.loadModule(eval, id, loc, true /* reload it */);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
resolver.loadModule(parameterized, id, loc, true /* reload it */);
return null;
});
}

ModuleIdentity loadModule(String modulePath)
throws FusionInterruptedException, FusionException
{
try
{
return myNamespace.resolveAndLoadModule(myEvaluator, modulePath);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
return withEvaluator(eval ->
myNamespace.resolveAndLoadModule(eval, modulePath));
}

/**
Expand All @@ -210,42 +210,28 @@ ModuleIdentity loadModule(String modulePath)
ModuleInstance instantiateLoadedModule(ModuleIdentity id)
throws FusionInterruptedException, FusionException
{
try
{
return getRegistry().instantiate(myEvaluator, id);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
return withEvaluator(eval ->
getRegistry().instantiate(eval, id));
}


void attachModule(StandardTopLevel src, String modulePath)
throws FusionInterruptedException, FusionException
{
try
{
myNamespace.attachModule(myEvaluator, src.myNamespace, modulePath);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
withEvaluator(eval -> {
myNamespace.attachModule(eval, src.myNamespace, modulePath);
return null;
});
}

@Override
public void requireModule(String modulePath)
throws FusionInterruptedException, FusionException
{
try
{
myNamespace.require(myEvaluator, modulePath);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
withEvaluator(eval -> {
myNamespace.require(eval, modulePath);
return null;
});
}


Expand All @@ -262,39 +248,25 @@ public void define(String name, Object value)
throw new IllegalArgumentException(msg);
}

try
{
withEvaluator(eval -> {
myNamespace.bind(name, fv);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
return null;
});
}


@Override
public Object lookup(String name)
throws FusionInterruptedException, FusionException
{
try
{
return myNamespace.lookup(name);
}
catch (FusionInterrupt e)
{
// I don't think this can happen, but I prefer to be consistent
// throughout this class to be more resilient to changes.
throw new FusionInterruptedException(e);
}
return withEvaluator(eval -> myNamespace.lookup(name));
}


private Procedure lookupProcedure(String procedureName)
throws FusionInterruptedException, FusionException
{
try
{
return withEvaluator(eval -> {
Object proc = lookup(procedureName);
if (proc instanceof Procedure)
{
Expand All @@ -309,12 +281,8 @@ private Procedure lookupProcedure(String procedureName)

throw new FusionException(printQuotedSymbol(procedureName) +
" is not a procedure: " +
safeWriteToString(myEvaluator, proc));
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
safeWriteToString(eval, proc));
});
}


Expand All @@ -336,15 +304,8 @@ private Object call(Procedure proc, Object... arguments)
arguments[i] = fv;
}

try
{
// TODO Should this set current_namespace?
return myEvaluator.callNonTail(proc, arguments);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
// TODO Should this set current_namespace?
return withEvaluator(eval -> eval.callNonTail(proc, arguments));
}


Expand Down Expand Up @@ -375,13 +336,9 @@ public Object call(Object procedure, Object... arguments)
public void ionize(Object value, IonWriter out)
throws FusionException
{
try
{
FusionIo.ionize(myEvaluator, out, value);
}
catch (FusionInterrupt e)
{
throw new FusionInterruptedException(e);
}
withEvaluator(eval -> {
FusionIo.ionize(eval, out, value);
return null;
});
}
}
Loading