Fork me on GitHub
#sci
<
2021-11-09
>
Ian Fernandez04:11:06

I'm trying to define a context and import some functions in this context, this is right, how can I make this work??

(def context
  (sci/init {'quil.core (into {}
                              (map (fn [[k v]]
                                    [k v])) (ns-publics 'quil.core))
             'reagent.core {'create-class r/create-class}
             'codemirror-sci {'->quil-canvas ->quil-canvas}}))
maybe is not working because sci can't use p5js?

borkdude08:11:59

Please be more specific. What error do you get? is p5js a JS library? Do you have a public repo? What program are you trying to run in the SCI context?

borkdude22:11:52

@U9ABG0ERZ I think you might need to add :classes {'js goog/global :allow :all} to your context options

borkdude22:11:14

and then you can access all JS library from the global environment. But more information is better

borkdude22:11:05

also you need to put the namespaces inside (sci/init {:namespaces {'quil.core ...}})

borkdude22:11:16

instead of putting the namespaces on the top level

Ian Fernandez03:11:23

(def context
  (sci/init {:classes {'js goog/global :allow :all}
             :namespaces {'quil.core (into {}
                                           (map (fn [[k v]]
                                                 [k v])) (ns-publics 'quil.core))
                          'reagent.core {'create-class r/create-class}
                          'codemirror-sci {'->quil-canvas ->quil-canvas}}}))
,,,,
(defn editor
  [source
   {:keys [eval?]}]
  (r/with-let [!view (r/atom nil)
               last-result (when eval? (r/atom (eval-string source)))
               mount! (fn [el]
                        (when el
                          (reset! !view (new EditorView
                                             (j/obj :state
                                                    (test-utils/make-state
                                                      (cond-> #js [extensions]
                                                              eval? (.concat #js [(extension {:modifier  "Alt"
                                                                                              :on-result (partial reset! last-result)})]))
                                                      source)
                                                    :parent el)))))]
              [:div
               [:div {:class "rounded-md mb-0 text-sm monospace overflow-auto relative border shadow-lg bg-white"
                      :ref   mount!
                      :style {:max-height 410}}]
               (when eval?
                 [:div.mt-3.mv-4.pl-6 {:style {:white-space "pre-wrap" :font-family "var(--code-font)"}}
                   @last-result])]
              (finally
                (j/call @!view :destroy))))
(->quil-canvas {:title "Oh so many grey circles" ;; Set the title of the sketch
     :settings #(smooth 2)                                ;; Turn on anti-aliasing
     :setup    setup                                        ;; Specify the setup fn
     :draw     draw                                         ;; Specify the draw fn
     :size     [323 200]})

;; gives :
#error {:message "Could not resolve symbol: ->quil-canvas", :data {:type :sci/error, :line 1, :column 6, :file nil, :phase "analysis"}}

Ian Fernandez03:11:42

I'm trying to add code-mirror + sci to run quil

borkdude09:11:36

@U9ABG0ERZ You need to write (quil.core/->quil->canvas ..) or evaluate (require '[quil.core :refer :all]) in the ctx beforehand

Ian Fernandez12:11:01

This ->quil-canvas. Is a function that's declared in the ns that I'm declaring context

borkdude12:11:21

I see that ->quil-canvas is in the namespace code-mirror-sci in the SCI context

borkdude12:11:56

The same holds there: use (require '[code-mirror-sci :refer [->quil-canvas]]) if you want to use it without a namespace prefix

borkdude12:11:49

or add it to the user namespace which is the default namespace

Ian Fernandez15:11:34

(require '[code-mirror-sci :refer [->quil-canvas]]) => inside the the context for sci to begin with it, right?

borkdude16:11:34

or just add it to the user namespace like {:namespaces {'user {->quil-canvas ...}}}

👍 1
borkdude16:11:59

unless you want to support that users can define their own namespaces

👍 1
borkdude16:11:09

then the refer idea is probably better

👍 1