From f136abff71fecb4fe984bb6fa377e66ba6f31acd Mon Sep 17 00:00:00 2001 From: "Todd V. Jonker" Date: Fri, 9 Oct 2026 11:54:24 -0700 Subject: [PATCH] Misc cleanup and small fixes Fix and incorrect optimization in `SyntaxWraps`. --- .../java/dev/ionfusion/fusion/FusionSexp.java | 40 ------------------- .../dev/ionfusion/fusion/ModuleNamespace.java | 12 ++++-- .../java/dev/ionfusion/fusion/NamedValue.java | 9 ++--- .../dev/ionfusion/fusion/NamespaceStore.java | 3 ++ .../dev/ionfusion/fusion/ObjectNameProc.java | 5 +-- .../java/dev/ionfusion/fusion/SyntaxSexp.java | 2 +- .../dev/ionfusion/fusion/SyntaxValue.java | 5 ++- .../dev/ionfusion/fusion/SyntaxWraps.java | 2 +- 8 files changed, 24 insertions(+), 54 deletions(-) diff --git a/runtime/src/main/java/dev/ionfusion/fusion/FusionSexp.java b/runtime/src/main/java/dev/ionfusion/fusion/FusionSexp.java index d2abb71b8..1298f2f20 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/FusionSexp.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/FusionSexp.java @@ -421,46 +421,6 @@ static List unsafeSexpToJavaList(Evaluator eval, Object sexp) } - /** - * @param sexp must be a proper sexp; it is not type-checked! - */ - static IonSexp unsafeCopyToIonSexp(Evaluator eval, - Object sexp, - ValueFactory factory, - boolean throwOnConversionFailure) - throws FusionException - { - BaseSexp base = (BaseSexp) sexp; - return base.copyToIonValue(eval, factory, throwOnConversionFailure); - } - - /** - * @param sexp must be a proper sexp; it is not type-checked! - */ - static IonSexp unsafeCopyToIonSexp(Evaluator eval, - Object sexp, - ValueFactory factory) - throws FusionException - { - BaseSexp base = (BaseSexp) sexp; - return base.copyToIonValue(eval, factory, true); - } - - /** - * @param sexp must be a proper sexp; it is not type-checked! - * - * @return null if the sexp and its contents cannot be ionized. - */ - static IonSexp unsafeCopyToIonSexpMaybe(Evaluator eval, - Object sexp, - ValueFactory factory) - throws FusionException - { - BaseSexp base = (BaseSexp) sexp; - return base.copyToIonValue(eval, factory, false); - } - - //======================================================================== diff --git a/runtime/src/main/java/dev/ionfusion/fusion/ModuleNamespace.java b/runtime/src/main/java/dev/ionfusion/fusion/ModuleNamespace.java index 9c4eab041..ca14b326e 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/ModuleNamespace.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/ModuleNamespace.java @@ -431,7 +431,12 @@ Binding resolveMaybe(BaseSymbol name, //================================================================================== - + /** + * For each storage slot, hold a symbol for reporting its name. + * These are only used for reporting forward reference errors and are not unique. + * From here it is attached to an expanded `module` form as a syntax property, + * and from there to {@link dev.ionfusion.fusion.ModuleForm.CompiledModule}. + */ private final List myDefinedNames = new ArrayList<>(); private final SyntaxWrap myWrap = new ModuleWrap(); @@ -570,13 +575,14 @@ public Object doEval(Evaluator eval, Store store) /** - * A reference to a module-level variable in the lexically enclosing - * namespace. + * A reference to a module-level variable in the lexically enclosing namespace. */ static final class CompiledModuleVariableReference implements CompiledForm { private final int myAddress; + + /** Used to make an identifier for forward-reference errors. */ private final ResourcePosition myPosition; CompiledModuleVariableReference(int address, ResourcePosition position) diff --git a/runtime/src/main/java/dev/ionfusion/fusion/NamedValue.java b/runtime/src/main/java/dev/ionfusion/fusion/NamedValue.java index 5d782296f..c1f12415c 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/NamedValue.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/NamedValue.java @@ -14,14 +14,14 @@ interface NamedValue { /** - * Suggests a name for the given value, if it can accept it. + * Suggests a name for the given value if it can accept it. */ @SuppressWarnings("unchecked") - static void inferObjectName(Object value, Object name) + static void inferObjectName(Object value, T name) { if (value instanceof NamedValue) { - ((NamedValue)value).inferObjectName(name); + ((NamedValue)value).inferObjectName(name); } } @@ -35,8 +35,7 @@ default void inferObjectName(T name) } /** - * Produces a Fusion value that's the object's name for the purpose of the - * {@code object_name} function. + * Returns a Fusion value that's the result for the {@code object_name} function. * The result must not change between invocations. * * @return the object name. If null, {@code object_name} returns void. diff --git a/runtime/src/main/java/dev/ionfusion/fusion/NamespaceStore.java b/runtime/src/main/java/dev/ionfusion/fusion/NamespaceStore.java index 2fc7c62dc..8ecdca689 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/NamespaceStore.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/NamespaceStore.java @@ -10,6 +10,9 @@ interface NamespaceStore { ModuleRegistry getRegistry(); + /** + * Provides runtime access to module-level definitions from outside the module. + */ Object lookupImport(int moduleAddress, int bindingAddress); /** diff --git a/runtime/src/main/java/dev/ionfusion/fusion/ObjectNameProc.java b/runtime/src/main/java/dev/ionfusion/fusion/ObjectNameProc.java index aea3d4862..1bbcbdf95 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/ObjectNameProc.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/ObjectNameProc.java @@ -9,8 +9,7 @@ * Implements {@code object_name}. * * @see NamedValue - * @see Racket Reference for - * object-name + * @see Racket Reference for object-name */ class ObjectNameProc extends Procedure1 @@ -21,7 +20,7 @@ Object doApply(Evaluator eval, Object arg) { if (arg instanceof NamedValue) { - Object o = ((NamedValue) arg).objectName(); + Object o = ((NamedValue) arg).objectName(); if (o != null) { return o; diff --git a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxSexp.java b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxSexp.java index 4b522ec7b..d4b65015a 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxSexp.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxSexp.java @@ -231,7 +231,7 @@ int size(Evaluator eval) @Override - SyntaxValue get(Evaluator eval, int index) + SyntaxValue get(Evaluator eval, int index) throws FusionException { return (SyntaxValue) unsafePairDot(eval, unwrap(eval), index); diff --git a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxValue.java b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxValue.java index d95aacf98..e33213ad4 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxValue.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxValue.java @@ -128,7 +128,10 @@ final boolean isAnyNull() final void write(Evaluator eval, Appendable out) throws IOException, FusionException { - Object content = unwrap(eval); + // Be careful not to propagate context here! This is called from `toString()`, + // which is called asynchronously by debuggers, which can cause context loss if + // it happens at the wrong time. + Object content = getContent(); // When possible, only write the explicit wrapper on the outside edge. if (eval != null && (boolean) WRITE_EXPLICIT_SYNTAX.currentValue(eval)) diff --git a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxWraps.java b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxWraps.java index 34928fd36..e733053e1 100644 --- a/runtime/src/main/java/dev/ionfusion/fusion/SyntaxWraps.java +++ b/runtime/src/main/java/dev/ionfusion/fusion/SyntaxWraps.java @@ -61,7 +61,7 @@ SyntaxWraps addWraps(SyntaxWraps wraps) int prefixLen = wraps.myWraps.length; int suffixLen = this.myWraps.length; - if (suffixLen == 0) return this; // Nothing to add + if (prefixLen == 0) return this; // Nothing to prepend int len = prefixLen + suffixLen;