paiq / blackcoffee

CoffeeScript + hygienic macros
MIT License
105 stars 9 forks source link

examples not working? #4

Closed loveencounterflow closed 10 years ago

loveencounterflow commented 10 years ago

when i do

route = __filename
macro ->
  # macro.fileToNode route
  macro.fileToNode '/tmp/test.coffee'

i either get an error that the variable is not defined, or that the literal path does not correspond to a file,—or else blackcoffee will hang indefinitely. it is not clear to me from the description that i should expect the first and the last behavior; only the error reporting would appear as correct behavior for me.

moreover, the third example

fileContents = macro ->
    macro.valToNode require('fs').readFileSync 'file.txt'

fails with SyntaxError: run-time error in macro: ReferenceError: require is not defined, which does the impossible and has an error be both a reference and a syntax error. it is anyhow unexpected given the description which maintains that running these lines should cause "fileContents [to] contain whatever is in 'file.txt'".

i managed to get the "Working with Nodes" examples to run, but of course i had to write custom Array::add etc methods to get it working. just saying here that 'modifying built-in prototypes' and 'macros' are orthogonal concepts—i understand this example comes from a particular use-case, but maybe a minimal working example without monkey patching would be more helpful. anyway, i was delighted to see you CAN extend the semantics of CS / JS operators, which i think is great.

vanviegen commented 10 years ago

I'm not sure what you're trying to accomplish in the first code fragment. The out-commented line would indeed throw a compile-time error, as the route variable is defined only at run-time, yet you're using it compile-time. The compiler should of course never hang indefinitely. Could you tell me how to reproduce this?

The second example you quote was indeed wrong. I've update the documentation.

I didn't want to give a full working example in the "Working with Nodes" section, because if we'd want to show something that makes sense and shows the breadth of what you can do, it'd be quite a bit of code. And as this code is just regular CoffeeScript, I'd say it would be mostly distracting. I've updated the documentation to explain what's missing though.

loveencounterflow commented 10 years ago

ok so i have to understand who sees what at which time with macros. definitely important here.

as for the hanging issue, the route given refers to the file being executed... after waiting a tad longer, i got a Maximum call stack size exceeded—it looks like it's hanging for a second or so. it did work with one of the files in examples/.