probmods / webchurch

A Church to Javascript compiler (DEPRECATED)
Other
140 stars 15 forks source link

loading external functions #63

Closed ngoodman closed 10 years ago

ngoodman commented 10 years ago

It would be very useful to have a way to load functions from a file/url. Both church code and js primitives should be supported.

For example: (load "http://foo.bar/baz.church") would load the definitions from baz.church (effectively splicing that file into the current program). (load "http://foo.bar/baz.js") would make any functions exported from baz.js available as (deterministic) primitives. (i.e. it would require the file in the preamble.)

longouyang commented 10 years ago

Added in 93396bbf832a59dd32eff068b8b019861017204d (you will need to run npm install after pulling this)

This is a work in progress.

Currently, in the browser, this works for both church and js code. However, you can only access urls if you are using the http:// protocol, rather than the file:// protocol - this means that if you want load, you need to use a local web server that serves http. I added one - you can run it using npm run-start server. A further restriction is that you can only load scripts from the same domain - this is the famous Same-Origin Policy that all browsers enforce for security. I created a directory called lib for putting common library scripts (e.g., box2d?)

A piece of code that should work is:

(load "lib/loadme.js")
(load "lib/loadme.church")
(f 5) ;; defined in loadme.js
(g 5) ;; defined in loadme.church

Currently on the command line, you can load both church and js code but only from disk (no network), but this wouldn't be too hard to add.

An implementation note: like eval, load(...) is not actually a builtin -- it desguars to eval(load_url(...))