···145145 | _ -> raise (Eval_error "unary minus on non-number")
146146147147let apply_index base idx =
148148- match idx with
149149- | Value.String s -> Value.field s base
150150- | Value.Number n when Float.is_integer n && Float.is_finite n ->
148148+ match (base, idx) with
149149+ | _, Value.Undefined -> Value.Undefined
150150+ | Value.Set xs, _ ->
151151+ (* For sets, [s[v]] is set-membership: returns [v] when present
152152+ and undefined otherwise. *)
153153+ if List.exists (fun v -> Value.compare v idx = 0) xs then idx
154154+ else Value.Undefined
155155+ | _, Value.String s -> Value.field s base
156156+ | _, Value.Number n when Float.is_integer n && Float.is_finite n ->
151157 Value.index (int_of_float n) base
152152- | Value.Undefined -> Value.Undefined
158158+ | Value.Object pairs, _ -> (
159159+ match List.assoc_opt idx pairs with
160160+ | Some v -> v
161161+ | None -> Value.Undefined)
153162 | _ -> raise (Eval_error "invalid index type")
154163155164let trim_chars cs s =