457 case OP_LOADK: |
457 case OP_LOADK: |
458 var constant = frame.f.constants[INS_Bx(instruction)]; |
458 var constant = frame.f.constants[INS_Bx(instruction)]; |
459 frame.reg[INS_A(instruction)] = new LValue(this, constant.type, constant.value); |
459 frame.reg[INS_A(instruction)] = new LValue(this, constant.type, constant.value); |
460 break; |
460 break; |
461 case OP_NEWTABLE: |
461 case OP_NEWTABLE: |
462 frame.reg[INS_A(instruction)] = new LValue(this, "table", {}); |
462 frame.reg[INS_A(instruction)] = new LValue(this, "table", []); |
463 break; |
463 break; |
464 case OP_GETTABLE: |
464 case OP_GETTABLE: |
465 var C = INS_C(instruction); |
465 var C = INS_C(instruction); |
466 var value = frame.reg[INS_B(instruction)].index(RK(frame, C)); |
466 var value = frame.reg[INS_B(instruction)].index(RK(frame, C)); |
467 frame.reg[INS_A(instruction)] = new LValue(this, value.type, value.value); |
467 frame.reg[INS_A(instruction)] = new LValue(this, value.type, value.value); |
694 registerLib: function (env, name, lib) |
694 registerLib: function (env, name, lib) |
695 { |
695 { |
696 var t; |
696 var t; |
697 if(name) |
697 if(name) |
698 { |
698 { |
699 t = this.LValue({}); // Create env[name] and put fns in there |
699 t = this.LValue([]); // Create env[name] and put fns in there |
700 env.setIndex(this.LValue(name), t); |
700 env.setIndex(this.LValue(name), t); |
701 } |
701 } |
702 else |
702 else |
703 t = env; // Import directly into env |
703 t = env; // Import directly into env |
704 |
704 |
712 |
712 |
713 var fs=require("fs"); |
713 var fs=require("fs"); |
714 var sys=require("sys"); |
714 var sys=require("sys"); |
715 var c = new LBinaryChunk(testvm, fs.readFileSync("luac.out", "binary")); |
715 var c = new LBinaryChunk(testvm, fs.readFileSync("luac.out", "binary")); |
716 |
716 |
717 var _G = testvm.LValue({}); |
717 var _G = testvm.LValue([]); |
718 |
718 |
719 // Standard library |
719 // Standard library |
720 |
720 |
721 var baselib = { |
721 var baselib = { |
722 print: function () |
722 print: function () |
825 testvm.registerLib(_G, null, baselib); |
825 testvm.registerLib(_G, null, baselib); |
826 testvm.registerLib(_G, "math", math); |
826 testvm.registerLib(_G, "math", math); |
827 testvm.registerLib(_G, "string", string); |
827 testvm.registerLib(_G, "string", string); |
828 |
828 |
829 // Metatable on environment to print out nil global accesses |
829 // Metatable on environment to print out nil global accesses |
830 var mt = testvm.LValue({}); |
830 var mt = testvm.LValue([]); |
831 mt.setIndex( |
831 mt.setIndex( |
832 testvm.LValue("__index"), |
832 testvm.LValue("__index"), |
833 testvm.LValue(function (t, k) { sys.puts("Access of nil global: "+k); }) |
833 testvm.LValue(function (t, k) { sys.puts("Access of nil global: "+k); }) |
834 ); |
834 ); |
835 _G.setMetatable(mt); |
835 _G.setMetatable(mt); |