Fork me on GitHub
#babashka
<
2020-01-22
>
jeroenvandijk08:01:33

I'm thinking of using https://github.com/borkdude/sci in AWS Lambda to have Clojure there as well (I'm writing javascript as we speak..). Has anyone done/thought about this?

borkdude09:01:58

@jeroenvandijk It is possible. Any reason you're not just using CLJS?

jeroenvandijk09:01:48

I've explained it here: https://clojurians.slack.com/archives/C1AL322AJ/p1579684099024600 TLDR; I want to use it in inline in my Cloudformation Templates

jeroenvandijk09:01:45

I guess I could use a CLJS layer as well and use eval. The work to set this up for me would be pretty similar. Maybe sci is easier to work with. I could try both actually

borkdude09:01:58

fwiw there is already a sci library on npm

jeroenvandijk09:01:11

yep, that made me think about it 🙂

borkdude09:01:13

it's a couple versions behind, although I could update it if there's interest

jeroenvandijk09:01:54

I'm trying to create an easy aws lambda environment now to test things

jeroenvandijk10:01:55

Another advantage of using sci of cljs would be that you can test (parts) in a jvm environment. node is not an environment something I feel super comfortable in, could be just me

jeroenvandijk10:01:04

I'm getting an error when trying the example in sci's README in node https://gist.github.com/jeroenvandijk/a15fa236ff86ad7b4cfa924c24aec1ba I don't really know what I'm doing. Seems to be a specific issue with dotimes

borkdude11:01:40

@jeroenvandijk Let me release a new version of sci for npm

borkdude11:01:53

@jeroenvandijk seems to work now

borkdude11:01:39

newest version is 0.0.12-alpha.12

borkdude11:01:55

I'm about to release a 0.0.12 version before I embark on a journey to add multimethods...

borkdude11:01:06

so if there's any issues, let me know

jeroenvandijk11:01:53

jep works now! Thanks

jeroenvandijk11:01:27

> I'm about to release a 0.0.12 version before I embark on a journey to add multimethods... Exciting and challenging I suppose :)

jeroenvandijk11:01:11

Do you know if there is something special regarding methods on objects?

(println (.toString "somethign to string"))
Gives
Error: Method toString on function String() { [native code] } not allowed! [at line , column ]
    at new yl (/Users/jeroen/Projects/Private/github/node_modules/@borkdude/sci/sci.js:670:76)
    at Function.zl.c (/Users/jeroen/Projects/Private/github/node_modules/@borkdude/sci/sci.js:672:71)
    at Func
Should I do something special to make this work?

borkdude12:01:15

@jeroenvandijk The interop part of sci for JS is probably not as complete as the JVM, simply because no-one needed this yet. For this example I solved that by just bringing in functions that do the interop: https://github.com/borkdude/sci-wallpaper-downloader/blob/master/main.js

jeroenvandijk12:01:37

ah thanks, good to know

borkdude12:01:06

It might be improved if it gets more attention though.

borkdude12:01:55

how do you get a class name from something like the String object in cljs?

borkdude12:01:42

it seems .-name works

borkdude12:01:36

@jeroenvandijk I pushed one commit to sci. This now is one step closer:

const { evalString } = require('@borkdude/sci');
const opts = {bindings: {f: function() { console.log('hello'); }},
              classes: {String: String}};
evalString("(f (.toString \"foo\"))", opts);
It just needs an implementation for https://github.com/borkdude/sci/blob/a7212aae5b6c98886ff030e98b5d8327b4ac66a5/src/sci/impl/interop.cljc#L26 which I could not figure out yet. You can build the npm library by executing script/compile-js and then e.g.:
cp out/min/sci.min.js /tmp/sci/node_modules/@borkdude/sci/sci.js

jeroenvandijk12:01:55

Thanks! I'll play with it

borkdude12:01:42

Pushed another commit. The static function call now seems to work:

const { evalString } = require('@borkdude/sci');
const opts = {bindings: {f: function(...args) { console.log(args.join(' ')); }},
              classes: {String: String}};
evalString("(f(String/fromCharCode 67))", opts);
prints:
C

jeroenvandijk13:01:00

That's quick. I can confirm the above works after pulling

borkdude13:01:32

@jeroenvandijk Pushed another thing. This now also works: evalString("(f(.charCodeAt \"foo\" 0))", opts);

borkdude13:01:07

I'm not sure if this works for every instance method call out there. I've tried something like this before, and then there was some problem I can't remember off the top of my head

jeroenvandijk13:01:00

Yes cool works as well

jeroenvandijk13:01:53

I'm guessing (.toString "s") is a special case. In the browser console "s".toString() does work

jeroenvandijk13:01:47

it says

Error: String.prototype.toString requires that 'this' be a String [at line 5, column 16]

borkdude13:01:38

maybe it would be cool if we could do:

const fs = require('fs');
const opts = {namespaces {fs fs}};
and then (:require [fs]) (fs/readFilesync ...)

borkdude13:01:01

I'll take a look at that later today, afk now

jeroenvandijk13:01:32

Yeah would be cool

jeroenvandijk13:01:24

My flow for node scripts is a bit painful running node myscript.js everytime. I'm wondering if it is not possible to load all the javascript files into Rhino (or another vm) and run javascript from the repl. I'll look into that later 🙈

borkdude13:01:56

oh funny, it seems this already works:

const fs = require('fs');
const opts = {bindings: {f: function(...args) { console.log('x', args.join(' ')); }},
              namespaces: {fs: fs}};

console.log(evalString("(str (fs/readFileSync \"sci.js\"))", opts));

borkdude14:01:24

(I should really add some tests for all the working examples posted so far)

👍 1
borkdude14:01:52

a bit weird, but this also seems to work:

const { evalString } = require('@borkdude/sci');
const fs = require('fs');

const opts = {namespaces: {
  console: console,
  fs: fs}};

evalString("(console/log (str (fs/readFileSync \"sci.js\")))", opts);

borkdude20:01:35

Published a new version of the npm package

🙌 1