manuel-serrano / hop

Multitier JavaScript
Other
142 stars 18 forks source link

Simple way to call a javascript function? #88

Open bentxt opened 2 years ago

bentxt commented 2 years ago

Hi I try a few thing with hopc -j and now I would like to call a js function like alert. I read all the documention available, but I'm still confused. The js-field stuff doesn' t make it clearer. I'm a also confused by the source code of hop. I see no clear way on create bindings or simply call from scm to js. I think I need help. thanks Ben

manuel-serrano commented 2 years ago

... you need help that I will try my best to provide :-)

It is easy to call JS from Scheme and vice versa but the interface is somehow asymmetric. So, before I can reply, could you clarify two points:

bentxt commented 2 years ago

The goal is to compile a simple scheme source file and have the generated javascript file run by node.

This is a little embarassing, but in the initial inquiry I was not completely honest, since I was using a custom built scheme2js binary, created in the scheme2js directory. On the one hand I have history with Hop and the scheme2js is what I used, on the other hand I didn't want to confuse/annoy you by not using hopc. See the rant below :-).

Now I see that hopc -j is actually more advanced since it compiles to the js output I was looking for:

cat hello.scm
(console.log "hello")

so I was using this:

/scheme2js.exe -o o.js hello.scm && cat o.js
/* scheme2js generated file Fri, 7 Jan 2022 11:30:58 +0100 */
(BgL_consoleze2logze2("hello"));

Instead of simply using hopc

hopc -j hello.scm
(console["log"]("hello"));

It would be so so absouletly great, if I could learn more about the scheme2js part of hop. I think you have libraries where you check the arity and I think even the type of the arguments. So I will have a look into the examples you mentioned and maybe we could continue some kind of conversation of how to make the client side of hop more accessible to outsiders like me.

---- rant ---- You have no idea how many times I refreshed this page, just to see if there is finally a response. How many times I read trough the original scheme2js papers from you and Florian, how desparate and confused I am to figure out the differences between the different runtime*.js files. And the worst thing: I'm not doing this for the first time, but in returning cycles over the last few years. So on the one hand I think, yes I might be crazy, on the other hand there is also a rational path the leads me repeatedly down this rabbit hole:

  1. looking for an alternative for javascript
  2. looking for an scheme to js compiler
  3. yes a compiler not an interpreter
  4. looking for a scheme compiler written in scheme
  5. with a reasonable runtime baggage
  6. hop
  7. try to work and separate the scheme2js section of hop
  8. overwhelmed with the code base and simple ffi problems
  9. go back to javascript
  10. goto 1
bigloo-dev commented 2 years ago

Yes, hopc -j is a wrapper to scheme2js so it is probably more convenient to use.

This is a little embarassing, but in the initial inquiry I was not completely honest, since I was using a custom built scheme2js Don't be embarrassed. We produce software for people to use them in their own way.

Regarding Scheme libraries, yes, arities and types are checked. When we compile down to JavaScript, we loose some of these tests because JavaScript is always very permissive.

BTW, it's perfectly okay to use Scheme, of course, but I'm more and more interested in a project where I built JavaScript extensions that makes it closer and closer to Scheme. In the mid-term, my objective is to have a language sitting on top of JavaScript that we can use similarly as we use Scheme (apart the macros, for sure). Some elements are already there, typically, the records that are similar to Bigloo classes and the vectors that are similar to Scheme vectors, lexical scoping is enforced, funcall arities are checked, etc. For activating these features, and the string "use hopscript" at the top of you JS source files.