Skip to content
This repository has been archived by the owner on May 31, 2020. It is now read-only.

Commit

Permalink
EXTENDED_ARG, BUILD_MAP and BUILD_CONST_KEY_MAP
Browse files Browse the repository at this point in the history
  • Loading branch information
abonie committed Aug 15, 2017
1 parent 4d86b52 commit 046b97c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 44 deletions.
102 changes: 64 additions & 38 deletions batavia/VirtualMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,54 +768,67 @@ VirtualMachine.prototype.unpack_code = function(code) {
var opcode = code.co_code.val[pos++]

// next opcode has 4-byte argument effectively.
// TODO 3.6
if (opcode === dis.EXTENDED_ARG) {
lo = code.co_code.val[pos++]
hi = code.co_code.val[pos++]
extra = (lo << 16) | (hi << 24)
// emulate four NOPs
unpacked_code[opcode_start_pos] = {
'opoffset': opcode_start_pos,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 1] = {
'opoffset': opcode_start_pos + 1,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 2] = {
'opoffset': opcode_start_pos + 2,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 3] = {
'opoffset': opcode_start_pos + 3,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
if (constants.BATAVIA_MAGIC === constants.BATAVIA_MAGIC_36) {
extra = code.co_code.val[pos++] << 8
unpacked_code[opcode_start_pos] = {
'opoffset': opcode_start_pos,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
} else {
lo = code.co_code.val[pos++]
hi = code.co_code.val[pos++]
extra = (lo << 16) | (hi << 24)
// emulate four NOPs
unpacked_code[opcode_start_pos] = {
'opoffset': opcode_start_pos,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 1] = {
'opoffset': opcode_start_pos + 1,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 2] = {
'opoffset': opcode_start_pos + 2,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
unpacked_code[opcode_start_pos + 3] = {
'opoffset': opcode_start_pos + 3,
'opcode': dis.NOP,
'op_method': this.dispatch_table[dis.NOP],
'args': [],
'next_pos': pos
}
}
continue
}

var intArg
if (constants.BATAVIA_MAGIC === constants.BATAVIA_MAGIC_36) {
intArg = code.co_code.val[pos++]
} else if (opcode >= dis.HAVE_ARGUMENT) {
lo = code.co_code.val[pos++]
hi = code.co_code.val[pos++]
intArg = lo | (hi << 8) | extra
extra = 0 // use extended arg if present
intArg = code.co_code.val[pos++] | extra
extra = 0
}

if (opcode >= dis.HAVE_ARGUMENT) { // XXX 3.6?
if (constants.BATAVIA_MAGIC !== constants.BATAVIA_MAGIC_36) {
lo = code.co_code.val[pos++]
hi = code.co_code.val[pos++]
intArg = lo | (hi << 8) | extra
extra = 0 // use extended arg if present
}

if (opcode in dis.hasconst) {
args = [code.co_consts[intArg]]
} else if (opcode in dis.hasfree) {
Expand Down Expand Up @@ -1408,6 +1421,7 @@ VirtualMachine.prototype.byte_BUILD_MAP = function(size) {
switch (constants.BATAVIA_MAGIC) {
case constants.BATAVIA_MAGIC_35:
case constants.BATAVIA_MAGIC_353:
case constants.BATAVIA_MAGIC_36:
var items = this.popn(size * 2)
var dict = new types.Dict()

Expand All @@ -1432,6 +1446,18 @@ VirtualMachine.prototype.byte_BUILD_MAP = function(size) {
}
}

VirtualMachine.prototype.byte_BUILD_CONST_KEY_MAP = function(size) {
var keys = this.pop()
var values = this.popn(size)
var dict = new types.Dict()

for (var i = 0; i < values.length; i += 1) {
dict.__setitem__(keys[i], values[i])
}
this.push(dict)
return
}

VirtualMachine.prototype.byte_STORE_MAP = function() {
switch (constants.BATAVIA_MAGIC) {
case constants.BATAVIA_MAGIC_35:
Expand Down
5 changes: 4 additions & 1 deletion batavia/modules/dis.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def_binary_op('BINARY_OR', 66)
def_inplace_op('INPLACE_POWER', 67)
def_op('GET_ITER', 68)

// introduced in Python 3.5+
// Introduced in Python 3.5
def_op('GET_YIELD_FROM_ITER', 69)

def_op('PRINT_EXPR', 70)
Expand Down Expand Up @@ -217,4 +217,7 @@ dis.hasfree[148] = 148
def_op('EXTENDED_ARG', 144)
dis.EXTENDED_ARG = 144

// Introduced in Python 3.6
def_op('BUILD_CONST_KEY_MAP', 156)

module.exports = dis
7 changes: 4 additions & 3 deletions beekeeper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ pull_request:
name: Python 3.5 tests
task: batavia-py35
critical: False
# - py3.6:
# name: Python 3.6 tests
# task: batavia-py36
- py3.6:
name: Python 3.6 tests
task: batavia-py36
critical: False
profile: hi-cpu
push:
- smoke-test:
Expand Down
5 changes: 3 additions & 2 deletions tests/datatypes/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def test_creation(self):
# keys with the same string representation
self.assertCodeExecution("""
x = {1: 2, '1': 1}
print(sorted(x.items()))
""", substitutions={'str() < int()': ['int() < str()']})
for kv in x.items():
print(type(kv[0]), kv[0], kv[1])
""")

def test_getitem(self):
# Simple existent key
Expand Down

0 comments on commit 046b97c

Please sign in to comment.