js-on-sx: Function constructor compiles + evaluates JS source
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 30s
Was unconditionally throwing "Function constructor not supported".
Now js-function-ctor joins param strings with commas, wraps the
body in (function(<params>){<body>}), and runs it through js-eval.
Now Function('a', 'b', 'return a + b')(3,4) === 7.
built-ins/Function: 0/14 → 4/14. conformance.sh: 148/148.
This commit is contained in:
@@ -23,7 +23,58 @@
|
||||
|
||||
;; ── Boolean coercion (ToBoolean) ──────────────────────────────────
|
||||
|
||||
(define js-function-global {:__callable__ (fn (&rest args) (error "TypeError: Function constructor not supported")) :prototype {:call (fn (&rest args) :js-undefined) :length 0 :bind (fn (&rest args) (fn () :js-undefined)) :toString (fn () "function () { [native code] }") :apply (fn (&rest args) :js-undefined) :name ""}})
|
||||
(define js-function-global {:__callable__ (fn (&rest args) (js-function-ctor args)) :prototype {:call (fn (&rest args) :js-undefined) :length 0 :bind (fn (&rest args) (fn () :js-undefined)) :toString (fn () "function () { [native code] }") :apply (fn (&rest args) :js-undefined) :name ""}})
|
||||
|
||||
(define
|
||||
js-function-ctor
|
||||
(fn
|
||||
(args)
|
||||
(cond
|
||||
((empty? args) (js-eval "(function(){})"))
|
||||
(else
|
||||
(let
|
||||
((all-strs (js-fn-args-to-strs args))
|
||||
(n (len args)))
|
||||
(let
|
||||
((param-strs (js-fn-take-init all-strs))
|
||||
(body-str (js-fn-take-last all-strs)))
|
||||
(js-eval
|
||||
(str "(function(" (js-fn-join-commas param-strs) "){" body-str "})"))))))))
|
||||
|
||||
(define
|
||||
js-fn-args-to-strs
|
||||
(fn
|
||||
(args)
|
||||
(cond
|
||||
((empty? args) (list))
|
||||
(else (cons (js-to-string (first args)) (js-fn-args-to-strs (rest args)))))))
|
||||
|
||||
(define
|
||||
js-fn-take-init
|
||||
(fn
|
||||
(lst)
|
||||
(cond
|
||||
((empty? lst) (list))
|
||||
((empty? (rest lst)) (list))
|
||||
(else (cons (first lst) (js-fn-take-init (rest lst)))))))
|
||||
|
||||
(define
|
||||
js-fn-take-last
|
||||
(fn
|
||||
(lst)
|
||||
(cond
|
||||
((empty? lst) "")
|
||||
((empty? (rest lst)) (first lst))
|
||||
(else (js-fn-take-last (rest lst))))))
|
||||
|
||||
(define
|
||||
js-fn-join-commas
|
||||
(fn
|
||||
(lst)
|
||||
(cond
|
||||
((empty? lst) "")
|
||||
((empty? (rest lst)) (first lst))
|
||||
(else (str (first lst) "," (js-fn-join-commas (rest lst)))))))
|
||||
|
||||
;; ── Numeric coercion (ToNumber) ───────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user