Skip to content

Instantly share code, notes, and snippets.

@robkuz
Last active May 6, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robkuz/48a66354bfa82f6efa4d6a188d5682b7 to your computer and use it in GitHub Desktop.
Save robkuz/48a66354bfa82f6efa4d6a188d5682b7 to your computer and use it in GitHub Desktop.
Access Global Object from Purescript
"use strict";
// module Main
exports.instGlobal = function(name){
return function (value) {
global[name]=value;
return global[name];
}
}
exports.getGlobal = function(name){ return global[name] }
module Main where
import Prelude
import Control.Monad.Eff
import Control.Monad.Eff.Console
foreign import instGlobal :: String -> String -> String
foreign import getGlobal :: String -> String
main :: forall e. Eff (console :: CONSOLE | e) Unit
main = do
log "Hello sailor!"
> instGlobal "D3" "its me!"
  "its me!"

> getGlobal "D3"
   /clean/.psci_modules/node_modules/Prelude/foreign.js:230
   TypeError: Cannot read property 'length' of undefined
   at exports.showStringImpl (/clean/.psci_modules/node_modules/Prelude/foreign.js:230:12)
   at /clean/.psci_modules/node_modules/Control.Monad.Eff.Console/index.js:15:51
 ...

I changed the return to "" + Object.keys(global); on both foreign fns. The interesting thing is. That on the instGlobal the keys is existing and when calling getGlobal its not there anymore.

Therefore this exception.

The question is why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment