Skip to content

Commit fb027ec

Browse files
committed
Initial commit of JS bytecode interpreter.
0 parents  commit fb027ec

17 files changed

+4150
-0
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*.pyc
2+
*~
3+
.*.sw[op]
4+
*.egg-info
5+
dist
6+
build
7+
local
8+
_build
9+
distribute-*
10+
batavia.js

AUTHORS

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Batavia was originally created in August 2015.
2+
3+
The PRIMARY AUTHORS are (and/or have been):
4+
Russell Keith-Magee
5+
6+
And here is an inevitably incomplete list of MUCH-APPRECIATED CONTRIBUTORS --
7+
people who have submitted patches, reported bugs, added translations, helped
8+
answer newbie questions, and generally made Batavia that much better:
9+

LICENSE

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
Copyright (c) 2015 Russell Keith-Magee.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without modification,
5+
are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice,
8+
this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright
11+
notice, this list of conditions and the following disclaimer in the
12+
documentation and/or other materials provided with the distribution.
13+
14+
3. Neither the name of Batavia nor the names of its contributors may
15+
be used to endorse or promote products derived from this software without
16+
specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
29+
30+
---
31+
32+
The bytecode interpreter for this project was derived from Byterun, written
33+
by Ned Batchelder.
34+
35+
https://github.com/nedbat/byterun
36+
37+
Copyright (c) 2013, Ned Batchelder
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy of
40+
this software and associated documentation files (the "Software"), to deal in
41+
the Software without restriction, including without limitation the rights to
42+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
43+
of the Software, and to permit persons to whom the Software is furnished to do
44+
so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.

README.rst

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Batavia
2+
=======
3+
4+
Tools to run Python bytecode in the browser.
5+
6+
Quickstart
7+
----------
8+
9+
See the `tests/test.html` file for an example of usage.
10+
11+
..Documentation
12+
..-------------
13+
14+
..Documentation for Batavia can be found on `Read The Docs`_.
15+
16+
Community
17+
---------
18+
19+
Batavia is part of the `BeeWare suite`_. You can talk to the community through:
20+
21+
* `@pybeeware on Twitter`_
22+
23+
* The `BeeWare Users Mailing list`_, for questions about how to use the BeeWare suite.
24+
25+
* The `BeeWare Developers Mailing list`_, for discussing the development of new features in the BeeWare suite, and ideas for new tools for the suite.
26+
27+
Contributing
28+
------------
29+
30+
If you experience problems with Batavia, `log them on GitHub`_. If you
31+
want to contribute code, please `fork the code`_ and `submit a pull request`_.
32+
33+
.. _BeeWare suite: http://pybee.org
34+
.. _Read The Docs: http://batavia.readthedocs.org
35+
.. _@pybeeware on Twitter: https://twitter.com/pybeeware
36+
.. _BeeWare Users Mailing list: https://groups.google.com/forum/#!forum/beeware-users
37+
.. _BeeWare Developers Mailing list: https://groups.google.com/forum/#!forum/beeware-developers
38+
.. _log them on Github: https://github.com/pybee/batavia/issues
39+
.. _fork the code: https://github.com/pybee/batavia
40+
.. _submit a pull request: https://github.com/pybee/batavia/pulls
41+

batavia/Block.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
function Block(type, handler, level) {
3+
this.type = type;
4+
this.handler = handler;
5+
this.level = level;
6+
}

batavia/Cell.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* A fake cell for closures.
3+
*
4+
* Closures keep names in scope by storing them not in a frame, but in a
5+
* separate object called a cell. Frames share references to cells, and
6+
* the LOAD_DEREF and STORE_DEREF opcodes get and set the value from cells.
7+
*
8+
* This class acts as a cell, though it has to jump through two hoops to make
9+
* the simulation complete:
10+
*
11+
* 1. In order to create actual FunctionType functions, we have to have
12+
* actual cell objects, which are difficult to make. See the twisty
13+
* double-lambda in __init__.
14+
*
15+
* 2. Actual cell objects can't be modified, so to implement STORE_DEREF,
16+
* we store a one-element list in our cell, and then use [0] as the
17+
* actual value.
18+
*/
19+
20+
function Cell(value) {
21+
this.contents = value;
22+
}
23+
24+
Cell.prototype.get = function() {
25+
return this.contents;
26+
};
27+
28+
Cell.prototype.set = function(value) {
29+
this.contents = value;
30+
};

batavia/Code.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
function Code(kwargs) {
3+
this.co_argcount = kwargs.argcount || 0;
4+
this.co_cellvars = kwargs.cellvars || [];
5+
this.co_code = kwargs.code;
6+
this.co_consts = kwargs.consts || [];
7+
this.co_filename = kwargs.filename || '<string>';
8+
this.co_firstlineno = kwargs.firstlineno || 1;
9+
this.co_flags = kwargs.flags || 0;
10+
this.co_freevars = kwargs.freevars || 0;
11+
this.co_lnotab = kwargs.lnotab || '';
12+
this.co_name = kwargs.name || '<module>';
13+
this.co_names = kwargs.names || [];
14+
this.co_nlocals = kwargs.nlocals || 0;
15+
this.co_stacksize = kwargs.stacksize || 0;
16+
this.co_varnames = kwargs.varnames || [];
17+
}

batavia/Frame.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
function Frame(kwargs) {
3+
var v;
4+
5+
this.f_code = kwargs.f_code;
6+
this.f_globals = kwargs.f_globals;
7+
this.f_locals = kwargs.f_locals;
8+
this.f_back = kwargs.f_back;
9+
this.stack = [];
10+
11+
if (this.f_back) {
12+
this.f_builtins = this.f_back.f_builtins;
13+
} else {
14+
this.f_builtins = this.f_locals['__builtins__'];
15+
if (this.f_builtins.hasOwnProperty('__dict__')) {
16+
this.f_builtins = this.f_builtins.__dict__;
17+
}
18+
}
19+
20+
this.f_lineno = this.f_code.co_firstlineno;
21+
this.f_lasti = 0;
22+
23+
if (this.f_code.co_cellvars.length > 0) {
24+
this.cells = {};
25+
if (this.f_back && !this.f_back.cells) {
26+
this.f_back.cells = {};
27+
}
28+
for (v in this.f_code.co_cellvars) {
29+
// Make a cell for the variable in our locals, or null.
30+
cell = new Cell(this.f_locals[v]);
31+
if (this.f_back) {
32+
this.f_back.cells[v] = this.cells[v] = cell;
33+
}
34+
}
35+
} else {
36+
this.cells = null;
37+
}
38+
39+
if (this.f_code.co_freevars.length > 0) {
40+
if (!this.cells) {
41+
this.cells = {};
42+
}
43+
for (v in this.f_code.co_freevars) {
44+
assert(this.cells !== null);
45+
assert(this.f_back.cells, "f_back.cells: " + this.f_back.cells);
46+
this.cells[v] = this.f_back.cells[v];
47+
}
48+
}
49+
this.block_stack = [];
50+
this.generator = null;
51+
52+
}
53+
54+
Frame.prototype.__repr__ = function() {
55+
return '<Frame at 0x' + id(self) + ': ' + this.f_code.co_filename +' @ ' + this.f_lineno + '>';
56+
};
57+
58+
Frame.prototype.line_number = function() {
59+
// Get the current line number the frame is executing.
60+
// We don't keep f_lineno up to date, so calculate it based on the
61+
// instruction address and the line number table.
62+
var lnotab = this.f_code.co_lnotab;
63+
var byte_increments = []; //six.iterbytes(lnotab[0::2]);
64+
var line_increments = []; //six.iterbytes(lnotab[1::2]);
65+
66+
byte_num = 0;
67+
line_num = this.f_code.co_firstlineno;
68+
69+
for (var incr in byte_increments) {
70+
var byte_incr = byte_increments[incr];
71+
var line_incr = line_increments[incr];
72+
73+
byte_num += byte_incr;
74+
if (byte_num > this.f_lasti) {
75+
break;
76+
}
77+
line_num += line_incr;
78+
}
79+
80+
return line_num;
81+
};

0 commit comments

Comments
 (0)