jscl-project / jscl

A Lisp-to-JavaScript compiler bootstrapped from Common Lisp
https://jscl-project.github.io
GNU General Public License v3.0
874 stars 108 forks source link

Expose JSCL function to Javascript world ? #447

Closed mmontone closed 1 year ago

mmontone commented 1 year ago

Hi.

I would like to expose some of my JSCL functions to Javascript, so that they can be called from JS event handlers.

I know there's jscl.evalString, but I'm wondering if there's another way.

I'm trying with something like:

(setf (jscl::oget #j:window "connect") (jscl::lisp-to-js #'connect))

where connect is the function I'm trying to expose. But calling connect from Javascript doesn't work.

mmontone commented 1 year ago

Also, some of my functions receive the Javascript event; so jscl.evalString is not sufficient.

hemml commented 1 year ago

You can call lisp functions directly, like:

jscl.packages.CL.symbols.["FORMAT"].fvalue(null,jscl.internals.js_to_lisp(true),jscl.internals.js_to_lisp("Hello!"))

The first argument is used to return multiple values, you may supply null here (if I'm understanding this properly)

mmontone commented 1 year ago

Thanks! I'll give it a try.

Now I'm thinking that implementing some JSCL "expose" function that generates that code from a function would be nice.

vlad-km commented 1 year ago

where connect is the function I'm trying to expose. But calling connect from Javascript doesn't work.

Not working how?

vlad-km commented 1 year ago

JSCL REPL

CL-USER> (setf #j:lispFromJS (lambda (x y z) (format t "~a ~a ~a~&" x y z)))
#<FUNCTION>
CL-USER> 1 2 3
1 2 3

JS Console

lispFromJS(1,2,3)
false
>var ff = function(a,b,c){ lispFromJS(a,b,c);}
undefined
>ff(1,2,3)
undefined
mmontone commented 1 year ago

Thanks! That works.