lvm.js

changeset 46
33ae4e0de8cc
parent 45
4d2cf0b5235d
parent 44
f5244bce28ce
child 47
b8c4273edbbb
equal deleted inserted replaced
45:4d2cf0b5235d 46:33ae4e0de8cc
258 function INS_sBx(ins) 258 function INS_sBx(ins)
259 { 259 {
260 return (INS_Bx(ins)-0x1FFFF); 260 return (INS_Bx(ins)-0x1FFFF);
261 } 261 }
262 262
263 function LFunction(chunk, env) 263 function LFunction(vm, chunk, env)
264 { 264 {
265 function F() {}; 265 function F() {};
266 F.prototype = chunk; 266 F.prototype = chunk;
267 var o = new F(); 267 var o = new F();
268 o.vm = vm;
268 o.environment = env; 269 o.environment = env;
269 o.chunk = chunk; 270 o.chunk = chunk;
270 o.upvalues = []; 271 o.upvalues = [];
271 return o; 272 return o;
272 } 273 }
373 } 374 }
374 break; 375 break;
375 case OP_CLOSURE: 376 case OP_CLOSURE:
376 var prototype_id = INS_Bx(instruction); 377 var prototype_id = INS_Bx(instruction);
377 var chunk = frame.f.chunk.prototypes[prototype_id]; 378 var chunk = frame.f.chunk.prototypes[prototype_id];
378 var f = new LFunction(chunk, frame.f.environment); 379 var f = new LFunction(this, chunk, frame.f.environment);
379 frame.reg[INS_A(instruction)] = new LValue("function", f); 380 frame.reg[INS_A(instruction)] = new LValue("function", f);
380 for(var i=0;i<chunk.numUpvalues;i++) 381 for(var i=0;i<chunk.numUpvalues;i++)
381 { 382 {
382 var upval_instruction = frame.f.instructions[frame.pc++]; 383 var upval_instruction = frame.f.instructions[frame.pc++];
383 switch(INS_OPCODE(upval_instruction)) 384 switch(INS_OPCODE(upval_instruction))
464 else 465 else
465 print = require("sys").puts; // Nodejs 466 print = require("sys").puts; // Nodejs
466 467
467 default_environment.setIndex(LValueFromString("print"), LValueFromFunction(print)); 468 default_environment.setIndex(LValueFromString("print"), LValueFromFunction(print));
468 469
469 var f = new LFunction(c, default_environment); 470 var f = new LFunction(this, c, default_environment);
470 471
471 try{ 472 try{
472 testvm.call(f); 473 testvm.call(f);
473 } 474 }
474 catch(e) 475 catch(e)
475 { 476 {
476 print("Error: " + e); 477 print("Error: " + e);
477 if(typeof(e) == "object" && "stack" in e) 478 if(typeof(e) == "object" && "stack" in e)
478 print(e.stack); 479 print(e.stack);
479 } 480 process.exit(1);
481 }

mercurial