new
(new Constructor. args*)
(new Constructor args*)
Source docstring:
The args, if any, are evaluated from left to right, and
passed to the JavaScript constructor. The constructed object is
returned.
(defmethod parse 'new
[_ env [_ ctor & args :as form] _ _]
(disallowing-recur
(let [enve (assoc env :context :expr)
ctorexpr (analyze enve ctor)
ctor-var (when (#{:var :local :js-var} (:op ctorexpr))
(resolve-existing-var env ctor))
record-args
(when (and (:record ctor-var) (not (-> ctor meta :internal-ctor)))
(repeat 3 (analyze enve nil)))
argexprs (into (vec (map #(analyze enve %) args)) record-args)
known-num-fields (:num-fields ctor-var)
argc (count args)]
(when (and (not (-> ctor meta :internal-ctor))
(some? known-num-fields)
(not (or (= known-num-fields argc)
(and (:record ctor-var)
(= (+ 2 known-num-fields) argc)))))
(warning :fn-arity env {:argc argc :ctor ctor}))
{:env env :op :new :form form :class ctorexpr :args argexprs
:children [:class :args]
:tag (let [tag (-> ctorexpr :info :tag)]
(if (and (js-tag? tag)
(not (prim-ctor? tag)))
'js (let [name (-> ctorexpr :info :name)]
(or (js-prim-ctor->tag name) name))))})))
(defmethod emit* :new
[{ctor :class :keys [args env]}]
(emit-wrap env
(emits "(new " ctor "("
(comma-sep args)
"))")))