Fork me on GitHub
#babashka
<
2020-01-28
>
jeroenvandijk19:01:54

I'm having some issues to use Sci with Nodejs in an asynchronous context. It seems (fn [] ..) doesn't return a javascript function that I can use outside of the evalString context. E.g. the following fails

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

var myFN = evalString("(fn [a] (+ a 10))")

console.log("v", myFN(4))
This gives
console.log("v", myFN(4))
                 ^

TypeError: myFN is not a function

jeroenvandijk19:01:28

Not sure if there is a better option, but a "solution" I found is by having a nested evaluate:

'use strict';

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

var evaluate = function(code, args) {
  evalString(code, {
    bindings: {
      args: args,
      println: function(...args) { console.log(args.join(' ')) },
      request: require('request')
      evalLater: function(s) { return function(...args) { 
       evaluate(s, args) } } },
    namespaces: {
        https: require('https'),
        http:  require('http')
    }}) };

exports.handler = function(event, context, callback) {
  evaluate({{clojure-code}}, 
    [event,
     context,
     function(response) { callback(null, response) }]);
};

jeroenvandijk19:01:14

In the CLJ sci this does work:

(let [f (sci/eval-string "(fn [a b] (+ a b))")]
  (f 1 2))

jeroenvandijk19:01:33

I'll create a Github issue

borkdude21:01:22

@jeroenvandijk

const { evalString, toJS } = require('@borkdude/sci');
and then apply the function toJS to the function.

borkdude21:01:12

Multimethods landed on bb master:

bb "
(derive ::rect ::shape)
(defmulti bar (fn [x y] [x y]))
(defmethod bar [::rect ::shape] [x y] :rect-shape)
(defmethod bar [::shape ::rect] [x y] :shape-rect)
(prefer-method bar [::rect ::shape] [::shape ::rect])
(bar ::rect ::rect)"
:rect-shape

👍 8
borkdude21:01:38

@jeroenvandijk The above approach works with toJS but I've now also pushed 0.0.13-alpha.2 to npm which don't add metadata to evaluated functions.

borkdude21:01:59

I published a new version of sci but somehow the js doesn't end up there...

$ ls node_modules/@borkdude/sci/
CHANGES.md   LICENSE      README.md    package.json
although it does mention on the npm website that the package is 480 kB

borkdude21:01:22

and didn't do anything else than previous times... I already tried it 4 times.. (now it's alpha.4)

borkdude22:01:15

npm seems to be back to normal.. @jeroenvandijk :

> const { evalString, toJS } = require('@borkdude/sci');
undefined
> f = evalString("(fn [] \"hello\")")
[Function: d] { i: 0, l: [Function (anonymous)], f: [Function: g] }
> f()
'hello'

❤️ 4
jeroenvandijk08:01:59

@U04V15CAJ First test seems to work with npm i @borkdude/[email protected] , but not with the latest npm i @borkdude/sci

borkdude08:01:29

what version do you get with "latest"?

jeroenvandijk08:01:46

Because the package doesn't have the sci.js file

jeroenvandijk08:01:54

latest is 0.0.13-alpha.5 according to npm

borkdude08:01:13

Yeah, I've had this problem several times before... that's why it got all the way up to alpha 5... 😞

borkdude08:01:20

Don't know what is causing this

jeroenvandijk08:01:30

Ok annoying 😞

jeroenvandijk08:01:49

I'll test with 4.2 I assume it is the same package than?

borkdude08:01:57

If you go to the npm page you do see that the package is around 480kb which indicates that the source should be in there somehow

borkdude08:01:33

This is my release script:

#!/usr/bin/env bash

set -Eeo pipefail

rm -rf /tmp/release-js
mkdir -p /tmp/release-js
cp out/min/sci.min.js /tmp/release-js/sci.js
cp README.md /tmp/release-js
cp package.json /tmp/release-js
cd /tmp/release-js
npm publish --access public

borkdude08:01:47

I do the same every time. It's a mystery to me why the sci.js sometimes is not there

borkdude08:01:37

I've now released alpha.6:

$ script/release-js
npm notice
npm notice :package:  @borkdude/[email protected]
npm notice === Tarball Contents ===
npm notice 468.3kB sci.js
npm notice 686B    package.json
npm notice 10.7kB  README.md
npm notice === Tarball Details ===
npm notice name:          @borkdude/sci
npm notice version:       0.0.13-alpha.6
npm notice package size:  106.9 kB
npm notice unpacked size: 479.7 kB
npm notice shasum:        427cca209aaa65b726053c7cc175e8ff36ec2df7
npm notice integrity:     sha512-kwqOlJl4znqZW[...]D4Anf92xallVw==
npm notice total files:   3
npm notice
+ @borkdude/[email protected]

borkdude08:01:54

Can you try this?

borkdude08:01:43

Seems to work here

jeroenvandijk08:01:14

I'm a bit confused by how npm install ... works, but with the explicity call npm install @borkdude/[email protected] it works