Mercurial > py-js
comparison py.js @ 0:e985e6d762b9 default tip
Origination.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Wed, 24 Sep 2008 16:47:18 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e985e6d762b9 |
---|---|
1 function Py(code) { | |
2 if ('Py' in this) | |
3 throw new Error("Py() must be called with 'new' keyword"); | |
4 | |
5 var self = this; | |
6 | |
7 var co_code = code.co_code; | |
8 var co_consts = code.co_consts; | |
9 var co_names = code.co_names; | |
10 | |
11 var ops = {}; | |
12 var stack = []; | |
13 var globals = {}; | |
14 var locals = {}; | |
15 | |
16 ops.LOAD_CONST = function LOAD_CONST(index) { | |
17 stack.push(co_consts[index]); | |
18 }; | |
19 | |
20 ops.LOAD_NAME = function LOAD_NAME(index) { | |
21 stack.push(locals[co_names[index]]); | |
22 }; | |
23 | |
24 ops.BINARY_ADD = function BINARY_ADD() { | |
25 stack.push(stack.pop() + stack.pop()); | |
26 }; | |
27 | |
28 ops.STORE_NAME = function STORE_NAME(index) { | |
29 locals[co_names[index]] = stack.pop(); | |
30 }; | |
31 | |
32 ops.STORE_GLOBAL = function STORE_GLOBAL(index) { | |
33 globals[co_names[index]] = stack.pop(); | |
34 }; | |
35 | |
36 ops.RETURN_VALUE = function RETURN_VALUE() { | |
37 self.returnValue = stack.pop(); | |
38 }; | |
39 | |
40 for (var i = 0; i < co_code.ops.length; i++) { | |
41 var op = co_code.ops[i].op; | |
42 var arg = co_code.ops[i].arg; | |
43 if (!(op in ops)) | |
44 throw new Error('No implementation for op ' + op); | |
45 ops[op](arg); | |
46 } | |
47 | |
48 self.locals = locals; | |
49 self.globals = globals; | |
50 } |