Skip to content

Commit c96432e

Browse files
Dale-Blackclaude
andcommitted
Fix kwargs extraction, plotly args, add Base.vect for array literals
- NamedTuple{(:x,:y)} is UnionAll not DataType — handle both - plotly(divid, traces, layout) positional arg order fixed - Base.vect([1,2,3]) → JS array literal [1, 2, 3] Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b31501b commit c96432e

2 files changed

Lines changed: 32 additions & 8 deletions

File tree

src/compiler/codegen.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,13 @@ function _extract_kwargs(ctx::JSCompilationContext, kwargs_ssa)
574574
field_names = Symbol[]
575575
if type_ssa isa Core.SSAValue
576576
type_type = ctx.code_info.ssavaluetypes[type_ssa.id]
577-
if type_type isa Core.Const && type_type.val isa DataType
577+
if type_type isa Core.Const
578578
nt_type = type_type.val
579-
if nt_type <: NamedTuple
579+
if nt_type isa DataType && nt_type <: NamedTuple
580580
field_names = collect(nt_type.parameters[1])
581+
elseif nt_type isa UnionAll && nt_type <: NamedTuple
582+
# NamedTuple{(:x,:y)} is UnionAll — field names in .body.parameters[1]
583+
field_names = collect(nt_type.body.parameters[1])
581584
end
582585
end
583586
end
@@ -818,6 +821,11 @@ function compile_call(ctx::JSCompilationContext, expr::Expr)
818821
end
819822
end
820823

824+
# Array literal: [1.0, 2.0, 3.0] → Base.vect(1.0, 2.0, 3.0) → [1.0, 2.0, 3.0]
825+
if resolved_fn === Base.vect
826+
return "[$(join(call_args, ", "))]"
827+
end
828+
821829
# Array creation: Float64[] → getindex(Float64) → []
822830
# Also handles array indexing: arr[i] → arr[i-1]
823831
if resolved_fn === Base.getindex

src/compiler/packages_plotly.jl

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,29 @@ end
3535
# ─── Plot function: creates/updates a Plotly chart ───
3636

3737
function _plotly_plot_compiler(ctx, kwargs, pos_args)
38-
# pos_args[1] = traces (array), pos_args[2] = layout (optional)
39-
# kwargs may include: divid (element ID)
40-
traces_js = length(pos_args) >= 1 ? pos_args[1] : "[]"
41-
layout_js = length(pos_args) >= 2 ? pos_args[2] : "{}"
38+
# Positional: plotly(divid, traces, layout) OR plot(traces, layout)
39+
# Detect by checking if first arg looks like a string (divid)
40+
el_id = "\"therapy-plot\""
41+
traces_js = "[]"
42+
layout_js = "{}"
4243

43-
# Get target element ID from kwargs or default
44-
el_id = get(kwargs, :divid, "\"therapy-plot\"")
44+
if length(pos_args) >= 3
45+
# plotly(divid, traces, layout)
46+
el_id = pos_args[1]
47+
traces_js = pos_args[2]
48+
layout_js = pos_args[3]
49+
elseif length(pos_args) >= 2
50+
# plot(traces, layout) or plotly(divid, traces)
51+
traces_js = pos_args[1]
52+
layout_js = pos_args[2]
53+
elseif length(pos_args) >= 1
54+
traces_js = pos_args[1]
55+
end
56+
57+
# Override with kwargs if present
58+
if haskey(kwargs, :divid)
59+
el_id = kwargs[:divid]
60+
end
4561

4662
return "(function() { var _el = document.getElementById($(el_id)); if (_el && typeof Plotly !== 'undefined') { Plotly.react(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); } else if (_el) { var _s = document.createElement('script'); _s.src = 'https://cdn.plot.ly/plotly-2.35.2.min.js'; _s.onload = function() { Plotly.newPlot(_el, $(traces_js), $(layout_js), {responsive: true, displayModeBar: false}); }; document.head.appendChild(_s); } }())"
4763
end

0 commit comments

Comments
 (0)