gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 155 forks source link

Magic Literal to expand Utility Functions #248

Open audreyt opened 11 years ago

audreyt commented 11 years ago

In code-injection contexts such as webworker-threads or plv8js, we couldn't simply inject the compiled function's string form into it, because import$ and replace$ etc are hoisted outside function scope.

Minimal example using webworker-threads:

{ Worker } = require \webworker-threads
w = new Worker -> x - /g/

Here the -> x - /g/ is compiled to return replace$.call(x, /g/, '') but replace$ is outside the thread scope and cannot be referred to.

One way out of it may be a special literal UTILS that expands all utility functions

w = new Worker -> x - /g/; '__UTILS__';

Another way may be a compilation mode that de-hoist the utilities and compile them into the function body.

audreyt commented 11 years ago

@DavidSouther introduced an --util flag that removes inline references altogether: https://github.com/DavidSouther/LiveScript/commit/5a2a06fdd2a0805a7b65cdeb960cbbfa52b209de

It would make sense, then, to generate a util-ls package (or have it as output of LiveScript.utils()) so we can inline them in one place.

DavidSouther commented 11 years ago

The --util functionality works by expecting the symbols needed to be on the global scope, either on GLOBAL or window. Will those definitions be available inside the worker, or will they be reinitialzed? I'll try and play around with it later today.

DavidSouther commented 11 years ago

also, https://github.com/DavidSouther/LiveScript/commit/e01926ebb27e15e805403ce329e68c54e3c6ff5b is the commit with LiveScript changes.

audreyt commented 11 years ago

Hi @DavidSouther -- if we have all utility functions accessible in string form, we can coax it into the webworker-threads context using w.thread.eval("function replace$ ..."). Thanks for looking into it!

DavidSouther commented 11 years ago

(Haven't forgotten about this, just been busy at work)