Fix process-bindings scope loss and async-invoke arity, bootstrap async adapter
Two bugs fixed:
1. process-bindings used merge(env) which returns {} for Env objects
(Env is not a dict subclass). Changed to env-extend in render.sx
and adapter-async.sx. This caused "Undefined symbol: theme" etc.
2. async-aser-eval-call passed evaled-args list to async-invoke(&rest),
double-wrapping it. Changed to inline apply + coroutine check.
Also: bootstrap define-async into sx_ref.py (Phase 6), replace ~1000 LOC
hand-written async_eval_ref.py with 24-line thin re-export shim.
Test runner now uses Env (not flat dict) for render envs to catch scope bugs.
8 new regression tests (4 scope chain, 2 native callable arity, 2 render).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -462,10 +462,7 @@ def invoke(f, *args):
|
||||
|
||||
def json_serialize(obj):
|
||||
import json
|
||||
try:
|
||||
return json.dumps(obj)
|
||||
except (TypeError, ValueError):
|
||||
return "{}"
|
||||
return json.dumps(obj)
|
||||
|
||||
|
||||
def is_empty_dict(d):
|
||||
@@ -1067,10 +1064,19 @@ import inspect
|
||||
|
||||
from shared.sx.primitives_io import (
|
||||
IO_PRIMITIVES, RequestContext, execute_io,
|
||||
css_class_collector as _css_class_collector_cv,
|
||||
_svg_context as _svg_context_cv,
|
||||
)
|
||||
|
||||
# Lazy imports to avoid circular dependency (html.py imports sx_ref.py)
|
||||
_css_class_collector_cv = None
|
||||
_svg_context_cv = None
|
||||
|
||||
def _ensure_html_imports():
|
||||
global _css_class_collector_cv, _svg_context_cv
|
||||
if _css_class_collector_cv is None:
|
||||
from shared.sx.html import css_class_collector, _svg_context
|
||||
_css_class_collector_cv = css_class_collector
|
||||
_svg_context_cv = _svg_context
|
||||
|
||||
# When True, async_aser expands known components server-side
|
||||
_expand_components_cv: contextvars.ContextVar[bool] = contextvars.ContextVar(
|
||||
"_expand_components_ref", default=False
|
||||
@@ -1094,18 +1100,22 @@ def expand_components_p():
|
||||
|
||||
|
||||
def svg_context_p():
|
||||
_ensure_html_imports()
|
||||
return _svg_context_cv.get(False)
|
||||
|
||||
|
||||
def svg_context_set(val):
|
||||
_ensure_html_imports()
|
||||
return _svg_context_cv.set(val)
|
||||
|
||||
|
||||
def svg_context_reset(token):
|
||||
_ensure_html_imports()
|
||||
_svg_context_cv.reset(token)
|
||||
|
||||
|
||||
def css_class_collect(val):
|
||||
_ensure_html_imports()
|
||||
collector = _css_class_collector_cv.get(None)
|
||||
if collector is not None:
|
||||
collector.update(str(val).split())
|
||||
@@ -1123,6 +1133,25 @@ def is_sx_expr(x):
|
||||
return isinstance(x, SxExpr)
|
||||
|
||||
|
||||
# Predicate helpers used by adapter-async (these are in PRIMITIVES but
|
||||
# the bootstrapped code calls them as plain functions)
|
||||
def string_p(x):
|
||||
return isinstance(x, str)
|
||||
|
||||
|
||||
def list_p(x):
|
||||
return isinstance(x, _b_list)
|
||||
|
||||
|
||||
def number_p(x):
|
||||
return isinstance(x, (int, float)) and not isinstance(x, bool)
|
||||
|
||||
|
||||
def sx_parse(src):
|
||||
from shared.sx.parser import parse_all
|
||||
return parse_all(src)
|
||||
|
||||
|
||||
def is_async_coroutine(x):
|
||||
return inspect.iscoroutine(x)
|
||||
|
||||
@@ -1199,48 +1228,16 @@ async def async_eval_slot_to_sx(expr, env, ctx=None):
|
||||
ctx = RequestContext()
|
||||
token = _expand_components_cv.set(True)
|
||||
try:
|
||||
return await _eval_slot_inner(expr, env, ctx)
|
||||
result = await async_eval_slot_inner(expr, env, ctx)
|
||||
if isinstance(result, SxExpr):
|
||||
return result
|
||||
if result is None or result is NIL:
|
||||
return SxExpr("")
|
||||
if isinstance(result, str):
|
||||
return SxExpr(result)
|
||||
return SxExpr(sx_serialize(result))
|
||||
finally:
|
||||
_expand_components_cv.reset(token)
|
||||
|
||||
|
||||
async def _eval_slot_inner(expr, env, ctx):
|
||||
if isinstance(expr, list) and expr:
|
||||
head = expr[0]
|
||||
if isinstance(head, Symbol) and head.name.startswith("~"):
|
||||
comp = env.get(head.name)
|
||||
if isinstance(comp, Component):
|
||||
result = await async_aser_component(comp, expr[1:], env, ctx)
|
||||
if isinstance(result, SxExpr):
|
||||
return result
|
||||
if result is None or result is NIL:
|
||||
return SxExpr("")
|
||||
if isinstance(result, str):
|
||||
return SxExpr(result)
|
||||
return SxExpr(sx_serialize(result))
|
||||
result = await async_aser(expr, env, ctx)
|
||||
result = await _maybe_expand_component_result(result, env, ctx)
|
||||
if isinstance(result, SxExpr):
|
||||
return result
|
||||
if result is None or result is NIL:
|
||||
return SxExpr("")
|
||||
if isinstance(result, str):
|
||||
return SxExpr(result)
|
||||
return SxExpr(sx_serialize(result))
|
||||
|
||||
|
||||
async def _maybe_expand_component_result(result, env, ctx):
|
||||
raw = None
|
||||
if isinstance(result, SxExpr):
|
||||
raw = str(result).strip()
|
||||
elif isinstance(result, str):
|
||||
raw = result.strip()
|
||||
if raw and raw.startswith("(~"):
|
||||
from shared.sx.parser import parse_all as _pa
|
||||
parsed = _pa(raw)
|
||||
if parsed:
|
||||
return await async_eval_slot_to_sx(parsed[0], env, ctx)
|
||||
return result
|
||||
'''
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -1366,7 +1363,8 @@ aser_special = _aser_special_with_continuations
|
||||
# Public API generator
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
def public_api_py(has_html: bool, has_sx: bool, has_deps: bool = False) -> str:
|
||||
def public_api_py(has_html: bool, has_sx: bool, has_deps: bool = False,
|
||||
has_async: bool = False) -> str:
|
||||
lines = [
|
||||
'',
|
||||
'# =========================================================================',
|
||||
@@ -1419,8 +1417,9 @@ def public_api_py(has_html: bool, has_sx: bool, has_deps: bool = False) -> str:
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
ADAPTER_FILES = {
|
||||
"html": ("adapter-html.sx", "adapter-html"),
|
||||
"sx": ("adapter-sx.sx", "adapter-sx"),
|
||||
"html": ("adapter-html.sx", "adapter-html"),
|
||||
"sx": ("adapter-sx.sx", "adapter-sx"),
|
||||
"async": ("adapter-async.sx", "adapter-async"),
|
||||
}
|
||||
|
||||
SPEC_MODULES = {
|
||||
|
||||
Reference in New Issue
Block a user