metajs icon indicating copy to clipboard operation
metajs copied to clipboard

Hoisting fails

Open omphalos opened this issue 12 years ago • 4 comments

The following test throws an exception, but shouldn't: x(); function x() { console.log(1); }

omphalos avatar May 12 '13 13:05 omphalos

Yeah, I was lazy about implementing another AST traversal to extract out the function declarations. We should do that. This also throws when it shouldn't, because we don't hoist var declarations either: console.log(a); var a;. I think it'd be nice to use an existing traversal library like estraverse rather than writing another huge switch-case statement.

int3 avatar May 16 '13 05:05 int3

@int3: I recommend both Constellation/estraverse and Constellation/escope. My favourite program doesn't work:

(function(){
  try { throw 0; } catch(a) { var a = 1; }
  console.log(a);
}());

michaelficarra avatar Jun 25 '13 01:06 michaelficarra

@michaelficarra I actually have another interpreter that implements hoisting (and several other features) correctly. It does use estraverse, but not escope -- I'll definitely have a look at the latter the next time I write another JS interpreter / tool. closure-interpreter isn't in CPS at the moment, but I think it should be quite easy to hack it into shape, and then hook it up to metajs' frontend. Maybe this weekend.

int3 avatar Jun 25 '13 02:06 int3

+1

kumavis avatar Aug 13 '13 21:08 kumavis